SimpleCloudNotifier/flutter/lib/main.dart

110 lines
3.7 KiB
Dart
Raw Normal View History

2024-05-31 15:22:27 +02:00
import 'package:firebase_messaging/firebase_messaging.dart';
2024-02-10 18:29:41 +01:00
import 'package:flutter/material.dart';
2024-02-11 01:08:51 +01:00
import 'package:provider/provider.dart';
2024-05-25 18:09:39 +02:00
import 'package:hive_flutter/hive_flutter.dart';
2024-05-31 15:22:27 +02:00
import 'package:simplecloudnotifier/api/api_client.dart';
2024-05-23 20:05:55 +02:00
import 'package:simplecloudnotifier/nav_layout.dart';
import 'package:simplecloudnotifier/state/app_theme.dart';
2024-05-25 18:09:39 +02:00
import 'package:simplecloudnotifier/state/application_log.dart';
import 'package:simplecloudnotifier/state/globals.dart';
import 'package:simplecloudnotifier/state/request_log.dart';
2024-05-23 20:05:55 +02:00
import 'package:simplecloudnotifier/state/user_account.dart';
2024-05-31 15:22:27 +02:00
import 'package:firebase_core/firebase_core.dart';
2024-05-31 23:21:24 +02:00
import 'package:toastification/toastification.dart';
2024-05-31 15:22:27 +02:00
import 'firebase_options.dart';
2024-05-23 20:05:55 +02:00
void main() async {
2024-05-25 18:09:39 +02:00
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();
await Globals().init();
Hive.registerAdapter(SCNRequestAdapter());
Hive.registerAdapter(SCNLogAdapter());
2024-05-26 00:20:25 +02:00
Hive.registerAdapter(SCNLogLevelAdapter());
2024-05-25 18:09:39 +02:00
try {
await Hive.openBox<SCNRequest>('scn-requests');
2024-05-26 00:20:25 +02:00
} catch (exc, trace) {
2024-05-25 18:09:39 +02:00
Hive.deleteBoxFromDisk('scn-requests');
await Hive.openBox<SCNRequest>('scn-requests');
2024-05-26 00:20:25 +02:00
ApplicationLog.error('Failed to open Hive-Box: scn-requests: ' + exc.toString(), trace: trace);
}
try {
2024-05-25 18:09:39 +02:00
await Hive.openBox<SCNLog>('scn-logs');
2024-05-26 00:20:25 +02:00
} catch (exc, trace) {
Hive.deleteBoxFromDisk('scn-logs');
await Hive.openBox<SCNLog>('scn-logs');
ApplicationLog.error('Failed to open Hive-Box: scn-logs: ' + exc.toString(), trace: trace);
2024-05-25 18:09:39 +02:00
}
2024-02-10 19:57:17 +01:00
2024-05-31 15:22:27 +02:00
UserAccount(); // ensure UserAccount is loaded
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
final notificationSettings = await FirebaseMessaging.instance.requestPermission(provisional: true);
FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) {
try {
setFirebaseToken(fcmToken);
} catch (exc, trace) {
ApplicationLog.error('Failed to set firebase token: ' + exc.toString(), trace: trace);
}
}).onError((dynamic err) {
ApplicationLog.error('Failed to listen to token refresh events: ' + (err?.toString() ?? ''));
});
2024-05-26 00:20:25 +02:00
ApplicationLog.debug('Application started');
2024-02-11 01:08:51 +01:00
runApp(
MultiProvider(
providers: [
2024-05-26 00:20:25 +02:00
ChangeNotifierProvider(create: (context) => UserAccount(), lazy: false),
ChangeNotifierProvider(create: (context) => AppTheme(), lazy: false),
2024-02-11 01:08:51 +01:00
],
child: const SCNApp(),
),
);
2024-02-10 18:29:41 +01:00
}
2024-05-31 15:22:27 +02:00
void setFirebaseToken(String fcmToken) async {
ApplicationLog.info('New firebase token: $fcmToken');
final acc = UserAccount();
if (acc.auth != null) {
if (acc.client == null) {
final client = await APIClient.addClient(acc.auth, fcmToken, Globals().platform, Globals().version, Globals().clientType);
acc.setClient(client);
} else {
final client = await APIClient.updateClient(acc.auth, acc.client!.clientID, fcmToken, Globals().platform, Globals().version);
acc.setClient(client);
}
}
}
2024-02-10 19:57:17 +01:00
class SCNApp extends StatelessWidget {
const SCNApp({super.key});
2024-02-10 18:29:41 +01:00
@override
Widget build(BuildContext context) {
2024-05-31 23:21:24 +02:00
return ToastificationWrapper(
config: ToastificationConfig(
itemWidth: 440,
marginBuilder: (alignment) => EdgeInsets.symmetric(vertical: 64),
animationDuration: Duration(milliseconds: 200),
),
child: Consumer<AppTheme>(
builder: (context, appTheme, child) => MaterialApp(
title: 'SimpleCloudNotifier',
theme: ThemeData(
//TODO color settings
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue, brightness: appTheme.darkMode ? Brightness.dark : Brightness.light),
useMaterial3: true,
),
home: SCNNavLayout(),
2024-02-11 01:08:51 +01:00
),
2024-02-10 18:29:41 +01:00
),
);
}
}