Migrate accounts to prefController

This commit is contained in:
Ming Ming 2024-10-19 01:47:25 +08:00
parent 4a1900b283
commit 25d7b79225
10 changed files with 22 additions and 33 deletions

View file

@ -27,9 +27,10 @@ part 'pref_controller/util.dart';
class PrefController {
PrefController(this.pref);
Future<bool> setAccounts(List<Account> value) => _set<List<Account>>(
Future<bool> setAccounts(List<Account>? value) => _setOrRemove<List<Account>>(
controller: _accountsController,
setter: (pref, value) => pref.setAccounts3(value),
remover: (pref) => pref.setAccounts3(null),
value: value,
);

View file

@ -1,6 +1,15 @@
part of '../pref_controller.dart';
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 getHomeAlbumsSortOr(int def) => getHomeAlbumsSort() ?? def;
Future<bool> setHomeAlbumsSort(int value) =>

View file

@ -29,8 +29,6 @@ class Pref {
Future<bool> Function(PrefKey key, T value) setFn) =>
setFn(key, value);
Future<bool> _remove(PrefKey key) => provider.remove(key);
final PrefProvider provider;
static Pref? _inst;

View file

@ -19,14 +19,6 @@ extension PrefExtension on Pref {
}
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 getHomePhotosZoomLevelOr(int def) => getHomePhotosZoomLevel() ?? def;

View file

@ -3,12 +3,11 @@ part of '../account_settings.dart';
@npLog
class _Bloc extends Bloc<_Event, _State> with BlocLogger {
_Bloc({
required DiContainer container,
required Account account,
required this.prefController,
required this.accountPrefController,
this.highlight,
}) : _c = container,
super(_State.init(
}) : super(_State.init(
account: account,
label: accountPrefController.accountLabelValue,
shareFolder: accountPrefController.shareFolderValue,
@ -95,7 +94,7 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger {
));
final revert = state.account;
try {
final accounts = _c.pref.getAccounts3()!;
final accounts = prefController.accountsValue;
if (accounts.contains(ev.account)) {
// conflict with another account. This normally won't happen because
// 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;
if (!await _c.pref.setAccounts3(accounts)) {
if (!await prefController.setAccounts(accounts)) {
_log.severe("[_onSetAccount] Failed while setAccounts3: ${ev.account}");
throw const _WritePrefError();
}
@ -153,7 +152,7 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger {
emit(state.copyWith(error: ExceptionEvent(ev.error, ev.stackTrace)));
}
final DiContainer _c;
final PrefController prefController;
final AccountPrefController accountPrefController;
final AccountSettingsOption? highlight;

View file

@ -3,16 +3,14 @@ import 'dart:async';
import 'package:copy_with/copy_with.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:kiwi/kiwi.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/bloc_util.dart';
import 'package:nc_photos/controller/account_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/pref.dart';
import 'package:nc_photos/exception_event.dart';
import 'package:nc_photos/help_utils.dart' as help_util;
import 'package:nc_photos/k.dart' as k;
@ -71,8 +69,8 @@ class AccountSettings extends StatelessWidget {
final accountController = context.read<AccountController>();
return BlocProvider(
create: (_) => _Bloc(
container: KiwiContainer().resolve(),
account: accountController.account,
prefController: context.read(),
accountPrefController: accountController.accountPrefController,
highlight: highlight,
),

View file

@ -11,12 +11,10 @@ class _Error {
@npLog
class _Bloc extends Bloc<_Event, _State>
with BlocLogger, BlocForEachMixin<_Event, _State> {
_Bloc(
DiContainer c, {
_Bloc({
required this.db,
required this.prefController,
}) : _c = c,
super(_State.init(
}) : super(_State.init(
isNewHttpEngine: prefController.isNewHttpEngineValue,
)) {
on<_Init>(_onInit);
@ -42,7 +40,7 @@ class _Bloc extends Bloc<_Event, _State>
_ClearCacheDatabase ev, Emitter<_State> emit) async {
_log.info(ev);
try {
final accounts = _c.pref.getAccounts3Or([]);
final accounts = prefController.accountsValue;
await db.clearAndInitWithAccounts(accounts.toDb());
emit(state.copyWith(lastSuccessful: ev));
} catch (e, stackTrace) {
@ -56,7 +54,6 @@ class _Bloc extends Bloc<_Event, _State>
prefController.setNewHttpEngine(ev.value);
}
final DiContainer _c;
final NpDb db;
final PrefController prefController;

View file

@ -3,14 +3,11 @@ import 'dart:async';
import 'package:copy_with/copy_with.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:kiwi/kiwi.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/bloc_util.dart';
import 'package:nc_photos/controller/pref_controller.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/widget/trusted_cert_manager.dart';
import 'package:np_codegen/np_codegen.dart';
@ -28,7 +25,6 @@ class ExpertSettings extends StatelessWidget {
Widget build(BuildContext context) {
return BlocProvider(
create: (_) => _Bloc(
KiwiContainer().resolve<DiContainer>(),
db: context.read(),
prefController: context.read(),
)..add(const _Init()),

View file

@ -9,7 +9,6 @@ import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/bloc_util.dart';
import 'package:nc_photos/controller/pref_controller.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/mobile/android/activity.dart';
import 'package:nc_photos/mobile/android/permission_util.dart';

View file

@ -116,7 +116,7 @@ class _Bloc extends Bloc<_Event, _State> with BlocLogger {
await CompatV46.insertDbAccounts(prefController, npDb);
} catch (e, stackTrace) {
_log.shout("[_upgrade46] Failed while clearDefaultCache", e, stackTrace);
unawaited(Pref().setAccounts3(null));
unawaited(prefController.setAccounts(null));
unawaited(prefController.setCurrentAccountIndex(null));
}
}