2022-05-16 20:26:39 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-06-08 17:12:44 +02:00
|
|
|
import 'package:canteenlib/canteenlib.dart';
|
2022-05-02 12:07:47 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-06-08 17:12:44 +02:00
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
|
|
import 'package:flutter_native_timezone/flutter_native_timezone.dart';
|
2022-05-16 20:26:39 +02:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2022-05-09 16:55:20 +02:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2022-06-08 17:12:44 +02:00
|
|
|
import 'package:timezone/timezone.dart' as tz;
|
2022-05-16 20:26:39 +02:00
|
|
|
|
|
|
|
import '../lang/lang.dart';
|
2022-06-08 17:12:44 +02:00
|
|
|
import '../loginmanager.dart';
|
|
|
|
import '../util.dart';
|
2022-05-02 12:07:47 +02:00
|
|
|
|
|
|
|
class Nastaveni extends StatefulWidget {
|
2022-06-08 17:12:44 +02:00
|
|
|
const Nastaveni({Key? key, required this.n}) : super(key: key);
|
|
|
|
|
|
|
|
final FlutterLocalNotificationsPlugin n;
|
2022-05-02 12:07:47 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<Nastaveni> createState() => _NastaveniState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _NastaveniState extends State<Nastaveni> {
|
2022-05-09 16:55:20 +02:00
|
|
|
bool _ukladatOffline = false;
|
2022-05-16 20:26:39 +02:00
|
|
|
bool _preskakovatVikend = false;
|
2022-05-26 16:35:02 +02:00
|
|
|
bool _kontrolovatTyden = false;
|
2022-05-31 19:55:28 +02:00
|
|
|
bool _oznameniObed = false;
|
2022-06-08 19:06:30 +02:00
|
|
|
bool _zapamatovany = false;
|
2022-10-20 17:44:04 +02:00
|
|
|
bool _autoburza = false;
|
2022-06-08 17:12:44 +02:00
|
|
|
TimeOfDay _oznameniCas = TimeOfDay.now();
|
2022-05-09 16:55:20 +02:00
|
|
|
|
|
|
|
void najitNastaveni() async {
|
|
|
|
var preferences = await SharedPreferences.getInstance();
|
2022-06-08 19:06:30 +02:00
|
|
|
_zapamatovany = await LoginManager.zapamatovat();
|
2022-05-16 20:26:39 +02:00
|
|
|
setState(() {
|
|
|
|
_ukladatOffline = preferences.getBool("offline") ?? false;
|
|
|
|
_preskakovatVikend = preferences.getBool("skip") ?? false;
|
2022-05-26 16:35:02 +02:00
|
|
|
_kontrolovatTyden = preferences.getBool("tyden") ?? false;
|
2022-05-31 19:55:28 +02:00
|
|
|
_oznameniObed = preferences.getBool("oznamit") ?? false;
|
2022-10-20 17:44:04 +02:00
|
|
|
_autoburza = preferences.getBool("autoburza") ?? false;
|
2022-09-06 20:02:17 +02:00
|
|
|
var casStr = preferences.getString("oznameni_cas");
|
|
|
|
if (casStr == null) {
|
2022-06-08 17:12:44 +02:00
|
|
|
var now = DateTime.now();
|
2022-09-02 20:19:17 +02:00
|
|
|
_oznameniCas = TimeOfDay.fromDateTime(
|
|
|
|
DateTime.now().add(const Duration(hours: 1)));
|
2022-06-08 17:12:44 +02:00
|
|
|
preferences.setString("oznameni_cas", now.toString());
|
|
|
|
} else {
|
2022-09-06 20:02:17 +02:00
|
|
|
_oznameniCas = TimeOfDay.fromDateTime(DateTime.parse(casStr));
|
2022-06-08 17:12:44 +02:00
|
|
|
}
|
2022-05-16 20:26:39 +02:00
|
|
|
});
|
2022-05-09 16:55:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void zmenitNastaveni(String key, bool value) async {
|
|
|
|
var preferences = await SharedPreferences.getInstance();
|
|
|
|
preferences.setBool(key, value);
|
|
|
|
}
|
|
|
|
|
2022-05-16 20:26:39 +02:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
najitNastaveni();
|
|
|
|
}
|
|
|
|
|
2022-05-02 12:07:47 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2022-05-09 16:55:20 +02:00
|
|
|
return Scaffold(
|
|
|
|
appBar: AppBar(
|
2022-05-16 20:26:39 +02:00
|
|
|
title: Text(Languages.of(context)!.settings),
|
2022-05-09 16:55:20 +02:00
|
|
|
),
|
|
|
|
body: Center(
|
2022-05-16 20:26:39 +02:00
|
|
|
child: SizedBox(
|
|
|
|
width: MediaQuery.of(context).size.width / 1.1,
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
Text(Languages.of(context)!.saveOffline),
|
|
|
|
Switch(
|
|
|
|
value: _ukladatOffline,
|
|
|
|
onChanged: (value) {
|
2022-05-20 08:03:53 +02:00
|
|
|
setState(() {
|
2022-05-16 20:26:39 +02:00
|
|
|
_ukladatOffline = value;
|
2022-05-20 08:03:53 +02:00
|
|
|
cistit(value);
|
2022-05-16 20:26:39 +02:00
|
|
|
zmenitNastaveni("offline", value);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
Text(Languages.of(context)!.skipWeekend),
|
|
|
|
Switch(
|
|
|
|
value: _preskakovatVikend,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_preskakovatVikend = value;
|
|
|
|
zmenitNastaveni("skip", value);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
],
|
2022-05-26 16:35:02 +02:00
|
|
|
),
|
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
Flexible(child: Text(Languages.of(context)!.checkOrdered)),
|
|
|
|
Switch(
|
|
|
|
value: _kontrolovatTyden,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_kontrolovatTyden = value;
|
|
|
|
zmenitNastaveni("tyden", value);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
],
|
2022-05-31 19:55:28 +02:00
|
|
|
),
|
2022-10-20 17:44:04 +02:00
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
|
|
|
Flexible(child: Text(Languages.of(context)!.autoburzaSetting)),
|
|
|
|
Switch(
|
|
|
|
value: _autoburza,
|
|
|
|
onChanged: (value) {
|
|
|
|
setState(() {
|
|
|
|
_kontrolovatTyden = value;
|
|
|
|
zmenitNastaveni("autoburza", value);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
],
|
|
|
|
),
|
2022-05-31 19:55:28 +02:00
|
|
|
Row(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
|
|
children: [
|
2022-06-08 17:12:44 +02:00
|
|
|
Flexible(child: Text(Languages.of(context)!.notifyLunch)),
|
2022-05-31 19:55:28 +02:00
|
|
|
Switch(
|
|
|
|
value: _oznameniObed,
|
2022-06-08 19:06:30 +02:00
|
|
|
thumbColor: (!_zapamatovany
|
|
|
|
? MaterialStateProperty.all(Colors.grey)
|
|
|
|
: null),
|
2022-05-31 19:55:28 +02:00
|
|
|
onChanged: (value) {
|
2022-06-08 19:06:30 +02:00
|
|
|
if (!_zapamatovany) {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (bc) => AlertDialog(
|
|
|
|
title: Text(Languages.of(context)!.error),
|
|
|
|
content:
|
|
|
|
Text(Languages.of(context)!.needRemember),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
child: Text(Languages.of(context)!.ok),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
setState(() {
|
|
|
|
_oznameniObed = value;
|
|
|
|
if (_oznameniObed) {
|
2022-06-15 20:04:30 +02:00
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) => AlertDialog(
|
|
|
|
title:
|
|
|
|
Text(Languages.of(context)!.warning),
|
|
|
|
content: Text(
|
|
|
|
Languages.of(context)!.notifyWarning),
|
|
|
|
actions: [
|
|
|
|
TextButton(
|
|
|
|
child:
|
|
|
|
Text(Languages.of(context)!.ok),
|
|
|
|
onPressed: () {
|
|
|
|
Navigator.of(context).pop();
|
|
|
|
},
|
|
|
|
)
|
|
|
|
],
|
|
|
|
));
|
2022-06-08 19:06:30 +02:00
|
|
|
vytvoritOznameni(casNaDate(_oznameniCas));
|
|
|
|
}
|
|
|
|
zmenitNastaveni("oznamit", value);
|
|
|
|
});
|
|
|
|
}
|
2022-05-31 19:55:28 +02:00
|
|
|
})
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Text(Languages.of(context)!.notifyAt),
|
2022-06-08 17:12:44 +02:00
|
|
|
TextButton(
|
2022-06-08 19:06:30 +02:00
|
|
|
onPressed: () async {
|
|
|
|
if (_oznameniObed) {
|
|
|
|
var cas = await showTimePicker(
|
|
|
|
context: context, initialTime: _oznameniCas);
|
|
|
|
if (cas != null) {
|
|
|
|
var prefs = await SharedPreferences.getInstance();
|
|
|
|
prefs.setString("oznameni_cas",
|
|
|
|
casNaDate(cas).toString()); // aktualizovat vybraný čas
|
|
|
|
var den = casNaDate(cas);
|
|
|
|
debugPrint(den.isAfter(DateTime.now()).toString());
|
|
|
|
if (den.isAfter(DateTime.now())) {
|
|
|
|
// znovu vytvořit oznámení POUZE když je čas v budoucnosti
|
|
|
|
vytvoritOznameni(den);
|
2022-06-08 17:12:44 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-08 19:06:30 +02:00
|
|
|
setState(() {
|
|
|
|
_oznameniCas = cas ?? _oznameniCas;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
child: Text(
|
|
|
|
"${(_oznameniCas.hour < 10 ? "0" : "") + _oznameniCas.hour.toString()}:${(_oznameniCas.minute < 10 ? "0" : "") + _oznameniCas.minute.toString()}",
|
|
|
|
style: TextStyle(
|
|
|
|
color: (!_oznameniObed) ? Colors.grey : Colors.purple),
|
|
|
|
),
|
|
|
|
),
|
2022-05-16 20:26:39 +02:00
|
|
|
],
|
|
|
|
),
|
2022-05-09 16:55:20 +02:00
|
|
|
)),
|
|
|
|
);
|
2022-05-02 12:07:47 +02:00
|
|
|
}
|
2022-05-20 08:03:53 +02:00
|
|
|
|
|
|
|
void cistit(bool value) async {
|
|
|
|
if (!value) {
|
|
|
|
Directory appDocDir = await getApplicationDocumentsDirectory();
|
|
|
|
for (var f in appDocDir.listSync()) {
|
|
|
|
// Vymažeme obsah
|
|
|
|
if (f.path.contains("jidelnicek")) {
|
|
|
|
f.deleteSync();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-08 19:06:30 +02:00
|
|
|
|
|
|
|
void vytvoritOznameni(DateTime den) async {
|
|
|
|
await widget.n.cancelAll();
|
|
|
|
var d = await LoginManager.getDetails(); // získat údaje
|
|
|
|
if (d != null) {
|
|
|
|
// Nové oznámení
|
|
|
|
var c = Canteen(d["url"]!);
|
|
|
|
if (await c.login(d["user"]!, d["pass"]!)) {
|
|
|
|
var jidla = await c.jidelnicekDen();
|
|
|
|
try {
|
|
|
|
var jidlo = jidla.jidla.singleWhere((element) => element.objednano);
|
|
|
|
|
|
|
|
const AndroidNotificationDetails androidSpec =
|
|
|
|
AndroidNotificationDetails('opencanteen', 'predobjedem',
|
|
|
|
channelDescription: 'Oznámení o dnešním jídle',
|
|
|
|
importance: Importance.max,
|
|
|
|
priority: Priority.high,
|
|
|
|
ticker: 'today meal');
|
|
|
|
const IOSNotificationDetails iOSpec =
|
|
|
|
IOSNotificationDetails(presentAlert: true, presentBadge: true);
|
|
|
|
var l =
|
|
|
|
tz.getLocation(await FlutterNativeTimezone.getLocalTimezone());
|
2022-09-06 20:02:17 +02:00
|
|
|
if (!mounted) return;
|
2022-06-08 19:06:30 +02:00
|
|
|
await widget.n.zonedSchedule(
|
|
|
|
// Vytvoří nové oznámení pro daný čas a datum
|
|
|
|
0,
|
|
|
|
Languages.of(context)!.lunchNotif,
|
2022-09-02 20:19:17 +02:00
|
|
|
"${jidlo.varianta} - ${jidlo.nazev}",
|
2022-06-08 19:06:30 +02:00
|
|
|
tz.TZDateTime.from(den, l),
|
|
|
|
const NotificationDetails(android: androidSpec, iOS: iOSpec),
|
|
|
|
androidAllowWhileIdle: true,
|
|
|
|
uiLocalNotificationDateInterpretation:
|
|
|
|
UILocalNotificationDateInterpretation.absoluteTime);
|
|
|
|
} on StateError catch (_) {
|
|
|
|
// nenalezeno
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-02 12:07:47 +02:00
|
|
|
}
|