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

158 lines
5.6 KiB
Dart
Raw Normal View History

import 'dart:io';
import 'package:flutter/cupertino.dart';
2022-04-04 20:22:30 +02:00
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
2022-04-05 19:48:14 +02:00
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_native_timezone/flutter_native_timezone.dart';
2022-04-04 20:22:30 +02:00
import 'package:canteenlib/canteenlib.dart';
2022-12-14 20:02:32 +01:00
import 'package:opencanteen/okna/login.dart';
import 'package:opencanteen/util.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:intl/intl.dart';
import 'package:timezone/data/latest_all.dart' as tz;
import 'package:timezone/timezone.dart' as tz;
2022-04-04 20:22:30 +02:00
2023-01-28 15:41:17 +01:00
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
2023-01-28 15:02:44 +01:00
import 'color_schemes.g.dart';
2023-01-28 15:41:17 +01:00
import 'loginmanager.dart';
2022-04-19 15:35:40 +02:00
/*
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
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
/// Used to setup notifications about ordered food
void setupNotification(SharedPreferences prefs, tz.Location l) async {
String title;
String locale = Intl.getCurrentLocale();
switch (locale) {
case "cs_CZ":
2023-01-28 15:41:17 +01:00
title = "Dnes máte objednáno";
break;
default:
2023-01-28 15:41:17 +01:00
title = "Today's ordered meal";
}
/*if (prefs.getBool("offline") ?? false) {
// TODO grab data from offline storage
} else {*/
// data from the web
var d = await LoginManager.getDetails(); // grab login
if (d != null) {
var c = Canteen(d["url"]!);
if (await c.login(d["user"]!, d["pass"]!)) {
var jidla = await c.jidelnicekDen();
try {
var jidlo = jidla.jidla
.singleWhere((element) => element.objednano); // grab ordered meal
2022-09-02 19:30:55 +02:00
var kdy = DateTime.parse(prefs.getString(
"oznameni_cas")!); // save the time the notif should be sent
var cas = timeToDate(
TimeOfDay(hour: kdy.hour, minute: kdy.minute),
);
if (cas.isBefore(DateTime.now())) return;
// notif data
const AndroidNotificationDetails androidSpec =
2022-06-15 20:12:33 +02:00
AndroidNotificationDetails('predobedem', 'Oznámení před obědem',
channelDescription: 'Oznámení o dnešním jídle',
importance: Importance.max,
priority: Priority.high,
styleInformation: BigTextStyleInformation(''),
ticker: 'today meal');
2022-09-02 19:30:55 +02:00
// plan through lib
await flutterLocalNotificationsPlugin.zonedSchedule(
0,
title,
"${jidlo.varianta} - ${jidlo.nazev}",
tz.TZDateTime.from(cas, l),
const NotificationDetails(android: androidSpec),
androidAllowWhileIdle: true,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
} on StateError catch (_) {
// no ordered meal found
}
}
// }
}
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
tz.initializeTimeZones();
var l = tz.getLocation(await FlutterNativeTimezone.getLocalTimezone());
tz.setLocalLocation(l);
var prefs = await SharedPreferences.getInstance();
if (prefs.getBool("oznamit") ?? false) {
setupNotification(prefs, l);
}
// notif library setup
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('notif_icon');
2022-12-12 16:48:19 +01:00
const ios = DarwinInitializationSettings();
const InitializationSettings initializationSettings =
InitializationSettings(android: initializationSettingsAndroid, iOS: ios);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
2022-09-02 19:30:55 +02:00
2022-04-04 20:22:30 +02:00
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return (Platform
.isAndroid) // run app based on current platform to make use of the platform's respective UI lib
? MaterialApp(
debugShowCheckedModeBanner: false,
localizationsDelegates: const [
2023-01-28 15:41:17 +01:00
AppLocalizations.delegate,
...GlobalMaterialLocalizations.delegates
],
2023-01-28 15:41:17 +01:00
supportedLocales: AppLocalizations.supportedLocales,
title: "OpenCanteen",
2023-01-28 15:02:44 +01:00
theme: ThemeData(useMaterial3: true, colorScheme: lightColorScheme),
darkTheme: ThemeData(
2023-01-28 15:02:44 +01:00
brightness: Brightness.dark,
useMaterial3: true,
colorScheme: darkColorScheme),
home: const LoginPage(),
)
: const CupertinoApp(
debugShowCheckedModeBanner: false,
localizationsDelegates: [
2023-01-28 15:41:17 +01:00
AppLocalizations.delegate,
...GlobalMaterialLocalizations.delegates
],
2023-01-28 15:41:17 +01:00
supportedLocales: AppLocalizations.supportedLocales,
title: "OpenCanteen",
theme: CupertinoThemeData(
primaryColor: Colors.purple,
),
home: LoginPage(),
);
2022-04-04 20:22:30 +02:00
}
}