Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ppupha committed Dec 13, 2023
1 parent 969dfd9 commit d049926
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
40 changes: 17 additions & 23 deletions lib/model/shared_postcard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,19 @@ class SharedPostcard {
owner == other.owner;

// fromJson method
factory SharedPostcard.fromJson(Map<String, dynamic> 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<String, dynamic> json) => SharedPostcard(
json['tokenID'] as String,
json['owner'] as String,
json['sharedAt'] == null
? null
: DateTime.parse(json['sharedAt'] as String),
);

Map<String, dynamic> toJson() {
return {
"tokenID": tokenID,
"owner": owner,
"sharedAt": sharedAt?.toIso8601String(),
};
}
Map<String, dynamic> toJson() => {
'tokenID': tokenID,
'owner': owner,
'sharedAt': sharedAt?.toIso8601String(),
};

bool get isExpired {
if (sharedAt == null) {
Expand All @@ -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<SharedPostcard> {
Future<List<SharedPostcard>> get expiredPostcards async {
final expiredPostcards = <SharedPostcard>[];
await Future.forEach(this, (SharedPostcard postcard) async {
await Future.wait(map((postcard) async {
if (postcard.isExpired) {
final token = await injector<AssetTokenDao>()
.findAssetTokenByIdAndOwner(postcard.tokenID, postcard.owner);
Expand All @@ -74,16 +68,16 @@ extension ListSharedPostcard on List<SharedPostcard> {
expiredPostcards.add(postcard);
}
}
});
}));
return expiredPostcards;
}
}

extension Unique<E, Id> on List<E> {
List<E> unique([Id Function(E element)? id, bool inplace = true]) {
final ids = <Id>{};
var list = inplace ? this : List<E>.from(this);
list.retainWhere((x) => ids.add(id != null ? id(x) : x as Id));
var list = inplace ? this : List<E>.from(this)
..retainWhere((x) => ids.add(id != null ? id(x) : x as Id));
return list;
}
}
8 changes: 2 additions & 6 deletions lib/util/asset_token_ext.dart
Original file line number Diff line number Diff line change
Expand Up @@ -765,11 +765,7 @@ extension PostcardExtension on AssetToken {
bool get isShareExpired {
final sharedPostcards =
injector<ConfigurationService>().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);
}
}

0 comments on commit d049926

Please sign in to comment.