Merge pull request #931 from pangeachat/store-construct-list-model

make ConstructListModel updatable and added models for vocab and morp…
pull/1476/head
ggurdin 1 year ago committed by GitHub
commit 532326a7a8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -25,6 +25,27 @@ class GetAnalyticsController {
CachedStreamController<AnalyticsStreamUpdate> analyticsStream = CachedStreamController<AnalyticsStreamUpdate> analyticsStream =
CachedStreamController<AnalyticsStreamUpdate>(); CachedStreamController<AnalyticsStreamUpdate>();
ConstructListModel vocabModel = ConstructListModel(
type: ConstructTypeEnum.vocab,
uses: [],
);
ConstructListModel grammarModel = ConstructListModel(
type: ConstructTypeEnum.morph,
uses: [],
);
List<OneConstructUse> get allConstructUses {
final List<OneConstructUse> storedUses = getConstructsLocal() ?? [];
final List<OneConstructUse> localUses = locallyCachedConstructs;
final List<OneConstructUse> allConstructs = [
...storedUses,
...localUses,
];
return allConstructs;
}
/// The previous XP points of the user, before the last update. /// The previous XP points of the user, before the last update.
/// Used for animating analytics updates. /// Used for animating analytics updates.
int? prevXP; int? prevXP;
@ -73,7 +94,11 @@ class GetAnalyticsController {
.listen(onAnalyticsUpdate); .listen(onAnalyticsUpdate);
_pangeaController.putAnalytics.lastUpdatedCompleter.future.then((_) { _pangeaController.putAnalytics.lastUpdatedCompleter.future.then((_) {
getConstructs().then((_) => updateAnalyticsStream()); getConstructs().then((_) {
vocabModel.updateConstructs(allConstructUses);
grammarModel.updateConstructs(allConstructUses);
updateAnalyticsStream();
});
}); });
} }
@ -87,6 +112,8 @@ class GetAnalyticsController {
} }
Future<void> onAnalyticsUpdate(AnalyticsUpdate analyticsUpdate) async { Future<void> onAnalyticsUpdate(AnalyticsUpdate analyticsUpdate) async {
vocabModel.updateConstructs(analyticsUpdate.newConstructs);
grammarModel.updateConstructs(analyticsUpdate.newConstructs);
if (analyticsUpdate.isLogout) return; if (analyticsUpdate.isLogout) return;
if (analyticsUpdate.type == AnalyticsUpdateType.server) { if (analyticsUpdate.type == AnalyticsUpdateType.server) {
await getConstructs(forceUpdate: true); await getConstructs(forceUpdate: true);
@ -134,18 +161,6 @@ class GetAnalyticsController {
return words.points + morphs.points; return words.points + morphs.points;
} }
List<OneConstructUse> get allConstructUses {
final List<OneConstructUse> storedUses = getConstructsLocal() ?? [];
final List<OneConstructUse> localUses = locallyCachedConstructs;
final List<OneConstructUse> allConstructs = [
...storedUses,
...localUses,
];
return allConstructs;
}
/// A local cache of eventIds and construct uses for messages sent since the last update. /// A local cache of eventIds and construct uses for messages sent since the last update.
/// It's a map of eventIDs to a list of OneConstructUses. Not just a list of OneConstructUses /// It's a map of eventIDs to a list of OneConstructUses. Not just a list of OneConstructUses
/// because, with practice activity constructs, we might need to add to the list for a given /// because, with practice activity constructs, we might need to add to the list for a given

@ -138,7 +138,11 @@ class PutAnalyticsController extends BaseController<AnalyticsStream> {
_addLocalMessage(eventID, constructs).then( _addLocalMessage(eventID, constructs).then(
(_) { (_) {
_clearDraftUses(roomID); _clearDraftUses(roomID);
_decideWhetherToUpdateAnalyticsRoom(level, data.origin); _decideWhetherToUpdateAnalyticsRoom(
level,
data.origin,
data.constructs,
);
}, },
); );
} }
@ -202,8 +206,12 @@ class PutAnalyticsController extends BaseController<AnalyticsStream> {
} }
final level = _pangeaController.getAnalytics.level; final level = _pangeaController.getAnalytics.level;
// the list 'uses' gets altered in the _addLocalMessage method,
// so copy it here to that the list of new uses is accurate
final List<OneConstructUse> newUses = List.from(uses);
_addLocalMessage('draft$roomID', uses).then( _addLocalMessage('draft$roomID', uses).then(
(_) => _decideWhetherToUpdateAnalyticsRoom(level, origin), (_) => _decideWhetherToUpdateAnalyticsRoom(level, origin, newUses),
); );
} }
@ -246,6 +254,7 @@ class PutAnalyticsController extends BaseController<AnalyticsStream> {
void _decideWhetherToUpdateAnalyticsRoom( void _decideWhetherToUpdateAnalyticsRoom(
int prevLevel, int prevLevel,
AnalyticsUpdateOrigin? origin, AnalyticsUpdateOrigin? origin,
List<OneConstructUse> newConstructs,
) { ) {
// cancel the last timer that was set on message event and // cancel the last timer that was set on message event and
// reset it to fire after _minutesBeforeUpdate minutes // reset it to fire after _minutesBeforeUpdate minutes
@ -266,7 +275,11 @@ class PutAnalyticsController extends BaseController<AnalyticsStream> {
newLevel > prevLevel newLevel > prevLevel
? sendLocalAnalyticsToAnalyticsRoom() ? sendLocalAnalyticsToAnalyticsRoom()
: analyticsUpdateStream.add( : analyticsUpdateStream.add(
AnalyticsUpdate(AnalyticsUpdateType.local, origin: origin), AnalyticsUpdate(
AnalyticsUpdateType.local,
newConstructs,
origin: origin,
),
); );
} }
@ -334,6 +347,7 @@ class PutAnalyticsController extends BaseController<AnalyticsStream> {
analyticsUpdateStream.add( analyticsUpdateStream.add(
AnalyticsUpdate( AnalyticsUpdate(
AnalyticsUpdateType.server, AnalyticsUpdateType.server,
[],
isLogout: onLogout, isLogout: onLogout,
), ),
); );
@ -397,7 +411,13 @@ enum AnalyticsUpdateOrigin {
class AnalyticsUpdate { class AnalyticsUpdate {
final AnalyticsUpdateType type; final AnalyticsUpdateType type;
final AnalyticsUpdateOrigin? origin; final AnalyticsUpdateOrigin? origin;
final List<OneConstructUse> newConstructs;
final bool isLogout; final bool isLogout;
AnalyticsUpdate(this.type, {this.isLogout = false, this.origin}); AnalyticsUpdate(
this.type,
this.newConstructs, {
this.isLogout = false,
this.origin,
});
} }

@ -1,138 +1,88 @@
import 'package:fluffychat/pangea/enum/construct_type_enum.dart'; import 'package:fluffychat/pangea/enum/construct_type_enum.dart';
import 'package:fluffychat/pangea/enum/construct_use_type_enum.dart'; import 'package:fluffychat/pangea/enum/construct_use_type_enum.dart';
import 'package:fluffychat/pangea/models/analytics/constructs_model.dart'; import 'package:fluffychat/pangea/models/analytics/constructs_model.dart';
import 'package:fluffychat/pangea/models/practice_activities.dart/practice_activity_model.dart';
/// A wrapper around a list of [OneConstructUse]s, used to simplify /// A wrapper around a list of [OneConstructUse]s, used to simplify
/// the process of filtering / sorting / displaying the events. /// the process of filtering / sorting / displaying the events.
/// Takes a construct type and a list of events /// Takes a construct type and a list of events
class ConstructListModel { class ConstructListModel {
final ConstructTypeEnum? type; final ConstructTypeEnum? type;
final List<OneConstructUse> _uses;
List<ConstructUses>? _constructList;
List<ConstructUseTypeUses>? _typedConstructs;
/// A map of lemmas to ConstructUses, each of which contains a lemma /// A map of lemmas to ConstructUses, each of which contains a lemma
/// key = lemmma + constructType.string, value = ConstructUses /// key = lemmma + constructType.string, value = ConstructUses
Map<String, ConstructUses>? _constructMap; final Map<String, ConstructUses> _constructMap = {};
/// Storing this to avoid re-running the sort operation each time this needs to
/// be accessed. It contains the same information as _constructMap, but sorted.
List<ConstructUses> constructList = [];
ConstructListModel({ ConstructListModel({
required this.type, required this.type,
required List<OneConstructUse> uses, required List<OneConstructUse> uses,
}) : _uses = uses; }) {
updateConstructs(uses);
List<OneConstructUse> get uses => }
_uses.where((use) => use.constructType == type || type == null).toList();
/// All unique lemmas used in the construct events
List<String> get lemmas => constructList.map((e) => e.lemma).toSet().toList();
/// All unique lemmas used in the construct events with non-zero points /// Given a list of new construct uses, update the map of construct
List<String> get lemmasWithPoints => /// IDs to ConstructUses and re-sort the list of ConstructUses
constructListWithPoints.map((e) => e.lemma).toSet().toList(); void updateConstructs(List<OneConstructUse> newUses) {
final List<OneConstructUse> filteredUses = newUses
.where((use) => use.constructType == type || type == null)
.toList();
_updateConstructMap(filteredUses);
_updateConstructList();
}
/// A map of lemmas to ConstructUses, each of which contains a lemma /// A map of lemmas to ConstructUses, each of which contains a lemma
/// key = lemmma + constructType.string, value = ConstructUses /// key = lemmma + constructType.string, value = ConstructUses
void _buildConstructMap() { void _updateConstructMap(final List<OneConstructUse> newUses) {
final Map<String, List<OneConstructUse>> lemmaToUses = {}; for (final use in newUses) {
for (final use in uses) {
if (use.lemma == null) continue; if (use.lemma == null) continue;
lemmaToUses[use.lemma! + use.constructType.string] ??= []; final currentUses = _constructMap[use.identifier.string] ??
lemmaToUses[use.lemma! + use.constructType.string]!.add(use); ConstructUses(
uses: [],
constructType: use.constructType,
lemma: use.lemma!,
);
currentUses.uses.add(use);
_constructMap[use.identifier.string] = currentUses;
} }
_constructMap = lemmaToUses.map(
(key, value) => MapEntry(
key,
ConstructUses(
uses: value,
constructType: value.first.constructType,
lemma: value.first.lemma!,
),
),
);
}
ConstructUses? getConstructUses(String lemma, ConstructTypeEnum type) {
if (_constructMap == null) _buildConstructMap();
return _constructMap![lemma + type.string];
} }
/// A list of ConstructUses, each of which contains a lemma and /// A list of ConstructUses, each of which contains a lemma and
/// a list of uses, sorted by the number of uses /// a list of uses, sorted by the number of uses
List<ConstructUses> get constructList { void _updateConstructList() {
// the list of uses doesn't change so we don't have to re-calculate this // TODO check how expensive this is
if (_constructList != null) return _constructList!; constructList = _constructMap.values.toList();
constructList.sort((a, b) {
if (_constructMap == null) _buildConstructMap();
_constructList = _constructMap!.values.toList();
_constructList!.sort((a, b) {
final comp = b.uses.length.compareTo(a.uses.length); final comp = b.uses.length.compareTo(a.uses.length);
if (comp != 0) return comp; if (comp != 0) return comp;
return a.lemma.compareTo(b.lemma); return a.lemma.compareTo(b.lemma);
}); });
}
return _constructList!; ConstructUses? getConstructUses(ConstructIdentifier identifier) {
return _constructMap[identifier.string];
} }
List<ConstructUses> get constructListWithPoints => List<ConstructUses> get constructListWithPoints =>
constructList.where((constructUse) => constructUse.points > 0).toList(); constructList.where((constructUse) => constructUse.points > 0).toList();
get maxXPPerLemma { /// All unique lemmas used in the construct events with non-zero points
return type != null List<String> get lemmasWithPoints =>
? type!.maxXPPerLemma constructListWithPoints.map((e) => e.lemma).toSet().toList();
: ConstructTypeEnum.vocab.maxXPPerLemma;
}
/// A list of ConstructUseTypeUses, each of which int get maxXPPerLemma =>
/// contains a lemma, a use type, and a list of uses type?.maxXPPerLemma ?? ConstructTypeEnum.vocab.maxXPPerLemma;
List<ConstructUseTypeUses> get typedConstructs {
if (_typedConstructs != null) return _typedConstructs!;
final List<ConstructUseTypeUses> typedConstructs = [];
for (final construct in constructList) {
final typeToUses = <ConstructUseTypeEnum, List<OneConstructUse>>{};
for (final use in construct.uses) {
typeToUses[use.useType] ??= [];
typeToUses[use.useType]!.add(use);
}
for (final typeEntry in typeToUses.entries) {
typedConstructs.add(
ConstructUseTypeUses(
lemma: construct.lemma,
constructType: typeEntry.value.first.constructType,
useType: typeEntry.key,
uses: typeEntry.value,
),
);
}
}
return typedConstructs;
}
/// The total number of points for all uses of this construct type /// The total number of points for all uses of this construct type
int get points { int get points {
// double totalPoints = 0; int totalPoints = 0;
return typedConstructs.fold<int>( for (final constructUse in _constructMap.values.toList()) {
0, totalPoints += constructUse.points;
(total, typedConstruct) => }
total + return totalPoints;
typedConstruct.useType.pointValue * typedConstruct.uses.length,
);
// Commenting this out for now
// Minimize the amount of points given for repeated uses of the same lemma.
// i.e., if a lemma is used 4 times without assistance, the point value for
// a use without assistance is 3. So the points would be
// 3/1 + 3/2 + 3/3 + 3/4 = 3 + 1.5 + 1 + 0.75 = 5.25 (instead of 12)
// for (final typedConstruct in typedConstructs) {
// final pointValue = typedConstruct.useType.pointValue;
// double calc = 0.0;
// for (int k = 1; k <= typedConstruct.uses.length; k++) {
// calc += pointValue / k;
// }
// totalPoints += calc;
// }
// return totalPoints.round();
} }
} }
@ -166,19 +116,3 @@ class ConstructUses {
return _lastUsed = lastUse; return _lastUsed = lastUse;
} }
} }
/// One lemma, a use type, and a list of uses
/// for that lemma and use type
class ConstructUseTypeUses {
final ConstructUseTypeEnum useType;
final ConstructTypeEnum constructType;
final String lemma;
final List<OneConstructUse> uses;
ConstructUseTypeUses({
required this.useType,
required this.constructType,
required this.lemma,
required this.uses,
});
}

@ -1,6 +1,7 @@
import 'dart:developer'; import 'dart:developer';
import 'package:fluffychat/pangea/enum/construct_use_type_enum.dart'; import 'package:fluffychat/pangea/enum/construct_use_type_enum.dart';
import 'package:fluffychat/pangea/models/practice_activities.dart/practice_activity_model.dart';
import 'package:fluffychat/pangea/utils/error_handler.dart'; import 'package:fluffychat/pangea/utils/error_handler.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:matrix/matrix.dart'; import 'package:matrix/matrix.dart';
@ -133,6 +134,11 @@ class OneConstructUse {
} }
int get pointValue => useType.pointValue; int get pointValue => useType.pointValue;
ConstructIdentifier get identifier => ConstructIdentifier(
lemma: lemma!,
type: constructType,
);
} }
class ConstructUseMetaData { class ConstructUseMetaData {

@ -51,6 +51,8 @@ class ConstructIdentifier {
int get hashCode { int get hashCode {
return lemma.hashCode ^ type.hashCode; return lemma.hashCode ^ type.hashCode;
} }
String get string => "$lemma-${type.string}";
} }
class CandidateMessage { class CandidateMessage {

@ -3,7 +3,6 @@ import 'dart:async';
import 'package:fluffychat/pages/chat_list/client_chooser_button.dart'; import 'package:fluffychat/pages/chat_list/client_chooser_button.dart';
import 'package:fluffychat/pangea/controllers/get_analytics_controller.dart'; import 'package:fluffychat/pangea/controllers/get_analytics_controller.dart';
import 'package:fluffychat/pangea/controllers/pangea_controller.dart'; import 'package:fluffychat/pangea/controllers/pangea_controller.dart';
import 'package:fluffychat/pangea/enum/construct_type_enum.dart';
import 'package:fluffychat/pangea/enum/progress_indicators_enum.dart'; import 'package:fluffychat/pangea/enum/progress_indicators_enum.dart';
import 'package:fluffychat/pangea/models/analytics/construct_list_model.dart'; import 'package:fluffychat/pangea/models/analytics/construct_list_model.dart';
import 'package:fluffychat/pangea/models/analytics/constructs_model.dart'; import 'package:fluffychat/pangea/models/analytics/constructs_model.dart';
@ -38,13 +37,6 @@ class LearningProgressIndicatorsState
/// A stream subscription to listen for updates to /// A stream subscription to listen for updates to
/// the analytics data, either locally or from events /// the analytics data, either locally or from events
StreamSubscription<AnalyticsStreamUpdate>? _analyticsUpdateSubscription; StreamSubscription<AnalyticsStreamUpdate>? _analyticsUpdateSubscription;
/// Vocabulary constructs model
ConstructListModel? words;
/// Morph constructs model
ConstructListModel? morphs;
bool loading = true; bool loading = true;
// Some buggy stuff is happening with this data not being updated at login, so switching // Some buggy stuff is happening with this data not being updated at login, so switching
@ -81,15 +73,6 @@ class LearningProgressIndicatorsState
/// Update the analytics data shown in the UI. This comes from a /// Update the analytics data shown in the UI. This comes from a
/// combination of stored events and locally cached data. /// combination of stored events and locally cached data.
Future<void> updateAnalyticsData(List<OneConstructUse> constructs) async { Future<void> updateAnalyticsData(List<OneConstructUse> constructs) async {
words = ConstructListModel(
type: ConstructTypeEnum.vocab,
uses: constructs,
);
morphs = ConstructListModel(
type: ConstructTypeEnum.morph,
uses: constructs,
);
currentConstructs = constructs; currentConstructs = constructs;
if (loading) loading = false; if (loading) loading = false;
if (mounted) setState(() {}); if (mounted) setState(() {});
@ -99,9 +82,9 @@ class LearningProgressIndicatorsState
ConstructListModel? getConstructsModel(ProgressIndicatorEnum indicator) { ConstructListModel? getConstructsModel(ProgressIndicatorEnum indicator) {
switch (indicator) { switch (indicator) {
case ProgressIndicatorEnum.wordsUsed: case ProgressIndicatorEnum.wordsUsed:
return words; return _pangeaController.getAnalytics.vocabModel;
case ProgressIndicatorEnum.morphsUsed: case ProgressIndicatorEnum.morphsUsed:
return morphs; return _pangeaController.getAnalytics.grammarModel;
default: default:
return null; return null;
} }
@ -111,9 +94,11 @@ class LearningProgressIndicatorsState
int? getProgressPoints(ProgressIndicatorEnum indicator) { int? getProgressPoints(ProgressIndicatorEnum indicator) {
switch (indicator) { switch (indicator) {
case ProgressIndicatorEnum.wordsUsed: case ProgressIndicatorEnum.wordsUsed:
return words?.lemmasWithPoints.length; return _pangeaController
.getAnalytics.vocabModel.lemmasWithPoints.length;
case ProgressIndicatorEnum.morphsUsed: case ProgressIndicatorEnum.morphsUsed:
return morphs?.lemmasWithPoints.length; return _pangeaController
.getAnalytics.grammarModel.lemmasWithPoints.length;
case ProgressIndicatorEnum.level: case ProgressIndicatorEnum.level:
return level; return level;
} }

@ -4,6 +4,7 @@ import 'package:fluffychat/pangea/matrix_event_wrappers/pangea_message_event.dar
import 'package:fluffychat/pangea/models/analytics/construct_list_model.dart'; import 'package:fluffychat/pangea/models/analytics/construct_list_model.dart';
import 'package:fluffychat/pangea/models/analytics/constructs_model.dart'; import 'package:fluffychat/pangea/models/analytics/constructs_model.dart';
import 'package:fluffychat/pangea/models/practice_activities.dart/message_activity_request.dart'; import 'package:fluffychat/pangea/models/practice_activities.dart/message_activity_request.dart';
import 'package:fluffychat/pangea/models/practice_activities.dart/practice_activity_model.dart';
import 'package:fluffychat/widgets/matrix.dart'; import 'package:fluffychat/widgets/matrix.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
@ -72,8 +73,10 @@ class TargetTokensController {
for (final construct in token.constructs) { for (final construct in token.constructs) {
final constructUseModel = constructList.getConstructUses( final constructUseModel = constructList.getConstructUses(
construct.id.lemma, ConstructIdentifier(
construct.id.type, lemma: construct.id.lemma,
type: construct.id.type,
),
); );
if (constructUseModel != null) { if (constructUseModel != null) {
construct.xp += constructUseModel.points; construct.xp += constructUseModel.points;

Loading…
Cancel
Save