2022-01-16 15:28:42 +01:00
|
|
|
// GCG.MeinCantor - Die Schulplattform für Cantorianer.
|
|
|
|
// Copyright (C) 2021-2022 Georg-Cantor-Gymnasium Halle (Saale)
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-08-27 19:24:30 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2022-01-19 19:30:09 +01:00
|
|
|
import 'package:meincantor/background_fetch.dart';
|
2021-08-27 19:24:30 +02:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2021-12-19 22:20:56 +01:00
|
|
|
import 'package:meincantor/dashboard.dart';
|
|
|
|
import 'package:meincantor/login.dart';
|
2021-08-27 19:24:30 +02:00
|
|
|
import 'dart:math';
|
2021-12-19 22:20:56 +01:00
|
|
|
import 'package:background_fetch/background_fetch.dart';
|
|
|
|
|
|
|
|
void backgroundFetchHeadlessTask(HeadlessTask task) async {
|
|
|
|
String taskId = task.taskId;
|
|
|
|
bool isTimeout = task.timeout;
|
|
|
|
if (isTimeout) {
|
|
|
|
// This task has exceeded its allowed running-time.
|
|
|
|
// You must stop what you're doing and immediately .finish(taskId)
|
|
|
|
print("[BackgroundFetch] Headless task timed-out: $taskId");
|
|
|
|
BackgroundFetch.finish(taskId);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
print('[BackgroundFetch] Headless event received.');
|
2022-01-19 19:30:09 +01:00
|
|
|
|
|
|
|
await backgroundFetchTimetable();
|
|
|
|
await backgroundFetchArticles();
|
|
|
|
|
2021-12-19 22:20:56 +01:00
|
|
|
BackgroundFetch.finish(taskId);
|
|
|
|
}
|
2021-08-27 19:24:30 +02:00
|
|
|
|
2021-12-13 13:39:06 +01:00
|
|
|
void main() async {
|
2021-12-19 22:20:56 +01:00
|
|
|
runApp(const App());
|
2022-01-19 19:30:09 +01:00
|
|
|
|
2021-12-19 22:20:56 +01:00
|
|
|
BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);
|
2021-12-13 13:39:06 +01:00
|
|
|
}
|
2021-08-27 19:24:30 +02:00
|
|
|
|
2021-12-19 22:20:56 +01:00
|
|
|
class App extends StatefulWidget {
|
2021-09-14 20:55:58 +02:00
|
|
|
const App({Key? key}) : super(key: key);
|
2021-12-19 22:20:56 +01:00
|
|
|
@override
|
|
|
|
_AppState createState() => _AppState();
|
|
|
|
}
|
2021-09-14 20:55:58 +02:00
|
|
|
|
2021-12-19 22:20:56 +01:00
|
|
|
class _AppState extends State<App> {
|
2021-08-27 19:24:30 +02:00
|
|
|
MaterialColor generateMaterialColor(Color color) {
|
|
|
|
return MaterialColor(color.value, {
|
|
|
|
50: tintColor(color, 0.5),
|
|
|
|
100: tintColor(color, 0.4),
|
|
|
|
200: tintColor(color, 0.3),
|
|
|
|
300: tintColor(color, 0.2),
|
|
|
|
400: tintColor(color, 0.1),
|
|
|
|
500: tintColor(color, 0),
|
|
|
|
600: tintColor(color, -0.1),
|
|
|
|
700: tintColor(color, -0.2),
|
|
|
|
800: tintColor(color, -0.3),
|
|
|
|
900: tintColor(color, -0.4),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
int tintValue(int value, double factor) =>
|
|
|
|
max(0, min((value + ((255 - value) * factor)).round(), 255));
|
|
|
|
|
|
|
|
Color tintColor(Color color, double factor) => Color.fromRGBO(
|
|
|
|
tintValue(color.red, factor),
|
|
|
|
tintValue(color.green, factor),
|
|
|
|
tintValue(color.blue, factor),
|
|
|
|
1);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
|
|
|
theme: ThemeData(
|
|
|
|
primaryColor: Palette.primary,
|
|
|
|
colorScheme: ColorScheme.fromSwatch(
|
2021-09-14 20:55:58 +02:00
|
|
|
primarySwatch: generateMaterialColor(Palette.primary),
|
2021-08-27 19:24:30 +02:00
|
|
|
).copyWith(
|
|
|
|
secondary: generateMaterialColor(Palette.accent),
|
|
|
|
),
|
|
|
|
),
|
2021-09-14 20:55:58 +02:00
|
|
|
darkTheme: ThemeData.from(
|
2021-11-16 19:41:35 +01:00
|
|
|
colorScheme: const ColorScheme.dark(
|
|
|
|
primary: Palette.accent, secondary: Palette.accent)),
|
2021-08-27 19:24:30 +02:00
|
|
|
title: "GCG.MeinCantor",
|
|
|
|
home: buildHomePage(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> apiKeyEmpty() async {
|
|
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
2021-09-14 20:55:58 +02:00
|
|
|
String? apiKey = prefs.getString("api_key");
|
|
|
|
return apiKey == null || apiKey.isEmpty;
|
2021-08-27 19:24:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Widget buildHomePage() {
|
|
|
|
return FutureBuilder(
|
2021-09-14 20:55:58 +02:00
|
|
|
future: apiKeyEmpty(),
|
|
|
|
builder: (context, snapshot) {
|
|
|
|
if (snapshot.data == true) {
|
|
|
|
return Login();
|
|
|
|
} else if (snapshot.data == false) {
|
|
|
|
return const Dashboard();
|
|
|
|
} else {
|
|
|
|
return const Center(child: CircularProgressIndicator());
|
|
|
|
}
|
|
|
|
});
|
2021-08-27 19:24:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Palette {
|
2021-09-14 20:55:58 +02:00
|
|
|
static const Color primary = Color(0xFF1A1A37);
|
|
|
|
static const Color accent = Color(0xFFFFBC3B);
|
|
|
|
}
|