2024-08-16 14:59:34 +00:00
|
|
|
import 'dart:ui';
|
|
|
|
|
2024-06-06 15:28:19 +00:00
|
|
|
import 'package:firebase_core/firebase_core.dart';
|
2024-08-16 14:59:34 +00:00
|
|
|
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
|
2024-09-15 07:55:14 +00:00
|
|
|
import 'package:flutter/material.dart' hide Notification;
|
2024-06-22 15:20:31 +00:00
|
|
|
import 'package:flutter_acrylic/flutter_acrylic.dart';
|
2024-09-15 07:55:14 +00:00
|
|
|
import 'package:flutter_background_service/flutter_background_service.dart';
|
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
2024-05-18 14:23:36 +00:00
|
|
|
import 'package:get/get.dart';
|
2024-07-27 12:27:29 +00:00
|
|
|
import 'package:go_router/go_router.dart';
|
2024-06-29 10:09:56 +00:00
|
|
|
import 'package:protocol_handler/protocol_handler.dart';
|
2024-07-31 14:48:22 +00:00
|
|
|
import 'package:provider/provider.dart';
|
2024-09-15 07:55:14 +00:00
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
2024-07-26 17:39:20 +00:00
|
|
|
import 'package:solian/bootstrapper.dart';
|
2024-06-06 15:28:19 +00:00
|
|
|
import 'package:solian/firebase_options.dart';
|
2024-09-15 07:55:14 +00:00
|
|
|
import 'package:solian/models/notification.dart';
|
2024-06-22 15:20:31 +00:00
|
|
|
import 'package:solian/platform.dart';
|
2024-08-01 14:13:08 +00:00
|
|
|
import 'package:solian/providers/attachment_uploader.dart';
|
2024-09-02 15:11:40 +00:00
|
|
|
import 'package:solian/providers/daily_sign.dart';
|
2024-09-13 16:30:33 +00:00
|
|
|
import 'package:solian/providers/database/database.dart';
|
|
|
|
import 'package:solian/providers/database/services/messages.dart';
|
2024-09-03 15:07:20 +00:00
|
|
|
import 'package:solian/providers/last_read.dart';
|
2024-08-19 11:36:01 +00:00
|
|
|
import 'package:solian/providers/link_expander.dart';
|
2024-09-12 16:17:56 +00:00
|
|
|
import 'package:solian/providers/navigation.dart';
|
2024-08-03 17:53:52 +00:00
|
|
|
import 'package:solian/providers/stickers.dart';
|
2024-07-31 14:48:22 +00:00
|
|
|
import 'package:solian/providers/theme_switcher.dart';
|
2024-07-16 11:46:53 +00:00
|
|
|
import 'package:solian/providers/websocket.dart';
|
2024-05-18 14:23:36 +00:00
|
|
|
import 'package:solian/providers/auth.dart';
|
2024-05-22 15:18:01 +00:00
|
|
|
import 'package:solian/providers/content/attachment.dart';
|
2024-07-23 17:17:41 +00:00
|
|
|
import 'package:solian/providers/call.dart';
|
2024-05-25 16:11:00 +00:00
|
|
|
import 'package:solian/providers/content/channel.dart';
|
2024-07-23 10:09:41 +00:00
|
|
|
import 'package:solian/providers/content/posts.dart';
|
2024-05-25 16:11:00 +00:00
|
|
|
import 'package:solian/providers/content/realm.dart';
|
2024-07-23 17:17:41 +00:00
|
|
|
import 'package:solian/providers/relation.dart';
|
2024-06-26 17:33:03 +00:00
|
|
|
import 'package:solian/providers/account_status.dart';
|
2024-05-18 10:17:16 +00:00
|
|
|
import 'package:solian/router.dart';
|
2024-07-07 06:41:23 +00:00
|
|
|
import 'package:solian/shells/system_shell.dart';
|
2024-05-18 10:17:16 +00:00
|
|
|
import 'package:solian/theme.dart';
|
|
|
|
import 'package:solian/translations.dart';
|
2024-07-27 12:27:29 +00:00
|
|
|
import 'package:flutter_web_plugins/url_strategy.dart' show usePathUrlStrategy;
|
2024-05-18 10:17:16 +00:00
|
|
|
|
2024-06-01 16:42:36 +00:00
|
|
|
void main() async {
|
2024-08-16 14:59:34 +00:00
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
2024-06-09 15:30:01 +00:00
|
|
|
|
2024-08-16 14:59:34 +00:00
|
|
|
await Future.wait([
|
|
|
|
_initializeFirebase(),
|
|
|
|
_initializePlatformComponents(),
|
2024-09-15 07:55:14 +00:00
|
|
|
_initializeBackgroundNotificationService(),
|
2024-08-16 14:59:34 +00:00
|
|
|
]);
|
2024-06-09 15:30:01 +00:00
|
|
|
|
2024-08-16 14:59:34 +00:00
|
|
|
GoRouter.optionURLReflectsImperativeAPIs = true;
|
2024-07-27 12:27:29 +00:00
|
|
|
|
2024-09-13 16:30:33 +00:00
|
|
|
Get.put(DatabaseProvider());
|
2024-09-11 11:56:32 +00:00
|
|
|
Get.put(AppTranslations());
|
|
|
|
await AppTranslations.init();
|
|
|
|
|
2024-08-16 14:59:34 +00:00
|
|
|
usePathUrlStrategy();
|
|
|
|
runApp(const SolianApp());
|
2024-05-18 10:17:16 +00:00
|
|
|
}
|
|
|
|
|
2024-07-05 15:37:54 +00:00
|
|
|
Future<void> _initializeFirebase() async {
|
|
|
|
await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
|
2024-08-16 14:59:34 +00:00
|
|
|
FlutterError.onError = (errorDetails) {
|
|
|
|
FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
|
|
|
|
};
|
|
|
|
PlatformDispatcher.instance.onError = (error, stack) {
|
|
|
|
FirebaseCrashlytics.instance.recordError(error, stack, fatal: true);
|
|
|
|
return true;
|
|
|
|
};
|
2024-07-05 15:37:54 +00:00
|
|
|
}
|
|
|
|
|
2024-09-15 07:55:14 +00:00
|
|
|
Future<void> _initializeBackgroundNotificationService() async {
|
|
|
|
final prefs = await SharedPreferences.getInstance();
|
|
|
|
if (prefs.getBool('service_background_notification') != true) return;
|
|
|
|
|
|
|
|
final service = FlutterBackgroundService();
|
|
|
|
|
|
|
|
await service.configure(
|
|
|
|
androidConfiguration: AndroidConfiguration(
|
|
|
|
onStart: onBackgroundNotificationServiceStart,
|
|
|
|
autoStart: true,
|
|
|
|
autoStartOnBoot: true,
|
|
|
|
isForegroundMode: false,
|
|
|
|
),
|
|
|
|
// This feature won't be able to use on iOS
|
|
|
|
// We got APNs support covered
|
|
|
|
iosConfiguration: IosConfiguration(
|
|
|
|
autoStart: false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
await service.startService();
|
|
|
|
}
|
|
|
|
|
|
|
|
@pragma('vm:entry-point')
|
|
|
|
void onBackgroundNotificationServiceStart(ServiceInstance service) async {
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
|
|
DartPluginRegistrant.ensureInitialized();
|
|
|
|
|
|
|
|
Get.put(AuthProvider());
|
|
|
|
Get.put(WebSocketProvider());
|
|
|
|
|
|
|
|
final auth = Get.find<AuthProvider>();
|
|
|
|
await auth.refreshAuthorizeStatus();
|
|
|
|
await auth.ensureCredentials();
|
|
|
|
if (!auth.isAuthorized.value) {
|
|
|
|
debugPrint(
|
|
|
|
'Background notification do nothing due to user didn\'t sign in.',
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const notificationChannelId = 'solian_notification_service';
|
|
|
|
|
|
|
|
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
|
|
|
|
FlutterLocalNotificationsPlugin();
|
|
|
|
|
|
|
|
final ws = Get.find<WebSocketProvider>();
|
|
|
|
await ws.connect();
|
|
|
|
debugPrint('Background notification has been started');
|
|
|
|
ws.stream.stream.listen(
|
|
|
|
(event) {
|
|
|
|
debugPrint(
|
|
|
|
'Background notification service incoming message: ${event.method} ${event.message}',
|
|
|
|
);
|
|
|
|
|
|
|
|
if (event.method == 'notifications.new' && event.payload != null) {
|
|
|
|
final data = Notification.fromJson(event.payload!);
|
|
|
|
debugPrint(
|
|
|
|
'Background notification service got a notification id=${data.id}',
|
|
|
|
);
|
|
|
|
flutterLocalNotificationsPlugin.show(
|
|
|
|
data.id,
|
|
|
|
data.title,
|
|
|
|
[data.subtitle, data.body].where((x) => x != null).join('\n'),
|
|
|
|
const NotificationDetails(
|
|
|
|
android: AndroidNotificationDetails(
|
|
|
|
notificationChannelId,
|
|
|
|
'Solian Notification Service',
|
|
|
|
channelDescription: 'Notifications that sent via Solar Network',
|
|
|
|
importance: Importance.high,
|
|
|
|
icon: 'mipmap/ic_launcher',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-05 15:37:54 +00:00
|
|
|
Future<void> _initializePlatformComponents() async {
|
|
|
|
if (!PlatformInfo.isWeb) {
|
|
|
|
await protocolHandler.register('solink');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PlatformInfo.isDesktop) {
|
|
|
|
await Window.initialize();
|
|
|
|
|
|
|
|
if (PlatformInfo.isMacOS) {
|
|
|
|
await Future.wait([
|
|
|
|
Window.hideTitle(),
|
|
|
|
Window.makeTitlebarTransparent(),
|
|
|
|
Window.enableFullSizeContentView(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-31 14:48:22 +00:00
|
|
|
final themeSwitcher = ThemeSwitcher(
|
2024-09-13 12:22:10 +00:00
|
|
|
lightThemeData: AppTheme.build(Brightness.light),
|
|
|
|
darkThemeData: AppTheme.build(Brightness.dark),
|
2024-07-31 17:21:27 +00:00
|
|
|
);
|
2024-07-31 14:48:22 +00:00
|
|
|
|
2024-05-18 10:17:16 +00:00
|
|
|
class SolianApp extends StatelessWidget {
|
|
|
|
const SolianApp({super.key});
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-07-31 14:48:22 +00:00
|
|
|
return ChangeNotifierProvider.value(
|
|
|
|
value: themeSwitcher,
|
|
|
|
child: Builder(builder: (context) {
|
|
|
|
final theme = Provider.of<ThemeSwitcher>(context);
|
|
|
|
|
|
|
|
return GetMaterialApp.router(
|
|
|
|
title: 'Solian',
|
|
|
|
theme: theme.lightThemeData,
|
|
|
|
darkTheme: theme.darkThemeData,
|
|
|
|
themeMode: ThemeMode.system,
|
|
|
|
routerDelegate: AppRouter.instance.routerDelegate,
|
|
|
|
routeInformationParser: AppRouter.instance.routeInformationParser,
|
|
|
|
routeInformationProvider: AppRouter.instance.routeInformationProvider,
|
|
|
|
backButtonDispatcher: AppRouter.instance.backButtonDispatcher,
|
2024-09-11 11:56:32 +00:00
|
|
|
translations: Get.find<AppTranslations>(),
|
2024-08-04 04:55:05 +00:00
|
|
|
locale: Get.deviceLocale,
|
2024-07-31 14:48:22 +00:00
|
|
|
fallbackLocale: const Locale('en', 'US'),
|
2024-09-11 11:56:32 +00:00
|
|
|
onInit: () {
|
|
|
|
_initializeProviders(context);
|
|
|
|
},
|
2024-07-31 14:48:22 +00:00
|
|
|
builder: (context, child) {
|
|
|
|
return SystemShell(
|
|
|
|
child: ScaffoldMessenger(
|
|
|
|
child: BootstrapperShell(
|
2024-09-07 09:48:07 +00:00
|
|
|
child: child ?? const SizedBox.shrink(),
|
2024-07-31 14:48:22 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
2024-05-18 10:17:16 +00:00
|
|
|
);
|
2024-07-31 14:48:22 +00:00
|
|
|
}),
|
2024-05-18 10:17:16 +00:00
|
|
|
);
|
|
|
|
}
|
2024-07-05 15:37:54 +00:00
|
|
|
|
2024-07-12 13:59:16 +00:00
|
|
|
void _initializeProviders(BuildContext context) async {
|
2024-09-12 16:17:56 +00:00
|
|
|
Get.put(NavigationStateProvider());
|
|
|
|
|
2024-07-05 15:37:54 +00:00
|
|
|
Get.lazyPut(() => AuthProvider());
|
2024-07-23 17:17:41 +00:00
|
|
|
Get.lazyPut(() => RelationshipProvider());
|
2024-07-23 10:09:41 +00:00
|
|
|
Get.lazyPut(() => PostProvider());
|
2024-08-03 17:53:52 +00:00
|
|
|
Get.lazyPut(() => StickerProvider());
|
2024-07-05 15:37:54 +00:00
|
|
|
Get.lazyPut(() => AttachmentProvider());
|
2024-07-16 11:46:53 +00:00
|
|
|
Get.lazyPut(() => WebSocketProvider());
|
2024-07-05 15:37:54 +00:00
|
|
|
Get.lazyPut(() => StatusProvider());
|
|
|
|
Get.lazyPut(() => ChannelProvider());
|
|
|
|
Get.lazyPut(() => RealmProvider());
|
2024-09-13 16:30:33 +00:00
|
|
|
Get.lazyPut(() => MessagesFetchingProvider());
|
2024-07-05 15:37:54 +00:00
|
|
|
Get.lazyPut(() => ChatCallProvider());
|
2024-08-01 14:13:08 +00:00
|
|
|
Get.lazyPut(() => AttachmentUploaderController());
|
2024-09-02 15:11:40 +00:00
|
|
|
Get.lazyPut(() => LinkExpandProvider());
|
|
|
|
Get.lazyPut(() => DailySignProvider());
|
2024-09-03 15:07:20 +00:00
|
|
|
Get.lazyPut(() => LastReadProvider());
|
2024-09-15 07:55:14 +00:00
|
|
|
|
|
|
|
Get.find<WebSocketProvider>().requestPermissions();
|
2024-07-05 15:37:54 +00:00
|
|
|
}
|
2024-05-18 10:17:16 +00:00
|
|
|
}
|