diff --git a/lib/pangea/analytics_misc/construct_list_model.dart b/lib/pangea/analytics_misc/construct_list_model.dart index 3da9df015..07323b499 100644 --- a/lib/pangea/analytics_misc/construct_list_model.dart +++ b/lib/pangea/analytics_misc/construct_list_model.dart @@ -36,16 +36,22 @@ class ConstructListModel { /// A list of unique grammar lemmas List grammarLemmasList = []; - List get unlockedGrammarLemmas { - final morphs = constructList(type: ConstructTypeEnum.morph); + List unlockedLemmas( + ConstructTypeEnum type, { + int threshold = 0, + }) { + final constructs = constructList(type: type); final List unlocked = []; - for (final morph in grammarLemmasList) { - final matches = morphs.where((m) => m.lemma == morph); + final constructsList = + type == ConstructTypeEnum.vocab ? vocabLemmasList : grammarLemmasList; + + for (final lemma in constructsList) { + final matches = constructs.where((m) => m.lemma == lemma); final totalPoints = matches.fold( 0, (total, match) => total + match.points, ); - if (totalPoints > 25) { + if (totalPoints > threshold) { unlocked.add(matches.first.id); } } diff --git a/lib/pangea/analytics_misc/get_analytics_controller.dart b/lib/pangea/analytics_misc/get_analytics_controller.dart index 8cc2fdf0f..e6526582e 100644 --- a/lib/pangea/analytics_misc/get_analytics_controller.dart +++ b/lib/pangea/analytics_misc/get_analytics_controller.dart @@ -101,7 +101,7 @@ class GetAnalyticsController extends BaseController { data: {}, ); } finally { - _updateAnalyticsStream(points: 0); + _updateAnalyticsStream(points: 0, newConstructs: []); if (!initCompleter.isCompleted) initCompleter.complete(); _initializing = false; } @@ -128,12 +128,32 @@ class GetAnalyticsController extends BaseController { final offset = _pangeaController.userController.publicProfile?.xpOffset ?? 0; - final prevUnlockedMorphs = constructListModel.unlockedGrammarLemmas.toSet(); + + final prevUnlockedMorphs = constructListModel + .unlockedLemmas( + ConstructTypeEnum.morph, + threshold: 25, + ) + .toSet(); + + final prevUnlockedVocab = + constructListModel.unlockedLemmas(ConstructTypeEnum.vocab).toSet(); + constructListModel.updateConstructs(analyticsUpdate.newConstructs, offset); - final newUnlockedMorphs = constructListModel.unlockedGrammarLemmas + + final newUnlockedMorphs = constructListModel + .unlockedLemmas( + ConstructTypeEnum.morph, + threshold: 25, + ) .toSet() .difference(prevUnlockedMorphs); + final newUnlockedVocab = constructListModel + .unlockedLemmas(ConstructTypeEnum.vocab) + .toSet() + .difference(prevUnlockedVocab); + if (analyticsUpdate.type == AnalyticsUpdateType.server) { await _getConstructs(forceUpdate: true); } @@ -154,6 +174,7 @@ class GetAnalyticsController extends BaseController { (previousValue, element) => previousValue + element.pointValue, ), targetID: analyticsUpdate.targetID, + newConstructs: [...newUnlockedMorphs, ...newUnlockedVocab], ); // Update public profile each time that new analytics are added. // If the level hasn't changed, this will not send an update to the server. @@ -166,11 +187,13 @@ class GetAnalyticsController extends BaseController { void _updateAnalyticsStream({ required int points, + required List newConstructs, String? targetID, }) => analyticsStream.add( AnalyticsStreamUpdate( points: points, + newConstructs: newConstructs, targetID: targetID, ), ); @@ -491,10 +514,12 @@ class AnalyticsCacheEntry { class AnalyticsStreamUpdate { final int points; + final List newConstructs; final String? targetID; AnalyticsStreamUpdate({ required this.points, + required this.newConstructs, this.targetID, }); }