opencanteen/lib/main.dart

295 lines
11 KiB
Dart
Raw Normal View History

2022-04-04 20:22:30 +02:00
import 'package:flutter/material.dart';
2022-04-05 19:48:14 +02:00
import 'package:flutter_localizations/flutter_localizations.dart';
2022-04-04 20:22:30 +02:00
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
2022-05-03 16:41:45 +02:00
import 'package:opencanteen/lang/lang_cz.dart';
2022-04-04 20:22:30 +02:00
import 'package:opencanteen/loginmanager.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:canteenlib/canteenlib.dart';
2022-05-03 16:41:45 +02:00
import 'lang/lang.dart';
import 'lang/lang_en.dart';
2022-04-19 15:35:40 +02:00
import 'okna/jidelnicek.dart';
/*
Copyright (C) 2022 Matyáš Caras a přispěvatelé
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
2022-05-03 16:41:45 +02:00
*/
2022-04-19 15:35:40 +02:00
2022-04-04 20:22:30 +02:00
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
2022-04-05 19:48:14 +02:00
debugShowCheckedModeBanner: false,
2022-05-03 16:41:45 +02:00
localizationsDelegates: const [
AppLocalizationsDelegate(),
...GlobalMaterialLocalizations.delegates
],
2022-05-15 16:59:55 +02:00
supportedLocales: const [Locale("cs", ""), Locale("en", "")],
title: "OpenCanteen",
2022-04-04 20:22:30 +02:00
theme: ThemeData(
primarySwatch: Colors.purple,
),
darkTheme: ThemeData(
brightness: Brightness.dark,
primarySwatch: Colors.purple,
),
home: const LoginPage(),
);
}
}
class LoginPage extends StatefulWidget {
const LoginPage({Key? key}) : super(key: key);
@override
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
TextEditingController userControl = TextEditingController();
TextEditingController passControl = TextEditingController();
TextEditingController canteenControl = TextEditingController();
bool rememberMe = false;
@override
void initState() {
super.initState();
LoginManager.getDetails().then((r) async {
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.none) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
2022-05-15 16:59:55 +02:00
SnackBar(
content: Text(Languages.of(context)!.errorContacting),
2022-04-04 20:22:30 +02:00
),
);
}
if (r != null) {
2022-04-05 19:48:14 +02:00
showDialog(
context: context,
barrierDismissible: false,
builder: (_) => Dialog(
child: SizedBox(
height: 100,
2022-05-15 16:59:55 +02:00
child: Row(children: [
const Padding(
2022-04-25 20:09:13 +02:00
padding: EdgeInsets.all(10),
child: CircularProgressIndicator(),
),
2022-05-15 16:59:55 +02:00
Text(Languages.of(context)!.loggingIn)
2022-04-05 19:48:14 +02:00
]),
),
));
2022-04-04 20:22:30 +02:00
var canteen = Canteen(r["url"]!);
var l = await canteen.login(r["user"]!, r["pass"]!);
if (!l) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
2022-05-15 16:59:55 +02:00
SnackBar(
content: Text(Languages.of(context)!.loginFailed),
2022-04-04 20:22:30 +02:00
),
);
return;
}
Navigator.pushReplacement(
context,
MaterialPageRoute(
2022-04-19 15:35:40 +02:00
builder: (context) => JidelnicekPage(
2022-04-04 20:22:30 +02:00
user: r["user"]!,
canteen: canteen,
)),
);
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
2022-04-25 20:35:57 +02:00
appBar: AppBar(
2022-05-15 16:59:55 +02:00
title: Text(Languages.of(context)!.logIn),
automaticallyImplyLeading: false,
2022-04-25 20:35:57 +02:00
),
body: Center(
child: SingleChildScrollView(
child: SizedBox(
width: MediaQuery.of(context).size.width - 50,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
2022-05-15 16:59:55 +02:00
Text(
Languages.of(context)!.logIn,
2022-04-25 20:35:57 +02:00
textAlign: TextAlign.center,
2022-05-15 16:59:55 +02:00
style: const TextStyle(
fontWeight: FontWeight.bold, fontSize: 40),
2022-04-25 20:35:57 +02:00
),
2022-05-15 16:59:55 +02:00
Text(
Languages.of(context)!.logIn,
2022-04-25 20:35:57 +02:00
textAlign: TextAlign.center,
),
TextField(
controller: userControl,
autofillHints: const [AutofillHints.username],
2022-05-15 16:59:55 +02:00
decoration: InputDecoration(
labelText: Languages.of(context)!.username),
2022-04-25 20:35:57 +02:00
),
TextField(
autofillHints: const [AutofillHints.password],
2022-05-15 16:59:55 +02:00
decoration: InputDecoration(
labelText: Languages.of(context)!.password),
2022-04-25 20:35:57 +02:00
controller: passControl,
obscureText: true,
),
TextField(
autofillHints: const [AutofillHints.url],
2022-05-15 16:59:55 +02:00
decoration: InputDecoration(
labelText: Languages.of(context)!.iCanteenUrl),
2022-04-25 20:35:57 +02:00
keyboardType: TextInputType.url,
controller: canteenControl,
),
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Switch(
value: rememberMe,
onChanged: (value) {
setState(() {
rememberMe = value;
});
}),
2022-05-15 16:59:55 +02:00
Text(Languages.of(context)!.rememberMe)
2022-04-25 20:35:57 +02:00
]),
TextButton(
onPressed: () async {
if (canteenControl.text.contains("http://")) {
// kontrolujeme šifrované spojení
var d = await showDialog<bool>(
context: context,
builder: (c) => AlertDialog(
2022-05-15 16:59:55 +02:00
title: Text(Languages.of(context)!.warning),
content: SingleChildScrollView(
2022-04-25 20:35:57 +02:00
child: Text(
2022-05-15 16:59:55 +02:00
Languages.of(context)!.httpLogin)),
2022-04-25 20:35:57 +02:00
actions: [
TextButton(
onPressed: () =>
Navigator.pop(c, true),
2022-05-15 16:59:55 +02:00
child:
Text(Languages.of(context)!.yes)),
2022-04-25 20:35:57 +02:00
TextButton(
onPressed: () =>
Navigator.pop(c, false),
2022-05-15 16:59:55 +02:00
child: Text(
Languages.of(context)!.noChange))
2022-04-25 20:35:57 +02:00
],
));
if (!d!) return;
}
// souhlas
const storage = FlutterSecureStorage();
var odsouhlasil = await storage.read(key: "oc_souhlas");
if (odsouhlasil == null || odsouhlasil != "ano") {
var d = await showDialog<bool>(
context: context,
builder: (c) => AlertDialog(
2022-05-15 16:59:55 +02:00
title: Text(Languages.of(context)!.warning),
content: SingleChildScrollView(
child: Text(Languages.of(context)!
.notOfficial)),
2022-04-25 20:35:57 +02:00
actions: [
TextButton(
onPressed: () =>
Navigator.pop(c, true),
2022-05-15 16:59:55 +02:00
child: Text(
Languages.of(context)!.agree)),
2022-04-25 20:35:57 +02:00
TextButton(
onPressed: () =>
Navigator.pop(c, false),
2022-05-15 16:59:55 +02:00
child: Text(
Languages.of(context)!.disagree))
2022-04-25 20:35:57 +02:00
],
));
if (!d!) return;
await storage.write(key: "oc_souhlas", value: "ano");
}
2022-04-26 18:20:42 +02:00
if (!canteenControl.text.startsWith("https://") &&
!canteenControl.text.startsWith("http://")) {
canteenControl.text =
"https://" + canteenControl.text;
}
2022-04-25 20:35:57 +02:00
var canteen = Canteen(canteenControl.text);
var l = await canteen.login(
userControl.text, passControl.text);
if (!l) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
2022-05-15 16:59:55 +02:00
SnackBar(
content: Text(Languages.of(context)!.loginFailed),
2022-04-25 20:35:57 +02:00
),
);
return;
}
if (rememberMe) {
LoginManager.setDetails(userControl.text,
passControl.text, canteenControl.text);
}
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => JidelnicekPage(
user: userControl.text,
canteen: canteen,
)),
);
},
2022-05-15 16:59:55 +02:00
child: Text(Languages.of(context)!.logIn)),
2022-04-25 20:35:57 +02:00
],
2022-04-04 20:22:30 +02:00
),
2022-04-25 20:35:57 +02:00
),
2022-04-04 20:22:30 +02:00
),
2022-04-25 20:35:57 +02:00
));
2022-04-04 20:22:30 +02:00
}
}
2022-05-03 16:41:45 +02:00
class AppLocalizationsDelegate extends LocalizationsDelegate<Languages> {
const AppLocalizationsDelegate();
@override
bool isSupported(Locale locale) => ['cs', 'en'].contains(locale.languageCode);
@override
Future<Languages> load(Locale locale) => _load(locale);
static Future<Languages> _load(Locale locale) async {
2022-05-15 16:59:55 +02:00
debugPrint(locale.countryCode);
2022-05-03 16:41:45 +02:00
switch (locale.languageCode) {
case 'cs':
return LanguageCz();
default:
return LanguageEn();
}
}
@override
bool shouldReload(LocalizationsDelegate<Languages> old) => false;
}