veilidchat/lib/app.dart

195 lines
7.9 KiB
Dart
Raw Normal View History

2023-07-26 14:20:29 -04:00
import 'package:animated_theme_switcher/animated_theme_switcher.dart';
2024-06-07 14:42:04 -04:00
import 'package:async_tools/async_tools.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
2023-07-26 15:58:38 -04:00
import 'package:flutter/foundation.dart';
2023-01-08 22:27:33 -05:00
import 'package:flutter/material.dart';
2024-04-10 16:13:08 -04:00
import 'package:flutter/services.dart';
2023-12-26 20:26:54 -05:00
import 'package:flutter_bloc/flutter_bloc.dart';
2023-07-07 19:33:28 -04:00
import 'package:flutter_localizations/flutter_localizations.dart';
2023-07-26 14:20:29 -04:00
import 'package:flutter_translate/flutter_translate.dart';
2023-07-24 09:31:51 -04:00
import 'package:form_builder_validators/form_builder_validators.dart';
import 'package:provider/provider.dart';
2024-06-07 14:42:04 -04:00
import 'package:veilid_support/veilid_support.dart';
2023-07-26 14:20:29 -04:00
2023-12-27 22:56:24 -05:00
import 'account_manager/account_manager.dart';
import 'init.dart';
import 'layout/splash.dart';
import 'notifications/notifications.dart';
2023-01-09 22:50:34 -05:00
import 'router/router.dart';
2024-01-08 21:37:08 -05:00
import 'settings/settings.dart';
2024-07-06 20:09:18 -04:00
import 'theme/theme.dart';
2023-08-08 02:03:26 -04:00
import 'tick.dart';
2024-04-10 16:13:08 -04:00
import 'tools/loggy.dart';
2024-02-12 10:35:41 -05:00
import 'veilid_processor/veilid_processor.dart';
2023-01-08 22:27:33 -05:00
2024-04-10 16:13:08 -04:00
class ReloadThemeIntent extends Intent {
const ReloadThemeIntent();
}
class AttachDetachIntent extends Intent {
const AttachDetachIntent();
2024-06-07 14:42:04 -04:00
}
class ScrollBehaviorModified extends ScrollBehavior {
const ScrollBehaviorModified();
@override
ScrollPhysics getScrollPhysics(BuildContext context) =>
const ClampingScrollPhysics();
}
2023-12-26 20:26:54 -05:00
class VeilidChatApp extends StatelessWidget {
2023-01-08 22:27:33 -05:00
const VeilidChatApp({
2024-01-08 21:37:08 -05:00
required this.initialThemeData,
2023-07-26 15:58:38 -04:00
super.key,
2023-07-26 14:20:29 -04:00
});
2023-01-08 22:27:33 -05:00
2023-12-27 22:56:24 -05:00
static const String name = 'VeilidChat';
2024-01-08 21:37:08 -05:00
final ThemeData initialThemeData;
2023-01-08 22:27:33 -05:00
2024-04-10 16:13:08 -04:00
void _reloadTheme(BuildContext context) {
log.info('Reloading theme');
final theme =
2024-07-26 16:51:03 -04:00
PreferencesRepository.instance.value.themePreference.themeData();
2024-04-10 16:13:08 -04:00
ThemeSwitcher.of(context).changeTheme(theme: theme);
2024-06-07 14:42:04 -04:00
// Hack to reload translations
final localizationDelegate = LocalizedApp.of(context).delegate;
singleFuture(this, () async {
await LocalizationDelegate.create(
fallbackLocale: localizationDelegate.fallbackLocale.toString(),
supportedLocales: localizationDelegate.supportedLocales
.map((x) => x.toString())
.toList());
});
}
void _attachDetach(BuildContext context) {
2024-06-07 14:42:04 -04:00
singleFuture(this, () async {
if (ProcessorRepository.instance.processorConnectionState.isAttached) {
log.info('Detaching');
await Veilid.instance.detach();
} else if (ProcessorRepository
.instance.processorConnectionState.isDetached) {
log.info('Attaching');
await Veilid.instance.attach();
}
});
2024-04-10 16:13:08 -04:00
}
2024-07-06 20:09:18 -04:00
Widget _buildShortcuts({required Widget Function(BuildContext) builder}) =>
2024-04-10 16:13:08 -04:00
ThemeSwitcher(
builder: (context) => Shortcuts(
shortcuts: <LogicalKeySet, Intent>{
LogicalKeySet(
LogicalKeyboardKey.alt, LogicalKeyboardKey.keyR):
const ReloadThemeIntent(),
2024-06-07 14:42:04 -04:00
LogicalKeySet(
LogicalKeyboardKey.alt, LogicalKeyboardKey.keyD):
const AttachDetachIntent(),
2024-04-10 16:13:08 -04:00
},
child: Actions(actions: <Type, Action<Intent>>{
ReloadThemeIntent: CallbackAction<ReloadThemeIntent>(
onInvoke: (intent) => _reloadTheme(context)),
AttachDetachIntent: CallbackAction<AttachDetachIntent>(
onInvoke: (intent) => _attachDetach(context)),
2024-04-10 16:13:08 -04:00
}, child: Focus(autofocus: true, child: builder(context)))));
2023-01-08 22:27:33 -05:00
@override
Widget build(BuildContext context) => FutureProvider<VeilidChatGlobalInit?>(
initialData: null,
create: (context) async => VeilidChatGlobalInit.initialize(),
2024-07-11 23:04:08 -04:00
builder: (context, __) {
final globalInit = context.watch<VeilidChatGlobalInit?>();
if (globalInit == null) {
// Splash screen until we're done with init
return const Splash();
}
// Once init is done, we proceed with the app
final localizationDelegate = LocalizedApp.of(context).delegate;
return ThemeProvider(
2024-04-05 22:03:04 -04:00
initTheme: initialThemeData,
builder: (context, theme) => LocalizationProvider(
2024-04-05 22:03:04 -04:00
state: LocalizationProvider.of(context).state,
child: MultiBlocProvider(
providers: [
BlocProvider<PreferencesCubit>(
create: (context) =>
PreferencesCubit(PreferencesRepository.instance),
),
BlocProvider<NotificationsCubit>(
create: (context) => NotificationsCubit(
const NotificationsState(queue: IList.empty()))),
2024-04-05 22:03:04 -04:00
BlocProvider<ConnectionStateCubit>(
create: (context) =>
ConnectionStateCubit(ProcessorRepository.instance)),
BlocProvider<RouterCubit>(
create: (context) =>
RouterCubit(AccountRepository.instance),
),
BlocProvider<LocalAccountsCubit>(
create: (context) =>
LocalAccountsCubit(AccountRepository.instance),
),
BlocProvider<UserLoginsCubit>(
create: (context) =>
UserLoginsCubit(AccountRepository.instance),
),
2024-06-16 22:12:24 -04:00
BlocProvider<ActiveLocalAccountCubit>(
2024-04-05 22:03:04 -04:00
create: (context) =>
2024-06-16 22:12:24 -04:00
ActiveLocalAccountCubit(AccountRepository.instance),
2024-04-05 22:03:04 -04:00
),
2024-06-18 21:20:06 -04:00
BlocProvider<PerAccountCollectionBlocMapCubit>(
create: (context) => PerAccountCollectionBlocMapCubit(
accountRepository: AccountRepository.instance,
locator: context.read)),
2024-04-05 22:03:04 -04:00
],
2024-07-06 20:09:18 -04:00
child:
BackgroundTicker(child: _buildShortcuts(builder: (context) {
final scale = theme.extension<ScaleScheme>()!;
final scaleConfig = theme.extension<ScaleConfig>()!;
final gradient = LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: scaleConfig.preferBorders &&
theme.brightness == Brightness.light
? [
scale.grayScale.hoverElementBackground,
scale.grayScale.subtleBackground,
]
: [
2024-07-08 21:29:52 -04:00
scale.primaryScale.hoverElementBackground,
scale.primaryScale.subtleBackground,
2024-07-06 20:09:18 -04:00
]);
return DecoratedBox(
decoration: BoxDecoration(gradient: gradient),
child: MaterialApp.router(
scrollBehavior: const ScrollBehaviorModified(),
2024-07-06 20:09:18 -04:00
debugShowCheckedModeBanner: false,
routerConfig: context.read<RouterCubit>().router(),
title: translate('app.title'),
theme: theme,
localizationsDelegates: [
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
FormBuilderLocalizations.delegate,
localizationDelegate
],
supportedLocales: localizationDelegate.supportedLocales,
locale: localizationDelegate.currentLocale,
));
})),
2024-04-05 22:03:04 -04:00
)),
);
});
2023-07-26 15:58:38 -04:00
2023-07-26 14:20:29 -04:00
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
2024-01-08 21:37:08 -05:00
properties
.add(DiagnosticsProperty<ThemeData>('themeData', initialThemeData));
2023-07-26 14:20:29 -04:00
}
2023-01-08 22:27:33 -05:00
}