From a7a542d771991b79eaaa47e9b345e7e3cd9091c9 Mon Sep 17 00:00:00 2001 From: phuoc Date: Thu, 7 Dec 2023 10:07:08 +0700 Subject: [PATCH] refactor remote config, viewonly Signed-off-by: phuoc --- lib/screen/app_router.dart | 46 ++++------- .../preview_detail/preview_detail_widget.dart | 2 +- .../postcard_detail_bloc.dart | 77 +++++++++++++------ .../postcard_detail_page.dart | 31 +++----- .../postcard_detail_state.dart | 4 + 5 files changed, 85 insertions(+), 75 deletions(-) diff --git a/lib/screen/app_router.dart b/lib/screen/app_router.dart index 961a1752b2..1e304bdd6b 100644 --- a/lib/screen/app_router.dart +++ b/lib/screen/app_router.dart @@ -220,6 +220,17 @@ class AppRouter { injector(), ); final identityBloc = IdentityBloc(injector(), injector()); + final postcardDetailBloc = PostcardDetailBloc( + injector(), + injector(), + injector(), + injector(), + injector(), + injector(), + injector(), + injector(), + injector(), + ); switch (settings.name) { case viewPlayListPage: @@ -382,15 +393,7 @@ class AppRouter { child: MultiBlocProvider( providers: [ BlocProvider(create: (_) => identityBloc), - BlocProvider( - create: (_) => PostcardDetailBloc( - injector(), - injector(), - injector(), - injector(), - injector(), - injector(), - )), + BlocProvider(create: (_) => postcardDetailBloc), ], child: StampPreview( payload: settings.arguments! as StampPreviewPayload), @@ -587,9 +590,7 @@ class AppRouter { injector(), ), ), - BlocProvider( - create: (_) => PostcardDetailBloc(injector(), injector(), - injector(), injector(), injector(), injector())), + BlocProvider(create: (_) => postcardDetailBloc), ], child: ArtworkPreviewPage( payload: settings.arguments! as ArtworkDetailPayload, @@ -683,15 +684,7 @@ class AppRouter { BlocProvider.value(value: accountsBloc), BlocProvider(create: (_) => identityBloc), BlocProvider(create: (_) => TravelInfoBloc()), - BlocProvider( - create: (_) => PostcardDetailBloc( - injector(), - injector(), - injector(), - injector(), - injector(), - injector(), - )), + BlocProvider(create: (_) => postcardDetailBloc), ], child: ClaimedPostcardDetailPage( key: payload.key, payload: payload))); @@ -1132,16 +1125,7 @@ class AppRouter { child: MultiBlocProvider( providers: [ BlocProvider.value(value: accountsBloc), - BlocProvider( - create: (_) => PostcardDetailBloc( - injector(), - injector(), - injector(), - injector(), - injector(), - injector(), - ), - ), + BlocProvider(create: (_) => postcardDetailBloc), ], child: PostcardLeaderboardPage( payload: settings.arguments! as PostcardLeaderboardPagePayload, diff --git a/lib/screen/detail/preview_detail/preview_detail_widget.dart b/lib/screen/detail/preview_detail/preview_detail_widget.dart index ead02618c2..0edb2217a5 100644 --- a/lib/screen/detail/preview_detail/preview_detail_widget.dart +++ b/lib/screen/detail/preview_detail/preview_detail_widget.dart @@ -194,7 +194,7 @@ class PostcardPreviewWidget extends StatefulWidget { class _PostcardPreviewWidgetState extends State with WidgetsBindingObserver, RouteAware { - final bloc = PostcardDetailBloc( + final bloc = PostcardDetailBloc(injector(), injector(), injector(), injector(), injector(), injector(), injector(), injector(), injector()); @override diff --git a/lib/screen/interactive_postcard/postcard_detail_bloc.dart b/lib/screen/interactive_postcard/postcard_detail_bloc.dart index 1601443d6b..58d451345d 100644 --- a/lib/screen/interactive_postcard/postcard_detail_bloc.dart +++ b/lib/screen/interactive_postcard/postcard_detail_bloc.dart @@ -8,7 +8,6 @@ import 'dart:async'; import 'package:autonomy_flutter/au_bloc.dart'; -import 'package:autonomy_flutter/common/injector.dart'; import 'package:autonomy_flutter/gateway/merchandise_api.dart'; import 'package:autonomy_flutter/model/pair.dart'; import 'package:autonomy_flutter/screen/detail/artwork_detail_page.dart'; @@ -16,6 +15,7 @@ import 'package:autonomy_flutter/screen/interactive_postcard/leaderboard/postcar import 'package:autonomy_flutter/screen/interactive_postcard/postcard_detail_state.dart'; import 'package:autonomy_flutter/service/configuration_service.dart'; import 'package:autonomy_flutter/service/postcard_service.dart'; +import 'package:autonomy_flutter/service/remote_config_service.dart'; import 'package:autonomy_flutter/util/asset_token_ext.dart'; import 'package:autonomy_flutter/util/constants.dart'; import 'package:autonomy_flutter/util/distance_formater.dart'; @@ -32,8 +32,10 @@ abstract class PostcardDetailEvent {} class PostcardDetailGetInfoEvent extends PostcardDetailEvent { final ArtworkIdentity identity; final bool useIndexer; + final bool isFromLeaderboard; - PostcardDetailGetInfoEvent(this.identity, {this.useIndexer = false}); + PostcardDetailGetInfoEvent(this.identity, + {this.useIndexer = false, this.isFromLeaderboard = false}); } class FetchLeaderboardEvent extends PostcardDetailEvent {} @@ -48,6 +50,9 @@ class PostcardDetailBloc final IndexerService _indexerService; final PostcardService _postcardService; final ConfigurationService _configurationService; + final TokensService _tokenService; + final MerchandiseApi _merchandiseApi; + final RemoteConfigService _remoteConfig; PostcardDetailBloc( this._assetTokenDao, @@ -56,9 +61,12 @@ class PostcardDetailBloc this._indexerService, this._postcardService, this._configurationService, - ) : super(PostcardDetailState(provenances: [])) { + this._tokenService, + this._merchandiseApi, + this._remoteConfig, + ) : super(PostcardDetailState(provenances: [], isViewOnly: true)) { on((event, emit) async { - if (event.useIndexer) { + if (event.useIndexer || event.isFromLeaderboard) { final request = QueryListTokensRequest( owners: [event.identity.owner], ); @@ -73,22 +81,26 @@ class PostcardDetailBloc assetToken.first.setAssetPrompt(tempsPrompt); } final paths = getUpdatingPath(assetToken.first); + + final isViewOnly = + await _isViewOnly(assetToken.first, event.isFromLeaderboard); emit(state.copyWith( assetToken: assetToken.first, provenances: assetToken.first.provenance, imagePath: paths.first, metadataPath: paths.second, + isViewOnly: isViewOnly, )); - final hasMerch = await _hasMerchProduct(assetToken.first.id); + final hasMerch = + await _showMerchProduct(assetToken.first, isViewOnly); if (hasMerch != state.showMerch) { emit(state.copyWith(showMerch: hasMerch)); } } return; } else { - final tokenService = injector(); - unawaited(tokenService.reindexAddresses([event.identity.owner])); + unawaited(_tokenService.reindexAddresses([event.identity.owner])); final assetToken = await _assetTokenDao.findAssetTokenByIdAndOwner( event.identity.id, event.identity.owner); if (assetToken == null) { @@ -102,19 +114,25 @@ class PostcardDetailBloc assetToken?.setAssetPrompt(tempsPrompt); } final paths = getUpdatingPath(assetToken); + final isViewOnly = + await _isViewOnly(assetToken, event.isFromLeaderboard); emit( state.copyWith( - assetToken: assetToken, - imagePath: paths.first, - metadataPath: paths.second, - ), + assetToken: assetToken, + imagePath: paths.first, + metadataPath: paths.second, + isViewOnly: isViewOnly), ); - final provenances = - await _provenanceDao.findProvenanceByTokenID(event.identity.id); - emit(state.copyWith(provenances: provenances)); + final showProvenances = + _remoteConfig.getBool(ConfigGroup.viewDetail, ConfigKey.provenance); + if (showProvenances) { + final provenances = + await _provenanceDao.findProvenanceByTokenID(event.identity.id); + emit(state.copyWith(provenances: provenances)); + } - final hasMerch = await _hasMerchProduct(assetToken?.id); + final hasMerch = await _showMerchProduct(assetToken, isViewOnly); if (hasMerch != state.showMerch) { emit(state.copyWith(showMerch: hasMerch)); } @@ -181,9 +199,8 @@ class PostcardDetailBloc String? imagePath; String? metadataPath; if (asset != null) { - final postcardService = injector(); final stampingPostcard = - postcardService.getStampingPostcardWithPath(asset.stampingPostcard!); + _postcardService.getStampingPostcardWithPath(asset.stampingPostcard!); final processingStampPostcard = asset.processingStampPostcard; final isStamped = asset.isStamped; if (!isStamped) { @@ -200,7 +217,7 @@ class PostcardDetailBloc } } else { if (stampingPostcard != null) { - unawaited(postcardService + unawaited(_postcardService .updateStampingPostcard([stampingPostcard], isRemove: true)); } if (processingStampPostcard != null) { @@ -213,13 +230,29 @@ class PostcardDetailBloc return Pair(imagePath, metadataPath); } - Future _hasMerchProduct(String? indexId) async { - if (indexId == null) { + Future _isViewOnly(AssetToken? asset, bool isFromLeaderboard) async { + if (asset == null) { + return true; + } + return (await asset.isViewOnly()) || isFromLeaderboard; + } + + Future _showMerchProduct(AssetToken? asset, bool isViewOnly) async { + if (asset == null) { + return false; + } + final isShowConfig = (asset.isCompleted || + !_remoteConfig.getBool( + ConfigGroup.merchandise, ConfigKey.mustCompleted)) && + _remoteConfig.getBool(ConfigGroup.merchandise, ConfigKey.enable) && + (_remoteConfig.getBool( + ConfigGroup.merchandise, ConfigKey.allowViewOnly) || + !isViewOnly); + if (!isShowConfig) { return false; } try { - final merchApi = injector(); - final products = await merchApi.getProducts(indexId); + final products = await _merchandiseApi.getProducts(asset.id); return products.isNotEmpty; } catch (e) { return false; diff --git a/lib/screen/interactive_postcard/postcard_detail_page.dart b/lib/screen/interactive_postcard/postcard_detail_page.dart index 2abd141782..cf6efa0402 100644 --- a/lib/screen/interactive_postcard/postcard_detail_page.dart +++ b/lib/screen/interactive_postcard/postcard_detail_page.dart @@ -127,8 +127,8 @@ class ClaimedPostcardDetailPageState extends State context.read().add( PostcardDetailGetInfoEvent( widget.payload.identities[widget.payload.currentIndex], - useIndexer: widget.payload.isFromLeaderboard || - widget.payload.useIndexer), + useIndexer: widget.payload.isFromLeaderboard, + isFromLeaderboard: widget.payload.useIndexer), ); context.read().add(FetchLeaderboardEvent()); context.read().add(FetchAllAddressesEvent()); @@ -322,8 +322,8 @@ class ClaimedPostcardDetailPageState extends State return; } final assetToken = state.assetToken; + if (assetToken != null) { - final viewOnly = isViewOnly || (await assetToken.isViewOnly()); if (!mounted) { return; } @@ -340,10 +340,10 @@ class ClaimedPostcardDetailPageState extends State } setState(() { currentAsset = state.assetToken; - isViewOnly = viewOnly; + isViewOnly = state.isViewOnly; isSending = state.assetToken?.isSending ?? false; }); - if (viewOnly) { + if (isViewOnly) { return; } if (withSharing) { @@ -431,8 +431,8 @@ class ClaimedPostcardDetailPageState extends State child: Semantics( label: 'artworkDotIcon', child: IconButton( - onPressed: () => unawaited( - _showArtworkOptionsDialog(context, asset)), + onPressed: () => unawaited(_showArtworkOptionsDialog( + context, asset, state.isViewOnly)), constraints: const BoxConstraints( maxWidth: 44, maxHeight: 44, @@ -556,16 +556,7 @@ class ClaimedPostcardDetailPageState extends State height: 20, ), ], - if ((asset.isCompleted || - !_remoteConfig.getBool( - ConfigGroup.merchandise, - ConfigKey.mustCompleted)) && - _remoteConfig.getBool(ConfigGroup.merchandise, - ConfigKey.enable) && - (_remoteConfig.getBool(ConfigGroup.merchandise, - ConfigKey.allowViewOnly) || - !isViewOnly) && - state.showMerch == true) ...[ + if (state.showMerch == true) ...[ _postcardPhysical(context, asset), const SizedBox( height: 20, @@ -890,8 +881,7 @@ class ClaimedPostcardDetailPageState extends State ), ), const SizedBox(height: 20), - ] else if (_remoteConfig.getBool( - ConfigGroup.viewDetail, ConfigKey.provenance)) ...[ + ] else ...[ if (provenances.isNotEmpty) PostcardContainer( child: _provenanceView(context, provenances)) @@ -927,9 +917,8 @@ class ClaimedPostcardDetailPageState extends State ); Future _showArtworkOptionsDialog( - BuildContext context, AssetToken asset) async { + BuildContext context, AssetToken asset, bool isViewOnly) async { final theme = Theme.of(context); - final isViewOnly = await asset.isViewOnly(); if (!mounted) { return; } diff --git a/lib/screen/interactive_postcard/postcard_detail_state.dart b/lib/screen/interactive_postcard/postcard_detail_state.dart index 9353e9a453..5cdd9cacff 100644 --- a/lib/screen/interactive_postcard/postcard_detail_state.dart +++ b/lib/screen/interactive_postcard/postcard_detail_state.dart @@ -18,9 +18,11 @@ class PostcardDetailState { PostcardLeaderboard? leaderboard; bool isFetchingLeaderboard; bool? showMerch; + bool isViewOnly; PostcardDetailState({ required this.provenances, + required this.isViewOnly, this.assetToken, this.imagePath, this.metadataPath, @@ -42,6 +44,7 @@ class PostcardDetailState { PostcardLeaderboard? leaderboard, bool? isFetchingLeaderboard, bool? showMerch, + bool? isViewOnly, }) => PostcardDetailState( assetToken: assetToken ?? this.assetToken, @@ -52,5 +55,6 @@ class PostcardDetailState { isFetchingLeaderboard: isFetchingLeaderboard ?? this.isFetchingLeaderboard, showMerch: showMerch ?? this.showMerch, + isViewOnly: isViewOnly ?? this.isViewOnly, ); }