2024-06-04 08:20:28 +02:00
|
|
|
import 'dart:io';
|
|
|
|
|
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-06-15 15:56:50 +02:00
|
|
|
import 'package:simplecloudnotifier/models/channel.dart';
|
2024-06-02 17:09:57 +02:00
|
|
|
import 'package:simplecloudnotifier/models/client.dart';
|
2024-06-15 15:56:50 +02:00
|
|
|
import 'package:simplecloudnotifier/models/message.dart';
|
2024-05-23 20:05:55 +02:00
|
|
|
import 'package:simplecloudnotifier/nav_layout.dart';
|
2024-06-13 17:00:08 +02:00
|
|
|
import 'package:simplecloudnotifier/state/app_bar_state.dart';
|
2024-05-23 20:05:55 +02:00
|
|
|
import 'package:simplecloudnotifier/state/app_theme.dart';
|
2024-05-25 18:09:39 +02:00
|
|
|
import 'package:simplecloudnotifier/state/application_log.dart';
|
2024-06-15 18:24:18 +02:00
|
|
|
import 'package:simplecloudnotifier/state/fb_message.dart';
|
2024-05-25 18:09:39 +02:00
|
|
|
import 'package:simplecloudnotifier/state/globals.dart';
|
|
|
|
import 'package:simplecloudnotifier/state/request_log.dart';
|
2024-06-02 17:09:57 +02:00
|
|
|
import 'package:simplecloudnotifier/state/app_auth.dart';
|
2024-05-31 15:22:27 +02:00
|
|
|
import 'package:firebase_core/firebase_core.dart';
|
2024-06-13 17:00:08 +02:00
|
|
|
import 'package:simplecloudnotifier/utils/navi.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-06-01 03:06:02 +02:00
|
|
|
print('[INIT] Application starting...');
|
|
|
|
|
2024-06-04 08:20:28 +02:00
|
|
|
print('[INIT] Ensure WidgetsFlutterBinding...');
|
|
|
|
|
2024-05-25 18:09:39 +02:00
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
|
2024-06-04 08:20:28 +02:00
|
|
|
print('[INIT] Init Globals...');
|
|
|
|
|
2024-05-25 18:09:39 +02:00
|
|
|
await Globals().init();
|
|
|
|
|
2024-06-04 08:20:28 +02:00
|
|
|
print('[INIT] Init Hive...');
|
|
|
|
|
|
|
|
await Hive.initFlutter();
|
|
|
|
|
2024-05-25 18:09:39 +02:00
|
|
|
Hive.registerAdapter(SCNRequestAdapter());
|
|
|
|
Hive.registerAdapter(SCNLogAdapter());
|
2024-05-26 00:20:25 +02:00
|
|
|
Hive.registerAdapter(SCNLogLevelAdapter());
|
2024-06-15 15:56:50 +02:00
|
|
|
Hive.registerAdapter(MessageAdapter());
|
|
|
|
Hive.registerAdapter(ChannelAdapter());
|
2024-06-15 18:24:18 +02:00
|
|
|
Hive.registerAdapter(FBMessageAdapter());
|
2024-05-25 18:09:39 +02:00
|
|
|
|
2024-06-04 08:20:28 +02:00
|
|
|
print('[INIT] Load Hive<scn-requests>...');
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-06-04 08:20:28 +02:00
|
|
|
print('[INIT] Load Hive<scn-logs>...');
|
|
|
|
|
2024-05-26 00:20:25 +02:00
|
|
|
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-06-15 15:56:50 +02:00
|
|
|
print('[INIT] Load Hive<scn-message-cache>...');
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Hive.openBox<Message>('scn-message-cache');
|
|
|
|
} catch (exc, trace) {
|
|
|
|
Hive.deleteBoxFromDisk('scn-message-cache');
|
|
|
|
await Hive.openBox<Message>('scn-message-cache');
|
|
|
|
ApplicationLog.error('Failed to open Hive-Box: scn-message-cache' + exc.toString(), trace: trace);
|
|
|
|
}
|
|
|
|
|
|
|
|
print('[INIT] Load Hive<scn-channel-cache>...');
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Hive.openBox<Channel>('scn-channel-cache');
|
|
|
|
} catch (exc, trace) {
|
|
|
|
Hive.deleteBoxFromDisk('scn-channel-cache');
|
|
|
|
await Hive.openBox<Channel>('scn-channel-cache');
|
|
|
|
ApplicationLog.error('Failed to open Hive-Box: scn-channel-cache' + exc.toString(), trace: trace);
|
|
|
|
}
|
|
|
|
|
2024-06-15 18:24:18 +02:00
|
|
|
print('[INIT] Load Hive<scn-fb-messages>...');
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Hive.openBox<FBMessage>('scn-fb-messages');
|
|
|
|
} catch (exc, trace) {
|
|
|
|
Hive.deleteBoxFromDisk('scn-fb-messages');
|
|
|
|
await Hive.openBox<FBMessage>('scn-fb-messages');
|
|
|
|
ApplicationLog.error('Failed to open Hive-Box: scn-fb-messages' + exc.toString(), trace: trace);
|
|
|
|
}
|
|
|
|
|
2024-06-04 08:20:28 +02:00
|
|
|
print('[INIT] Load AppAuth...');
|
|
|
|
|
2024-06-02 17:09:57 +02:00
|
|
|
final appAuth = AppAuth(); // ensure UserAccount is loaded
|
|
|
|
|
|
|
|
if (appAuth.isAuth()) {
|
|
|
|
try {
|
2024-06-04 08:20:28 +02:00
|
|
|
print('[INIT] Load User...');
|
2024-06-02 17:09:57 +02:00
|
|
|
await appAuth.loadUser();
|
2024-06-04 08:20:28 +02:00
|
|
|
//TODO fallback to cached user (perhaps event use cached client (if exists) directly and only update/load in background)
|
2024-06-02 17:09:57 +02:00
|
|
|
} catch (exc, trace) {
|
|
|
|
ApplicationLog.error('Failed to load user (on startup): ' + exc.toString(), trace: trace);
|
|
|
|
}
|
|
|
|
try {
|
2024-06-04 08:20:28 +02:00
|
|
|
print('[INIT] Load Client...');
|
2024-06-02 17:09:57 +02:00
|
|
|
await appAuth.loadClient();
|
2024-06-04 08:20:28 +02:00
|
|
|
//TODO fallback to cached client (perhaps event use cached client (if exists) directly and only update/load in background)
|
2024-06-02 17:09:57 +02:00
|
|
|
} catch (exc, trace) {
|
|
|
|
ApplicationLog.error('Failed to load user (on startup): ' + exc.toString(), trace: trace);
|
|
|
|
}
|
|
|
|
}
|
2024-05-31 15:22:27 +02:00
|
|
|
|
2024-06-04 08:20:28 +02:00
|
|
|
if (!Platform.isLinux) {
|
|
|
|
print('[INIT] Init Firebase...');
|
|
|
|
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
|
2024-05-31 15:22:27 +02:00
|
|
|
|
2024-06-04 08:20:28 +02:00
|
|
|
print('[INIT] Request Notification permissions...');
|
|
|
|
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-31 15:22:27 +02:00
|
|
|
|
|
|
|
try {
|
2024-06-04 08:20:28 +02:00
|
|
|
print('[INIT] Query firebase token...');
|
|
|
|
final fcmToken = await FirebaseMessaging.instance.getToken();
|
|
|
|
if (fcmToken != null) {
|
|
|
|
setFirebaseToken(fcmToken);
|
|
|
|
}
|
2024-05-31 15:22:27 +02:00
|
|
|
} catch (exc, trace) {
|
2024-06-04 08:20:28 +02:00
|
|
|
ApplicationLog.error('Failed to get+set firebase token: ' + exc.toString(), trace: trace);
|
2024-05-31 15:22:27 +02:00
|
|
|
}
|
2024-06-15 18:24:18 +02:00
|
|
|
|
|
|
|
FirebaseMessaging.onBackgroundMessage(_onBackgroundMessage);
|
|
|
|
FirebaseMessaging.onMessage.listen(_onForegroundMessage);
|
2024-06-04 08:20:28 +02:00
|
|
|
} else {
|
|
|
|
print('[INIT] Skip Firebase init (Platform == Linux)...');
|
2024-06-01 03:06:02 +02:00
|
|
|
}
|
|
|
|
|
2024-06-04 08:20:28 +02:00
|
|
|
ApplicationLog.debug('[INIT] Application started');
|
2024-05-26 00:20:25 +02:00
|
|
|
|
2024-02-11 01:08:51 +01:00
|
|
|
runApp(
|
|
|
|
MultiProvider(
|
|
|
|
providers: [
|
2024-06-02 17:09:57 +02:00
|
|
|
ChangeNotifierProvider(create: (context) => AppAuth(), lazy: false),
|
2024-05-26 00:20:25 +02:00
|
|
|
ChangeNotifierProvider(create: (context) => AppTheme(), lazy: false),
|
2024-06-13 17:00:08 +02:00
|
|
|
ChangeNotifierProvider(create: (context) => AppBarState(), lazy: false),
|
2024-02-11 01:08:51 +01:00
|
|
|
],
|
2024-06-13 17:00:08 +02:00
|
|
|
child: SCNApp(),
|
2024-02-11 01:08:51 +01:00
|
|
|
),
|
|
|
|
);
|
2024-02-10 18:29:41 +01:00
|
|
|
}
|
|
|
|
|
2024-06-15 18:24:18 +02:00
|
|
|
class SCNApp extends StatelessWidget {
|
|
|
|
SCNApp({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
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',
|
|
|
|
navigatorObservers: [Navi.routeObserver, Navi.modalRouteObserver],
|
|
|
|
theme: ThemeData(
|
|
|
|
//TODO color settings
|
|
|
|
colorScheme: ColorScheme.fromSeed(seedColor: Colors.blue, brightness: appTheme.darkMode ? Brightness.dark : Brightness.light),
|
|
|
|
useMaterial3: true,
|
|
|
|
),
|
|
|
|
home: SCNNavLayout(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-31 15:22:27 +02:00
|
|
|
void setFirebaseToken(String fcmToken) async {
|
2024-06-02 17:09:57 +02:00
|
|
|
final acc = AppAuth();
|
2024-06-01 03:06:02 +02:00
|
|
|
|
|
|
|
final oldToken = Globals().getPrefFCMToken();
|
|
|
|
|
2024-06-02 17:09:57 +02:00
|
|
|
await Globals().setPrefFCMToken(fcmToken);
|
2024-06-01 03:06:02 +02:00
|
|
|
|
|
|
|
ApplicationLog.info('New firebase token received', additional: 'Token: $fcmToken (old: $oldToken)');
|
|
|
|
|
2024-06-02 17:09:57 +02:00
|
|
|
if (!acc.isAuth()) return;
|
2024-06-01 03:06:02 +02:00
|
|
|
|
2024-06-02 17:09:57 +02:00
|
|
|
Client? client;
|
|
|
|
try {
|
2024-06-15 15:56:50 +02:00
|
|
|
client = await acc.loadClient(forceIfOlder: Duration(seconds: 60));
|
2024-06-02 17:09:57 +02:00
|
|
|
} catch (exc, trace) {
|
|
|
|
ApplicationLog.error('Failed to get client: ' + exc.toString(), trace: trace);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-08 14:16:07 +02:00
|
|
|
if (oldToken != null && oldToken == fcmToken && client != null && client.fcmToken == fcmToken) {
|
2024-06-02 17:09:57 +02:00
|
|
|
ApplicationLog.info('Firebase token unchanged - do nothing', additional: 'Token: $fcmToken');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (client == null) {
|
|
|
|
// should not really happen - perhaps someone externally deleted the client?
|
|
|
|
final newClient = await APIClient.addClient(acc, fcmToken, Globals().deviceModel, Globals().version, Globals().hostname, Globals().clientType);
|
|
|
|
acc.setClientAndClientID(newClient);
|
|
|
|
await acc.save();
|
|
|
|
} else {
|
|
|
|
final newClient = await APIClient.updateClient(acc, client.clientID, fcmToken, Globals().deviceModel, Globals().hostname, Globals().version);
|
|
|
|
acc.setClientAndClientID(newClient);
|
|
|
|
await acc.save();
|
2024-05-31 15:22:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-15 18:24:18 +02:00
|
|
|
Future<void> _onBackgroundMessage(RemoteMessage message) async {
|
|
|
|
await _receiveMessage(message, false);
|
|
|
|
}
|
2024-02-10 18:29:41 +01:00
|
|
|
|
2024-06-15 18:24:18 +02:00
|
|
|
void _onForegroundMessage(RemoteMessage message) {
|
|
|
|
_receiveMessage(message, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _receiveMessage(RemoteMessage message, bool foreground) async {
|
|
|
|
// ensure init
|
|
|
|
Hive.openBox<SCNLog>('scn-logs');
|
|
|
|
|
|
|
|
ApplicationLog.info('Received FB message (${foreground ? 'foreground' : 'background'}): ${message.messageId ?? 'NULL'}');
|
|
|
|
FBMessageLog.insert(message);
|
2024-02-10 18:29:41 +01:00
|
|
|
}
|