From 230a09cd9e72d73942808ccbb9d16c04f7a08fed Mon Sep 17 00:00:00 2001 From: peppelg Date: Sat, 2 Nov 2019 16:36:03 +0100 Subject: [PATCH] aggiunto orario --- README.md | 2 +- lib/api.dart | 62 +++++++++----- lib/backdropWidgets.dart | 7 ++ lib/database.dart | 26 ++++++ lib/main.dart | 2 + lib/orario.dart | 175 +++++++++++++++++++++++++++++++++++++++ lib/redirectRoute.dart | 15 ++-- pubspec.yaml | 2 +- 8 files changed, 260 insertions(+), 31 deletions(-) create mode 100644 lib/database.dart create mode 100644 lib/orario.dart diff --git a/README.md b/README.md index c12351c..d51d7ab 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/lib/api.dart b/lib/api.dart index cb3ee2f..2bde103 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -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 = { @@ -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'; @@ -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'], @@ -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)) diff --git a/lib/backdropWidgets.dart b/lib/backdropWidgets.dart index 0dca89d..a173142 100644 --- a/lib/backdropWidgets.dart +++ b/lib/backdropWidgets.dart @@ -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: () { diff --git a/lib/database.dart b/lib/database.dart new file mode 100644 index 0000000..dcaa742 --- /dev/null +++ b/lib/database.dart @@ -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; + } +} diff --git a/lib/main.dart b/lib/main.dart index 6946234..ae30020 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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'; @@ -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() diff --git a/lib/orario.dart b/lib/orario.dart new file mode 100644 index 0000000..8f8ebeb --- /dev/null +++ b/lib/orario.dart @@ -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 createState() { + return _OrarioRouteState(); + } +} + +class _OrarioRouteState extends State { + Map tabellaOrario = {}; + final nuovoNomeMateria = TextEditingController(); + final GlobalKey _refreshIndicatorKey = + new GlobalKey(); + + @override + Widget build(BuildContext context) { + var orarioColumns = [DataColumn(label: Text(''))]; + for (var giorno in tabellaOrario.keys) { + orarioColumns.add(DataColumn(label: Text(giorno))); + } + var orarioRows = []; + 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 = []; + 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 = [ + 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: [ + IconButton( + icon: Icon(Icons.refresh), + onPressed: () { + resetVoti(context); + }) + ], + frontLayer: RefreshIndicator( + key: _refreshIndicatorKey, + onRefresh: aggiornaOrario, + child: ListView( + children: new List.from([])..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: [ + 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: [ + 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: [ + 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()); + } +} diff --git a/lib/redirectRoute.dart b/lib/redirectRoute.dart index ac87a9e..74a7a57 100644 --- a/lib/redirectRoute.dart +++ b/lib/redirectRoute.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'database.dart'; import 'api.dart'; class RedirectRoute extends StatelessWidget { @@ -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'); + } } -} \ No newline at end of file +} diff --git a/pubspec.yaml b/pubspec.yaml index d2a14a1..2f2aa20 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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"