1
0
mirror of https://github.com/go-vikunja/app synced 2024-10-04 21:28:30 +00:00
app-mirror-github/lib/main.dart

190 lines
6.1 KiB
Dart
Raw Normal View History

2022-08-24 21:45:12 +00:00
import 'dart:io';
import 'package:dynamic_color/dynamic_color.dart';
2024-04-05 12:35:23 +00:00
import 'package:flutter/foundation.dart';
2018-09-14 16:59:13 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:vikunja_app/api/task_implementation.dart';
import 'package:vikunja_app/api/client.dart';
import 'package:vikunja_app/service/services.dart';
2023-01-07 15:55:40 +00:00
import 'package:workmanager/workmanager.dart';
2018-09-22 20:56:16 +00:00
import 'package:vikunja_app/global.dart';
2019-03-16 13:29:00 +00:00
import 'package:vikunja_app/pages/home.dart';
import 'package:vikunja_app/pages/user/login.dart';
2019-03-11 20:38:05 +00:00
import 'package:vikunja_app/theme/theme.dart';
import 'package:timezone/data/latest_all.dart' as tz;
import 'package:flutter_downloader/flutter_downloader.dart';
import 'api/user_implementation.dart';
import 'managers/notifications.dart';
2022-08-24 21:45:12 +00:00
class IgnoreCertHttpOverrides extends HttpOverrides {
2022-08-27 21:04:43 +00:00
bool ignoreCerts = false;
IgnoreCertHttpOverrides(bool _ignore) {
ignoreCerts = _ignore;
}
2022-08-24 21:45:12 +00:00
@override
2022-08-27 21:04:43 +00:00
HttpClient createHttpClient(SecurityContext? context) {
2022-08-24 21:45:12 +00:00
return super.createHttpClient(context)
..badCertificateCallback = (_, __, ___) => ignoreCerts;
}
}
2023-01-07 15:55:40 +00:00
@pragma('vm:entry-point')
void callbackDispatcher() {
2024-04-05 12:35:23 +00:00
if (kIsWeb) {
return;
}
Workmanager().executeTask((task, inputData) async {
print(
"Native called background task: $task"); //simpleTask will be emitted here.
if (task == "update-tasks" && inputData != null) {
2023-05-16 23:22:59 +00:00
Client client = Client(null,
token: inputData["client_token"],
base: inputData["client_base"],
authenticated: true);
tz.initializeTimeZones();
return SettingsManager(new FlutterSecureStorage())
.getIgnoreCertificates()
.then((value) async {
print("ignoring: $value");
client.reloadIgnoreCerts(value == "1");
TaskAPIService taskService = TaskAPIService(client);
NotificationClass nc = NotificationClass();
await nc.notificationInitializer();
return nc
.scheduleDueNotifications(taskService)
.then((value) => Future.value(true));
});
} else if (task == "refresh-token") {
print("running refresh from workmanager");
final FlutterSecureStorage _storage = new FlutterSecureStorage();
var currentUser = await _storage.read(key: 'currentUser');
if (currentUser == null) {
return Future.value(true);
}
var token = await _storage.read(key: currentUser);
var base = await _storage.read(key: '${currentUser}_base');
if (token == null || base == null) {
return Future.value(true);
}
Client client = Client(null);
client.configure(token: token, base: base, authenticated: true);
// load new token from server to avoid expiration
String? newToken = await UserAPIService(client).getToken();
if (newToken != null) {
_storage.write(key: currentUser, value: newToken);
}
return Future.value(true);
} else {
return Future.value(true);
2023-01-07 15:55:40 +00:00
}
});
}
2023-05-16 23:22:59 +00:00
final globalSnackbarKey = GlobalKey<ScaffoldMessengerState>();
final globalNavigatorKey = GlobalKey<NavigatorState>();
2023-01-07 15:55:40 +00:00
void main() async {
2023-01-07 15:55:40 +00:00
WidgetsFlutterBinding.ensureInitialized();
await Permission.notification.isDenied.then((value) {
if (value) {
Permission.notification.request();
}
});
2024-04-05 12:35:23 +00:00
try {
if (!kIsWeb) {
await FlutterDownloader.initialize();
}
} catch (e) {
print("Failed to initialize downloader: $e");
}
try {
if (!kIsWeb) {
Workmanager().initialize(callbackDispatcher, isInDebugMode: false);
}
} catch (e) {
print("Failed to initialize workmanager: $e");
}
runApp(VikunjaGlobal(
2024-04-05 12:35:23 +00:00
child: new VikunjaApp(
home: HomePage(),
key: UniqueKey(),
navkey: globalNavigatorKey,
),
login: new VikunjaApp(
home: LoginPage(),
key: UniqueKey(),
),
));
2022-08-24 21:45:12 +00:00
}
2023-05-17 10:52:26 +00:00
final ValueNotifier<bool> updateTheme = ValueNotifier(false);
2018-09-14 16:59:13 +00:00
2018-09-15 15:01:45 +00:00
class VikunjaApp extends StatelessWidget {
final Widget home;
2023-05-16 23:22:59 +00:00
final GlobalKey<NavigatorState>? navkey;
const VikunjaApp({Key? key, required this.home, this.navkey})
: super(key: key);
2024-04-05 12:35:23 +00:00
Future<ThemeData> getThemedata() async {
FlutterThemeMode themeMode = FlutterThemeMode.light;
try {
SettingsManager manager = SettingsManager(new FlutterSecureStorage());
themeMode = await manager.getThemeMode();
} catch (e) {
print("Failed to get theme mode: $e");
}
switch (themeMode) {
case FlutterThemeMode.dark:
return buildVikunjaDarkTheme();
case FlutterThemeMode.materialYouLight:
return buildVikunjaMaterialLightTheme();
case FlutterThemeMode.materialYouDark:
return buildVikunjaMaterialDarkTheme();
default:
return buildVikunjaTheme();
}
}
2018-09-14 16:59:13 +00:00
@override
Widget build(BuildContext context) {
return new ValueListenableBuilder(
valueListenable: updateTheme,
builder: (_, mode, __) {
return FutureBuilder<ThemeData>(
2024-04-05 12:35:23 +00:00
future: getThemedata(),
builder: (BuildContext context, AsyncSnapshot<ThemeData> data) {
if (data.hasData) {
return new DynamicColorBuilder(
builder: (lightTheme, darkTheme) {
ThemeData? themeData = data.data;
2024-04-05 12:35:23 +00:00
if (data.data == FlutterThemeMode.materialYouLight)
themeData = themeData?.copyWith(colorScheme: lightTheme);
2024-04-05 12:35:23 +00:00
else if (data.data == FlutterThemeMode.materialYouDark)
themeData = themeData?.copyWith(colorScheme: darkTheme);
return MaterialApp(
title: 'Vikunja',
theme: themeData,
scaffoldMessengerKey: globalSnackbarKey,
navigatorKey: navkey,
// <= this
home: this.home,
);
});
} else {
return Center(child: CircularProgressIndicator());
}
});
});
2018-09-14 16:59:13 +00:00
}
2018-09-16 19:47:53 +00:00
}