Migrate hasNewSharedAlbum in account pref

This commit is contained in:
Ming Ming 2024-10-23 00:25:26 +08:00
parent 2eb9ac1d87
commit ce2bc4f9c0
9 changed files with 54 additions and 20 deletions

View file

@ -110,7 +110,7 @@ extension AccountExtension on Account {
///
/// Return true if two Accounts point to the same user on server. Be careful
/// that this does NOT mean that the two Accounts are identical (e.g., they
/// can have difference password)
/// can have different password)
bool compareServerIdentity(Account other) {
return scheme == other.scheme &&
address == other.address &&

View file

@ -6,6 +6,7 @@ import 'package:np_codegen/np_codegen.dart';
import 'package:rxdart/rxdart.dart';
part 'account_pref_controller.g.dart';
part 'account_pref_controller/util.dart';
@npLog
@npSubjectAccessor
@ -45,6 +46,12 @@ class AccountPrefController {
value: value,
);
Future<void> setNewSharedAlbum(bool value) => _set<bool>(
controller: _hasNewSharedAlbumController,
setter: (pref, value) => pref.setNewSharedAlbum(value),
value: value,
);
Future<void> _set<T>({
required BehaviorSubject<T> controller,
required Future<bool> Function(AccountPref pref, T value) setter,
@ -79,4 +86,7 @@ class AccountPrefController {
@npSubjectAccessor
late final _isEnableMemoryAlbumController =
BehaviorSubject.seeded(_accountPref.isEnableMemoryAlbumOr(true));
@npSubjectAccessor
late final _hasNewSharedAlbumController =
BehaviorSubject.seeded(_accountPref.hasNewSharedAlbum() ?? false);
}

View file

@ -43,4 +43,11 @@ extension $AccountPrefControllerNpSubjectAccessor on AccountPrefController {
Stream<bool> get isEnableMemoryAlbumChange =>
isEnableMemoryAlbum.distinct().skip(1);
bool get isEnableMemoryAlbumValue => _isEnableMemoryAlbumController.value;
// _hasNewSharedAlbumController
ValueStream<bool> get hasNewSharedAlbum =>
_hasNewSharedAlbumController.stream;
Stream<bool> get hasNewSharedAlbumNew => hasNewSharedAlbum.skip(1);
Stream<bool> get hasNewSharedAlbumChange =>
hasNewSharedAlbum.distinct().skip(1);
bool get hasNewSharedAlbumValue => _hasNewSharedAlbumController.value;
}

View file

@ -0,0 +1,8 @@
part of '../account_pref_controller.dart';
extension on AccountPref {
bool? hasNewSharedAlbum() =>
provider.getBool(AccountPrefKey.hasNewSharedAlbum);
// Future<bool> setNewSharedAlbum(bool value) =>
// provider.setBool(AccountPrefKey.hasNewSharedAlbum, value);
}

View file

@ -175,9 +175,6 @@ extension AccountPrefExtension on AccountPref {
value,
(key, value) => provider.setString(key, value));
bool? hasNewSharedAlbum() =>
provider.getBool(AccountPrefKey.hasNewSharedAlbum);
bool hasNewSharedAlbumOr([bool def = false]) => hasNewSharedAlbum() ?? def;
Future<bool> setNewSharedAlbum(bool value) => _set<bool>(
AccountPrefKey.hasNewSharedAlbum,
value,

View file

@ -61,11 +61,17 @@ class Home extends StatefulWidget {
@npLog
class _HomeState extends State<Home> with TickerProviderStateMixin {
@override
initState() {
void initState() {
super.initState();
final accountController = context.read<AccountController>();
_importPotentialSharedAlbum().then((value) {
if (value.isNotEmpty) {
AccountPref.of(widget.account).setNewSharedAlbum(true);
// check if account changed
if (accountController.account.compareServerIdentity(widget.account)) {
accountController.accountPrefController.setNewSharedAlbum(true);
} else {
AccountPref.of(widget.account).setNewSharedAlbum(true);
}
}
});
_animationController.value = 1;

View file

@ -12,6 +12,7 @@ import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/bloc_util.dart';
import 'package:nc_photos/cache_manager_util.dart';
import 'package:nc_photos/controller/account_controller.dart';
import 'package:nc_photos/controller/account_pref_controller.dart';
import 'package:nc_photos/controller/collections_controller.dart';
import 'package:nc_photos/controller/pref_controller.dart';
import 'package:nc_photos/entity/album/provider.dart';
@ -19,7 +20,6 @@ import 'package:nc_photos/entity/collection.dart';
import 'package:nc_photos/entity/collection/content_provider/album.dart';
import 'package:nc_photos/entity/collection/content_provider/nc_album.dart';
import 'package:nc_photos/entity/collection/util.dart' as collection_util;
import 'package:nc_photos/entity/pref.dart';
import 'package:nc_photos/exception_event.dart';
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/np_api_util.dart';

View file

@ -28,7 +28,10 @@ class _ButtonGrid extends StatelessWidget {
_ButtonGridItemView(
icon: Icons.share_outlined,
label: L10n.global().collectionSharingLabel,
isShowIndicator: AccountPref.of(account).hasNewSharedAlbumOr(),
isShowIndicator: context
.read<AccountController>()
.accountPrefController
.hasNewSharedAlbumValue,
isEnabled: isEnabled,
onTap: () {
onSharingPressed?.call();

View file

@ -19,10 +19,8 @@ import 'package:nc_photos/entity/album/data_source.dart';
import 'package:nc_photos/entity/collection/builder.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file/data_source.dart';
import 'package:nc_photos/entity/pref.dart';
import 'package:nc_photos/entity/share.dart';
import 'package:nc_photos/exception_event.dart';
import 'package:nc_photos/object_extension.dart';
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/use_case/import_potential_shared_album.dart';
import 'package:nc_photos/widget/collection_browser.dart';
@ -41,10 +39,6 @@ part 'sharing_browser/bloc.dart';
part 'sharing_browser/state_event.dart';
part 'sharing_browser/type.dart';
typedef _BlocBuilder = BlocBuilder<_Bloc, _State>;
typedef _BlocListener = BlocListener<_Bloc, _State>;
// typedef _BlocSelector<T> = BlocSelector<_Bloc, _State, T>;
class SharingBrowserArguments {
SharingBrowserArguments(this.account);
@ -86,14 +80,12 @@ class _WrappedSharingBrowser extends StatefulWidget {
class _WrappedSharingBrowserState extends State<_WrappedSharingBrowser>
with RouteAware, PageVisibilityMixin {
@override
initState() {
void initState() {
super.initState();
_bloc.add(const _Init());
AccountPref.of(_bloc.account).run((obj) {
if (obj.hasNewSharedAlbumOr()) {
obj.setNewSharedAlbum(false);
}
});
if (context.bloc.accountPrefController.hasNewSharedAlbumValue) {
context.bloc.accountPrefController.setNewSharedAlbum(false);
}
}
@override
@ -360,3 +352,14 @@ const _leadingSize = 56.0;
DateFormat _getDateFormat(BuildContext context) => DateFormat(
DateFormat.YEAR_ABBR_MONTH_DAY,
Localizations.localeOf(context).languageCode);
typedef _BlocBuilder = BlocBuilder<_Bloc, _State>;
typedef _BlocListener = BlocListener<_Bloc, _State>;
// typedef _BlocListenerT<T> = BlocListenerT<_Bloc, _State, T>;
// typedef _BlocSelector<T> = BlocSelector<_Bloc, _State, T>;
extension on BuildContext {
_Bloc get bloc => read<_Bloc>();
// _State get state => bloc.state;
// void addEvent(_Event event) => bloc.add(event);
}