From 549311535c51c3529c8780d97d8b9e002ee8c715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Tue, 4 Jun 2024 08:20:28 +0200 Subject: [PATCH] Fix linux build (does not support fcm) --- flutter/lib/main.dart | 62 +++++++++++++------ flutter/lib/pages/account/account.dart | 21 +++++-- .../lib/pages/message_list/message_list.dart | 3 + flutter/pubspec.lock | 2 +- flutter/pubspec.yaml | 1 + 5 files changed, 64 insertions(+), 25 deletions(-) diff --git a/flutter/lib/main.dart b/flutter/lib/main.dart index d39bba6..764e6ee 100644 --- a/flutter/lib/main.dart +++ b/flutter/lib/main.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; @@ -17,15 +19,24 @@ import 'firebase_options.dart'; void main() async { print('[INIT] Application starting...'); + print('[INIT] Ensure WidgetsFlutterBinding...'); + WidgetsFlutterBinding.ensureInitialized(); - await Hive.initFlutter(); + print('[INIT] Init Globals...'); + await Globals().init(); + print('[INIT] Init Hive...'); + + await Hive.initFlutter(); + Hive.registerAdapter(SCNRequestAdapter()); Hive.registerAdapter(SCNLogAdapter()); Hive.registerAdapter(SCNLogLevelAdapter()); + print('[INIT] Load Hive...'); + try { await Hive.openBox('scn-requests'); } catch (exc, trace) { @@ -34,6 +45,8 @@ void main() async { ApplicationLog.error('Failed to open Hive-Box: scn-requests: ' + exc.toString(), trace: trace); } + print('[INIT] Load Hive...'); + try { await Hive.openBox('scn-logs'); } catch (exc, trace) { @@ -42,45 +55,58 @@ void main() async { ApplicationLog.error('Failed to open Hive-Box: scn-logs: ' + exc.toString(), trace: trace); } + print('[INIT] Load AppAuth...'); + final appAuth = AppAuth(); // ensure UserAccount is loaded if (appAuth.isAuth()) { try { + print('[INIT] Load User...'); await appAuth.loadUser(); + //TODO fallback to cached user (perhaps event use cached client (if exists) directly and only update/load in background) } catch (exc, trace) { ApplicationLog.error('Failed to load user (on startup): ' + exc.toString(), trace: trace); } try { + print('[INIT] Load Client...'); await appAuth.loadClient(); + //TODO fallback to cached client (perhaps event use cached client (if exists) directly and only update/load in background) } catch (exc, trace) { ApplicationLog.error('Failed to load user (on startup): ' + exc.toString(), trace: trace); } } - await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); + if (!Platform.isLinux) { + print('[INIT] Init Firebase...'); + await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform); - await FirebaseMessaging.instance.requestPermission(provisional: true); + 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() ?? '')); + }); - FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) { try { - setFirebaseToken(fcmToken); + print('[INIT] Query firebase token...'); + final fcmToken = await FirebaseMessaging.instance.getToken(); + if (fcmToken != null) { + setFirebaseToken(fcmToken); + } } catch (exc, trace) { - ApplicationLog.error('Failed to set firebase token: ' + exc.toString(), trace: trace); + ApplicationLog.error('Failed to get+set firebase token: ' + exc.toString(), trace: trace); } - }).onError((dynamic err) { - ApplicationLog.error('Failed to listen to token refresh events: ' + (err?.toString() ?? '')); - }); - - try { - final fcmToken = await FirebaseMessaging.instance.getToken(); - if (fcmToken != null) { - setFirebaseToken(fcmToken); - } - } catch (exc, trace) { - ApplicationLog.error('Failed to get+set firebase token: ' + exc.toString(), trace: trace); + } else { + print('[INIT] Skip Firebase init (Platform == Linux)...'); } - ApplicationLog.debug('Application started'); + ApplicationLog.debug('[INIT] Application started'); runApp( MultiProvider( diff --git a/flutter/lib/pages/account/account.dart b/flutter/lib/pages/account/account.dart index 46f298c..3d903e0 100644 --- a/flutter/lib/pages/account/account.dart +++ b/flutter/lib/pages/account/account.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/material.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; @@ -9,6 +11,7 @@ import 'package:simplecloudnotifier/state/application_log.dart'; import 'package:simplecloudnotifier/state/globals.dart'; import 'package:simplecloudnotifier/state/app_auth.dart'; import 'package:simplecloudnotifier/utils/toaster.dart'; +import 'package:uuid/uuid.dart'; class AccountRootPage extends StatefulWidget { const AccountRootPage({super.key}); @@ -447,15 +450,21 @@ class _AccountRootPageState extends State { final acc = Provider.of(context, listen: false); try { - final notificationSettings = await FirebaseMessaging.instance.requestPermission(provisional: true); + final String? fcmToken; + if (Platform.isLinux) { + Toaster.warn("Unsupported Platform", 'Your platform is not supported by FCM - notifications will not work'); + fcmToken = '(linux-' + Uuid().v4() + ')'; + } else { + final notificationSettings = await FirebaseMessaging.instance.requestPermission(provisional: true); - if (notificationSettings.authorizationStatus == AuthorizationStatus.denied) { - Toaster.error("Missing Permission", 'Please allow notifications to create an account'); - return; + if (notificationSettings.authorizationStatus == AuthorizationStatus.denied) { + Toaster.error("Missing Permission", 'Please allow notifications to create an account'); + return; + } + + fcmToken = await FirebaseMessaging.instance.getToken(); } - final fcmToken = await FirebaseMessaging.instance.getToken(); - if (fcmToken == null) { Toaster.warn("Missing Token", 'No FCM Token found, please allow notifications, ensure you have a network connection and restart the app'); return; diff --git a/flutter/lib/pages/message_list/message_list.dart b/flutter/lib/pages/message_list/message_list.dart index 570a668..cce810c 100644 --- a/flutter/lib/pages/message_list/message_list.dart +++ b/flutter/lib/pages/message_list/message_list.dart @@ -12,6 +12,9 @@ import 'package:simplecloudnotifier/pages/message_list/message_list_item.dart'; class MessageListPage extends StatefulWidget { const MessageListPage({super.key}); + //TODO reload on switch to tab + //TODO reload on app to foreground + @override State createState() => _MessageListPageState(); } diff --git a/flutter/pubspec.lock b/flutter/pubspec.lock index bfd33f7..a98ce15 100644 --- a/flutter/pubspec.lock +++ b/flutter/pubspec.lock @@ -941,7 +941,7 @@ packages: source: hosted version: "3.1.1" uuid: - dependency: transitive + dependency: "direct main" description: name: uuid sha256: "814e9e88f21a176ae1359149021870e87f7cddaf633ab678a5d2b0bff7fd1ba8" diff --git a/flutter/pubspec.yaml b/flutter/pubspec.yaml index 27f3355..a56972a 100644 --- a/flutter/pubspec.yaml +++ b/flutter/pubspec.yaml @@ -29,6 +29,7 @@ dependencies: firebase_messaging: ^14.9.4 device_info_plus: ^10.1.0 toastification: ^2.0.0 + uuid: ^4.4.0 dependency_overrides: