feat: Externalize settings into LocalUserAppState, fix bugs

This commit is contained in:
Anton Stubenbord
2023-04-26 01:29:14 +02:00
parent 8c2a6928b4
commit 37c9559888
41 changed files with 340 additions and 316 deletions

View File

@@ -1,19 +0,0 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/bloc/paperless_server_information_state.dart';
class PaperlessServerInformationCubit
extends Cubit<PaperlessServerInformationState> {
final PaperlessServerStatsApi _api;
PaperlessServerInformationCubit(this._api)
: super(PaperlessServerInformationState());
Future<void> updateInformtion() async {
final information = await _api.getServerInformation();
emit(PaperlessServerInformationState(
isLoaded: true,
information: information,
));
}
}

View File

@@ -0,0 +1,17 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/bloc/server_information_state.dart';
class ServerInformationCubit extends Cubit<ServerInformationState> {
final PaperlessServerStatsApi _api;
ServerInformationCubit(this._api) : super(ServerInformationState());
Future<void> updateInformation() async {
final information = await _api.getServerInformation();
emit(ServerInformationState(
isLoaded: true,
information: information,
));
}
}

View File

@@ -1,10 +1,10 @@
import 'package:paperless_api/paperless_api.dart';
class PaperlessServerInformationState {
class ServerInformationState {
final bool isLoaded;
final PaperlessServerInformationModel? information;
PaperlessServerInformationState({
ServerInformationState({
this.isLoaded = false,
this.information,
});

View File

@@ -2,35 +2,37 @@ import 'package:hive_flutter/adapters.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/config/hive/custom_adapters/theme_mode_adapter.dart';
import 'package:paperless_mobile/core/database/tables/global_settings.dart';
import 'package:paperless_mobile/core/database/tables/user_app_state.dart';
import 'package:paperless_mobile/core/database/tables/local_user_app_state.dart';
import 'package:paperless_mobile/core/database/tables/user_credentials.dart';
import 'package:paperless_mobile/features/login/model/authentication_information.dart';
import 'package:paperless_mobile/features/login/model/client_certificate.dart';
import 'package:paperless_mobile/core/database/tables/user_account.dart';
import 'package:paperless_mobile/core/database/tables/local_user_account.dart';
import 'package:paperless_mobile/features/settings/model/color_scheme_option.dart';
import 'package:paperless_mobile/core/database/tables/user_settings.dart';
import 'package:paperless_mobile/core/database/tables/local_user_settings.dart';
import 'package:paperless_mobile/features/settings/model/view_type.dart';
class HiveBoxes {
HiveBoxes._();
static const globalSettings = 'globalSettings';
static const authentication = 'authentication';
static const userCredentials = 'userCredentials';
static const userAccount = 'userAccount';
static const userAppState = 'userAppState';
static const userSettings = 'userSettings';
static const localUserCredentials = 'localUserCredentials';
static const localUserAccount = 'localUserAccount';
static const localUserAppState = 'localUserAppState';
static const localUserSettings = 'localUserSettings';
}
class HiveTypeIds {
HiveTypeIds._();
static const globalSettings = 0;
static const userSettings = 1;
static const localUserSettings = 1;
static const themeMode = 2;
static const colorSchemeOption = 3;
static const authentication = 4;
static const clientCertificate = 5;
static const userCredentials = 6;
static const userAccount = 7;
static const userAppState = 8;
static const localUserCredentials = 6;
static const localUserAccount = 7;
static const localUserAppState = 8;
static const viewType = 9;
}
void registerHiveAdapters() {
@@ -40,10 +42,11 @@ void registerHiveAdapters() {
Hive.registerAdapter(GlobalSettingsAdapter());
Hive.registerAdapter(AuthenticationInformationAdapter());
Hive.registerAdapter(ClientCertificateAdapter());
Hive.registerAdapter(UserSettingsAdapter());
Hive.registerAdapter(LocalUserSettingsAdapter());
Hive.registerAdapter(UserCredentialsAdapter());
Hive.registerAdapter(UserAccountAdapter());
Hive.registerAdapter(UserAppStateAdapter());
Hive.registerAdapter(LocalUserAccountAdapter());
Hive.registerAdapter(LocalUserAppStateAdapter());
Hive.registerAdapter(ViewTypeAdapter());
}
extension HiveSingleValueBox<T> on Box<T> {

View File

@@ -1,11 +1,11 @@
import 'package:hive_flutter/adapters.dart';
import 'package:paperless_mobile/core/config/hive/hive_config.dart';
import 'package:paperless_mobile/core/database/tables/user_settings.dart';
import 'package:paperless_mobile/core/database/tables/local_user_settings.dart';
part 'user_account.g.dart';
part 'local_user_account.g.dart';
@HiveType(typeId: HiveTypeIds.userAccount)
class UserAccount extends HiveObject {
@HiveType(typeId: HiveTypeIds.localUserAccount)
class LocalUserAccount extends HiveObject {
@HiveField(0)
final String serverUrl;
@@ -19,9 +19,9 @@ class UserAccount extends HiveObject {
final String id;
@HiveField(4)
UserSettings settings;
LocalUserSettings settings;
UserAccount({
LocalUserAccount({
required this.id,
required this.serverUrl,
required this.username,

View File

@@ -0,0 +1,40 @@
import 'package:hive_flutter/adapters.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/config/hive/hive_config.dart';
import 'package:paperless_mobile/features/settings/model/view_type.dart';
part 'local_user_app_state.g.dart';
///
/// Object used for the persistence of app state, e.g. set filters,
/// search history and implicit settings.
///
@HiveType(typeId: HiveTypeIds.localUserAppState)
class LocalUserAppState extends HiveObject {
@HiveField(0)
final String userId;
@HiveField(1)
DocumentFilter currentDocumentFilter;
@HiveField(2)
List<String> documentSearchHistory;
@HiveField(3)
ViewType documentsPageViewType;
@HiveField(4)
ViewType savedViewsViewType;
@HiveField(5)
ViewType documentSearchViewType;
LocalUserAppState({
required this.userId,
this.currentDocumentFilter = const DocumentFilter(),
this.documentSearchHistory = const [],
this.documentsPageViewType = ViewType.list,
this.documentSearchViewType = ViewType.list,
this.savedViewsViewType = ViewType.list,
});
}

View File

@@ -1,15 +1,14 @@
import 'package:hive/hive.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/config/hive/hive_config.dart';
part 'user_settings.g.dart';
part 'local_user_settings.g.dart';
@HiveType(typeId: HiveTypeIds.userSettings)
class UserSettings with HiveObjectMixin {
@HiveType(typeId: HiveTypeIds.localUserSettings)
class LocalUserSettings with HiveObjectMixin {
@HiveField(0)
bool isBiometricAuthenticationEnabled;
UserSettings({
LocalUserSettings({
this.isBiometricAuthenticationEnabled = false,
});
}

View File

@@ -1,22 +0,0 @@
import 'package:hive_flutter/adapters.dart';
import 'package:paperless_api/paperless_api.dart';
import 'package:paperless_mobile/core/config/hive/hive_config.dart';
part 'user_app_state.g.dart';
@HiveType(typeId: HiveTypeIds.userAppState)
class UserAppState extends HiveObject {
@HiveField(0)
final String userId;
@HiveField(1)
DocumentFilter currentDocumentFilter;
@HiveField(2)
List<String> documentSearchHistory;
UserAppState({
required this.userId,
this.currentDocumentFilter = const DocumentFilter(),
this.documentSearchHistory = const [],
});
}

View File

@@ -4,7 +4,7 @@ import 'package:paperless_mobile/features/login/model/client_certificate.dart';
part 'user_credentials.g.dart';
@HiveType(typeId: HiveTypeIds.userCredentials)
@HiveType(typeId: HiveTypeIds.localUserCredentials)
class UserCredentials extends HiveObject {
@HiveField(0)
final String token;

View File

@@ -1,4 +1,3 @@
import 'dart:convert';
import 'dart:io';
import 'package:dio/dio.dart';
@@ -8,14 +7,18 @@ import 'package:paperless_mobile/core/interceptor/retry_on_connection_change_int
import 'package:paperless_mobile/features/login/model/client_certificate.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
/// Manages the security context, authentication and base request URL for
/// an underlying [Dio] client which is injected into all services
/// requiring authenticated access to the Paperless HTTP API.
class SessionManager {
final Dio client;
final List<Interceptor> interceptors;
PaperlessServerInformationModel serverInformation;
final Dio _client;
PaperlessServerInformationModel _serverInformation;
SessionManager([this.interceptors = const []])
: client = _initDio(interceptors),
serverInformation = PaperlessServerInformationModel();
Dio get client => _client;
SessionManager([List<Interceptor> interceptors = const []])
: _client = _initDio(interceptors),
_serverInformation = PaperlessServerInformationModel();
static Dio _initDio(List<Interceptor> interceptors) {
//en- and decoded by utf8 by default
@@ -63,8 +66,7 @@ class SessionManager {
);
final adapter = IOHttpClientAdapter()
..onHttpClientCreate = (client) => HttpClient(context: context)
..badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
..badCertificateCallback = (X509Certificate cert, String host, int port) => true;
client.httpClientAdapter = adapter;
}
@@ -80,7 +82,7 @@ class SessionManager {
}
if (serverInformation != null) {
this.serverInformation = serverInformation;
_serverInformation = serverInformation;
}
}
@@ -88,6 +90,6 @@ class SessionManager {
client.httpClientAdapter = IOHttpClientAdapter();
client.options.baseUrl = '';
client.options.headers.remove(HttpHeaders.authorizationHeader);
serverInformation = PaperlessServerInformationModel();
_serverInformation = PaperlessServerInformationModel();
}
}