mirror of
https://gitlab.com/nkming2/nc-photos.git
synced 2025-01-22 16:56:19 +01:00
Migrate accounts to prefController
This commit is contained in:
parent
4a1900b283
commit
25d7b79225
10 changed files with 22 additions and 33 deletions
|
@ -27,9 +27,10 @@ part 'pref_controller/util.dart';
|
||||||
class PrefController {
|
class PrefController {
|
||||||
PrefController(this.pref);
|
PrefController(this.pref);
|
||||||
|
|
||||||
Future<bool> setAccounts(List<Account> value) => _set<List<Account>>(
|
Future<bool> setAccounts(List<Account>? value) => _setOrRemove<List<Account>>(
|
||||||
controller: _accountsController,
|
controller: _accountsController,
|
||||||
setter: (pref, value) => pref.setAccounts3(value),
|
setter: (pref, value) => pref.setAccounts3(value),
|
||||||
|
remover: (pref) => pref.setAccounts3(null),
|
||||||
value: value,
|
value: value,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,15 @@
|
||||||
part of '../pref_controller.dart';
|
part of '../pref_controller.dart';
|
||||||
|
|
||||||
extension on Pref {
|
extension on Pref {
|
||||||
|
Future<bool> setAccounts3(List<Account>? value) {
|
||||||
|
if (value == null) {
|
||||||
|
return provider.remove(PrefKey.accounts3);
|
||||||
|
} else {
|
||||||
|
final jsons = value.map((e) => jsonEncode(e.toJson())).toList();
|
||||||
|
return provider.setStringList(PrefKey.accounts3, jsons);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int? getHomeAlbumsSort() => provider.getInt(PrefKey.homeAlbumsSort);
|
int? getHomeAlbumsSort() => provider.getInt(PrefKey.homeAlbumsSort);
|
||||||
int getHomeAlbumsSortOr(int def) => getHomeAlbumsSort() ?? def;
|
int getHomeAlbumsSortOr(int def) => getHomeAlbumsSort() ?? def;
|
||||||
Future<bool> setHomeAlbumsSort(int value) =>
|
Future<bool> setHomeAlbumsSort(int value) =>
|
||||||
|
|
|
@ -29,8 +29,6 @@ class Pref {
|
||||||
Future<bool> Function(PrefKey key, T value) setFn) =>
|
Future<bool> Function(PrefKey key, T value) setFn) =>
|
||||||
setFn(key, value);
|
setFn(key, value);
|
||||||
|
|
||||||
Future<bool> _remove(PrefKey key) => provider.remove(key);
|
|
||||||
|
|
||||||
final PrefProvider provider;
|
final PrefProvider provider;
|
||||||
|
|
||||||
static Pref? _inst;
|
static Pref? _inst;
|
||||||
|
|
|
@ -19,14 +19,6 @@ extension PrefExtension on Pref {
|
||||||
}
|
}
|
||||||
|
|
||||||
List<Account> getAccounts3Or(List<Account> def) => getAccounts3() ?? def;
|
List<Account> getAccounts3Or(List<Account> def) => getAccounts3() ?? def;
|
||||||
Future<bool> setAccounts3(List<Account>? value) {
|
|
||||||
if (value == null) {
|
|
||||||
return _remove(PrefKey.accounts3);
|
|
||||||
} else {
|
|
||||||
final jsons = value.map((e) => jsonEncode(e.toJson())).toList();
|
|
||||||
return provider.setStringList(PrefKey.accounts3, jsons);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int? getHomePhotosZoomLevel() => provider.getInt(PrefKey.homePhotosZoomLevel);
|
int? getHomePhotosZoomLevel() => provider.getInt(PrefKey.homePhotosZoomLevel);
|
||||||
int getHomePhotosZoomLevelOr(int def) => getHomePhotosZoomLevel() ?? def;
|
int getHomePhotosZoomLevelOr(int def) => getHomePhotosZoomLevel() ?? def;
|
||||||
|
|
|
@ -3,12 +3,11 @@ part of '../account_settings.dart';
|
||||||
@npLog
|
@npLog
|
||||||
class _Bloc extends Bloc<_Event, _State> with BlocLogger {
|
class _Bloc extends Bloc<_Event, _State> with BlocLogger {
|
||||||
_Bloc({
|
_Bloc({
|
||||||
required DiContainer container,
|
|
||||||
required Account account,
|
required Account account,
|
||||||
|
required this.prefController,
|
||||||
required this.accountPrefController,
|
required this.accountPrefController,
|
||||||
this.highlight,
|
this.highlight,
|
||||||
}) : _c = container,
|
}) : super(_State.init(
|
||||||
super(_State.init(
|
|
||||||
account: account,
|
account: account,
|
||||||
label: accountPrefController.accountLabelValue,
|
label: accountPrefController.accountLabelValue,
|
||||||
shareFolder: accountPrefController.shareFolderValue,
|
shareFolder: accountPrefController.shareFolderValue,
|
||||||
|
@ -95,7 +94,7 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger {
|
||||||
));
|
));
|
||||||
final revert = state.account;
|
final revert = state.account;
|
||||||
try {
|
try {
|
||||||
final accounts = _c.pref.getAccounts3()!;
|
final accounts = prefController.accountsValue;
|
||||||
if (accounts.contains(ev.account)) {
|
if (accounts.contains(ev.account)) {
|
||||||
// conflict with another account. This normally won't happen because
|
// conflict with another account. This normally won't happen because
|
||||||
// the app passwords are unique to each entry, but just in case
|
// the app passwords are unique to each entry, but just in case
|
||||||
|
@ -109,7 +108,7 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger {
|
||||||
}
|
}
|
||||||
|
|
||||||
accounts[index] = ev.account;
|
accounts[index] = ev.account;
|
||||||
if (!await _c.pref.setAccounts3(accounts)) {
|
if (!await prefController.setAccounts(accounts)) {
|
||||||
_log.severe("[_onSetAccount] Failed while setAccounts3: ${ev.account}");
|
_log.severe("[_onSetAccount] Failed while setAccounts3: ${ev.account}");
|
||||||
throw const _WritePrefError();
|
throw const _WritePrefError();
|
||||||
}
|
}
|
||||||
|
@ -153,7 +152,7 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger {
|
||||||
emit(state.copyWith(error: ExceptionEvent(ev.error, ev.stackTrace)));
|
emit(state.copyWith(error: ExceptionEvent(ev.error, ev.stackTrace)));
|
||||||
}
|
}
|
||||||
|
|
||||||
final DiContainer _c;
|
final PrefController prefController;
|
||||||
final AccountPrefController accountPrefController;
|
final AccountPrefController accountPrefController;
|
||||||
final AccountSettingsOption? highlight;
|
final AccountSettingsOption? highlight;
|
||||||
|
|
||||||
|
|
|
@ -3,16 +3,14 @@ import 'dart:async';
|
||||||
import 'package:copy_with/copy_with.dart';
|
import 'package:copy_with/copy_with.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:kiwi/kiwi.dart';
|
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:nc_photos/account.dart';
|
import 'package:nc_photos/account.dart';
|
||||||
import 'package:nc_photos/app_localizations.dart';
|
import 'package:nc_photos/app_localizations.dart';
|
||||||
import 'package:nc_photos/bloc_util.dart';
|
import 'package:nc_photos/bloc_util.dart';
|
||||||
import 'package:nc_photos/controller/account_controller.dart';
|
import 'package:nc_photos/controller/account_controller.dart';
|
||||||
import 'package:nc_photos/controller/account_pref_controller.dart';
|
import 'package:nc_photos/controller/account_pref_controller.dart';
|
||||||
import 'package:nc_photos/di_container.dart';
|
import 'package:nc_photos/controller/pref_controller.dart';
|
||||||
import 'package:nc_photos/entity/person.dart';
|
import 'package:nc_photos/entity/person.dart';
|
||||||
import 'package:nc_photos/entity/pref.dart';
|
|
||||||
import 'package:nc_photos/exception_event.dart';
|
import 'package:nc_photos/exception_event.dart';
|
||||||
import 'package:nc_photos/help_utils.dart' as help_util;
|
import 'package:nc_photos/help_utils.dart' as help_util;
|
||||||
import 'package:nc_photos/k.dart' as k;
|
import 'package:nc_photos/k.dart' as k;
|
||||||
|
@ -71,8 +69,8 @@ class AccountSettings extends StatelessWidget {
|
||||||
final accountController = context.read<AccountController>();
|
final accountController = context.read<AccountController>();
|
||||||
return BlocProvider(
|
return BlocProvider(
|
||||||
create: (_) => _Bloc(
|
create: (_) => _Bloc(
|
||||||
container: KiwiContainer().resolve(),
|
|
||||||
account: accountController.account,
|
account: accountController.account,
|
||||||
|
prefController: context.read(),
|
||||||
accountPrefController: accountController.accountPrefController,
|
accountPrefController: accountController.accountPrefController,
|
||||||
highlight: highlight,
|
highlight: highlight,
|
||||||
),
|
),
|
||||||
|
|
|
@ -11,12 +11,10 @@ class _Error {
|
||||||
@npLog
|
@npLog
|
||||||
class _Bloc extends Bloc<_Event, _State>
|
class _Bloc extends Bloc<_Event, _State>
|
||||||
with BlocLogger, BlocForEachMixin<_Event, _State> {
|
with BlocLogger, BlocForEachMixin<_Event, _State> {
|
||||||
_Bloc(
|
_Bloc({
|
||||||
DiContainer c, {
|
|
||||||
required this.db,
|
required this.db,
|
||||||
required this.prefController,
|
required this.prefController,
|
||||||
}) : _c = c,
|
}) : super(_State.init(
|
||||||
super(_State.init(
|
|
||||||
isNewHttpEngine: prefController.isNewHttpEngineValue,
|
isNewHttpEngine: prefController.isNewHttpEngineValue,
|
||||||
)) {
|
)) {
|
||||||
on<_Init>(_onInit);
|
on<_Init>(_onInit);
|
||||||
|
@ -42,7 +40,7 @@ class _Bloc extends Bloc<_Event, _State>
|
||||||
_ClearCacheDatabase ev, Emitter<_State> emit) async {
|
_ClearCacheDatabase ev, Emitter<_State> emit) async {
|
||||||
_log.info(ev);
|
_log.info(ev);
|
||||||
try {
|
try {
|
||||||
final accounts = _c.pref.getAccounts3Or([]);
|
final accounts = prefController.accountsValue;
|
||||||
await db.clearAndInitWithAccounts(accounts.toDb());
|
await db.clearAndInitWithAccounts(accounts.toDb());
|
||||||
emit(state.copyWith(lastSuccessful: ev));
|
emit(state.copyWith(lastSuccessful: ev));
|
||||||
} catch (e, stackTrace) {
|
} catch (e, stackTrace) {
|
||||||
|
@ -56,7 +54,6 @@ class _Bloc extends Bloc<_Event, _State>
|
||||||
prefController.setNewHttpEngine(ev.value);
|
prefController.setNewHttpEngine(ev.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
final DiContainer _c;
|
|
||||||
final NpDb db;
|
final NpDb db;
|
||||||
final PrefController prefController;
|
final PrefController prefController;
|
||||||
|
|
||||||
|
|
|
@ -3,14 +3,11 @@ import 'dart:async';
|
||||||
import 'package:copy_with/copy_with.dart';
|
import 'package:copy_with/copy_with.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:kiwi/kiwi.dart';
|
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:nc_photos/app_localizations.dart';
|
import 'package:nc_photos/app_localizations.dart';
|
||||||
import 'package:nc_photos/bloc_util.dart';
|
import 'package:nc_photos/bloc_util.dart';
|
||||||
import 'package:nc_photos/controller/pref_controller.dart';
|
import 'package:nc_photos/controller/pref_controller.dart';
|
||||||
import 'package:nc_photos/db/entity_converter.dart';
|
import 'package:nc_photos/db/entity_converter.dart';
|
||||||
import 'package:nc_photos/di_container.dart';
|
|
||||||
import 'package:nc_photos/entity/pref.dart';
|
|
||||||
import 'package:nc_photos/snack_bar_manager.dart';
|
import 'package:nc_photos/snack_bar_manager.dart';
|
||||||
import 'package:nc_photos/widget/trusted_cert_manager.dart';
|
import 'package:nc_photos/widget/trusted_cert_manager.dart';
|
||||||
import 'package:np_codegen/np_codegen.dart';
|
import 'package:np_codegen/np_codegen.dart';
|
||||||
|
@ -28,7 +25,6 @@ class ExpertSettings extends StatelessWidget {
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BlocProvider(
|
return BlocProvider(
|
||||||
create: (_) => _Bloc(
|
create: (_) => _Bloc(
|
||||||
KiwiContainer().resolve<DiContainer>(),
|
|
||||||
db: context.read(),
|
db: context.read(),
|
||||||
prefController: context.read(),
|
prefController: context.read(),
|
||||||
)..add(const _Init()),
|
)..add(const _Init()),
|
||||||
|
|
|
@ -9,7 +9,6 @@ import 'package:nc_photos/app_localizations.dart';
|
||||||
import 'package:nc_photos/bloc_util.dart';
|
import 'package:nc_photos/bloc_util.dart';
|
||||||
import 'package:nc_photos/controller/pref_controller.dart';
|
import 'package:nc_photos/controller/pref_controller.dart';
|
||||||
import 'package:nc_photos/db/entity_converter.dart';
|
import 'package:nc_photos/db/entity_converter.dart';
|
||||||
import 'package:nc_photos/entity/pref.dart';
|
|
||||||
import 'package:nc_photos/k.dart' as k;
|
import 'package:nc_photos/k.dart' as k;
|
||||||
import 'package:nc_photos/mobile/android/activity.dart';
|
import 'package:nc_photos/mobile/android/activity.dart';
|
||||||
import 'package:nc_photos/mobile/android/permission_util.dart';
|
import 'package:nc_photos/mobile/android/permission_util.dart';
|
||||||
|
|
|
@ -116,7 +116,7 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger {
|
||||||
await CompatV46.insertDbAccounts(prefController, npDb);
|
await CompatV46.insertDbAccounts(prefController, npDb);
|
||||||
} catch (e, stackTrace) {
|
} catch (e, stackTrace) {
|
||||||
_log.shout("[_upgrade46] Failed while clearDefaultCache", e, stackTrace);
|
_log.shout("[_upgrade46] Failed while clearDefaultCache", e, stackTrace);
|
||||||
unawaited(Pref().setAccounts3(null));
|
unawaited(prefController.setAccounts(null));
|
||||||
unawaited(prefController.setCurrentAccountIndex(null));
|
unawaited(prefController.setCurrentAccountIndex(null));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue