Skip to content

Commit

Permalink
refactor remote config, viewonly
Browse files Browse the repository at this point in the history
Signed-off-by: phuoc <[email protected]>
  • Loading branch information
phuocbitmark committed Dec 7, 2023
1 parent e1fa099 commit a7a542d
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 75 deletions.
46 changes: 15 additions & 31 deletions lib/screen/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,17 @@ class AppRouter {
injector<AuditService>(),
);
final identityBloc = IdentityBloc(injector<AppDatabase>(), injector());
final postcardDetailBloc = PostcardDetailBloc(
injector(),
injector(),
injector(),
injector(),
injector(),
injector(),
injector(),
injector(),
injector(),
);

switch (settings.name) {
case viewPlayListPage:
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class PostcardPreviewWidget extends StatefulWidget {

class _PostcardPreviewWidgetState extends State<PostcardPreviewWidget>
with WidgetsBindingObserver, RouteAware {
final bloc = PostcardDetailBloc(
final bloc = PostcardDetailBloc(injector(), injector(), injector(),
injector(), injector(), injector(), injector(), injector(), injector());

@override
Expand Down
77 changes: 55 additions & 22 deletions lib/screen/interactive_postcard/postcard_detail_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
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';
import 'package:autonomy_flutter/screen/interactive_postcard/leaderboard/postcard_leaderboard.dart';
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';
Expand All @@ -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 {}
Expand All @@ -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,
Expand All @@ -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<PostcardDetailGetInfoEvent>((event, emit) async {
if (event.useIndexer) {
if (event.useIndexer || event.isFromLeaderboard) {
final request = QueryListTokensRequest(
owners: [event.identity.owner],
);
Expand All @@ -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<TokensService>();
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) {
Expand All @@ -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));
}
Expand Down Expand Up @@ -181,9 +199,8 @@ class PostcardDetailBloc
String? imagePath;
String? metadataPath;
if (asset != null) {
final postcardService = injector<PostcardService>();
final stampingPostcard =
postcardService.getStampingPostcardWithPath(asset.stampingPostcard!);
_postcardService.getStampingPostcardWithPath(asset.stampingPostcard!);
final processingStampPostcard = asset.processingStampPostcard;
final isStamped = asset.isStamped;
if (!isStamped) {
Expand All @@ -200,7 +217,7 @@ class PostcardDetailBloc
}
} else {
if (stampingPostcard != null) {
unawaited(postcardService
unawaited(_postcardService
.updateStampingPostcard([stampingPostcard], isRemove: true));
}
if (processingStampPostcard != null) {
Expand All @@ -213,13 +230,29 @@ class PostcardDetailBloc
return Pair(imagePath, metadataPath);
}

Future<bool> _hasMerchProduct(String? indexId) async {
if (indexId == null) {
Future<bool> _isViewOnly(AssetToken? asset, bool isFromLeaderboard) async {
if (asset == null) {
return true;
}
return (await asset.isViewOnly()) || isFromLeaderboard;
}

Future<bool> _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<MerchandiseApi>();
final products = await merchApi.getProducts(indexId);
final products = await _merchandiseApi.getProducts(asset.id);
return products.isNotEmpty;
} catch (e) {
return false;
Expand Down
31 changes: 10 additions & 21 deletions lib/screen/interactive_postcard/postcard_detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
context.read<PostcardDetailBloc>().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<PostcardDetailBloc>().add(FetchLeaderboardEvent());
context.read<AccountsBloc>().add(FetchAllAddressesEvent());
Expand Down Expand Up @@ -322,8 +322,8 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
return;
}
final assetToken = state.assetToken;

if (assetToken != null) {
final viewOnly = isViewOnly || (await assetToken.isViewOnly());
if (!mounted) {
return;
}
Expand All @@ -340,10 +340,10 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
}
setState(() {
currentAsset = state.assetToken;
isViewOnly = viewOnly;
isViewOnly = state.isViewOnly;
isSending = state.assetToken?.isSending ?? false;
});
if (viewOnly) {
if (isViewOnly) {
return;
}
if (withSharing) {
Expand Down Expand Up @@ -431,8 +431,8 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
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,
Expand Down Expand Up @@ -556,16 +556,7 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
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,
Expand Down Expand Up @@ -890,8 +881,7 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
),
),
const SizedBox(height: 20),
] else if (_remoteConfig.getBool(
ConfigGroup.viewDetail, ConfigKey.provenance)) ...[
] else ...[
if (provenances.isNotEmpty)
PostcardContainer(
child: _provenanceView(context, provenances))
Expand Down Expand Up @@ -927,9 +917,8 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
);

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;
}
Expand Down
4 changes: 4 additions & 0 deletions lib/screen/interactive_postcard/postcard_detail_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -42,6 +44,7 @@ class PostcardDetailState {
PostcardLeaderboard? leaderboard,
bool? isFetchingLeaderboard,
bool? showMerch,
bool? isViewOnly,
}) =>
PostcardDetailState(
assetToken: assetToken ?? this.assetToken,
Expand All @@ -52,5 +55,6 @@ class PostcardDetailState {
isFetchingLeaderboard:
isFetchingLeaderboard ?? this.isFetchingLeaderboard,
showMerch: showMerch ?? this.showMerch,
isViewOnly: isViewOnly ?? this.isViewOnly,
);
}

0 comments on commit a7a542d

Please sign in to comment.