Skip to content

Commit

Permalink
refactor is not owner concept
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 a7a542d commit 721e594
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
57 changes: 29 additions & 28 deletions lib/screen/interactive_postcard/postcard_detail_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ abstract class PostcardDetailEvent {}
class PostcardDetailGetInfoEvent extends PostcardDetailEvent {
final ArtworkIdentity identity;
final bool useIndexer;
final bool isFromLeaderboard;

PostcardDetailGetInfoEvent(this.identity,
{this.useIndexer = false, this.isFromLeaderboard = false});
PostcardDetailGetInfoEvent(this.identity, {this.useIndexer = false});
}

class FetchLeaderboardEvent extends PostcardDetailEvent {}
Expand Down Expand Up @@ -64,9 +62,9 @@ class PostcardDetailBloc
this._tokenService,
this._merchandiseApi,
this._remoteConfig,
) : super(PostcardDetailState(provenances: [], isViewOnly: true)) {
) : super(PostcardDetailState(provenances: [])) {
on<PostcardDetailGetInfoEvent>((event, emit) async {
if (event.useIndexer || event.isFromLeaderboard) {
if (event.useIndexer) {
final request = QueryListTokensRequest(
owners: [event.identity.owner],
);
Expand All @@ -82,21 +80,14 @@ class PostcardDetailBloc
}
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,
showMerch: false,
enableMerch: false,
));

final hasMerch =
await _showMerchProduct(assetToken.first, isViewOnly);
if (hasMerch != state.showMerch) {
emit(state.copyWith(showMerch: hasMerch));
}
}
return;
} else {
Expand All @@ -114,8 +105,7 @@ class PostcardDetailBloc
assetToken?.setAssetPrompt(tempsPrompt);
}
final paths = getUpdatingPath(assetToken);
final isViewOnly =
await _isViewOnly(assetToken, event.isFromLeaderboard);
final isViewOnly = await _isViewOnly(assetToken);
emit(
state.copyWith(
assetToken: assetToken,
Expand All @@ -132,9 +122,12 @@ class PostcardDetailBloc
emit(state.copyWith(provenances: provenances));
}

final hasMerch = await _showMerchProduct(assetToken, isViewOnly);
if (hasMerch != state.showMerch) {
emit(state.copyWith(showMerch: hasMerch));
final showMerch =
await _showMerchProduct(assetToken, isViewOnly ?? true);
if (showMerch != state.showMerch) {
emit(state.copyWith(
showMerch: showMerch,
enableMerch: showMerch && _enableMerch(assetToken)));
}

if (assetToken != null &&
Expand Down Expand Up @@ -230,32 +223,40 @@ class PostcardDetailBloc
return Pair(imagePath, metadataPath);
}

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

Future<bool> _showMerchProduct(AssetToken? asset, bool isViewOnly) async {
if (asset == null) {
return false;
}
final isShowConfig = (asset.isCompleted ||
!_remoteConfig.getBool(
ConfigGroup.merchandise, ConfigKey.mustCompleted)) &&
final isShowConfig =
_remoteConfig.getBool(ConfigGroup.merchandise, ConfigKey.enable) &&
(_remoteConfig.getBool(
ConfigGroup.merchandise, ConfigKey.allowViewOnly) ||
!isViewOnly);
(_remoteConfig.getBool(
ConfigGroup.merchandise, ConfigKey.allowViewOnly) ||
!isViewOnly);
if (!isShowConfig) {
return false;
}
try {
final products = await _merchandiseApi.getProducts(asset.id);
return products.isNotEmpty;
} catch (e) {
return true;
}
}

bool _enableMerch(AssetToken? asset) {
if (asset == null) {
return false;
}
final isEnable = asset.isCompleted ||
!_remoteConfig.getBool(
ConfigGroup.merchandise, ConfigKey.mustCompleted);
return isEnable;
}
}
25 changes: 13 additions & 12 deletions lib/screen/interactive_postcard/postcard_detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
with AfterLayoutMixin<ClaimedPostcardDetailPage> {
late ScrollController _scrollController;
late bool withSharing;
late bool isViewOnly;
late bool isNotOwner;
late bool isSending;
late bool alreadyShowPopup;
late bool isProcessingStampPostcard;
Expand All @@ -118,7 +118,7 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
@override
void initState() {
_scrollController = ScrollController();
isViewOnly = widget.payload.isFromLeaderboard;
isNotOwner = widget.payload.isFromLeaderboard;
isSending = false;
alreadyShowPopup = false;
isProcessingStampPostcard = false;
Expand All @@ -127,8 +127,8 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
context.read<PostcardDetailBloc>().add(
PostcardDetailGetInfoEvent(
widget.payload.identities[widget.payload.currentIndex],
useIndexer: widget.payload.isFromLeaderboard,
isFromLeaderboard: widget.payload.useIndexer),
useIndexer: widget.payload.useIndexer ||
widget.payload.isFromLeaderboard),
);
context.read<PostcardDetailBloc>().add(FetchLeaderboardEvent());
context.read<AccountsBloc>().add(FetchAllAddressesEvent());
Expand Down Expand Up @@ -304,7 +304,8 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
if (previous.assetToken?.isCompleted != true &&
current.assetToken?.isCompleted == true &&
current.assetToken?.isAlreadyShowYouDidIt == false &&
!isViewOnly) {
!isNotOwner &&
current.isViewOnly == false) {
unawaited(_youDidIt(context, current.assetToken!));
}
return true;
Expand Down Expand Up @@ -340,10 +341,10 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
}
setState(() {
currentAsset = state.assetToken;
isViewOnly = state.isViewOnly;
isNotOwner = isNotOwner || (state.isViewOnly ?? true);
isSending = state.assetToken?.isSending ?? false;
});
if (isViewOnly) {
if (isNotOwner) {
return;
}
if (withSharing) {
Expand Down Expand Up @@ -431,8 +432,8 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
child: Semantics(
label: 'artworkDotIcon',
child: IconButton(
onPressed: () => unawaited(_showArtworkOptionsDialog(
context, asset, state.isViewOnly)),
onPressed: () => unawaited(
_showArtworkOptionsDialog(context, asset)),
constraints: const BoxConstraints(
maxWidth: 44,
maxHeight: 44,
Expand Down Expand Up @@ -623,7 +624,7 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
'place_15_stamps'.tr(),
style: theme.textTheme.moMASans400Black12,
);
if (asset.isCompleted || isViewOnly || !asset.isLastOwner) {
if (asset.isCompleted || isNotOwner || !asset.isLastOwner) {
return const SizedBox();
}
if (isProcessingStampPostcard ||
Expand Down Expand Up @@ -917,7 +918,7 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
);

Future _showArtworkOptionsDialog(
BuildContext context, AssetToken asset, bool isViewOnly) async {
BuildContext context, AssetToken asset) async {
final theme = Theme.of(context);
if (!mounted) {
return;
Expand Down Expand Up @@ -961,7 +962,7 @@ class ClaimedPostcardDetailPageState extends State<ClaimedPostcardDetailPage>
Navigator.of(context).pop();
},
),
if (!isViewOnly) ...[
if (!isNotOwner) ...[
if (_remoteConfig.getBool(
ConfigGroup.feature, ConfigKey.downloadStamp))
OptionItem(
Expand Down
8 changes: 6 additions & 2 deletions lib/screen/interactive_postcard/postcard_detail_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@ class PostcardDetailState {
PostcardLeaderboard? leaderboard;
bool isFetchingLeaderboard;
bool? showMerch;
bool isViewOnly;
bool? enableMerch;
bool? isViewOnly;

PostcardDetailState({
required this.provenances,
required this.isViewOnly,
this.isViewOnly,
this.assetToken,
this.imagePath,
this.metadataPath,
this.leaderboard,
this.isFetchingLeaderboard = false,
this.showMerch,
this.enableMerch,
});

ArtworkDetailState toArtworkDetailState() => ArtworkDetailState(
Expand All @@ -45,6 +47,7 @@ class PostcardDetailState {
bool? isFetchingLeaderboard,
bool? showMerch,
bool? isViewOnly,
bool? enableMerch,
}) =>
PostcardDetailState(
assetToken: assetToken ?? this.assetToken,
Expand All @@ -56,5 +59,6 @@ class PostcardDetailState {
isFetchingLeaderboard ?? this.isFetchingLeaderboard,
showMerch: showMerch ?? this.showMerch,
isViewOnly: isViewOnly ?? this.isViewOnly,
enableMerch: enableMerch ?? this.enableMerch,
);
}

0 comments on commit 721e594

Please sign in to comment.