mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-09 02:07:58 -06:00
feat: Implement switching between accounts (multi user support), still WIP
This commit is contained in:
258
lib/main.dart
258
lib/main.dart
@@ -2,6 +2,7 @@ import 'dart:io';
|
||||
|
||||
import 'package:connectivity_plus/connectivity_plus.dart';
|
||||
import 'package:device_info_plus/device_info_plus.dart';
|
||||
import 'package:dynamic_color/dynamic_color.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart' as cm;
|
||||
@@ -9,14 +10,13 @@ import 'package:flutter_displaymode/flutter_displaymode.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:flutter_native_splash/flutter_native_splash.dart';
|
||||
import 'package:hive_flutter/adapters.dart';
|
||||
|
||||
import 'package:hydrated_bloc/hydrated_bloc.dart';
|
||||
import 'package:intl/date_symbol_data_local.dart';
|
||||
import 'package:intl/intl_standalone.dart';
|
||||
import 'package:local_auth/local_auth.dart';
|
||||
import 'package:package_info_plus/package_info_plus.dart';
|
||||
import 'package:paperless_api/paperless_api.dart';
|
||||
import 'package:paperless_mobile/core/bloc/bloc_changes_observer.dart';
|
||||
import 'package:paperless_mobile/constants.dart';
|
||||
import 'package:paperless_mobile/core/bloc/connectivity_cubit.dart';
|
||||
import 'package:paperless_mobile/core/bloc/paperless_server_information_cubit.dart';
|
||||
import 'package:paperless_mobile/core/config/hive/hive_config.dart';
|
||||
@@ -28,32 +28,33 @@ import 'package:paperless_mobile/core/repository/saved_view_repository.dart';
|
||||
import 'package:paperless_mobile/core/security/session_manager.dart';
|
||||
import 'package:paperless_mobile/core/service/connectivity_status_service.dart';
|
||||
import 'package:paperless_mobile/core/service/dio_file_service.dart';
|
||||
import 'package:paperless_mobile/core/type/types.dart';
|
||||
import 'package:paperless_mobile/features/app_intro/application_intro_slideshow.dart';
|
||||
import 'package:paperless_mobile/features/home/view/home_page.dart';
|
||||
import 'package:paperless_mobile/features/home/view/widget/verify_identity_page.dart';
|
||||
import 'package:paperless_mobile/features/login/cubit/authentication_cubit.dart';
|
||||
import 'package:paperless_mobile/features/login/model/client_certificate.dart';
|
||||
import 'package:paperless_mobile/features/login/model/login_form_credentials.dart';
|
||||
import 'package:paperless_mobile/features/login/model/user_account.dart';
|
||||
import 'package:paperless_mobile/features/login/services/authentication_service.dart';
|
||||
import 'package:paperless_mobile/features/login/view/login_page.dart';
|
||||
import 'package:paperless_mobile/features/notifications/services/local_notification_service.dart';
|
||||
import 'package:paperless_mobile/features/settings/global_app_settings.dart';
|
||||
import 'package:paperless_mobile/features/settings/cubit/application_settings_cubit.dart';
|
||||
import 'package:paperless_mobile/features/settings/user_app_settings.dart';
|
||||
import 'package:paperless_mobile/features/settings/model/global_settings.dart';
|
||||
import 'package:paperless_mobile/features/settings/model/user_settings.dart';
|
||||
import 'package:paperless_mobile/features/settings/view/widgets/global_settings_builder.dart';
|
||||
import 'package:paperless_mobile/features/sharing/share_intent_queue.dart';
|
||||
import 'package:paperless_mobile/features/tasks/cubit/task_status_cubit.dart';
|
||||
import 'package:paperless_mobile/generated/l10n/app_localizations.dart';
|
||||
import 'package:paperless_mobile/helpers/message_helpers.dart';
|
||||
import 'package:paperless_mobile/routes/document_details_route.dart';
|
||||
import 'package:paperless_mobile/theme.dart';
|
||||
import 'package:paperless_mobile/constants.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:receive_sharing_intent/receive_sharing_intent.dart';
|
||||
import 'package:dynamic_color/dynamic_color.dart';
|
||||
|
||||
String get defaultPreferredLocaleSubtag {
|
||||
String preferredLocale = Platform.localeName.split("_").first;
|
||||
if (!S.supportedLocales
|
||||
.any((locale) => locale.languageCode == preferredLocale)) {
|
||||
if (!S.supportedLocales.any((locale) => locale.languageCode == preferredLocale)) {
|
||||
preferredLocale = 'en';
|
||||
}
|
||||
return preferredLocale;
|
||||
@@ -61,23 +62,25 @@ String get defaultPreferredLocaleSubtag {
|
||||
|
||||
Future<void> _initHive() async {
|
||||
await Hive.initFlutter();
|
||||
//TODO: REMOVE!
|
||||
// await getApplicationDocumentsDirectory().then((value) => value.delete(recursive: true));
|
||||
|
||||
registerHiveAdapters();
|
||||
final globalSettingsBox =
|
||||
await Hive.openBox<GlobalAppSettings>(HiveBoxes.globalSettings);
|
||||
if (!globalSettingsBox.containsKey(HiveBoxSingleValueKey.value)) {
|
||||
await globalSettingsBox.put(
|
||||
HiveBoxSingleValueKey.value,
|
||||
GlobalAppSettings(preferredLocaleSubtag: defaultPreferredLocaleSubtag),
|
||||
);
|
||||
await Hive.openBox<UserAccount>(HiveBoxes.userAccount);
|
||||
await Hive.openBox<UserSettings>(HiveBoxes.userSettings);
|
||||
final globalSettingsBox = await Hive.openBox<GlobalSettings>(HiveBoxes.globalSettings);
|
||||
|
||||
if (!globalSettingsBox.hasValue) {
|
||||
await globalSettingsBox
|
||||
.setValue(GlobalSettings(preferredLocaleSubtag: defaultPreferredLocaleSubtag));
|
||||
}
|
||||
}
|
||||
|
||||
void main() async {
|
||||
await _initHive();
|
||||
final widgetsBinding = WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
final globalSettings = Hive.box<GlobalAppSettings>(HiveBoxes.globalSettings)
|
||||
.get(HiveBoxSingleValueKey.value)!;
|
||||
final globalSettingsBox = Hive.box<GlobalSettings>(HiveBoxes.globalSettings);
|
||||
final globalSettings = globalSettingsBox.getValue()!;
|
||||
|
||||
await findSystemLocale();
|
||||
packageInfo = await PackageInfo.fromPlatform();
|
||||
@@ -95,9 +98,8 @@ void main() async {
|
||||
final connectivityStatusService = ConnectivityStatusServiceImpl(connectivity);
|
||||
final localAuthService = LocalAuthenticationService(localAuthentication);
|
||||
|
||||
final hiveDir = await getApplicationDocumentsDirectory();
|
||||
HydratedBloc.storage = await HydratedStorage.build(
|
||||
storageDirectory: hiveDir,
|
||||
storageDirectory: await getApplicationDocumentsDirectory(),
|
||||
);
|
||||
|
||||
FlutterNativeSplash.preserve(widgetsBinding: widgetsBinding);
|
||||
@@ -136,34 +138,27 @@ void main() async {
|
||||
localAuthService,
|
||||
authApi,
|
||||
sessionManager,
|
||||
labelRepository,
|
||||
savedViewRepository,
|
||||
statsApi,
|
||||
);
|
||||
|
||||
if (globalSettings.currentLoggedInUser != null) {
|
||||
await authCubit
|
||||
.restoreSessionState();
|
||||
}
|
||||
|
||||
if (authCubit.state.isAuthenticated) {
|
||||
final auth = authCubit.state.authentication!;
|
||||
sessionManager.updateSettings(
|
||||
baseUrl: auth.serverUrl,
|
||||
authToken: auth.token,
|
||||
clientCertificate: auth.clientCertificate,
|
||||
);
|
||||
await authCubit.restoreSessionState();
|
||||
}
|
||||
|
||||
final localNotificationService = LocalNotificationService();
|
||||
await localNotificationService.initialize();
|
||||
|
||||
//Update language header in interceptor on language change.
|
||||
globalSettings.addListener(
|
||||
() => languageHeaderInterceptor.preferredLocaleSubtag =
|
||||
globalSettings.preferredLocaleSubtag,
|
||||
);
|
||||
globalSettingsBox.listenable().addListener(() {
|
||||
languageHeaderInterceptor.preferredLocaleSubtag = globalSettings.preferredLocaleSubtag;
|
||||
});
|
||||
|
||||
runApp(
|
||||
MultiProvider(
|
||||
providers: [
|
||||
Provider<LocalAuthenticationService>.value(value: localAuthService),
|
||||
Provider<PaperlessAuthenticationApi>.value(value: authApi),
|
||||
Provider<PaperlessDocumentsApi>.value(value: documentsApi),
|
||||
Provider<PaperlessLabelsApi>.value(value: labelsApi),
|
||||
@@ -181,8 +176,7 @@ void main() async {
|
||||
Provider<ConnectivityStatusService>.value(
|
||||
value: connectivityStatusService,
|
||||
),
|
||||
Provider<LocalNotificationService>.value(
|
||||
value: localNotificationService),
|
||||
Provider<LocalNotificationService>.value(value: localNotificationService),
|
||||
Provider.value(value: DocumentChangedNotifier()),
|
||||
],
|
||||
child: MultiRepositoryProvider(
|
||||
@@ -212,59 +206,48 @@ class PaperlessMobileEntrypoint extends StatefulWidget {
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<PaperlessMobileEntrypoint> createState() =>
|
||||
_PaperlessMobileEntrypointState();
|
||||
State<PaperlessMobileEntrypoint> createState() => _PaperlessMobileEntrypointState();
|
||||
}
|
||||
|
||||
class _PaperlessMobileEntrypointState extends State<PaperlessMobileEntrypoint> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MultiBlocProvider(
|
||||
providers: [
|
||||
BlocProvider(
|
||||
create: (context) => PaperlessServerInformationCubit(
|
||||
context.read<PaperlessServerStatsApi>(),
|
||||
),
|
||||
),
|
||||
],
|
||||
child: GlobalSettingsBuilder(
|
||||
builder: (context, settings) {
|
||||
return DynamicColorBuilder(
|
||||
builder: (lightDynamic, darkDynamic) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: true,
|
||||
title: "Paperless Mobile",
|
||||
theme: buildTheme(
|
||||
brightness: Brightness.light,
|
||||
dynamicScheme: lightDynamic,
|
||||
preferredColorScheme: settings.preferredColorSchemeOption,
|
||||
),
|
||||
darkTheme: buildTheme(
|
||||
brightness: Brightness.dark,
|
||||
dynamicScheme: darkDynamic,
|
||||
preferredColorScheme: settings.preferredColorSchemeOption,
|
||||
),
|
||||
themeMode: settings.preferredThemeMode,
|
||||
supportedLocales: S.supportedLocales,
|
||||
locale: Locale.fromSubtags(
|
||||
languageCode: settings.preferredLocaleSubtag,
|
||||
),
|
||||
localizationsDelegates: const [
|
||||
S.delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
],
|
||||
routes: {
|
||||
DocumentDetailsRoute.routeName: (context) =>
|
||||
const DocumentDetailsRoute(),
|
||||
},
|
||||
home: const AuthenticationWrapper(),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
return GlobalSettingsBuilder(
|
||||
builder: (context, settings) {
|
||||
return DynamicColorBuilder(
|
||||
builder: (lightDynamic, darkDynamic) {
|
||||
return MaterialApp(
|
||||
debugShowCheckedModeBanner: true,
|
||||
title: "Paperless Mobile",
|
||||
theme: buildTheme(
|
||||
brightness: Brightness.light,
|
||||
dynamicScheme: lightDynamic,
|
||||
preferredColorScheme: settings.preferredColorSchemeOption,
|
||||
),
|
||||
darkTheme: buildTheme(
|
||||
brightness: Brightness.dark,
|
||||
dynamicScheme: darkDynamic,
|
||||
preferredColorScheme: settings.preferredColorSchemeOption,
|
||||
),
|
||||
themeMode: settings.preferredThemeMode,
|
||||
supportedLocales: S.supportedLocales,
|
||||
locale: Locale.fromSubtags(
|
||||
languageCode: settings.preferredLocaleSubtag,
|
||||
),
|
||||
localizationsDelegates: const [
|
||||
S.delegate,
|
||||
GlobalMaterialLocalizations.delegate,
|
||||
GlobalCupertinoLocalizations.delegate,
|
||||
GlobalWidgetsLocalizations.delegate,
|
||||
],
|
||||
routes: {
|
||||
DocumentDetailsRoute.routeName: (context) => const DocumentDetailsRoute(),
|
||||
},
|
||||
home: const AuthenticationWrapper(),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -279,25 +262,23 @@ class AuthenticationWrapper extends StatefulWidget {
|
||||
class _AuthenticationWrapperState extends State<AuthenticationWrapper> {
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
FlutterNativeSplash.remove();
|
||||
super.didChangeDependencies();
|
||||
FlutterNativeSplash.remove();
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Temporary Fix: Can be removed if the flutter engine implements the fix itself
|
||||
|
||||
// Activate the highest supported refresh rate on the device
|
||||
if (Platform.isAndroid) {
|
||||
_setOptimalDisplayMode();
|
||||
}
|
||||
initializeDateFormatting();
|
||||
// For sharing files coming from outside the app while the app is still opened
|
||||
ReceiveSharingIntent.getMediaStream()
|
||||
.listen(ShareIntentQueue.instance.addAll);
|
||||
ReceiveSharingIntent.getMediaStream().listen(ShareIntentQueue.instance.addAll);
|
||||
// For sharing files coming from outside the app while the app is closed
|
||||
ReceiveSharingIntent.getInitialMedia()
|
||||
.then(ShareIntentQueue.instance.addAll);
|
||||
ReceiveSharingIntent.getInitialMedia().then(ShareIntentQueue.instance.addAll);
|
||||
}
|
||||
|
||||
Future<void> _setOptimalDisplayMode() async {
|
||||
@@ -309,8 +290,7 @@ class _AuthenticationWrapperState extends State<AuthenticationWrapper> {
|
||||
.toList()
|
||||
..sort((a, b) => b.refreshRate.compareTo(a.refreshRate));
|
||||
|
||||
final DisplayMode mostOptimalMode =
|
||||
sameResolution.isNotEmpty ? sameResolution.first : active;
|
||||
final DisplayMode mostOptimalMode = sameResolution.isNotEmpty ? sameResolution.first : active;
|
||||
debugPrint('Setting refresh rate to ${mostOptimalMode.refreshRate}');
|
||||
|
||||
await FlutterDisplayMode.setPreferredMode(mostOptimalMode);
|
||||
@@ -318,36 +298,76 @@ class _AuthenticationWrapperState extends State<AuthenticationWrapper> {
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return BlocConsumer<AuthenticationCubit, AuthenticationState>(
|
||||
listener: (context, authState) {
|
||||
final bool showIntroSlider =
|
||||
authState.isAuthenticated && !authState.wasLoginStored;
|
||||
if (showIntroSlider) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const ApplicationIntroSlideshow(),
|
||||
fullscreenDialog: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
return BlocBuilder<AuthenticationCubit, AuthenticationState>(
|
||||
builder: (context, authentication) {
|
||||
if (authentication.isAuthenticated &&
|
||||
(authentication.wasLocalAuthenticationSuccessful ?? true)) {
|
||||
return BlocProvider(
|
||||
create: (context) =>
|
||||
TaskStatusCubit(context.read<PaperlessTasksApi>()),
|
||||
if (authentication.isAuthenticated) {
|
||||
return MultiBlocProvider(
|
||||
// This key will cause the subtree to be remounted, which will again
|
||||
// call the provider's create callback! Without this key, the blocs
|
||||
// would not be recreated on account switch!
|
||||
key: ValueKey(authentication.userId),
|
||||
providers: [
|
||||
BlocProvider(
|
||||
create: (context) => TaskStatusCubit(
|
||||
context.read<PaperlessTasksApi>(),
|
||||
),
|
||||
),
|
||||
BlocProvider<PaperlessServerInformationCubit>(
|
||||
create: (context) => PaperlessServerInformationCubit(
|
||||
context.read<PaperlessServerStatsApi>(),
|
||||
),
|
||||
),
|
||||
],
|
||||
child: const HomePage(),
|
||||
);
|
||||
} else {
|
||||
if (authentication.wasLoginStored &&
|
||||
!(authentication.wasLocalAuthenticationSuccessful ?? false)) {
|
||||
return const VerifyIdentityPage();
|
||||
}
|
||||
return const LoginPage();
|
||||
} else if (authentication.showBiometricAuthenticationScreen) {
|
||||
return const VerifyIdentityPage();
|
||||
}
|
||||
return LoginPage(
|
||||
submitText: S.of(context)!.signIn,
|
||||
onSubmit: _onLogin,
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
void _onLogin(
|
||||
BuildContext context,
|
||||
String username,
|
||||
String password,
|
||||
String serverUrl,
|
||||
ClientCertificate? clientCertificate,
|
||||
) async {
|
||||
try {
|
||||
await context.read<AuthenticationCubit>().login(
|
||||
credentials: LoginFormCredentials(username: username, password: password),
|
||||
serverUrl: serverUrl,
|
||||
clientCertificate: clientCertificate,
|
||||
);
|
||||
// Show onboarding after first login!
|
||||
final globalSettings = GlobalSettings.boxedValue;
|
||||
if (globalSettings.showOnboarding) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => const ApplicationIntroSlideshow(),
|
||||
fullscreenDialog: true,
|
||||
),
|
||||
).then((value) {
|
||||
globalSettings.showOnboarding = false;
|
||||
globalSettings.save();
|
||||
});
|
||||
}
|
||||
} on PaperlessServerException catch (error, stackTrace) {
|
||||
showErrorMessage(context, error, stackTrace);
|
||||
} on PaperlessValidationErrors catch (error, stackTrace) {
|
||||
if (error.hasFieldUnspecificError) {
|
||||
showLocalizedError(context, error.fieldUnspecificError!);
|
||||
} else {
|
||||
showGenericError(context, error.values.first, stackTrace);
|
||||
}
|
||||
} catch (unknownError, stackTrace) {
|
||||
showGenericError(context, unknownError.toString(), stackTrace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user