2022-11-21 20:26:55 +01:00
|
|
|
import 'dart:convert';
|
2022-10-04 17:45:31 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-05-16 20:26:39 +02:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
2022-12-14 20:02:32 +01:00
|
|
|
import 'package:opencanteen/okna/login.dart';
|
2023-01-28 14:30:54 +01:00
|
|
|
import 'package:opencanteen/pw/platformbutton.dart';
|
2022-05-16 20:26:39 +02:00
|
|
|
import 'package:opencanteen/util.dart';
|
2022-09-26 16:46:22 +02:00
|
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
2022-11-21 20:26:55 +01:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2022-05-16 20:26:39 +02:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
|
2023-01-28 15:41:17 +01:00
|
|
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
2022-05-16 20:26:39 +02:00
|
|
|
|
2023-01-28 14:30:54 +01:00
|
|
|
class OfflineMealView extends StatefulWidget {
|
2023-11-28 20:09:17 +01:00
|
|
|
const OfflineMealView({super.key});
|
2022-05-16 20:26:39 +02:00
|
|
|
@override
|
2023-01-28 14:30:54 +01:00
|
|
|
State<OfflineMealView> createState() => _OfflineMealViewState();
|
2022-05-16 20:26:39 +02:00
|
|
|
}
|
|
|
|
|
2023-01-28 14:30:54 +01:00
|
|
|
class _OfflineMealViewState extends State<OfflineMealView> {
|
|
|
|
List<Widget> content = [const CircularProgressIndicator()]; // view content
|
|
|
|
var _skipWeekend = false; // skip weekend setting
|
|
|
|
DateTime currentDay = DateTime.now(); // the day we are supposed to show
|
|
|
|
String dayOWeek = ""; // the name of the day (to show to user)
|
|
|
|
List<List<OfflineMeal>> data = []; // meal data
|
|
|
|
var mealIndex = 0; // index of the currently shown day
|
2022-11-21 20:26:55 +01:00
|
|
|
|
2023-01-28 14:30:54 +01:00
|
|
|
/// Loads the offline data from local storage
|
|
|
|
void loadFromFile() async {
|
2022-11-21 20:26:55 +01:00
|
|
|
Directory appDocDir = await getApplicationDocumentsDirectory();
|
|
|
|
for (var f in appDocDir.listSync()) {
|
|
|
|
if (f.path.contains("jidelnicek")) {
|
|
|
|
var soubor = File(f.path);
|
|
|
|
var input = await soubor.readAsString();
|
|
|
|
var r = jsonDecode(input);
|
2023-01-28 14:30:54 +01:00
|
|
|
List<OfflineMeal> jidla = [];
|
2022-11-21 20:26:55 +01:00
|
|
|
for (var j in r) {
|
2023-01-28 14:30:54 +01:00
|
|
|
jidla.add(OfflineMeal(
|
|
|
|
name: j["nazev"],
|
|
|
|
variant: j["varianta"],
|
|
|
|
ordered: j["objednano"],
|
|
|
|
price: j["cena"],
|
|
|
|
onExchange: j["naBurze"],
|
|
|
|
day: DateTime.parse(j["den"])));
|
2022-11-21 20:26:55 +01:00
|
|
|
}
|
|
|
|
data.add(jidla);
|
|
|
|
}
|
|
|
|
}
|
2023-01-28 14:30:54 +01:00
|
|
|
loadFood();
|
2022-11-21 20:26:55 +01:00
|
|
|
}
|
|
|
|
|
2023-01-28 14:30:54 +01:00
|
|
|
Future<void> loadFood() async {
|
|
|
|
var jidelnicek = data[mealIndex];
|
|
|
|
currentDay = jidelnicek[0].day;
|
|
|
|
switch (currentDay.weekday) {
|
2022-05-16 20:26:39 +02:00
|
|
|
case 2:
|
2023-01-28 15:41:17 +01:00
|
|
|
dayOWeek = AppLocalizations.of(context)!.tuesday;
|
2022-05-16 20:26:39 +02:00
|
|
|
break;
|
|
|
|
case 3:
|
2023-01-28 15:41:17 +01:00
|
|
|
dayOWeek = AppLocalizations.of(context)!.wednesday;
|
2022-05-16 20:26:39 +02:00
|
|
|
break;
|
|
|
|
case 4:
|
2023-01-28 15:41:17 +01:00
|
|
|
dayOWeek = AppLocalizations.of(context)!.thursday;
|
2022-05-16 20:26:39 +02:00
|
|
|
break;
|
|
|
|
case 5:
|
2023-01-28 15:41:17 +01:00
|
|
|
dayOWeek = AppLocalizations.of(context)!.friday;
|
2022-05-16 20:26:39 +02:00
|
|
|
break;
|
|
|
|
case 6:
|
2023-01-28 15:41:17 +01:00
|
|
|
dayOWeek = AppLocalizations.of(context)!.saturday;
|
2022-05-16 20:26:39 +02:00
|
|
|
break;
|
|
|
|
case 7:
|
2023-01-28 15:41:17 +01:00
|
|
|
dayOWeek = AppLocalizations.of(context)!.sunday;
|
2022-05-16 20:26:39 +02:00
|
|
|
break;
|
|
|
|
default:
|
2023-01-28 15:41:17 +01:00
|
|
|
dayOWeek = AppLocalizations.of(context)!.monday;
|
2022-05-16 20:26:39 +02:00
|
|
|
}
|
2023-01-28 14:30:54 +01:00
|
|
|
content = [];
|
|
|
|
for (OfflineMeal j in jidelnicek) {
|
|
|
|
content.add(
|
2022-05-16 20:26:39 +02:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(top: 15),
|
|
|
|
child: InkWell(
|
2022-11-21 20:26:55 +01:00
|
|
|
child: Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
2023-01-28 14:30:54 +01:00
|
|
|
Text(j.variant),
|
2022-11-21 20:26:55 +01:00
|
|
|
const SizedBox(width: 10),
|
|
|
|
Flexible(
|
|
|
|
child: Text(
|
2023-01-28 14:30:54 +01:00
|
|
|
j.name,
|
2022-11-21 20:26:55 +01:00
|
|
|
),
|
2022-05-16 20:26:39 +02:00
|
|
|
),
|
2023-01-28 14:30:54 +01:00
|
|
|
Text((j.onExchange)
|
2023-01-28 15:41:17 +01:00
|
|
|
? AppLocalizations.of(context)!.inExchange
|
2023-01-28 14:30:54 +01:00
|
|
|
: "${j.price} Kč"),
|
2022-11-21 20:26:55 +01:00
|
|
|
Checkbox(
|
2023-01-28 14:30:54 +01:00
|
|
|
value: j.ordered,
|
2022-12-12 16:48:19 +01:00
|
|
|
fillColor: MaterialStateProperty.all(Colors.grey),
|
|
|
|
onChanged: (v) async {
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
)
|
2022-11-21 20:26:55 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
2022-05-16 20:26:39 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
setState(() {});
|
|
|
|
}
|
|
|
|
|
2023-01-28 14:30:54 +01:00
|
|
|
void click(String value, BuildContext context) async {
|
2023-01-28 15:41:17 +01:00
|
|
|
if (value == AppLocalizations.of(context)!.signOut) {
|
2022-05-16 20:26:39 +02:00
|
|
|
const storage = FlutterSecureStorage();
|
|
|
|
storage.deleteAll();
|
|
|
|
Navigator.pushReplacement(
|
2023-01-28 14:30:54 +01:00
|
|
|
context, platformRouter((c) => const LoginPage()));
|
2023-01-28 15:41:17 +01:00
|
|
|
} else if (value == AppLocalizations.of(context)!.review) {
|
2023-01-28 14:30:54 +01:00
|
|
|
launchUrl(
|
|
|
|
Uri.parse((Platform.isAndroid)
|
|
|
|
? "market://details?id=cz.hernikplays.opencanteen"
|
|
|
|
: "https://apps.apple.com/cz/app/opencanteen/id1621124445"),
|
2022-12-12 17:52:33 +01:00
|
|
|
mode: LaunchMode.externalApplication);
|
2023-01-28 15:41:17 +01:00
|
|
|
} else if (value == AppLocalizations.of(context)!.reportBugs) {
|
2022-10-04 17:45:31 +02:00
|
|
|
launchUrl(Uri.parse("https://forms.gle/jKN7QeFJwpaApSbC8"),
|
|
|
|
mode: LaunchMode.externalApplication);
|
2023-01-28 15:41:17 +01:00
|
|
|
} else if (value == AppLocalizations.of(context)!.about) {
|
2022-09-26 16:46:22 +02:00
|
|
|
var packageInfo = await PackageInfo.fromPlatform();
|
|
|
|
if (!mounted) return;
|
|
|
|
showAboutDialog(
|
2023-01-28 14:30:54 +01:00
|
|
|
context: context,
|
|
|
|
applicationName: "OpenCanteen",
|
|
|
|
applicationLegalese:
|
2023-01-28 15:41:17 +01:00
|
|
|
"${AppLocalizations.of(context)!.copyright}\n${AppLocalizations.of(context)!.license}",
|
2023-01-28 14:30:54 +01:00
|
|
|
applicationVersion: packageInfo.version,
|
|
|
|
children: [
|
|
|
|
PlatformButton(
|
|
|
|
onPressed: (() => launchUrl(
|
|
|
|
Uri.parse("https://git.mnau.xyz/hernik/opencanteen"))),
|
2023-01-28 15:41:17 +01:00
|
|
|
text: AppLocalizations.of(context)!.source,
|
2023-01-28 14:30:54 +01:00
|
|
|
)
|
|
|
|
],
|
|
|
|
);
|
2022-05-16 20:26:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void didChangeDependencies() {
|
|
|
|
super.didChangeDependencies();
|
2023-01-28 14:30:54 +01:00
|
|
|
loadSettings();
|
2022-11-21 20:26:55 +01:00
|
|
|
}
|
|
|
|
|
2023-01-28 14:30:54 +01:00
|
|
|
void loadSettings() async {
|
2022-11-21 20:26:55 +01:00
|
|
|
var prefs = await SharedPreferences.getInstance();
|
|
|
|
_skipWeekend = prefs.getBool("skip") ?? false;
|
|
|
|
if (!mounted) return;
|
2023-01-28 14:30:54 +01:00
|
|
|
loadFromFile();
|
2022-05-16 20:26:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2023-01-28 15:41:17 +01:00
|
|
|
title: Text(AppLocalizations.of(context)!.menu),
|
2022-05-16 20:26:39 +02:00
|
|
|
automaticallyImplyLeading: false,
|
|
|
|
actions: [
|
|
|
|
PopupMenuButton(
|
2023-01-28 14:30:54 +01:00
|
|
|
onSelected: ((String value) => click(value, context)),
|
2022-05-16 20:26:39 +02:00
|
|
|
itemBuilder: (BuildContext context) {
|
|
|
|
return {
|
2023-01-28 15:41:17 +01:00
|
|
|
AppLocalizations.of(context)!.reportBugs,
|
|
|
|
AppLocalizations.of(context)!.review,
|
|
|
|
AppLocalizations.of(context)!.about,
|
|
|
|
AppLocalizations.of(context)!.signOut
|
2022-05-16 20:26:39 +02:00
|
|
|
}.map((String choice) {
|
|
|
|
return PopupMenuItem<String>(
|
|
|
|
value: choice,
|
|
|
|
child: Text(choice),
|
|
|
|
);
|
|
|
|
}).toList();
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
body: RefreshIndicator(
|
|
|
|
child: Center(
|
|
|
|
child: SizedBox(
|
2022-09-06 20:02:17 +02:00
|
|
|
width: MediaQuery.of(context).size.width - 50,
|
2022-05-16 20:26:39 +02:00
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
const SizedBox(height: 10),
|
|
|
|
Text(
|
2023-01-28 15:41:17 +01:00
|
|
|
AppLocalizations.of(context)!.offline,
|
2022-05-16 20:26:39 +02:00
|
|
|
style: const TextStyle(fontWeight: FontWeight.bold),
|
|
|
|
),
|
2023-01-28 15:41:17 +01:00
|
|
|
Text(AppLocalizations.of(context)!.mustLogout),
|
2022-05-16 20:26:39 +02:00
|
|
|
const SizedBox(height: 10),
|
2022-11-21 20:26:55 +01:00
|
|
|
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
if (data.length <= 1) return;
|
2023-01-28 14:30:54 +01:00
|
|
|
content = [const CircularProgressIndicator()];
|
2022-11-21 20:26:55 +01:00
|
|
|
setState(() {
|
2023-01-28 14:30:54 +01:00
|
|
|
if (currentDay.weekday == 1 && _skipWeekend) {
|
2022-11-21 20:26:55 +01:00
|
|
|
// pokud je pondělí a chceme přeskočit víkend
|
2023-01-28 14:30:54 +01:00
|
|
|
if (mealIndex - 2 >= 0) {
|
|
|
|
mealIndex -= data.length - 3;
|
2022-11-21 20:26:55 +01:00
|
|
|
} else {
|
2023-01-28 14:30:54 +01:00
|
|
|
mealIndex = data.length - 1;
|
2022-11-21 20:26:55 +01:00
|
|
|
}
|
2023-01-28 14:30:54 +01:00
|
|
|
} else if (mealIndex == 0) {
|
|
|
|
mealIndex = data.length - 1;
|
2022-11-21 20:26:55 +01:00
|
|
|
} else {
|
2023-01-28 14:30:54 +01:00
|
|
|
mealIndex -= 1;
|
2022-11-21 20:26:55 +01:00
|
|
|
}
|
|
|
|
|
2023-01-28 14:30:54 +01:00
|
|
|
loadFood();
|
2022-11-21 20:26:55 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.arrow_left)),
|
2023-01-28 14:30:54 +01:00
|
|
|
PlatformButton(
|
2022-11-21 20:26:55 +01:00
|
|
|
onPressed: () async {},
|
2023-01-28 14:30:54 +01:00
|
|
|
text:
|
|
|
|
"${currentDay.day}. ${currentDay.month}. ${currentDay.year} - $dayOWeek"),
|
2022-11-21 20:26:55 +01:00
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
if (data.length <= 1) return;
|
2023-01-28 14:30:54 +01:00
|
|
|
content = [const CircularProgressIndicator()];
|
2022-11-21 20:26:55 +01:00
|
|
|
setState(() {
|
2023-01-28 14:30:54 +01:00
|
|
|
if (currentDay.weekday == 5 && _skipWeekend) {
|
2022-11-21 20:26:55 +01:00
|
|
|
// pokud je pondělí a chceme přeskočit víkend
|
2023-01-28 14:30:54 +01:00
|
|
|
if (mealIndex + 2 <= data.length - 1) {
|
|
|
|
mealIndex += 2;
|
2022-11-21 20:26:55 +01:00
|
|
|
} else {
|
2023-01-28 14:30:54 +01:00
|
|
|
mealIndex = 0;
|
2022-11-21 20:26:55 +01:00
|
|
|
}
|
2023-01-28 14:30:54 +01:00
|
|
|
} else if (mealIndex == data.length) {
|
|
|
|
mealIndex = 0;
|
2022-11-21 20:26:55 +01:00
|
|
|
} else {
|
2023-01-28 14:30:54 +01:00
|
|
|
mealIndex += 1;
|
2022-11-21 20:26:55 +01:00
|
|
|
}
|
2023-01-28 14:30:54 +01:00
|
|
|
loadFood();
|
2022-11-21 20:26:55 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
icon: const Icon(Icons.arrow_right),
|
|
|
|
),
|
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
2023-01-28 14:30:54 +01:00
|
|
|
mealIndex = 0;
|
2022-11-21 20:26:55 +01:00
|
|
|
},
|
|
|
|
icon: const Icon(Icons.today))
|
|
|
|
]),
|
2022-05-16 20:26:39 +02:00
|
|
|
SingleChildScrollView(
|
|
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
|
|
child: Column(
|
2023-01-28 14:30:54 +01:00
|
|
|
children: content,
|
2022-05-16 20:26:39 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2023-01-28 14:30:54 +01:00
|
|
|
onRefresh: () => Navigator.pushReplacement(
|
|
|
|
context, platformRouter((context) => const LoginPage())),
|
2022-05-16 20:26:39 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|