Inject Pref to use cases

This commit is contained in:
Ming Ming 2021-11-01 22:43:02 +08:00
parent 7583b09bb8
commit 456975360a
9 changed files with 21 additions and 15 deletions

View file

@ -11,14 +11,14 @@ import 'package:shared_preferences/shared_preferences.dart';
class Pref {
factory Pref() => _inst;
Pref._();
Pref.scoped();
Future<void> init() async {
if (await CompatV32.isPrefNeedMigration()) {
await CompatV32.migratePref();
}
return SharedPreferences.getInstance().then((pref) {
_inst._pref = pref;
_pref = pref;
});
}
@ -207,7 +207,7 @@ class Pref {
}
}
static late final _inst = Pref._();
static late final _inst = Pref.scoped();
late SharedPreferences _pref;
}

View file

@ -15,7 +15,7 @@ import 'package:nc_photos/use_case/update_album.dart';
import 'package:nc_photos/use_case/update_album_with_actual_items.dart';
class AddToAlbum {
const AddToAlbum(this.albumRepo, this.shareRepo, this.appDb);
const AddToAlbum(this.albumRepo, this.shareRepo, this.appDb, this.pref);
/// Add a list of AlbumItems to [album]
Future<Album> call(
@ -42,7 +42,7 @@ class AddToAlbum {
);
await UpdateAlbum(albumRepo)(account, newAlbum);
if (Pref().isLabEnableSharedAlbumOr(false)) {
if (pref.isLabEnableSharedAlbumOr(false)) {
final newFiles =
items.whereType<AlbumFileItem>().map((e) => e.file).toList();
if (newFiles.isNotEmpty) {
@ -99,6 +99,7 @@ class AddToAlbum {
final AlbumRepo albumRepo;
final ShareRepo shareRepo;
final AppDb appDb;
final Pref pref;
static final _log = Logger("use_case.add_to_album.AddToAlbum");
}

View file

@ -12,12 +12,12 @@ import 'package:nc_photos/use_case/remove_share.dart';
import 'package:nc_photos/use_case/unshare_file_from_album.dart';
class RemoveAlbum {
const RemoveAlbum(this.fileRepo, this.albumRepo, this.shareRepo);
const RemoveAlbum(this.fileRepo, this.albumRepo, this.shareRepo, this.pref);
/// Remove an album
Future<void> call(Account account, Album album) async {
_log.info("[call] Remove album: $album");
if (Pref().isLabEnableSharedAlbumOr(false)) {
if (pref.isLabEnableSharedAlbumOr(false)) {
final files = <File>[];
if (album.provider is AlbumStaticProvider) {
files.addAll(AlbumStaticProvider.of(album)
@ -59,6 +59,7 @@ class RemoveAlbum {
final FileRepo fileRepo;
final AlbumRepo albumRepo;
final ShareRepo shareRepo;
final Pref pref;
static final _log = Logger("use_case.remove_album.RemoveAlbum");
}

View file

@ -16,7 +16,7 @@ import 'package:nc_photos/use_case/update_album_with_actual_items.dart';
class RemoveFromAlbum {
const RemoveFromAlbum(
this.albumRepo, this.shareRepo, this.fileRepo, this.appDb);
this.albumRepo, this.shareRepo, this.fileRepo, this.appDb, this.pref);
/// Remove a list of AlbumItems from [album]
///
@ -49,7 +49,7 @@ class RemoveFromAlbum {
}
await UpdateAlbum(albumRepo)(account, newAlbum);
if (Pref().isLabEnableSharedAlbumOr(false)) {
if (pref.isLabEnableSharedAlbumOr(false)) {
final removeFiles =
items.whereType<AlbumFileItem>().map((e) => e.file).toList();
if (removeFiles.isNotEmpty) {
@ -72,6 +72,7 @@ class RemoveFromAlbum {
final ShareRepo shareRepo;
final FileRepo fileRepo;
final AppDb appDb;
final Pref pref;
static final _log = Logger("use_case.remove_from_album.RemoveFromAlbum");
}

View file

@ -408,7 +408,7 @@ class _AlbumBrowserState extends State<AlbumBrowser>
final albumRepo = AlbumRepo(AlbumCachedDataSource(AppDb()));
final shareRepo = ShareRepo(ShareRemoteDataSource());
final fileRepo = FileRepo(FileCachedDataSource(AppDb()));
await RemoveFromAlbum(albumRepo, shareRepo, fileRepo, AppDb())(
await RemoveFromAlbum(albumRepo, shareRepo, fileRepo, AppDb(), Pref())(
widget.account, _album!, selectedItems);
SnackBarManager().showSnackBar(SnackBar(
content: Text(L10n.global().removeSelectedFromAlbumSuccessNotification(

View file

@ -394,7 +394,8 @@ class _HomeAlbumsState extends State<HomeAlbums>
final failures = <Album>[];
for (final a in selectedAlbums) {
try {
await RemoveAlbum(fileRepo, albumRepo, shareRepo)(widget.account, a);
await RemoveAlbum(fileRepo, albumRepo, shareRepo, Pref())(
widget.account, a);
} catch (e, stackTrace) {
_log.shout(
"[_onSelectionDeletePressed] Failed while removing album: '${a.name}'",

View file

@ -429,7 +429,7 @@ class _HomePhotosState extends State<HomePhotos>
.toList();
final albumRepo = AlbumRepo(AlbumCachedDataSource(AppDb()));
final shareRepo = ShareRepo(ShareRemoteDataSource());
await AddToAlbum(albumRepo, shareRepo, AppDb())(
await AddToAlbum(albumRepo, shareRepo, AppDb(), Pref())(
widget.account, value, selected);
if (mounted) {
setState(() {

View file

@ -379,7 +379,7 @@ class _PersonBrowserState extends State<PersonBrowser>
.toList();
final albumRepo = AlbumRepo(AlbumCachedDataSource(AppDb()));
final shareRepo = ShareRepo(ShareRemoteDataSource());
await AddToAlbum(albumRepo, shareRepo, AppDb())(
await AddToAlbum(albumRepo, shareRepo, AppDb(), Pref())(
widget.account, value, selected);
setState(() {
clearSelectedItems();

View file

@ -28,6 +28,7 @@ import 'package:nc_photos/mobile/platform.dart'
import 'package:nc_photos/notified_action.dart';
import 'package:nc_photos/platform/features.dart' as features;
import 'package:nc_photos/platform/k.dart' as platform_k;
import 'package:nc_photos/pref.dart';
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/theme.dart';
import 'package:nc_photos/use_case/add_to_album.dart';
@ -307,7 +308,8 @@ class _ViewerDetailPaneState extends State<ViewerDetailPane> {
.items
.whereType<AlbumFileItem>()
.firstWhere((element) => element.file.path == widget.file.path);
await RemoveFromAlbum(albumRepo, shareRepo, fileRepo, AppDb())(
await RemoveFromAlbum(
albumRepo, shareRepo, fileRepo, AppDb(), Pref())(
widget.account, widget.album!, [thisItem]);
if (mounted) {
Navigator.of(context).pop();
@ -511,7 +513,7 @@ class _ViewerDetailPaneState extends State<ViewerDetailPane> {
));
return Future.error(ArgumentError("File already in album"));
}
await AddToAlbum(albumRepo, shareRepo, AppDb())(
await AddToAlbum(albumRepo, shareRepo, AppDb(), Pref())(
widget.account, album, [newItem]);
} catch (e, stacktrace) {
_log.shout("[_addToAlbum] Failed while updating album", e, stacktrace);