feat: práce na implementaci výběru více účtů

This commit is contained in:
Matyáš Caras 2022-09-12 16:24:06 +02:00
parent c6a60744b7
commit 02078987c6
3 changed files with 99 additions and 42 deletions

View file

@ -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;
}
} }

View file

@ -59,7 +59,8 @@ 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) {
var d = await LoginManager.getDetails("0s"); // získat údaje
if (d != null) { if (d != null) {
var c = Canteen(d["url"]!); var c = Canteen(d["url"]!);
if (await c.login(d["user"]!, d["pass"]!)) { if (await c.login(d["user"]!, d["pass"]!)) {
@ -101,6 +102,7 @@ void oznamitPredem(SharedPreferences prefs, tz.Location l) async {
// } // }
} }
} }
}
void main() async { void main() async {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
@ -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,
)
], ],
), ),
), ),

View file

@ -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"]!);