nc-photos/app/lib/controller/account_pref_controller.dart

86 lines
2.6 KiB
Dart
Raw Normal View History

2023-06-10 12:44:02 +02:00
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
import 'package:nc_photos/entity/person.dart';
2023-07-17 09:35:45 +02:00
import 'package:nc_photos/entity/pref.dart';
2023-06-10 12:44:02 +02:00
import 'package:np_codegen/np_codegen.dart';
import 'package:rxdart/rxdart.dart';
part 'account_pref_controller.g.dart';
@npLog
class AccountPrefController {
AccountPrefController({
required this.account,
}) : _accountPref = AccountPref.of(account);
void dispose() {
_shareFolderController.close();
_accountLabelController.close();
}
2023-06-10 12:44:02 +02:00
ValueStream<String> get shareFolder => _shareFolderController.stream;
Future<void> setShareFolder(String value) async {
final backup = _shareFolderController.value;
_shareFolderController.add(value);
try {
if (!await _accountPref.setShareFolder(value)) {
throw StateError("Unknown error");
}
} catch (e, stackTrace) {
_log.severe("[setShareFolder] Failed setting preference", e, stackTrace);
_shareFolderController
..addError(e, stackTrace)
..add(backup);
}
}
ValueStream<String?> get accountLabel => _accountLabelController.stream;
Future<void> setAccountLabel(String? value) async {
final backup = _accountLabelController.value;
_accountLabelController.add(value);
try {
if (!await _accountPref.setAccountLabel(value)) {
throw StateError("Unknown error");
}
} catch (e, stackTrace) {
_log.severe("[setAccountLabel] Failed setting preference", e, stackTrace);
_accountLabelController
..addError(e, stackTrace)
..add(backup);
}
}
ValueStream<PersonProvider> get personProvider =>
_personProviderController.stream;
Future<void> setPersonProvider(PersonProvider value) async {
final backup = _personProviderController.value;
_personProviderController.add(value);
try {
if (!await _accountPref.setPersonProvider(value.index)) {
throw StateError("Unknown error");
}
} catch (e, stackTrace) {
_log.severe(
"[setPersonProvider] Failed setting preference", e, stackTrace);
_personProviderController
..addError(e, stackTrace)
..add(backup);
}
}
2023-07-03 19:23:42 +02:00
AccountPref get raw => _accountPref;
2023-06-10 12:44:02 +02:00
final Account account;
final AccountPref _accountPref;
late final _shareFolderController =
BehaviorSubject.seeded(_accountPref.getShareFolderOr(""));
late final _accountLabelController =
BehaviorSubject.seeded(_accountPref.getAccountLabel());
late final _personProviderController = BehaviorSubject.seeded(
PersonProvider.fromValue(_accountPref.getPersonProviderOr()));
2023-06-10 12:44:02 +02:00
}