Compare commits
1 commit
Author | SHA1 | Date | |
---|---|---|---|
|
02078987c6 |
3 changed files with 99 additions and 42 deletions
|
@ -1,16 +1,46 @@
|
||||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||||
|
|
||||||
class LoginManager {
|
class LoginManager {
|
||||||
static Future<Map<String, String>?> getDetails() async {
|
static Future<Map<String, String>?> getDetails(String id) async {
|
||||||
// zkontrolovat secure storage pokud je něco uložené
|
// zkontrolovat secure storage pokud je něco uložené
|
||||||
const storage = FlutterSecureStorage();
|
const storage = FlutterSecureStorage();
|
||||||
var user = await storage.read(key: "oc_user");
|
var user = await storage.read(key: "oc_user_$id");
|
||||||
var pass = await storage.read(key: "oc_pass");
|
var pass = await storage.read(key: "oc_pass_$id");
|
||||||
var url = await storage.read(key: "oc_url");
|
var url = await storage.read(key: "oc_url_$id");
|
||||||
if (user == null || pass == null || url == null) return null;
|
if (user == null || pass == null || url == null) return null;
|
||||||
return {"user": user, "pass": pass, "url": url};
|
return {"user": user, "pass": pass, "url": url};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Future<Map<int, List<String>>> ziskatVsechnyUlozene() async {
|
||||||
|
const storage = FlutterSecureStorage();
|
||||||
|
|
||||||
|
if (await storage.containsKey(key: "oc_user")) {
|
||||||
|
await prevest();
|
||||||
|
}
|
||||||
|
var vsechno = await storage.readAll();
|
||||||
|
vsechno.removeWhere((key, value) => key.startsWith("oc_pass_"));
|
||||||
|
var uzivatele = <int, List<String>>{};
|
||||||
|
vsechno.forEach((key, value) {
|
||||||
|
if (key.startsWith("oc_user_")) {
|
||||||
|
var user = int.parse(key.substring(8));
|
||||||
|
if (uzivatele[user] == null) {
|
||||||
|
uzivatele[user] = [value];
|
||||||
|
} else {
|
||||||
|
uzivatele[user]!.add(value);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var user = int.parse(key.substring(8));
|
||||||
|
if (uzivatele[user] == null) {
|
||||||
|
uzivatele[user] = [value];
|
||||||
|
} else {
|
||||||
|
uzivatele[user]!.add(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
print(uzivatele);
|
||||||
|
return uzivatele;
|
||||||
|
}
|
||||||
|
|
||||||
static Future<void> setDetails(String user, String pass, String url) async {
|
static Future<void> setDetails(String user, String pass, String url) async {
|
||||||
const storage = FlutterSecureStorage();
|
const storage = FlutterSecureStorage();
|
||||||
await storage.write(key: "oc_user", value: user);
|
await storage.write(key: "oc_user", value: user);
|
||||||
|
@ -22,4 +52,21 @@ class LoginManager {
|
||||||
const storage = FlutterSecureStorage();
|
const storage = FlutterSecureStorage();
|
||||||
return await storage.containsKey(key: "oc_pass");
|
return await storage.containsKey(key: "oc_pass");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Převést na nový formát ukládání
|
||||||
|
static Future<bool> prevest() async {
|
||||||
|
const storage = FlutterSecureStorage();
|
||||||
|
var user = await storage.read(key: "oc_user");
|
||||||
|
var pass = await storage.read(key: "oc_pass");
|
||||||
|
var url = await storage.read(key: "oc_url");
|
||||||
|
if (user == null || pass == null || url == null) return false;
|
||||||
|
await storage.write(key: "oc_user_0", value: user);
|
||||||
|
await storage.write(key: "oc_pass_0", value: pass);
|
||||||
|
await storage.write(key: "oc_url_0", value: url);
|
||||||
|
|
||||||
|
await storage.delete(key: "oc_user");
|
||||||
|
await storage.delete(key: "oc_pass");
|
||||||
|
await storage.delete(key: "oc_url");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,46 +59,48 @@ void oznamitPredem(SharedPreferences prefs, tz.Location l) async {
|
||||||
// TODO možnost brát z offline dat
|
// TODO možnost brát z offline dat
|
||||||
} else {*/
|
} else {*/
|
||||||
// bere online
|
// bere online
|
||||||
var d = await LoginManager.getDetails(); // získat údaje
|
if ((await LoginManager.ziskatVsechnyUlozene()).isNotEmpty) {
|
||||||
if (d != null) {
|
var d = await LoginManager.getDetails("0s"); // získat údaje
|
||||||
var c = Canteen(d["url"]!);
|
if (d != null) {
|
||||||
if (await c.login(d["user"]!, d["pass"]!)) {
|
var c = Canteen(d["url"]!);
|
||||||
var jidla = await c.jidelnicekDen();
|
if (await c.login(d["user"]!, d["pass"]!)) {
|
||||||
try {
|
var jidla = await c.jidelnicekDen();
|
||||||
var jidlo = jidla.jidla.singleWhere(
|
try {
|
||||||
(element) => element.objednano); // získá objednané jídlo
|
var jidlo = jidla.jidla.singleWhere(
|
||||||
var kdy = DateTime.parse(prefs.getString(
|
(element) => element.objednano); // získá objednané jídlo
|
||||||
"oznameni_cas")!); // uložíme čas, kdy se má odeslat oznámení
|
var kdy = DateTime.parse(prefs.getString(
|
||||||
|
"oznameni_cas")!); // uložíme čas, kdy se má odeslat oznámení
|
||||||
|
|
||||||
// data o oznámení
|
// data o oznámení
|
||||||
const AndroidNotificationDetails androidSpec =
|
const AndroidNotificationDetails androidSpec =
|
||||||
AndroidNotificationDetails('predobedem', 'Oznámení před obědem',
|
AndroidNotificationDetails('predobedem', 'Oznámení před obědem',
|
||||||
channelDescription: 'Oznámení o dnešním jídle',
|
channelDescription: 'Oznámení o dnešním jídle',
|
||||||
importance: Importance.max,
|
importance: Importance.max,
|
||||||
priority: Priority.high,
|
priority: Priority.high,
|
||||||
ticker: 'today meal');
|
ticker: 'today meal');
|
||||||
const IOSNotificationDetails iOSpec =
|
const IOSNotificationDetails iOSpec =
|
||||||
IOSNotificationDetails(presentAlert: true, presentBadge: true);
|
IOSNotificationDetails(presentAlert: true, presentBadge: true);
|
||||||
|
|
||||||
// naplánovat
|
// naplánovat
|
||||||
await flutterLocalNotificationsPlugin.zonedSchedule(
|
await flutterLocalNotificationsPlugin.zonedSchedule(
|
||||||
0,
|
0,
|
||||||
title,
|
title,
|
||||||
"${jidlo.varianta} - ${jidlo.nazev}",
|
"${jidlo.varianta} - ${jidlo.nazev}",
|
||||||
tz.TZDateTime.from(
|
tz.TZDateTime.from(
|
||||||
casNaDate(
|
casNaDate(
|
||||||
TimeOfDay(hour: kdy.hour, minute: kdy.minute),
|
TimeOfDay(hour: kdy.hour, minute: kdy.minute),
|
||||||
),
|
),
|
||||||
l),
|
l),
|
||||||
const NotificationDetails(android: androidSpec, iOS: iOSpec),
|
const NotificationDetails(android: androidSpec, iOS: iOSpec),
|
||||||
androidAllowWhileIdle: true,
|
androidAllowWhileIdle: true,
|
||||||
uiLocalNotificationDateInterpretation:
|
uiLocalNotificationDateInterpretation:
|
||||||
UILocalNotificationDateInterpretation.absoluteTime);
|
UILocalNotificationDateInterpretation.absoluteTime);
|
||||||
} on StateError catch (_) {
|
} on StateError catch (_) {
|
||||||
// nenalezeno
|
// nenalezeno
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -183,7 +185,7 @@ class _LoginPageState extends State<LoginPage> {
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
LoginManager.getDetails().then((r) async {
|
LoginManager.ziskatVsechnyUlozene().then((j) async {
|
||||||
if (Platform.isIOS) {
|
if (Platform.isIOS) {
|
||||||
// žádat o oprávnění na iOS
|
// žádat o oprávnění na iOS
|
||||||
await flutterLocalNotificationsPlugin
|
await flutterLocalNotificationsPlugin
|
||||||
|
@ -195,6 +197,11 @@ class _LoginPageState extends State<LoginPage> {
|
||||||
sound: true,
|
sound: true,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Map<String, String>? r;
|
||||||
|
if (j.isNotEmpty) {
|
||||||
|
r = await LoginManager.getDetails(
|
||||||
|
"0"); // TODO: změnit, aby šlo vybírat účet, který se použije
|
||||||
|
}
|
||||||
if (r != null) {
|
if (r != null) {
|
||||||
// Automaticky přihlásit
|
// Automaticky přihlásit
|
||||||
showDialog(
|
showDialog(
|
||||||
|
@ -410,6 +417,9 @@ class _LoginPageState extends State<LoginPage> {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
child: Text(Languages.of(context)!.logIn)),
|
child: Text(Languages.of(context)!.logIn)),
|
||||||
|
const Divider(
|
||||||
|
height: 5,
|
||||||
|
)
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
|
@ -217,7 +217,7 @@ class _NastaveniState extends State<Nastaveni> {
|
||||||
|
|
||||||
void vytvoritOznameni(DateTime den) async {
|
void vytvoritOznameni(DateTime den) async {
|
||||||
await widget.n.cancelAll();
|
await widget.n.cancelAll();
|
||||||
var d = await LoginManager.getDetails(); // získat údaje
|
var d = await LoginManager.getDetails("0"); // získat údaje TODO: změnit
|
||||||
if (d != null) {
|
if (d != null) {
|
||||||
// Nové oznámení
|
// Nové oznámení
|
||||||
var c = Canteen(d["url"]!);
|
var c = Canteen(d["url"]!);
|
||||||
|
|
Reference in a new issue