feat: práce na implementaci výběru více účtů
This commit is contained in:
parent
c6a60744b7
commit
02078987c6
3 changed files with 99 additions and 42 deletions
|
@ -1,16 +1,46 @@
|
|||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
|
||||
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é
|
||||
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");
|
||||
var user = await storage.read(key: "oc_user_$id");
|
||||
var pass = await storage.read(key: "oc_pass_$id");
|
||||
var url = await storage.read(key: "oc_url_$id");
|
||||
if (user == null || pass == null || url == null) return null;
|
||||
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 {
|
||||
const storage = FlutterSecureStorage();
|
||||
await storage.write(key: "oc_user", value: user);
|
||||
|
@ -22,4 +52,21 @@ class LoginManager {
|
|||
const storage = FlutterSecureStorage();
|
||||
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,7 +59,8 @@ void oznamitPredem(SharedPreferences prefs, tz.Location l) async {
|
|||
// TODO možnost brát z offline dat
|
||||
} else {*/
|
||||
// bere online
|
||||
var d = await LoginManager.getDetails(); // získat údaje
|
||||
if ((await LoginManager.ziskatVsechnyUlozene()).isNotEmpty) {
|
||||
var d = await LoginManager.getDetails("0s"); // získat údaje
|
||||
if (d != null) {
|
||||
var c = Canteen(d["url"]!);
|
||||
if (await c.login(d["user"]!, d["pass"]!)) {
|
||||
|
@ -100,6 +101,7 @@ void oznamitPredem(SharedPreferences prefs, tz.Location l) async {
|
|||
}
|
||||
// }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() async {
|
||||
|
@ -183,7 +185,7 @@ class _LoginPageState extends State<LoginPage> {
|
|||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
LoginManager.getDetails().then((r) async {
|
||||
LoginManager.ziskatVsechnyUlozene().then((j) async {
|
||||
if (Platform.isIOS) {
|
||||
// žádat o oprávnění na iOS
|
||||
await flutterLocalNotificationsPlugin
|
||||
|
@ -195,6 +197,11 @@ class _LoginPageState extends State<LoginPage> {
|
|||
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) {
|
||||
// Automaticky přihlásit
|
||||
showDialog(
|
||||
|
@ -410,6 +417,9 @@ class _LoginPageState extends State<LoginPage> {
|
|||
}
|
||||
},
|
||||
child: Text(Languages.of(context)!.logIn)),
|
||||
const Divider(
|
||||
height: 5,
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
|
|
|
@ -217,7 +217,7 @@ class _NastaveniState extends State<Nastaveni> {
|
|||
|
||||
void vytvoritOznameni(DateTime den) async {
|
||||
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) {
|
||||
// Nové oznámení
|
||||
var c = Canteen(d["url"]!);
|
||||
|
|
Reference in a new issue