import 'package:logging/logging.dart'; import 'package:nc_photos/di_container.dart'; import 'package:nc_photos/entity/pref.dart'; import 'package:nc_photos/language_util.dart' as language_util; import 'package:nc_photos/lazy.dart'; import 'package:nc_photos/widget/gps_map.dart'; import 'package:np_codegen/np_codegen.dart'; import 'package:rxdart/rxdart.dart'; part 'pref_controller.g.dart'; @npLog class PrefController { PrefController(this._c); ValueStream get language => _languageStream(); Future setAppLanguage(language_util.AppLanguage value) async { final backup = _languageController.value; _languageController.add(value.langId); try { if (!await _c.pref.setLanguage(value.langId)) { throw StateError("Unknown error"); } } catch (e, stackTrace) { _log.severe("[setAppLanguage] Failed setting preference", e, stackTrace); _languageController ..addError(e, stackTrace) ..add(backup); } } ValueStream get albumBrowserZoomLevel => _albumBrowserZoomLevelController.stream; Future setAlbumBrowserZoomLevel(int value) => _set( controller: _albumBrowserZoomLevelController, setter: (pref, value) => pref.setAlbumBrowserZoomLevel(value), value: value, ); ValueStream get homeAlbumsSort => _homeAlbumsSortController.stream; Future setHomeAlbumsSort(int value) => _set( controller: _homeAlbumsSortController, setter: (pref, value) => pref.setHomeAlbumsSort(value), value: value, ); ValueStream get isEnableExif => _isEnableExifController.stream; Future setEnableExif(bool value) => _set( controller: _isEnableExifController, setter: (pref, value) => pref.setEnableExif(value), value: value, ); ValueStream get shouldProcessExifWifiOnly => _shouldProcessExifWifiOnlyController.stream; Future setProcessExifWifiOnly(bool value) => _set( controller: _shouldProcessExifWifiOnlyController, setter: (pref, value) => pref.setProcessExifWifiOnly(value), value: value, ); ValueStream get memoriesRange => _memoriesRangeController.stream; Future setMemoriesRange(int value) => _set( controller: _memoriesRangeController, setter: (pref, value) => pref.setMemoriesRange(value), value: value, ); ValueStream get isPhotosTabSortByName => _isPhotosTabSortByNameController.stream; Future setPhotosTabSortByName(bool value) => _set( controller: _isPhotosTabSortByNameController, setter: (pref, value) => pref.setPhotosTabSortByName(value), value: value, ); ValueStream get viewerScreenBrightness => _viewerScreenBrightnessController.stream; Future setViewerScreenBrightness(int value) => _set( controller: _viewerScreenBrightnessController, setter: (pref, value) => pref.setViewerScreenBrightness(value), value: value, ); ValueStream get isViewerForceRotation => _isViewerForceRotationController.stream; Future setViewerForceRotation(bool value) => _set( controller: _isViewerForceRotationController, setter: (pref, value) => pref.setViewerForceRotation(value), value: value, ); ValueStream get gpsMapProvider => _gpsMapProviderController.stream; Future setGpsMapProvider(GpsMapProvider value) => _set( controller: _gpsMapProviderController, setter: (pref, value) => pref.setGpsMapProvider(value.index), value: value, ); ValueStream get isAlbumBrowserShowDate => _isAlbumBrowserShowDateController.stream; Future setAlbumBrowserShowDate(bool value) => _set( controller: _isAlbumBrowserShowDateController, setter: (pref, value) => pref.setAlbumBrowserShowDate(value), value: value, ); Future _set({ required BehaviorSubject controller, required Future Function(Pref pref, T value) setter, required T value, }) async { final backup = controller.value; controller.add(value); try { if (!await setter(_c.pref, value)) { throw StateError("Unknown error"); } } catch (e, stackTrace) { _log.severe("[_set] Failed setting preference", e, stackTrace); controller ..addError(e, stackTrace) ..add(backup); } } language_util.AppLanguage _langIdToAppLanguage(int langId) { try { return language_util.supportedLanguages[langId]!; } catch (_) { return language_util.supportedLanguages[0]!; } } final DiContainer _c; late final _languageController = BehaviorSubject.seeded(_c.pref.getLanguageOr(0)); late final _languageStream = Lazy( () => _languageController .map(_langIdToAppLanguage) .publishValueSeeded(_langIdToAppLanguage(_languageController.value)) ..connect(), ); late final _albumBrowserZoomLevelController = BehaviorSubject.seeded(_c.pref.getAlbumBrowserZoomLevelOr(0)); late final _homeAlbumsSortController = BehaviorSubject.seeded(_c.pref.getHomeAlbumsSortOr(0)); late final _isEnableExifController = BehaviorSubject.seeded(_c.pref.isEnableExifOr(true)); late final _shouldProcessExifWifiOnlyController = BehaviorSubject.seeded(_c.pref.shouldProcessExifWifiOnlyOr(true)); late final _memoriesRangeController = BehaviorSubject.seeded(_c.pref.getMemoriesRangeOr(2)); late final _isPhotosTabSortByNameController = BehaviorSubject.seeded(_c.pref.isPhotosTabSortByNameOr(false)); late final _viewerScreenBrightnessController = BehaviorSubject.seeded(_c.pref.getViewerScreenBrightnessOr(-1)); late final _isViewerForceRotationController = BehaviorSubject.seeded(_c.pref.isViewerForceRotationOr(false)); late final _gpsMapProviderController = BehaviorSubject.seeded( GpsMapProvider.fromValue(_c.pref.getGpsMapProviderOr(0))); late final _isAlbumBrowserShowDateController = BehaviorSubject.seeded(_c.pref.isAlbumBrowserShowDateOr(false)); }