Zprovoznit offline ukladani jidelnicku a nastaveni

This commit is contained in:
Matyáš Caras 2022-05-16 20:26:39 +02:00
parent 9686b259d9
commit 1a602f0d2c
10 changed files with 377 additions and 53 deletions

View file

@ -3,6 +3,7 @@
- Stránka s možnostmi nastavení aplikace - Stránka s možnostmi nastavení aplikace
- Přidán anglický překlad - Přidán anglický překlad
- Opravy chyb - Opravy chyb
- Přidání možnosti přeskočení víkendu
# 0.1.1 # 0.1.1
- Přidán RefreshIndicator na obrazovku s jídelníčkem - Přidán RefreshIndicator na obrazovku s jídelníčkem
- Přidáno odsazení od okrajů u jídelníčku - Přidáno odsazení od okrajů u jídelníčku

View file

@ -10,7 +10,7 @@
<key>CFBundleDevelopmentRegion</key> <key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string> <string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleDisplayName</key> <key>CFBundleDisplayName</key>
<string>Opencanteen</string> <string>OpenCanteen</string>
<key>CFBundleExecutable</key> <key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string> <string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key> <key>CFBundleIdentifier</key>

View file

@ -112,4 +112,11 @@ abstract class Languages {
String get settings; String get settings;
String get saveOffline; String get saveOffline;
String get skipWeekend;
// Offline
String get offline;
String get mustLogout;
} }

View file

