This repository has been archived on 2024-07-21. You can view files and clone it, but cannot push or open issues or pull requests.
opencanteen/lib/okna/login.dart

256 lines
9.5 KiB
Dart
Raw Normal View History

2022-12-12 16:48:19 +01:00
import 'package:canteenlib/canteenlib.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
2022-12-14 20:02:32 +01:00
import 'package:opencanteen/okna/welcome.dart';
import 'package:opencanteen/pw/platformbutton.dart';
import 'package:opencanteen/pw/platformfield.dart';
2022-12-12 16:48:19 +01:00
2023-01-28 15:41:17 +01:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2022-12-12 16:48:19 +01:00
import '../../loginmanager.dart';
import '../../main.dart';
import '../../util.dart';
import '../pw/platformswitch.dart';
2022-12-12 16:48:19 +01:00
import 'jidelnicek.dart';
import 'offline_jidelnicek.dart';
2022-12-14 20:02:32 +01:00
class LoginPage extends StatefulWidget {
const LoginPage({Key? key}) : super(key: key);
2022-12-12 16:48:19 +01:00
@override
2022-12-14 20:02:32 +01:00
State<LoginPage> createState() => _LoginPageState();
2022-12-12 16:48:19 +01:00
}
2022-12-14 20:02:32 +01:00
class _LoginPageState extends State<LoginPage> {
2022-12-12 16:48:19 +01:00
TextEditingController userControl = TextEditingController();
TextEditingController passControl = TextEditingController();
TextEditingController canteenControl = TextEditingController();
bool rememberMe = false;
bool _showUrl = false;
String dropdownUrl = instance.first["url"] ?? "";
@override
void initState() {
super.initState();
LoginManager.getDetails().then((r) async {
// request android notification access
2022-12-12 16:48:19 +01:00
flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.requestPermission();
if (r != null) {
// Autologin
2022-12-12 16:48:19 +01:00
showDialog(
context: context,
barrierDismissible: false,
builder: (_) => Dialog(
child: SizedBox(
height: 100,
child: Row(children: [
const Padding(
padding: EdgeInsets.all(10),
child: CircularProgressIndicator(),
),
2023-01-28 15:41:17 +01:00
Text(AppLocalizations.of(context)!.loggingIn)
2022-12-12 16:48:19 +01:00
]),
),
));
var canteen = Canteen(r["url"]!);
try {
var l = await canteen.login(r["user"]!, r["pass"]!);
if (!l) {
if (!mounted) return;
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
2023-01-28 15:41:17 +01:00
content: Text(AppLocalizations.of(context)!.loginFailed),
2022-12-12 16:48:19 +01:00
),
);
return;
}
const storage = FlutterSecureStorage();
var odsouhlasil = await storage.read(key: "oc_souhlas");
if (!mounted) return;
if (odsouhlasil == null || odsouhlasil != "ano") {
Navigator.pushAndRemoveUntil(
context,
platformRouter(
(c) => WelcomePage(canteen: canteen),
2022-12-12 16:48:19 +01:00
),
(route) => false);
} else {
Navigator.pushAndRemoveUntil(
context,
platformRouter(
(context) => MealView(canteen: canteen),
2022-12-12 16:48:19 +01:00
),
(route) => false);
}
} on PlatformException {
if (!mounted) return;
Navigator.of(context).pop();
2023-01-28 15:41:17 +01:00
showInfo(context, AppLocalizations.of(context)!.corrupted);
2022-12-12 16:48:19 +01:00
} catch (_) {
if (!mounted) return;
Navigator.of(context).pop();
2023-01-28 15:41:17 +01:00
showInfo(context, AppLocalizations.of(context)!.errorContacting);
2022-12-12 16:48:19 +01:00
goOffline();
}
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
2023-01-28 15:41:17 +01:00
title: Text(AppLocalizations.of(context)!.logIn),
automaticallyImplyLeading: false,
),
body: Center(
child: SingleChildScrollView(
child: SizedBox(
width: MediaQuery.of(context).size.width - 50,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
2023-01-28 15:41:17 +01:00
AppLocalizations.of(context)!.appName,
textAlign: TextAlign.center,
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 40),
),
Text(
2023-01-28 15:41:17 +01:00
AppLocalizations.of(context)!.logIn,
textAlign: TextAlign.center,
),
PlatformField(
controller: userControl,
autofillHints: const [AutofillHints.username],
2023-01-28 15:41:17 +01:00
labelText: AppLocalizations.of(context)!.username,
),
PlatformField(
autofillHints: const [AutofillHints.password],
2023-01-28 15:41:17 +01:00
labelText: AppLocalizations.of(context)!.password,
controller: passControl,
obscureText: true,
),
const SizedBox(
height: 10,
),
DropdownButton(
isExpanded: true,
value: dropdownUrl,
items: instance.map<DropdownMenuItem<String>>((e) {
return DropdownMenuItem<String>(
value: e["url"],
child: Text(e["name"]!),
);
}).toList(),
onChanged: (String? value) {
setState(() {
if (value == "") {
_showUrl = true;
} else {
_showUrl = false;
}
dropdownUrl = value!;
});
},
),
AnimatedOpacity(
opacity: _showUrl ? 1.0 : 0.0,
duration: const Duration(milliseconds: 300),
child: PlatformField(
autofillHints: const [AutofillHints.url],
2023-01-28 15:41:17 +01:00
labelText: AppLocalizations.of(context)!.iCanteenUrl,
keyboardType: TextInputType.url,
controller: canteenControl,
2022-12-12 16:48:19 +01:00
),
),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
PlatformSwitch(
value: rememberMe,
onChanged: (value) {
2022-12-12 16:48:19 +01:00
setState(() {
rememberMe = value;
2022-12-12 16:48:19 +01:00
});
},
),
2023-01-28 15:41:17 +01:00
Text(AppLocalizations.of(context)!.rememberMe)
]),
PlatformButton(
onPressed: () async {
var canteenUrl = (dropdownUrl == "")
? canteenControl.text
: dropdownUrl;
if (!canteenUrl.startsWith("https://") &&
!canteenUrl.startsWith("http://")) {
canteenUrl = "https://$canteenUrl";
}
var canteen = Canteen(canteenUrl);
try {
var l = await canteen.login(
userControl.text, passControl.text);
if (!l) {
if (!mounted) return;
2023-01-28 15:41:17 +01:00
showInfo(context,
AppLocalizations.of(context)!.loginFailed);
return;
}
if (rememberMe) {
LoginManager.setDetails(
userControl.text, passControl.text, canteenUrl);
2022-12-12 16:48:19 +01:00
}
// souhlas
const storage = FlutterSecureStorage();
var odsouhlasil = await storage.read(key: "oc_souhlas");
if (!mounted) return;
if (odsouhlasil == null || odsouhlasil != "ano") {
Navigator.pushAndRemoveUntil(
context,
platformRouter(
(context) => WelcomePage(
canteen: canteen,
),
2022-12-12 16:48:19 +01:00
),
(route) => false);
} else {
Navigator.pushAndRemoveUntil(
context,
platformRouter(
(context) => MealView(
canteen: canteen,
),
),
(route) => false);
2022-12-12 16:48:19 +01:00
}
} on PlatformException {
if (!mounted) return;
2023-01-28 15:41:17 +01:00
showInfo(
context, AppLocalizations.of(context)!.corrupted);
} on Exception catch (_) {
if (!mounted) return;
2023-01-28 15:41:17 +01:00
showInfo(context,
AppLocalizations.of(context)!.errorContacting);
//goOffline();
}
},
2023-01-28 15:41:17 +01:00
text: AppLocalizations.of(context)!.logIn),
],
2022-12-12 16:48:19 +01:00
),
),
),
),
);
2022-12-12 16:48:19 +01:00
}
/// Switch to offline view
2022-12-12 16:48:19 +01:00
void goOffline() async {
if (!mounted) return;
Navigator.pushAndRemoveUntil(context,
platformRouter((context) => const OfflineMealView()), (route) => false);
2022-12-12 16:48:19 +01:00
}
}