Surface/lib/main.dart

232 lines
7.7 KiB
Dart
Raw Normal View History

2024-12-15 04:10:45 +00:00
import 'dart:async';
import 'dart:io';
import 'package:bitsdojo_window/bitsdojo_window.dart';
2024-11-09 17:04:39 +00:00
import 'package:croppy/croppy.dart';
2024-11-08 16:09:46 +00:00
import 'package:easy_localization/easy_localization.dart';
import 'package:easy_localization_loader/easy_localization_loader.dart';
2024-11-20 16:18:11 +00:00
import 'package:firebase_core/firebase_core.dart';
2024-11-10 14:56:18 +00:00
import 'package:flutter/foundation.dart';
2024-11-08 16:09:46 +00:00
import 'package:flutter/material.dart';
import 'package:gap/gap.dart';
2024-11-23 16:22:08 +00:00
import 'package:go_router/go_router.dart';
2024-11-16 05:54:36 +00:00
import 'package:hive_flutter/hive_flutter.dart';
2024-11-08 16:09:46 +00:00
import 'package:provider/provider.dart';
2024-11-09 03:16:14 +00:00
import 'package:relative_time/relative_time.dart';
2024-11-08 16:09:46 +00:00
import 'package:responsive_framework/responsive_framework.dart';
import 'package:styled_widget/styled_widget.dart';
2024-11-20 16:18:11 +00:00
import 'package:surface/firebase_options.dart';
2024-11-16 13:15:55 +00:00
import 'package:surface/providers/channel.dart';
2024-11-24 12:23:06 +00:00
import 'package:surface/providers/chat_call.dart';
import 'package:surface/providers/config.dart';
2024-12-14 06:46:11 +00:00
import 'package:surface/providers/link_preview.dart';
2024-11-13 16:08:09 +00:00
import 'package:surface/providers/navigation.dart';
import 'package:surface/providers/notification.dart';
2024-11-25 16:00:09 +00:00
import 'package:surface/providers/post.dart';
2024-11-30 14:39:49 +00:00
import 'package:surface/providers/relationship.dart';
2024-11-09 04:04:03 +00:00
import 'package:surface/providers/sn_attachment.dart';
2024-11-08 16:09:46 +00:00
import 'package:surface/providers/sn_network.dart';
import 'package:surface/providers/theme.dart';
import 'package:surface/providers/user_directory.dart';
2024-11-08 16:09:46 +00:00
import 'package:surface/providers/userinfo.dart';
2024-11-14 16:24:46 +00:00
import 'package:surface/providers/websocket.dart';
2024-12-14 10:18:13 +00:00
import 'package:surface/providers/widget.dart';
2024-11-08 16:09:46 +00:00
import 'package:surface/router.dart';
2024-11-16 13:15:55 +00:00
import 'package:surface/types/chat.dart';
import 'package:surface/types/realm.dart';
2024-11-23 16:22:08 +00:00
import 'package:flutter_web_plugins/url_strategy.dart' show usePathUrlStrategy;
import 'package:surface/widgets/dialog.dart';
2024-12-13 17:36:23 +00:00
import 'package:surface/widgets/version_label.dart';
2024-11-08 16:09:46 +00:00
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await EasyLocalization.ensureInitialized();
2024-11-16 05:54:36 +00:00
await Hive.initFlutter();
2024-11-16 13:15:55 +00:00
Hive.registerAdapter(SnChannelImplAdapter());
Hive.registerAdapter(SnRealmImplAdapter());
Hive.registerAdapter(SnChannelMemberImplAdapter());
Hive.registerAdapter(SnChatMessageImplAdapter());
2024-11-16 05:54:36 +00:00
2024-11-20 16:18:11 +00:00
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
2024-11-23 16:22:08 +00:00
GoRouter.optionURLReflectsImperativeAPIs = true;
usePathUrlStrategy();
if (!kIsWeb && (Platform.isWindows || Platform.isLinux || Platform.isMacOS)) {
doWhenWindowReady(() {
appWindow.minSize = Size(480, 640);
appWindow.size = Size(1280, 720);
appWindow.alignment = Alignment.center;
appWindow.show();
});
}
runApp(const SolianApp());
2024-11-08 16:09:46 +00:00
}
class SolianApp extends StatelessWidget {
const SolianApp({super.key});
@override
Widget build(BuildContext context) {
return ResponsiveBreakpoints.builder(
child: EasyLocalization(
path: 'assets/translations',
supportedLocales: [
Locale('en', 'US'),
Locale('zh', 'CN'),
Locale('zh', 'TW'),
Locale('zh', 'HK'),
],
2024-11-08 16:09:46 +00:00
fallbackLocale: Locale('en', 'US'),
useFallbackTranslations: true,
assetLoader: JsonAssetLoader(),
child: MultiProvider(
providers: [
Provider(create: (ctx) => ConfigProvider()),
2024-11-14 16:24:46 +00:00
// Display layer
ChangeNotifierProvider(create: (_) => ThemeProvider()),
ChangeNotifierProvider(create: (ctx) => NavigationProvider()),
2024-12-14 10:18:13 +00:00
// System extensions layer
Provider(create: (ctx) => HomeWidgetProvider(ctx)),
2024-11-14 16:24:46 +00:00
// Data layer
2024-12-15 14:54:00 +00:00
Provider(create: (ctx) => SnNetworkProvider(ctx)),
Provider(create: (ctx) => UserDirectoryProvider(ctx)),
2024-11-09 04:04:03 +00:00
Provider(create: (ctx) => SnAttachmentProvider(ctx)),
2024-11-25 16:00:09 +00:00
Provider(create: (ctx) => SnPostContentProvider(ctx)),
2024-11-30 14:39:49 +00:00
Provider(create: (ctx) => SnRelationshipProvider(ctx)),
2024-12-14 06:46:11 +00:00
Provider(create: (ctx) => SnLinkPreviewProvider(ctx)),
2024-11-09 10:28:45 +00:00
ChangeNotifierProvider(create: (ctx) => UserProvider(ctx)),
2024-11-14 16:24:46 +00:00
ChangeNotifierProvider(create: (ctx) => WebSocketProvider(ctx)),
2024-11-21 14:55:00 +00:00
ChangeNotifierProvider(create: (ctx) => NotificationProvider(ctx)),
2024-11-16 13:15:55 +00:00
ChangeNotifierProvider(create: (ctx) => ChatChannelProvider(ctx)),
2024-11-24 12:23:06 +00:00
ChangeNotifierProvider(create: (ctx) => ChatCallProvider(ctx)),
2024-11-08 16:09:46 +00:00
],
child: _AppDelegate(),
2024-11-08 16:09:46 +00:00
),
),
breakpoints: [
const Breakpoint(start: 0, end: 450, name: MOBILE),
const Breakpoint(start: 451, end: 800, name: TABLET),
const Breakpoint(start: 801, end: 1920, name: DESKTOP),
],
);
}
}
class _AppDelegate extends StatelessWidget {
2024-12-14 06:46:11 +00:00
const _AppDelegate();
@override
Widget build(BuildContext context) {
2024-11-13 16:08:09 +00:00
context.read<NavigationProvider>();
2024-11-14 16:24:46 +00:00
context.read<WebSocketProvider>();
2024-11-16 13:15:55 +00:00
context.read<ChatChannelProvider>();
2024-11-21 14:55:00 +00:00
context.read<NotificationProvider>();
final th = context.watch<ThemeProvider>();
return MaterialApp.router(
theme: th.theme?.light,
darkTheme: th.theme?.dark,
locale: context.locale,
supportedLocales: context.supportedLocales,
localizationsDelegates: [
CroppyLocalizations.delegate,
RelativeTimeLocalizations.delegate,
...context.localizationDelegates,
],
routerConfig: appRouter,
builder: (context, child) {
2024-12-14 06:46:11 +00:00
return _AppSplashScreen(
key: const Key('global-splash-screen'),
child: child!,
);
},
);
}
}
class _AppSplashScreen extends StatefulWidget {
final Widget child;
const _AppSplashScreen({super.key, required this.child});
@override
State<_AppSplashScreen> createState() => _AppSplashScreenState();
}
class _AppSplashScreenState extends State<_AppSplashScreen> {
bool _isReady = false;
Future<void> _initialize() async {
try {
final config = context.read<ConfigProvider>();
await config.initialize();
if (!mounted) return;
2024-12-14 10:18:13 +00:00
final home = context.read<HomeWidgetProvider>();
await home.initialize();
if (!mounted) return;
final sn = context.read<SnNetworkProvider>();
await sn.initializeUserAgent();
if (!mounted) return;
final ua = context.read<UserProvider>();
await ua.initialize();
if (!mounted) return;
final ws = context.read<WebSocketProvider>();
await ws.tryConnect();
if (!mounted) return;
final notify = context.read<NotificationProvider>();
await notify.registerPushNotifications();
} catch (err) {
if (!mounted) return;
await context.showErrorDialog(err);
} finally {
setState(() => _isReady = true);
}
}
@override
void initState() {
super.initState();
_initialize();
}
@override
Widget build(BuildContext context) {
if (!_isReady) {
return Scaffold(
backgroundColor: Theme.of(context).colorScheme.surface,
body: Container(
constraints: const BoxConstraints(maxWidth: 180),
child: Column(
2024-12-14 06:46:11 +00:00
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
2024-12-15 14:54:00 +00:00
if (MediaQuery.of(context).platformBrightness == Brightness.dark)
Image.asset("assets/icon/icon-dark.png", width: 64, height: 64)
else
Image.asset("assets/icon/icon.png", width: 64, height: 64),
const Gap(6),
LinearProgressIndicator(
backgroundColor: Theme.of(context).colorScheme.surfaceContainer,
),
const Gap(20),
Text('appInitializing'.tr(), textAlign: TextAlign.center),
2024-12-13 17:36:23 +00:00
AppVersionLabel(),
],
),
).center(),
);
}
return widget.child;
}
}