mirror of
https://github.com/Xevion/paperless-mobile.git
synced 2025-12-15 00:12:22 -06:00
feat: view type stored in view/cubit, itself, cleanup coe
This commit is contained in:
64
lib/features/settings/cubit/application_settings_cubit.dart
Normal file
64
lib/features/settings/cubit/application_settings_cubit.dart
Normal file
@@ -0,0 +1,64 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:hydrated_bloc/hydrated_bloc.dart';
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:paperless_mobile/features/login/services/authentication_service.dart';
|
||||
import 'package:paperless_mobile/features/settings/model/color_scheme_option.dart';
|
||||
import 'package:paperless_mobile/generated/l10n.dart';
|
||||
|
||||
part 'application_settings_cubit.g.dart';
|
||||
part 'application_settings_state.dart';
|
||||
|
||||
class ApplicationSettingsCubit extends HydratedCubit<ApplicationSettingsState> {
|
||||
final LocalAuthenticationService _localAuthenticationService;
|
||||
ApplicationSettingsCubit(this._localAuthenticationService)
|
||||
: super(ApplicationSettingsState.defaultSettings);
|
||||
|
||||
Future<void> setLocale(String? localeSubtag) async {
|
||||
final updatedSettings = state.copyWith(preferredLocaleSubtag: localeSubtag);
|
||||
_updateSettings(updatedSettings);
|
||||
}
|
||||
|
||||
Future<void> setIsBiometricAuthenticationEnabled(
|
||||
bool isEnabled, {
|
||||
required String localizedReason,
|
||||
}) async {
|
||||
final isActionAuthorized = await _localAuthenticationService
|
||||
.authenticateLocalUser(localizedReason);
|
||||
if (isActionAuthorized) {
|
||||
final updatedSettings =
|
||||
state.copyWith(isLocalAuthenticationEnabled: isEnabled);
|
||||
_updateSettings(updatedSettings);
|
||||
}
|
||||
}
|
||||
|
||||
void setThemeMode(ThemeMode? selectedMode) {
|
||||
final updatedSettings = state.copyWith(preferredThemeMode: selectedMode);
|
||||
_updateSettings(updatedSettings);
|
||||
}
|
||||
|
||||
void setColorSchemeOption(ColorSchemeOption schemeOption) {
|
||||
final updatedSettings =
|
||||
state.copyWith(preferredColorSchemeOption: schemeOption);
|
||||
_updateSettings(updatedSettings);
|
||||
}
|
||||
|
||||
void _updateSettings(ApplicationSettingsState settings) async {
|
||||
emit(settings);
|
||||
}
|
||||
|
||||
@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();
|
||||
}
|
||||
53
lib/features/settings/cubit/application_settings_state.dart
Normal file
53
lib/features/settings/cubit/application_settings_state.dart
Normal file
@@ -0,0 +1,53 @@
|
||||
part of 'application_settings_cubit.dart';
|
||||
|
||||
///
|
||||
/// State holding the current application settings such as selected language, theme mode and more.
|
||||
///
|
||||
@JsonSerializable()
|
||||
class ApplicationSettingsState {
|
||||
static final defaultSettings = ApplicationSettingsState(
|
||||
preferredLocaleSubtag: _defaultPreferredLocaleSubtag,
|
||||
);
|
||||
|
||||
final bool isLocalAuthenticationEnabled;
|
||||
final String preferredLocaleSubtag;
|
||||
final ThemeMode preferredThemeMode;
|
||||
final ColorSchemeOption preferredColorSchemeOption;
|
||||
|
||||
ApplicationSettingsState({
|
||||
required this.preferredLocaleSubtag,
|
||||
this.preferredThemeMode = ThemeMode.system,
|
||||
this.isLocalAuthenticationEnabled = false,
|
||||
this.preferredColorSchemeOption = ColorSchemeOption.classic,
|
||||
});
|
||||
|
||||
Map<String, dynamic> toJson() => _$ApplicationSettingsStateToJson(this);
|
||||
factory ApplicationSettingsState.fromJson(Map<String, dynamic> json) =>
|
||||
_$ApplicationSettingsStateFromJson(json);
|
||||
|
||||
ApplicationSettingsState copyWith({
|
||||
bool? isLocalAuthenticationEnabled,
|
||||
String? preferredLocaleSubtag,
|
||||
ThemeMode? preferredThemeMode,
|
||||
ColorSchemeOption? preferredColorSchemeOption,
|
||||
}) {
|
||||
return ApplicationSettingsState(
|
||||
isLocalAuthenticationEnabled:
|
||||
isLocalAuthenticationEnabled ?? this.isLocalAuthenticationEnabled,
|
||||
preferredLocaleSubtag:
|
||||
preferredLocaleSubtag ?? this.preferredLocaleSubtag,
|
||||
preferredThemeMode: preferredThemeMode ?? this.preferredThemeMode,
|
||||
preferredColorSchemeOption:
|
||||
preferredColorSchemeOption ?? this.preferredColorSchemeOption,
|
||||
);
|
||||
}
|
||||
|
||||
static String get _defaultPreferredLocaleSubtag {
|
||||
String preferredLocale = Platform.localeName.split("_").first;
|
||||
if (!S.delegate.supportedLocales
|
||||
.any((locale) => locale.languageCode == preferredLocale)) {
|
||||
preferredLocale = 'en';
|
||||
}
|
||||
return preferredLocale;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user