From bfe8789485887aa88a69c5c8cbefd9224caf2c7d Mon Sep 17 00:00:00 2001 From: ggurdin <46800240+ggurdin@users.noreply.github.com> Date: Mon, 13 Jan 2025 10:58:10 -0500 Subject: [PATCH] fix: in lastUsedByActivityType, check for uses with specific activity type (#1413) --- lib/pangea/models/pangea_token_model.dart | 25 +++++++++++++---------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/lib/pangea/models/pangea_token_model.dart b/lib/pangea/models/pangea_token_model.dart index 16a7c6b51..77df8c97f 100644 --- a/lib/pangea/models/pangea_token_model.dart +++ b/lib/pangea/models/pangea_token_model.dart @@ -466,17 +466,20 @@ class PangeaToken { } /// lastUsed by activity type - DateTime? _lastUsedByActivityType(ActivityTypeEnum a) => - constructs.where((c) => a.constructFilter(c.id)).fold( - null, - (previousValue, element) { - if (previousValue == null) return element.lastUsed; - if (element.lastUsed == null) return previousValue; - return element.lastUsed!.isAfter(previousValue) - ? element.lastUsed - : previousValue; - }, - ); + DateTime? _lastUsedByActivityType(ActivityTypeEnum a) { + final List filteredConstructs = + constructs.where((c) => a.constructFilter(c.id)).toList(); + final correctUseTimestamps = filteredConstructs + .expand((c) => c.uses) + .where((u) => u.useType == a.correctUse) + .map((u) => u.timeStamp) + .toList(); + + if (correctUseTimestamps.isEmpty) return null; + + // return the most recent timestamp + return correctUseTimestamps.reduce((a, b) => a.isAfter(b) ? a : b); + } /// daysSinceLastUse by activity type int daysSinceLastUseByType(ActivityTypeEnum a) {