WIP - Replaced get_it + injectable with Provider

This commit is contained in:
Anton Stubenbord
2022-12-21 01:14:06 +01:00
parent 10149fb7c1
commit 60aecb549d
59 changed files with 1099 additions and 1362 deletions

View File

@@ -1,24 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:paperless_mobile/core/store/local_vault.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
import 'package:paperless_mobile/features/settings/model/application_settings_state.dart';
import 'package:injectable/injectable.dart';
import 'package:paperless_mobile/features/settings/model/view_type.dart';
@prod
@test
@lazySingleton
class ApplicationSettingsCubit extends Cubit<ApplicationSettingsState> {
final LocalVault localVault;
ApplicationSettingsCubit(this.localVault)
: super(ApplicationSettingsState.defaultSettings);
Future<void> initialize() async {
final settings = (await localVault.loadApplicationSettings()) ??
ApplicationSettingsState.defaultSettings;
emit(settings);
}
class ApplicationSettingsCubit extends HydratedCubit<ApplicationSettingsState> {
ApplicationSettingsCubit() : super(ApplicationSettingsState.defaultSettings);
Future<void> setLocale(String? localeSubtag) async {
final updatedSettings = state.copyWith(preferredLocaleSubtag: localeSubtag);
@@ -42,11 +28,20 @@ class ApplicationSettingsCubit extends Cubit<ApplicationSettingsState> {
}
Future<void> _updateSettings(ApplicationSettingsState settings) async {
await localVault.storeApplicationSettings(settings);
emit(settings);
}
void clear() {
@override
Future<void> clear() async {
await super.clear();
emit(ApplicationSettingsState.defaultSettings);
}
@override
ApplicationSettingsState? fromJson(Map<String, dynamic> json) =>
ApplicationSettingsState.fromJson(json);
@override
Map<String, dynamic>? toJson(ApplicationSettingsState state) =>
state.toJson();
}

View File

@@ -1,71 +1,45 @@
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:paperless_mobile/core/type/types.dart';
import 'package:paperless_mobile/features/settings/model/view_type.dart';
part 'application_settings_state.g.dart';
///
/// State holding the current application settings such as selected language, theme mode and more.
///
///
@JsonSerializable()
class ApplicationSettingsState {
static final defaultSettings = ApplicationSettingsState(
isLocalAuthenticationEnabled: false,
preferredLocaleSubtag: Platform.localeName.split('_').first,
preferredThemeMode: ThemeMode.system,
preferredViewType: ViewType.list,
showInboxOnStartup: true,
);
static const isLocalAuthenticationEnabledKey = "isLocalAuthenticationEnabled";
static const preferredLocaleSubtagKey = "localeSubtag";
static const preferredThemeModeKey = "preferredThemeModeKey";
static const preferredViewTypeKey = 'preferredViewType';
static const showInboxOnStartupKey = 'showinboxOnStartup';
final bool isLocalAuthenticationEnabled;
final String preferredLocaleSubtag;
final ThemeMode preferredThemeMode;
final ViewType preferredViewType;
final bool showInboxOnStartup;
ApplicationSettingsState({
required this.preferredLocaleSubtag,
required this.preferredThemeMode,
required this.isLocalAuthenticationEnabled,
required this.preferredViewType,
required this.showInboxOnStartup,
});
JSON toJson() {
return {
isLocalAuthenticationEnabledKey: isLocalAuthenticationEnabled,
preferredLocaleSubtagKey: preferredLocaleSubtag,
preferredThemeModeKey: preferredThemeMode.name,
preferredViewTypeKey: preferredViewType.name,
};
}
ApplicationSettingsState.fromJson(JSON json)
: isLocalAuthenticationEnabled = json[isLocalAuthenticationEnabledKey] ??
defaultSettings.isLocalAuthenticationEnabled,
preferredLocaleSubtag = json[preferredLocaleSubtagKey] ??
defaultSettings.preferredLocaleSubtag,
preferredThemeMode = json.containsKey(preferredThemeModeKey)
? ThemeMode.values.byName(json[preferredThemeModeKey])
: defaultSettings.preferredThemeMode,
preferredViewType = json.containsKey(preferredViewTypeKey)
? ViewType.values.byName(json[preferredViewTypeKey])
: defaultSettings.preferredViewType,
showInboxOnStartup =
json[showInboxOnStartupKey] ?? defaultSettings.showInboxOnStartup;
Map<String, dynamic> toJson() => _$ApplicationSettingsStateToJson(this);
factory ApplicationSettingsState.fromJson(Map<String, dynamic> json) =>
_$ApplicationSettingsStateFromJson(json);
ApplicationSettingsState copyWith({
bool? isLocalAuthenticationEnabled,
String? preferredLocaleSubtag,
ThemeMode? preferredThemeMode,
ViewType? preferredViewType,
bool? showInboxOnStartup,
}) {
return ApplicationSettingsState(
isLocalAuthenticationEnabled:
@@ -74,7 +48,6 @@ class ApplicationSettingsState {
preferredLocaleSubtag ?? this.preferredLocaleSubtag,
preferredThemeMode: preferredThemeMode ?? this.preferredThemeMode,
preferredViewType: preferredViewType ?? this.preferredViewType,
showInboxOnStartup: showInboxOnStartup ?? this.showInboxOnStartup,
);
}
}

View File

@@ -0,0 +1,39 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'application_settings_state.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
ApplicationSettingsState _$ApplicationSettingsStateFromJson(
Map<String, dynamic> json) =>
ApplicationSettingsState(
preferredLocaleSubtag: json['preferredLocaleSubtag'] as String,
preferredThemeMode:
$enumDecode(_$ThemeModeEnumMap, json['preferredThemeMode']),
isLocalAuthenticationEnabled:
json['isLocalAuthenticationEnabled'] as bool,
preferredViewType:
$enumDecode(_$ViewTypeEnumMap, json['preferredViewType']),
);
Map<String, dynamic> _$ApplicationSettingsStateToJson(
ApplicationSettingsState instance) =>
<String, dynamic>{
'isLocalAuthenticationEnabled': instance.isLocalAuthenticationEnabled,
'preferredLocaleSubtag': instance.preferredLocaleSubtag,
'preferredThemeMode': _$ThemeModeEnumMap[instance.preferredThemeMode]!,
'preferredViewType': _$ViewTypeEnumMap[instance.preferredViewType]!,
};
const _$ThemeModeEnumMap = {
ThemeMode.system: 'system',
ThemeMode.light: 'light',
ThemeMode.dark: 'dark',
};
const _$ViewTypeEnumMap = {
ViewType.grid: 'grid',
ViewType.list: 'list',
};

View File

@@ -1,10 +1,10 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:paperless_mobile/di_initializer.dart';
import 'package:paperless_mobile/features/login/services/authentication_service.dart';
import 'package:paperless_mobile/features/settings/bloc/application_settings_cubit.dart';
import 'package:paperless_mobile/features/settings/model/application_settings_state.dart';
import 'package:paperless_mobile/generated/l10n.dart';
import 'package:provider/provider.dart';
class BiometricAuthenticationSetting extends StatelessWidget {
const BiometricAuthenticationSetting({super.key});
@@ -28,8 +28,9 @@ class BiometricAuthenticationSetting extends StatelessWidget {
: S
.of(context)
.appSettingsDisableBiometricAuthenticationReasonText;
final changeValue = await getIt<LocalAuthenticationService>()
.authenticateLocalUser(localizedReason);
final changeValue =
await Provider.of<LocalAuthenticationService>(context)
.authenticateLocalUser(localizedReason);
if (changeValue) {
settingsBloc.setIsBiometricAuthenticationEnabled(val);
}

View File

@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart' as cm;
import 'package:paperless_mobile/core/service/file_service.dart';
import 'package:paperless_mobile/di_initializer.dart';
import 'package:provider/provider.dart';
class ClearStorageSetting extends StatelessWidget {
const ClearStorageSetting({super.key});
@@ -12,12 +12,10 @@ class ClearStorageSetting extends StatelessWidget {
title: Text("Clear data"),
subtitle:
Text("Remove downloaded files, scans and clear the cache's content"),
onTap: _clearCache,
onTap: () {
Provider.of<cm.CacheManager>(context).emptyCache();
FileService.clearUserData();
},
);
}
void _clearCache() async {
getIt<cm.CacheManager>().emptyCache();
FileService.clearUserData();
}
}