fix: pick another emoji if duplicate emojis in activities (#3804)
This commit is contained in:
parent
10a7024c64
commit
be771abff1
3 changed files with 34 additions and 62 deletions
|
|
@ -1,5 +1,4 @@
|
|||
import 'package:fluffychat/pangea/constructs/construct_form.dart';
|
||||
import 'package:fluffychat/pangea/events/models/pangea_token_model.dart';
|
||||
import 'package:fluffychat/pangea/lemmas/lemma_info_response.dart';
|
||||
import 'package:fluffychat/pangea/practice_activities/activity_type_enum.dart';
|
||||
import 'package:fluffychat/pangea/practice_activities/message_activity_request.dart';
|
||||
|
|
@ -21,35 +20,26 @@ class EmojiActivityGenerator {
|
|||
MessageActivityRequest req,
|
||||
) async {
|
||||
final Map<ConstructForm, List<String>> matchInfo = {};
|
||||
final List<MapEntry<PangeaToken, List<String>>> tokensWithUserEmojis = [];
|
||||
final List<PangeaToken> tokensNeedingServerEmojis = [];
|
||||
//if user saved emojis, use those, otherwise generate.
|
||||
for (final token in req.targetTokens) {
|
||||
final List<String> userSavedEmojis = token.vocabConstructID.userSetEmoji;
|
||||
|
||||
if (userSavedEmojis.isNotEmpty) {
|
||||
tokensWithUserEmojis.add(MapEntry(token, userSavedEmojis));
|
||||
matchInfo[token.vocabForm] = userSavedEmojis;
|
||||
} else {
|
||||
tokensNeedingServerEmojis.add(token);
|
||||
matchInfo[token.vocabForm] = [];
|
||||
}
|
||||
}
|
||||
|
||||
for (final entry in tokensWithUserEmojis) {
|
||||
matchInfo[entry.key.vocabForm] = entry.value;
|
||||
}
|
||||
final List<Future<LemmaInfoResponse>> lemmaInfoFutures = req.targetTokens
|
||||
.map((token) => token.vocabConstructID.getLemmaInfo())
|
||||
.toList();
|
||||
|
||||
if (tokensNeedingServerEmojis.isNotEmpty) {
|
||||
final List<Future<LemmaInfoResponse>> lemmaInfoFutures =
|
||||
tokensNeedingServerEmojis
|
||||
.map((token) => token.vocabConstructID.getLemmaInfo())
|
||||
.toList();
|
||||
final List<LemmaInfoResponse> lemmaInfos =
|
||||
await Future.wait(lemmaInfoFutures);
|
||||
|
||||
final List<LemmaInfoResponse> lemmaInfos =
|
||||
await Future.wait(lemmaInfoFutures);
|
||||
|
||||
for (int i = 0; i < tokensNeedingServerEmojis.length; i++) {
|
||||
matchInfo[tokensNeedingServerEmojis[i].vocabForm] = lemmaInfos[i].emoji;
|
||||
}
|
||||
for (int i = 0; i < req.targetTokens.length; i++) {
|
||||
final formKey = req.targetTokens[i].vocabForm;
|
||||
matchInfo[formKey] ??= [];
|
||||
matchInfo[formKey]!.addAll(lemmaInfos[i].emoji);
|
||||
}
|
||||
|
||||
return MessageActivityResponse(
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ import 'package:flutter/material.dart';
|
|||
|
||||
import 'package:collection/collection.dart';
|
||||
|
||||
import 'package:fluffychat/pangea/common/utils/error_handler.dart';
|
||||
import 'package:fluffychat/pangea/constructs/construct_form.dart';
|
||||
import 'package:fluffychat/pangea/practice_activities/practice_choice.dart';
|
||||
|
||||
|
|
@ -18,52 +17,35 @@ class PracticeMatchActivity {
|
|||
}) {
|
||||
for (final ith in matchInfo.entries) {
|
||||
debugPrint(
|
||||
'Construct: ${ith.key}, Forms: ${ith.value}',
|
||||
'Construct: ${ith.key.form}, Forms: ${ith.value}',
|
||||
);
|
||||
}
|
||||
// for all the entries in matchInfo, remove an Strings that appear in multiple entries
|
||||
final Map<String, int> allForms = {};
|
||||
for (final ith in matchInfo.entries) {
|
||||
for (final form in ith.value) {
|
||||
if (allForms.containsKey(form)) {
|
||||
allForms[form] = allForms[form]! + 1;
|
||||
} else {
|
||||
allForms[form] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (final ith in matchInfo.entries) {
|
||||
if (ith.value.isEmpty) {
|
||||
matchInfo.remove(ith.key);
|
||||
continue;
|
||||
final List<String> usedForms = [];
|
||||
for (final matchEntry in matchInfo.entries) {
|
||||
if (matchEntry.value.isEmpty) {
|
||||
throw Exception(
|
||||
"No forms available for construct ${matchEntry.key}",
|
||||
);
|
||||
}
|
||||
choices.add(
|
||||
PracticeChoice(
|
||||
choiceContent: ith.value.firstWhere(
|
||||
(element) => allForms[element] == 1,
|
||||
orElse: () {
|
||||
ErrorHandler.logError(
|
||||
m: "no unique emoji for construct",
|
||||
data: {
|
||||
'construct': ith.key,
|
||||
'forms': ith.value,
|
||||
"practice_match": toJson(),
|
||||
},
|
||||
);
|
||||
final String first = ith.value.first;
|
||||
// remove the element from the other entry to avoid duplicates
|
||||
for (final ith in matchInfo.entries) {
|
||||
ith.value.removeWhere((choice) => choice == first);
|
||||
}
|
||||
return ith.value.first;
|
||||
},
|
||||
),
|
||||
form: ith.key,
|
||||
|
||||
final String choiceContent = matchEntry.value.firstWhere(
|
||||
(element) => !usedForms.contains(element),
|
||||
orElse: () => throw Exception(
|
||||
"No unique form available for construct ${matchEntry.key}",
|
||||
),
|
||||
);
|
||||
|
||||
choices.add(
|
||||
PracticeChoice(
|
||||
choiceContent: choiceContent,
|
||||
form: matchEntry.key,
|
||||
),
|
||||
);
|
||||
|
||||
usedForms.add(choiceContent);
|
||||
debugPrint(
|
||||
'Added PracticeChoice Construct: ${ith.key}, Forms: ${ith.value}',
|
||||
'Added PracticeChoice Construct: ${matchEntry.key}, Forms: ${matchEntry.value}',
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -173,7 +173,7 @@ class MessageSelectionPositionerState extends State<MessageSelectionPositioner>
|
|||
);
|
||||
|
||||
double get columnWidth => FluffyThemes.isColumnMode(context)
|
||||
? (FluffyThemes.columnWidth + FluffyThemes.navRailWidth + 1.0)
|
||||
? (FluffyThemes.columnWidth + FluffyThemes.navRailWidth + 2.0)
|
||||
: 0;
|
||||
|
||||
double get _toolbarMaxWidth {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue