From d049926ab51d2d169c14e8d1179572b882eb99eb Mon Sep 17 00:00:00 2001 From: PuPha Date: Wed, 13 Dec 2023 11:26:22 +0700 Subject: [PATCH] Update --- lib/model/shared_postcard.dart | 40 +++++++++++++++------------------- lib/util/asset_token_ext.dart | 8 ++----- 2 files changed, 19 insertions(+), 29 deletions(-) diff --git a/lib/model/shared_postcard.dart b/lib/model/shared_postcard.dart index 28f6ff9e9..dfcfb63a0 100644 --- a/lib/model/shared_postcard.dart +++ b/lib/model/shared_postcard.dart @@ -29,23 +29,19 @@ class SharedPostcard { owner == other.owner; // fromJson method - factory SharedPostcard.fromJson(Map json) { - return SharedPostcard( - json["tokenID"] as String, - json["owner"] as String, - json["sharedAt"] == null - ? null - : DateTime.parse(json["sharedAt"] as String), - ); - } + factory SharedPostcard.fromJson(Map json) => SharedPostcard( + json['tokenID'] as String, + json['owner'] as String, + json['sharedAt'] == null + ? null + : DateTime.parse(json['sharedAt'] as String), + ); - Map toJson() { - return { - "tokenID": tokenID, - "owner": owner, - "sharedAt": sharedAt?.toIso8601String(), - }; - } + Map toJson() => { + 'tokenID': tokenID, + 'owner': owner, + 'sharedAt': sharedAt?.toIso8601String(), + }; bool get isExpired { if (sharedAt == null) { @@ -57,15 +53,13 @@ class SharedPostcard { } @override - int get hashCode { - return tokenID.hashCode ^ owner.hashCode; - } + int get hashCode => tokenID.hashCode ^ owner.hashCode; } extension ListSharedPostcard on List { Future> get expiredPostcards async { final expiredPostcards = []; - await Future.forEach(this, (SharedPostcard postcard) async { + await Future.wait(map((postcard) async { if (postcard.isExpired) { final token = await injector() .findAssetTokenByIdAndOwner(postcard.tokenID, postcard.owner); @@ -74,7 +68,7 @@ extension ListSharedPostcard on List { expiredPostcards.add(postcard); } } - }); + })); return expiredPostcards; } } @@ -82,8 +76,8 @@ extension ListSharedPostcard on List { extension Unique on List { List unique([Id Function(E element)? id, bool inplace = true]) { final ids = {}; - var list = inplace ? this : List.from(this); - list.retainWhere((x) => ids.add(id != null ? id(x) : x as Id)); + var list = inplace ? this : List.from(this) + ..retainWhere((x) => ids.add(id != null ? id(x) : x as Id)); return list; } } diff --git a/lib/util/asset_token_ext.dart b/lib/util/asset_token_ext.dart index ef7a6988a..7894b3002 100644 --- a/lib/util/asset_token_ext.dart +++ b/lib/util/asset_token_ext.dart @@ -765,11 +765,7 @@ extension PostcardExtension on AssetToken { bool get isShareExpired { final sharedPostcards = injector().getSharedPostcard(); - final sharedPostcard = sharedPostcards.firstWhereOrNull( - (element) => element.owner == owner && element.tokenID == id); - if (sharedPostcard == null) { - return false; - } - return sharedPostcard.isExpired; + return sharedPostcards.any((element) => + element.owner == owner && element.tokenID == id && element.isExpired); } }