2022-05-16 20:26:39 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
2023-01-28 14:30:54 +01:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2022-04-04 20:22:30 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-05-31 19:55:28 +02:00
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
2022-04-05 19:48:14 +02:00
|
|
|
import 'package:flutter_localizations/flutter_localizations.dart';
|
2022-06-08 17:12:44 +02:00
|
|
|
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';
|
2022-05-16 20:26:39 +02:00
|
|
|
import 'package:opencanteen/util.dart';
|
2022-05-31 19:55:28 +02:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2022-06-08 11:29:53 +02:00
|
|
|
import 'package:intl/intl.dart';
|
2022-06-08 17:12:44 +02:00
|
|
|
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
|
|
|
|
2022-05-31 19:55:28 +02:00
|
|
|
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
|
|
|
|
FlutterLocalNotificationsPlugin();
|
|
|
|
|
2023-01-28 14:30:54 +01:00
|
|
|
/// Used to setup notifications about ordered food
|
|
|
|
void setupNotification(SharedPreferences prefs, tz.Location l) async {
|
2022-06-08 11:29:53 +02:00
|
|
|
String title;
|
2022-06-08 17:12:44 +02:00
|
|
|
|
|
|
|
String locale = Intl.getCurrentLocale();
|
|
|
|
switch (locale) {
|
|
|
|
case "cs_CZ":
|
2023-01-28 15:41:17 +01:00
|
|
|
title = "Dnes máte objednáno";
|
2022-06-08 17:12:44 +02:00
|
|
|
break;
|
|
|
|
default:
|
2023-01-28 15:41:17 +01:00
|
|
|
title = "Today's ordered meal";
|
2022-06-08 11:29:53 +02:00
|
|
|
}
|
2022-06-08 17:12:44 +02:00
|
|
|
|
2022-06-15 20:04:30 +02:00
|
|
|
/*if (prefs.getBool("offline") ?? false) {
|
2023-01-28 14:30:54 +01:00
|
|
|
// TODO grab data from offline storage
|
2022-06-15 20:04:30 +02:00
|
|
|
} else {*/
|
2023-01-28 14:30:54 +01:00
|
|
|
// data from the web
|
|
|
|
var d = await LoginManager.getDetails(); // grab login
|
2022-06-15 20:04:30 +02:00
|
|
|
if (d != null) {
|
|
|
|
var c = Canteen(d["url"]!);
|
|
|
|
if (await c.login(d["user"]!, d["pass"]!)) {
|
|
|
|
var jidla = await c.jidelnicekDen();
|
|
|
|
try {
|
2023-01-28 14:30:54 +01:00
|
|
|
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(
|
2023-01-28 14:30:54 +01:00
|
|
|
"oznameni_cas")!); // save the time the notif should be sent
|
|
|
|
var cas = timeToDate(
|
2022-10-04 17:45:31 +02:00
|
|
|
TimeOfDay(hour: kdy.hour, minute: kdy.minute),
|
|
|
|
);
|
|
|
|
if (cas.isBefore(DateTime.now())) return;
|
2023-01-28 14:30:54 +01:00
|
|
|
// notif data
|
2022-06-15 20:04:30 +02:00
|
|
|
const AndroidNotificationDetails androidSpec =
|
2022-06-15 20:12:33 +02:00
|
|
|
AndroidNotificationDetails('predobedem', 'Oznámení před obědem',
|
2022-06-15 20:04:30 +02:00
|
|
|
channelDescription: 'Oznámení o dnešním jídle',
|
|
|
|
importance: Importance.max,
|
|
|
|
priority: Priority.high,
|
2022-11-17 12:19:38 +01:00
|
|
|
styleInformation: BigTextStyleInformation(''),
|
2022-06-15 20:04:30 +02:00
|
|
|
ticker: 'today meal');
|
2022-09-02 19:30:55 +02:00
|
|
|
|
2023-01-28 14:30:54 +01:00
|
|
|
// plan through lib
|
2022-06-15 20:04:30 +02:00
|
|
|
await flutterLocalNotificationsPlugin.zonedSchedule(
|
|
|
|
0,
|
|
|
|
title,
|
2022-09-02 20:19:17 +02:00
|
|
|
"${jidlo.varianta} - ${jidlo.nazev}",
|
2022-10-04 17:45:31 +02:00
|
|
|
tz.TZDateTime.from(cas, l),
|
2022-11-17 12:19:38 +01:00
|
|
|
const NotificationDetails(android: androidSpec),
|
2022-06-15 20:04:30 +02:00
|
|
|
androidAllowWhileIdle: true,
|
|
|
|
uiLocalNotificationDateInterpretation:
|
|
|
|
UILocalNotificationDateInterpretation.absoluteTime);
|
|
|
|
} on StateError catch (_) {
|
2023-01-28 14:30:54 +01:00
|
|
|
// no ordered meal found
|
2022-06-08 11:29:53 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-15 20:04:30 +02:00
|
|
|
// }
|
2022-06-08 11:29:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 17:12:44 +02:00
|
|
|
void main() async {
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
tz.initializeTimeZones();
|
|
|
|
var l = tz.getLocation(await FlutterNativeTimezone.getLocalTimezone());
|
|
|
|
tz.setLocalLocation(l);
|
2022-05-31 19:55:28 +02:00
|
|
|
|
2022-06-08 17:12:44 +02:00
|
|
|
var prefs = await SharedPreferences.getInstance();
|
2022-05-31 19:55:28 +02:00
|
|
|
if (prefs.getBool("oznamit") ?? false) {
|
2023-01-28 14:30:54 +01:00
|
|
|
setupNotification(prefs, l);
|
2022-05-31 19:55:28 +02:00
|
|
|
}
|
|
|
|
|
2023-01-28 14:30:54 +01:00
|
|
|
// notif library setup
|
2022-05-31 19:55:28 +02:00
|
|
|
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);
|
2022-11-17 12:19:38 +01:00
|
|
|
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) {
|
2023-01-28 14:30:54 +01:00
|
|
|
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,
|
2023-01-28 14:30:54 +01:00
|
|
|
...GlobalMaterialLocalizations.delegates
|
|
|
|
],
|
2023-01-28 15:41:17 +01:00
|
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
2023-01-28 14:30:54 +01:00
|
|
|
title: "OpenCanteen",
|
2023-01-28 15:02:44 +01:00
|
|
|
theme: ThemeData(useMaterial3: true, colorScheme: lightColorScheme),
|
2023-01-28 14:30:54 +01:00
|
|
|
darkTheme: ThemeData(
|
2023-01-28 15:02:44 +01:00
|
|
|
brightness: Brightness.dark,
|
|
|
|
useMaterial3: true,
|
|
|
|
colorScheme: darkColorScheme),
|
2023-01-28 14:30:54 +01:00
|
|
|
home: const LoginPage(),
|
|
|
|
)
|
|
|
|
: const CupertinoApp(
|
|
|
|
debugShowCheckedModeBanner: false,
|
|
|
|
localizationsDelegates: [
|
2023-01-28 15:41:17 +01:00
|
|
|
AppLocalizations.delegate,
|
2023-01-28 14:30:54 +01:00
|
|
|
...GlobalMaterialLocalizations.delegates
|
|
|
|
],
|
2023-01-28 15:41:17 +01:00
|
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
2023-01-28 14:30:54 +01:00
|
|
|
title: "OpenCanteen",
|
|
|
|
theme: CupertinoThemeData(
|
|
|
|
primaryColor: Colors.purple,
|
|
|
|
),
|
|
|
|
home: LoginPage(),
|
|
|
|
);
|
2022-04-04 20:22:30 +02:00
|
|
|
}
|
|
|
|
}
|