Skip to content

Commit

Permalink
aggiunto orario
Browse files Browse the repository at this point in the history
  • Loading branch information
peppelg committed Nov 2, 2019
1 parent 71a1be6 commit 230a09c
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 31 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# argo_scuolanext_famiglia_unofficial_app
App open-source non ufficiale per Argo ScuolaNext Famiglia.

- [Download](https://github.com/peppelg/argo_scuolanext_famiglia_unofficial_app/releases/download/1.0.0%2B7/app-release.apk)
- [Download](https://github.com/peppelg/argo_scuolanext_famiglia_unofficial_app/releases/download/1.0.0%2B8/app-release.apk)
- [Sito dell'app](https://peppelg.space/argo_famiglia)
- [Canale Telegram](https://t.me/scuolanext)

Expand Down
62 changes: 39 additions & 23 deletions lib/api.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';
import 'package:intl/intl.dart';
import 'dart:async';
import 'dart:io';
import 'dart:convert';
import 'database.dart';

var endpoint = 'https://www.portaleargo.it/famiglia/api/rest';
var verifyHeaders = {
Expand Down Expand Up @@ -48,28 +46,20 @@ var fullHeaders = {

Future saveToken(
auth_token, cod_min, prg_scheda, prg_alunno, prg_scuola) async {
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
Hive.init(appDocPath);
var box = await Hive.openBox('argo_famiglia');
box.put('auth_token', auth_token);
box.put('cod_min', cod_min);
box.put('prg_scheda', prg_scheda);
box.put('prg_alunno', prg_alunno);
box.put('prg_scuola', prg_scuola);
await Database.put('auth_token', auth_token);
await Database.put('cod_min', cod_min);
await Database.put('prg_scheda', prg_scheda);
await Database.put('prg_alunno', prg_alunno);
await Database.put('prg_scuola', prg_scuola);
}

Future loadToken() async {
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
Hive.init(appDocPath);
var box = await Hive.openBox('argo_famiglia');
if (await box.get('auth_token') != null) {
fullHeaders['x-auth-token'] = await box.get('auth_token');
fullHeaders['x-cod-min'] = await box.get('cod_min');
fullHeaders['x-prg-scheda'] = await box.get('prg_scheda');
fullHeaders['x-prg-alunno'] = await box.get('prg_alunno');
fullHeaders['x-prg-scuola'] = await box.get('prg_scuola');
if (await Database.get('auth_token') != null) {
fullHeaders['x-auth-token'] = await Database.get('auth_token');
fullHeaders['x-cod-min'] = await Database.get('cod_min');
fullHeaders['x-prg-scheda'] = await Database.get('prg_scheda');
fullHeaders['x-prg-alunno'] = await Database.get('prg_alunno');
fullHeaders['x-prg-scuola'] = await Database.get('prg_scuola');
return 'OK';
} else {
return 'login';
Expand Down Expand Up @@ -129,7 +119,7 @@ Future login(school, username, password) async {
fullHeaders['x-prg-scheda'] = info[0]['prgScheda'].toString();
fullHeaders['x-prg-scuola'] = info[0]['prgScuola'].toString();
fullHeaders['x-prg-alunno'] = info[0]['prgAlunno'].toString();
saveToken(
await saveToken(
fullHeaders['x-auth-token'],
fullHeaders['x-cod-min'],
fullHeaders['x-prg-scheda'],
Expand Down Expand Up @@ -257,6 +247,32 @@ Future argomenti() async {
return listaArgomenti;
}

Future orario() async {
var response = await argoRequest(
fullHeaders, 'orario', {'page': '1', 'start': '0', 'limit': '25'});
if (response.containsKey('error')) {
Fluttertoast.showToast(msg: 'Errore sconosciuto:\n\n' + response['error']);
return {};
}
var tabellaOrario = {};
for (var ora in response['dati']) {
if (!tabellaOrario.containsKey(ora['giorno'])) {
tabellaOrario[ora['giorno']] = [];
}
if (!ora.containsKey('lezioni')) {
ora['lezioni'] = [
{'materia': '---', 'docente': '---'}
];
}
tabellaOrario[ora['giorno']].add({
'ora': ora['numOra'],
'materia': ora['lezioni'][0]['materia'],
'prof': ora['lezioni'][0]['docente']
});
}
return tabellaOrario;
}

Future oggi(data) async {
data = DateFormat('yyyy-MM-dd')
.format(DateFormat('dd/MM/y').parse(data))
Expand Down
7 changes: 7 additions & 0 deletions lib/backdropWidgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ getBackdrop(context) {
},
child: Text('Assenze', style: TextStyle(color: Colors.white)),
)),
Expanded(
child: FlatButton(
onPressed: () {
Navigator.of(context).pushReplacementNamed('/orario');
},
child: Text('Orario', style: TextStyle(color: Colors.white)),
)),
Expanded(
child: FlatButton(
onPressed: () {
Expand Down
26 changes: 26 additions & 0 deletions lib/database.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:hive/hive.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';

class Database {
static var box;
static Future init() async {
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
Hive.init(appDocPath);
box = await Hive.openBox('argo_famiglia');
if (box != null) {
return 'ok';
}
}

static Future put(key, value) async {
var r = await box.put(key, value);
return r;
}

static Future get(key) async {
var r = await box.get(key);
return r;
}
}
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'compiti.dart';
import 'lezioni.dart';
import 'note.dart';
import 'oggi.dart';
import 'orario.dart';
import 'info.dart';
import 'debugApi.dart';
import 'aggiornamento.dart';
Expand Down Expand Up @@ -69,6 +70,7 @@ class MyApp extends StatelessWidget {
'/compiti': (BuildContext context) => new CompitiRoute(),
'/lezioni': (BuildContext context) => new LezioniRoute(),
'/oggi': (BuildContext context) => new OggiRoute(),
'/orario': (BuildContext context) => new OrarioRoute(),
'/info': (BuildContext context) => new InfoRoute(),
'/debugApi': (BuildContext context) => new DebugApiRoute(),
'/aggiornamento': (BuildContext context) => new AggiornamentoRoute()
Expand Down
175 changes: 175 additions & 0 deletions lib/orario.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import 'package:flutter/material.dart';
import 'package:backdrop/backdrop.dart';
import 'backdropWidgets.dart';
import 'api.dart';
import 'database.dart';

class OrarioRoute extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _OrarioRouteState();
}
}

class _OrarioRouteState extends State<OrarioRoute> {
Map tabellaOrario = {};
final nuovoNomeMateria = TextEditingController();
final GlobalKey<RefreshIndicatorState> _refreshIndicatorKey =
new GlobalKey<RefreshIndicatorState>();

@override
Widget build(BuildContext context) {
var orarioColumns = [DataColumn(label: Text(''))];
for (var giorno in tabellaOrario.keys) {
orarioColumns.add(DataColumn(label: Text(giorno)));
}
var orarioRows = <DataRow>[];
var tborario = {}; //orario ordinato in base all'ora
tabellaOrario.forEach((k, v) {
for (var materia in v) {
if (!tborario.containsKey(materia['ora'])) {
tborario[materia['ora']] = [materia['ora'].toString()];
}
tborario[materia['ora']].add(materia['materia']);
}
});
tborario.forEach((ora, v) {
var cells = <DataCell>[];
v.asMap().forEach((k, materia) {
cells.add(DataCell(Text(materia), onTap: () {
//preme materia x edit
if (k != 0) {
//non deve click l'ora
var giorno = tabellaOrario.keys.toList()[k -
1]; //k-1 = numero del coso orizzontale, -1 xk si deve togliere colonna orario
modificaMateriaDialog(giorno, ora, context);
}
}));
});
orarioRows.add(DataRow(cells: cells));
});
var widgetsOrario = <Widget>[
SingleChildScrollView(
scrollDirection: Axis.vertical,
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: DataTable(columns: orarioColumns, rows: orarioRows)))
];
return BackdropScaffold(
title: Text('Orario'),
backLayer: getBackdrop(context),
iconPosition: BackdropIconPosition.leading,
actions: <Widget>[
IconButton(
icon: Icon(Icons.refresh),
onPressed: () {
resetVoti(context);
})
],
frontLayer: RefreshIndicator(
key: _refreshIndicatorKey,
onRefresh: aggiornaOrario,
child: ListView(
children: new List.from(<Widget>[])..addAll(widgetsOrario))));
}

Future aggiornaOrario() async {
var nuovoOrario = await Database.get('orario');
if (nuovoOrario == null) {
nuovoOrario = await orario();
await Database.put('orario', nuovoOrario);
}
setState(() {
tabellaOrario = nuovoOrario;
});
}

Future modificaMateriaDialog(giorno, ora, context) async {
var materia = tabellaOrario[giorno][ora - 1]; //liste partono da 0, ore da 1
nuovoNomeMateria.text = materia['materia'];
showDialog(
context: context,
barrierDismissible: true,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Modifica materia'),
content: SingleChildScrollView(
child: ListBody(children: <Widget>[
Text('Giorno: ' +
giorno +
'\nOra: ' +
ora.toString() +
'\nProf: ' +
materia['prof']),
Padding(
padding: EdgeInsets.only(top: 20),
child: TextField(
controller: nuovoNomeMateria,
obscureText: false,
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: 'Nome materia',
),
))
])),
actions: <Widget>[
FlatButton(
child: Text('Salva'),
onPressed: () {
Navigator.of(context).pop();
modificaMateria(giorno, ora);
},
),
FlatButton(
child: Text('Chiudi'),
onPressed: () {
Navigator.of(context).pop();
},
),
]);
});
}

Future modificaMateria(giorno, ora) async {
setState(() {
tabellaOrario[giorno][ora - 1]['materia'] = nuovoNomeMateria.text;
});
await Database.put('orario', tabellaOrario);
}

Future resetVoti(context) async {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Reset orario'),
content: Text(
'Sei sicuro di voler scaricare il nuovo orario da Argo? I nomi delle materie modificate verranno persi.'),
actions: <Widget>[
FlatButton(
child: new Text('SÌ'),
onPressed: () async {
await Database.put('orario', null);
Navigator.of(context).pop();
WidgetsBinding.instance.addPostFrameCallback(
(_) => _refreshIndicatorKey.currentState.show());
},
),
FlatButton(
child: new Text('NO'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
},
);
}

void initState() {
super.initState();
WidgetsBinding.instance
.addPostFrameCallback((_) => _refreshIndicatorKey.currentState.show());
}
}
15 changes: 9 additions & 6 deletions lib/redirectRoute.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'database.dart';
import 'api.dart';

class RedirectRoute extends StatelessWidget {
Expand All @@ -13,10 +14,12 @@ class RedirectRoute extends StatelessWidget {
}

Future tryLogin(context) async {
var loggedIn = await loadToken();
if (loggedIn == 'OK') {
Navigator.of(context).pushReplacementNamed('/voti');
} else {
Navigator.of(context).pushReplacementNamed('/login');
if (await Database.init() == 'ok') {
var loggedIn = await loadToken();
if (loggedIn == 'OK') {
Navigator.of(context).pushReplacementNamed('/voti');
} else {
Navigator.of(context).pushReplacementNamed('/login');
}
}
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ description: App non ufficiale per Argo ScuolaNext.
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+7
version: 1.0.0+8

environment:
sdk: ">=2.1.0 <3.0.0"
Expand Down

0 comments on commit 230a09c

Please sign in to comment.