@ -69,6 +69,9 @@ class LanguageCz extends Languages {
@override @override
String get monday => "Pondělí"; String get monday => "Pondělí";
@override
String get mustLogout => "Online přejdete přetažením dolů.";
@override @override
String get no => "Ne"; String get no => "Ne";
@ -85,6 +88,9 @@ class LanguageCz extends Languages {
String get notOfficial => String get notOfficial =>
"Toto není oficiální aplikace k ovládání iCanteen. Autor neručí za ztráty nebo nefunkčnost v souvislosti s používáním této aplikace. Tato zpráva se znovu neukáže."; "Toto není oficiální aplikace k ovládání iCanteen. Autor neručí za ztráty nebo nefunkčnost v souvislosti s používáním této aplikace. Tato zpráva se znovu neukáže.";
@override
String get offline => "JSTE OFFLINE";
@override @override
String get ok => "OK"; String get ok => "OK";
@ -124,6 +130,9 @@ class LanguageCz extends Languages {
@override @override
String get signOut => "Odhlásit se"; String get signOut => "Odhlásit se";
@override
String get skipWeekend => "Při procházení menu přeskočit víkend";
@override @override
String get sunday => "Neděle"; String get sunday => "Neděle";

View file

@ -69,6 +69,9 @@ class LanguageEn extends Languages {
@override @override
String get monday => "Monday"; String get monday => "Monday";
@override
String get mustLogout => "To go online, pull down.";
@override @override
String get no => "No"; String get no => "No";
@ -85,6 +88,9 @@ class LanguageEn extends Languages {
String get notOfficial => String get notOfficial =>
"This is not an official app for accessing iCanteen. The author is not responsible for non-functionality or losses while using this app. This message will not appear again."; "This is not an official app for accessing iCanteen. The author is not responsible for non-functionality or losses while using this app. This message will not appear again.";
@override
String get offline => "YOU ARE OFFLINE";
@override @override
String get ok => "OK"; String get ok => "OK";
@ -116,7 +122,7 @@ class LanguageEn extends Languages {
String get saturday => "Saturday"; String get saturday => "Saturday";
@override @override
String get saveOffline => "Save today`s menu offline"; String get saveOffline => "Save today's menu offline";
@override @override
String get settings => "Settings"; String get settings => "Settings";
@ -124,6 +130,9 @@ class LanguageEn extends Languages {
@override @override
String get signOut => "Sign out"; String get signOut => "Sign out";
@override
String get skipWeekend => "Skip weekends when browsing menu";
@override @override
String get sunday => "Sunday"; String get sunday => "Sunday";

View file

@ -1,3 +1,6 @@
import 'dart:convert';
import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart'; import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
@ -5,6 +8,9 @@ import 'package:opencanteen/lang/lang_cz.dart';
import 'package:opencanteen/loginmanager.dart'; import 'package:opencanteen/loginmanager.dart';
import 'package:connectivity_plus/connectivity_plus.dart'; import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:canteenlib/canteenlib.dart'; import 'package:canteenlib/canteenlib.dart';
import 'package:opencanteen/okna/offline_jidelnicek.dart';
import 'package:opencanteen/util.dart';
import 'package:path_provider/path_provider.dart';
import 'lang/lang.dart'; import 'lang/lang.dart';
import 'lang/lang_en.dart'; import 'lang/lang_en.dart';
@ -82,6 +88,7 @@ class _LoginPageState extends State<LoginPage> {
content: Text(Languages.of(context)!.errorContacting), content: Text(Languages.of(context)!.errorContacting),
), ),
); );
goOffline();
} }
if (r != null) { if (r != null) {
@ -101,24 +108,34 @@ class _LoginPageState extends State<LoginPage> {
), ),
)); ));
var canteen = Canteen(r["url"]!); var canteen = Canteen(r["url"]!);
var l = await canteen.login(r["user"]!, r["pass"]!); try {
if (!l) { var l = await canteen.login(r["user"]!, r["pass"]!);
if (!l) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(Languages.of(context)!.loginFailed),
),
);
return;
}
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => JidelnicekPage(
user: r["user"]!,
canteen: canteen,
)),
);
} catch (_) {
ScaffoldMessenger.of(context).hideCurrentSnackBar(); ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text(Languages.of(context)!.loginFailed), content: Text(Languages.of(context)!.errorContacting),
), ),
); );
return; goOffline();
} }
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => JidelnicekPage(
user: r["user"]!,
canteen: canteen,
)),
);
} }
}); });
} }
@ -138,7 +155,7 @@ class _LoginPageState extends State<LoginPage> {
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
Text( Text(
Languages.of(context)!.logIn, Languages.of(context)!.appName,
textAlign: TextAlign.center, textAlign: TextAlign.center,
style: const TextStyle( style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 40), fontWeight: FontWeight.bold, fontSize: 40),
@ -237,29 +254,41 @@ class _LoginPageState extends State<LoginPage> {
"https://" + canteenControl.text; "https://" + canteenControl.text;
} }
var canteen = Canteen(canteenControl.text); var canteen = Canteen(canteenControl.text);
var l = await canteen.login( try {
userControl.text, passControl.text); var l = await canteen.login(
if (!l) { userControl.text, passControl.text);
if (!l) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content:
Text(Languages.of(context)!.loginFailed),
),
);
return;
}
if (rememberMe) {
LoginManager.setDetails(userControl.text,
passControl.text, canteenControl.text);
}
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => JidelnicekPage(
user: userControl.text,
canteen: canteen,
)),
);
} on Exception catch (_) {
ScaffoldMessenger.of(context).hideCurrentSnackBar(); ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text(Languages.of(context)!.loginFailed), content:
Text(Languages.of(context)!.errorContacting),
), ),
); );
return; goOffline();
} }
if (rememberMe) {
LoginManager.setDetails(userControl.text,
passControl.text, canteenControl.text);
}
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => JidelnicekPage(
user: userControl.text,
canteen: canteen,
)),
);
}, },
child: Text(Languages.of(context)!.logIn)), child: Text(Languages.of(context)!.logIn)),
], ],
@ -268,6 +297,32 @@ class _LoginPageState extends State<LoginPage> {
), ),
)); ));
} }
void goOffline() async {
Directory appDocDir = await getApplicationDocumentsDirectory();
var den = DateTime.now();
var soubor = File(appDocDir.path +
"/jidelnicek_${den.year}-${den.month}-${den.day}.json");
if (soubor.existsSync()) {
// načteme offline jídelníček
var input = await soubor.readAsString();
var r = jsonDecode(input);
List<OfflineJidlo> jidla = [];
for (var j in r) {
jidla.add(OfflineJidlo(
nazev: j["nazev"],
varianta: j["varianta"],
objednano: j["objednano"],
cena: j["cena"],
naBurze: j["naBurze"],
den: DateTime.parse(j["den"])));
}
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: ((context) => OfflineJidelnicek(jidla: jidla))));
}
}
} }
class AppLocalizationsDelegate extends LocalizationsDelegate<Languages> { class AppLocalizationsDelegate extends LocalizationsDelegate<Languages> {
@ -280,7 +335,6 @@ class AppLocalizationsDelegate extends LocalizationsDelegate<Languages> {
Future<Languages> load(Locale locale) => _load(locale); Future<Languages> load(Locale locale) => _load(locale);
static Future<Languages> _load(Locale locale) async { static Future<Languages> _load(Locale locale) async {
debugPrint(locale.countryCode);
switch (locale.languageCode) { switch (locale.languageCode) {
case 'cs': case 'cs':
return LanguageCz(); return LanguageCz();

View file

@ -4,6 +4,7 @@ import 'dart:io';
import 'package:canteenlib/canteenlib.dart'; import 'package:canteenlib/canteenlib.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:opencanteen/okna/nastaveni.dart';
import 'package:opencanteen/util.dart'; import 'package:opencanteen/util.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
@ -27,6 +28,7 @@ class _JidelnicekPageState extends State<JidelnicekPage> {
DateTime den = DateTime.now(); DateTime den = DateTime.now();
String denTydne = ""; String denTydne = "";
double kredit = 0.0; double kredit = 0.0;
bool _skipWeekend = false;
Future<void> nactiJidlo() async { Future<void> nactiJidlo() async {
obsah = [const CircularProgressIndicator()]; obsah = [const CircularProgressIndicator()];
switch (den.weekday) { switch (den.weekday) {
@ -207,9 +209,17 @@ class _JidelnicekPageState extends State<JidelnicekPage> {
} else if (value == Languages.of(context)!.about) { } else if (value == Languages.of(context)!.about) {
Navigator.push( Navigator.push(
context, MaterialPageRoute(builder: (c) => const AboutPage())); context, MaterialPageRoute(builder: (c) => const AboutPage()));
} else if (value == Languages.of(context)!.settings) {
Navigator.push(
context, MaterialPageRoute(builder: (c) => const Nastaveni()));
} }
} }
void nactiNastaveni() async {
var prefs = await SharedPreferences.getInstance();
_skipWeekend = prefs.getBool("skip") ?? false;
}
/// uložení jídelníčku pro dnešek offline /// uložení jídelníčku pro dnešek offline
void ulozitDnesekOffline() async { void ulozitDnesekOffline() async {
var prefs = await SharedPreferences.getInstance(); var prefs = await SharedPreferences.getInstance();
@ -233,7 +243,8 @@ class _JidelnicekPageState extends State<JidelnicekPage> {
"varianta": jidlo.varianta, "varianta": jidlo.varianta,
"objednano": jidlo.objednano, "objednano": jidlo.objednano,
"cena": jidlo.cena, "cena": jidlo.cena,
"naBurze": jidlo.naBurze "naBurze": jidlo.naBurze,
"den": den.toString()
}); });
} }
await soubor.writeAsString(json.encode(jidla)); await soubor.writeAsString(json.encode(jidla));
@ -243,6 +254,7 @@ class _JidelnicekPageState extends State<JidelnicekPage> {
@override @override
void didChangeDependencies() { void didChangeDependencies() {
super.didChangeDependencies(); super.didChangeDependencies();
nactiNastaveni();
ulozitDnesekOffline(); ulozitDnesekOffline();
nactiJidlo(); nactiJidlo();
} }
@ -259,6 +271,7 @@ class _JidelnicekPageState extends State<JidelnicekPage> {
itemBuilder: (BuildContext context) { itemBuilder: (BuildContext context) {
return { return {
Languages.of(context)!.reportBugs, Languages.of(context)!.reportBugs,
Languages.of(context)!.settings,
Languages.of(context)!.about, Languages.of(context)!.about,
Languages.of(context)!.signOut Languages.of(context)!.signOut
}.map((String choice) { }.map((String choice) {
@ -283,6 +296,9 @@ class _JidelnicekPageState extends State<JidelnicekPage> {
onPressed: () { onPressed: () {
setState(() { setState(() {
den = den.subtract(const Duration(days: 1)); den = den.subtract(const Duration(days: 1));
if (den.weekday == 7 && _skipWeekend) {
den = den.subtract(const Duration(days: 2));
}
nactiJidlo(); nactiJidlo();
}); });
}, },
@ -308,6 +324,9 @@ class _JidelnicekPageState extends State<JidelnicekPage> {
onPressed: () { onPressed: () {
setState(() { setState(() {
den = den.add(const Duration(days: 1)); den = den.add(const Duration(days: 1));
if (den.weekday == 6 && _skipWeekend) {
den = den.add(const Duration(days: 2));
}
nactiJidlo(); nactiJidlo();
}); });
}, },
@ -328,11 +347,17 @@ class _JidelnicekPageState extends State<JidelnicekPage> {
if (details.primaryVelocity?.compareTo(0) == -1) { if (details.primaryVelocity?.compareTo(0) == -1) {
setState(() { setState(() {
den = den.add(const Duration(days: 1)); den = den.add(const Duration(days: 1));
if (den.weekday == 6 && _skipWeekend) {
den = den.add(const Duration(days: 2));
}
nactiJidlo(); nactiJidlo();
}); });
} else { } else {
setState(() { setState(() {
den = den.subtract(const Duration(days: 1)); den = den.subtract(const Duration(days: 1));
if (den.weekday == 7 && _skipWeekend) {
den = den.subtract(const Duration(days: 2));
}
nactiJidlo(); nactiJidlo();
}); });
} }

View file

@ -1,6 +1,10 @@
import 'dart:io';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
// TODO
import '../lang/lang.dart';
class Nastaveni extends StatefulWidget { class Nastaveni extends StatefulWidget {
const Nastaveni({Key? key}) : super(key: key); const Nastaveni({Key? key}) : super(key: key);
@ -11,10 +15,14 @@ class Nastaveni extends StatefulWidget {
class _NastaveniState extends State<Nastaveni> { class _NastaveniState extends State<Nastaveni> {
bool _ukladatOffline = false; bool _ukladatOffline = false;
bool _preskakovatVikend = false;
void najitNastaveni() async { void najitNastaveni() async {
var preferences = await SharedPreferences.getInstance(); var preferences = await SharedPreferences.getInstance();
_ukladatOffline = preferences.getBool("offline") ?? false; setState(() {
_ukladatOffline = preferences.getBool("offline") ?? false;
_preskakovatVikend = preferences.getBool("skip") ?? false;
});
} }
void zmenitNastaveni(String key, bool value) async { void zmenitNastaveni(String key, bool value) async {
@ -22,29 +30,63 @@ class _NastaveniState extends State<Nastaveni> {
preferences.setBool(key, value); preferences.setBool(key, value);
} }
@override
void initState() {
super.initState();
najitNastaveni();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: const Text('Nastavení'), title: Text(Languages.of(context)!.settings),
), ),
body: Center( body: Center(
child: Column( child: SizedBox(
children: [ width: MediaQuery.of(context).size.width / 1.1,
Row( child: Column(
children: [ children: [
const Text("Ukládat jídelníček na dnešní den offline"), Row(
Switch( mainAxisAlignment: MainAxisAlignment.spaceBetween,
value: _ukladatOffline, children: [
onChanged: (value) { Text(Languages.of(context)!.saveOffline),
setState(() { Switch(
_ukladatOffline = value; value: _ukladatOffline,
zmenitNastaveni("offline", value); onChanged: (value) {
}); setState(() async {
}) _ukladatOffline = value;
], if (!value) {
) Directory appDocDir =
], await getApplicationDocumentsDirectory();
for (var f in appDocDir.listSync()) {
// Vymažeme obsah
if (f.path.contains("jidelnicek")) {
f.deleteSync();
}
}
}
zmenitNastaveni("offline", value);
});
})
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(Languages.of(context)!.skipWeekend),
Switch(
value: _preskakovatVikend,
onChanged: (value) {
setState(() {
_preskakovatVikend = value;
zmenitNastaveni("skip", value);
});
})
],
)
],
),
)), )),
); );
} }

View file

@ -0,0 +1,160 @@
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:opencanteen/okna/nastaveni.dart';
import 'package:opencanteen/util.dart';
import 'package:url_launcher/url_launcher.dart';
import '../lang/lang.dart';
import '../main.dart';
import 'about.dart';
class OfflineJidelnicek extends StatefulWidget {
const OfflineJidelnicek({Key? key, required this.jidla}) : super(key: key);
final List<OfflineJidlo> jidla;
@override
State<OfflineJidelnicek> createState() => _OfflineJidelnicekState();
}
class _OfflineJidelnicekState extends State<OfflineJidelnicek> {
List<Widget> obsah = [const CircularProgressIndicator()];
DateTime den = DateTime.now();
String denTydne = "";
Future<void> nactiJidlo() async {
den = widget.jidla[0].den;
switch (den.weekday) {
case 2:
denTydne = Languages.of(context)!.tuesday;
break;
case 3:
denTydne = Languages.of(context)!.wednesday;
break;
case 4:
denTydne = Languages.of(context)!.thursday;
break;
case 5:
denTydne = Languages.of(context)!.friday;
break;
case 6:
denTydne = Languages.of(context)!.saturday;
break;
case 7:
denTydne = Languages.of(context)!.sunday;
break;
default:
denTydne = Languages.of(context)!.monday;
}
obsah = [];
for (OfflineJidlo j in widget.jidla) {
obsah.add(
Padding(
padding: const EdgeInsets.only(top: 15),
child: InkWell(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(j.varianta),
const SizedBox(width: 10),
Flexible(
child: Text(
j.nazev,
),
),
Text((j.naBurze)
? Languages.of(context)!.inExchange
: "${j.cena}"),
Checkbox(
value: j.objednano,
fillColor: MaterialStateProperty.all(Colors.grey),
onChanged: (v) async {
return;
})
],
)),
),
);
}
setState(() {});
}
void kliknuti(String value, BuildContext context) {
if (value == Languages.of(context)!.signOut) {
const storage = FlutterSecureStorage();
storage.deleteAll();
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (c) => const LoginPage()));
} else if (value == Languages.of(context)!.reportBugs) {
launch("https://github.com/hernikplays/opencanteen/issues/new/choose");
} else if (value == Languages.of(context)!.about) {
Navigator.push(
context, MaterialPageRoute(builder: (c) => const AboutPage()));
} else if (value == Languages.of(context)!.settings) {
Navigator.push(
context, MaterialPageRoute(builder: (c) => const Nastaveni()));
}
}
@override
void didChangeDependencies() {
super.didChangeDependencies();
nactiJidlo();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(Languages.of(context)!.menu),
automaticallyImplyLeading: false,
actions: [
PopupMenuButton(
onSelected: ((String value) => kliknuti(value, context)),
itemBuilder: (BuildContext context) {
return {
Languages.of(context)!.reportBugs,
Languages.of(context)!.settings,
Languages.of(context)!.about,
Languages.of(context)!.signOut
}.map((String choice) {
return PopupMenuItem<String>(
value: choice,
child: Text(choice),
);
}).toList();
},
),
],
),
body: RefreshIndicator(
child: Center(
child: SizedBox(
child: Column(
children: [
const SizedBox(height: 10),
Text(
Languages.of(context)!.offline,
style: const TextStyle(fontWeight: FontWeight.bold),
),
Text(Languages.of(context)!.mustLogout),
const SizedBox(height: 10),
TextButton(
child:
Text("${den.day}. ${den.month}. ${den.year} - $denTydne"),
onPressed: () => {},
),
SingleChildScrollView(
physics: const AlwaysScrollableScrollPhysics(),
child: Column(
children: obsah,
),
),
],
),
width: MediaQuery.of(context).size.width - 50,
),
),
onRefresh: () => Navigator.pushReplacement(context,
MaterialPageRoute(builder: ((context) => const LoginPage()))),
),
);
}
}

View file

@ -66,3 +66,20 @@ Drawer drawerGenerator(
} }
return drawer; return drawer;
} }
class OfflineJidlo {
String nazev;
String varianta;
bool objednano;
double cena;
bool naBurze;
DateTime den;
OfflineJidlo(
{required this.nazev,
required this.varianta,
required this.objednano,
required this.cena,
required this.naBurze,
required DateTime this.den});
}