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/main.dart

230 lines
8.7 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';
import 'package:opencanteen/loginmanager.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:opencanteen/okna/home.dart';
import 'package:canteenlib/canteenlib.dart';
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,
localizationsDelegates: GlobalMaterialLocalizations.delegates,
supportedLocales: const [Locale("cs")],
2022-04-04 20:22:30 +02:00
title: 'OpenCanteen',
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(
const SnackBar(
content: Text(
"Nastala chyba při kontaktování serveru, zkontrolujte připojení"),
),
);
}
if (r != null) {
2022-04-05 19:48:14 +02:00
showDialog(
context: context,
barrierDismissible: false,
builder: (_) => Dialog(
child: SizedBox(
height: 100,
child: Row(children: const [
CircularProgressIndicator(),
Text("Přihlašuji vás")
]),
),
));
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(
const SnackBar(
content: Text("Přihlášení se nezdařilo, zkontrolujte údaje"),
),
);
return;
}
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => HomePage(
user: r["user"]!,
canteen: canteen,
)),
);
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Přihlášení"),
),
body: Center(
2022-04-05 19:48:14 +02:00
child: SizedBox(
2022-04-04 20:22:30 +02:00
width: MediaQuery.of(context).size.width - 50,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'OpenCanteen',
textAlign: TextAlign.center,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 40),
),
const Text(
'Přihlášení',
textAlign: TextAlign.center,
),
TextField(
controller: userControl,
autofillHints: const [AutofillHints.username],
decoration:
const InputDecoration(labelText: 'Uživatelské jméno'),
),
TextField(
autofillHints: const [AutofillHints.password],
decoration: const InputDecoration(labelText: 'Heslo'),
controller: passControl,
obscureText: true,
),
TextField(
autofillHints: const [AutofillHints.url],
decoration: const InputDecoration(labelText: 'iCanteen URL'),
keyboardType: TextInputType.url,
controller: canteenControl,
),
2022-04-05 19:48:14 +02:00
Row(mainAxisAlignment: MainAxisAlignment.center, children: [
Switch(
value: rememberMe,
onChanged: (value) {
setState(() {
rememberMe = value;
});
}),
const Text("Zapamatovat si mě")
]),
2022-04-04 20:22:30 +02:00
TextButton(
onPressed: () async {
if (canteenControl.text.contains("http://")) {
// kontrolujeme šifrované spojení
var d = await showDialog<bool>(
context: context,
builder: (c) => AlertDialog(
title: const Text("Varování!"),
content: const SingleChildScrollView(
child: Text(
"Snažíte se přihlásit přes nešifrované spojení HTTP, jste si jisti, že tak chcete učinit?")),
actions: [
TextButton(
onPressed: () => Navigator.pop(c, true),
child: const Text("Ano")),
TextButton(
onPressed: () => Navigator.pop(c, false),
child: const Text("Ne, změnit"))
],
));
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(
title: const Text("Pozor"),
content: const SingleChildScrollView(
child: Text(
"Toto není oficiální aplikace k ovládání iCanteen. Autor neručí za ztráty nebo nefunkčnost v souvislosti s používáním této aplikace. Tato zpráva se znovu neukáže.")),
actions: [
TextButton(
onPressed: () => Navigator.pop(c, true),
child: const Text("Souhlasím")),
TextButton(
onPressed: () => Navigator.pop(c, false),
child: const Text("Nesouhlasím"))
],
));
if (!d!) return;
await storage.write(key: "oc_souhlas", value: "ano");
}
var canteen = Canteen(canteenControl.text);
var l =
await canteen.login(userControl.text, passControl.text);
if (!l) {
ScaffoldMessenger.of(context).hideCurrentSnackBar();
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
"Přihlášení se nezdařilo, zkontrolujte údaje"),
),
);
return;
}
if (rememberMe) {
LoginManager.setDetails(userControl.text,
passControl.text, canteenControl.text);
}
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) => HomePage(
user: userControl.text,
canteen: canteen,
)),
);
},
child: const Text("Přihlásit se")),
],
),
),
),
);
}
}