fix: remove clicked new token from new tokens cache immeadiatley instead of waiting for new token animation to finish (#4952)

This commit is contained in:
ggurdin 2025-12-29 11:18:01 -05:00 committed by GitHub
parent ff90fc2947
commit eb3fd515fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 42 additions and 26 deletions

View file

@ -216,28 +216,35 @@ class MessageOverlayController extends State<MessageSelectionOverlay>
);
}
if (mounted) {
setState(() {});
if (selectedToken != null && isNewToken(selectedToken!)) {
final token = selectedToken!;
final constructs = [
OneConstructUse(
useType: ConstructUseTypeEnum.click,
lemma: token.lemma.text,
constructType: ConstructTypeEnum.vocab,
metadata: ConstructUseMetaData(
roomId: event.room.id,
timeStamp: DateTime.now(),
eventId: event.eventId,
),
category: token.pos,
form: token.text.content,
xp: ConstructUseTypeEnum.click.pointValue,
if (!mounted) return;
if (selectedToken != null && isNewToken(selectedToken!)) {
TokensUtil.collectToken(event.eventId, selectedToken!.text);
final token = selectedToken!;
final constructs = [
OneConstructUse(
useType: ConstructUseTypeEnum.click,
lemma: token.lemma.text,
constructType: ConstructTypeEnum.vocab,
metadata: ConstructUseMetaData(
roomId: event.room.id,
timeStamp: DateTime.now(),
eventId: event.eventId,
),
];
addAnalytics(constructs, "word-zoom-card-${token.text.uniqueKey}");
}
category: token.pos,
form: token.text.content,
xp: ConstructUseTypeEnum.click.pointValue,
),
];
addAnalytics(constructs, "word-zoom-card-${token.text.uniqueKey}")
.then((_) {
TokensUtil.clearNewTokenCache();
if (mounted) setState(() {});
});
return;
}
setState(() {});
}
PangeaMessageEvent get pangeaMessageEvent => PangeaMessageEvent(

View file

@ -74,7 +74,7 @@ class _NewWordOverlayState extends State<NewWordOverlay>
WidgetsBinding.instance.addPostFrameCallback((_) {
_showFlyingWidget();
_controller?.forward().then((_) {
TokensUtil.clearNewTokenCache();
TokensUtil.clearRecentlyCollected();
widget.onDismiss?.call();
});
});

View file

@ -47,6 +47,7 @@ class TokensUtil {
/// A cache of calculated adjacent token positions
static final Map<String, _TokenPositionCacheItem> _tokenPositionCache = {};
static final Map<String, _NewTokenCacheItem> _newTokenCache = {};
static PangeaTokenText? _lastCollected;
static const Duration _cacheDuration = Duration(minutes: 1);
@ -124,10 +125,20 @@ class TokensUtil {
return newTokens.any((t) => t == token.text);
}
static clearNewTokenCache() {
static void clearNewTokenCache() {
_newTokenCache.clear();
}
static void collectToken(String eventId, PangeaTokenText token) {
_newTokenCache[eventId]?.tokens.remove(token);
_lastCollected = token;
}
static bool isRecentlyCollected(PangeaTokenText token) =>
_lastCollected == token;
static void clearRecentlyCollected() => _lastCollected = null;
static List<TokenPosition>? _getCachedTokenPositions(String eventID) {
final cacheItem = _tokenPositionCache[eventID];
if (cacheItem == null) return null;

View file

@ -47,7 +47,6 @@ class ReadingAssistanceContent extends StatelessWidget {
token: overlayController.selectedToken!.text,
construct: overlayController.selectedToken!.vocabConstructID,
event: overlayController.event,
wordIsNew: overlayController.isNewToken(overlayController.selectedToken!),
onClose: () => overlayController.updateSelectedSpan(null),
langCode: overlayController.pangeaMessageEvent.messageDisplayLangCode,
onDismissNewWordOverlay: () => overlayController.setState(() {}),

View file

@ -11,6 +11,7 @@ import 'package:fluffychat/pangea/languages/p_language_store.dart';
import 'package:fluffychat/pangea/lemmas/lemma_info_response.dart';
import 'package:fluffychat/pangea/phonetic_transcription/phonetic_transcription_widget.dart';
import 'package:fluffychat/pangea/toolbar/reading_assistance/new_word_overlay.dart';
import 'package:fluffychat/pangea/toolbar/reading_assistance/tokens_util.dart';
import 'package:fluffychat/pangea/toolbar/word_card/lemma_meaning_display.dart';
import 'package:fluffychat/pangea/toolbar/word_card/lemma_reaction_picker.dart';
import 'package:fluffychat/pangea/toolbar/word_card/message_unsubscribed_card.dart';
@ -24,7 +25,6 @@ class WordZoomWidget extends StatelessWidget {
final String langCode;
final VoidCallback? onClose;
final bool wordIsNew;
final Event? event;
final VoidCallback? onDismissNewWordOverlay;
@ -36,7 +36,6 @@ class WordZoomWidget extends StatelessWidget {
required this.construct,
required this.langCode,
this.onClose,
this.wordIsNew = false,
this.event,
this.onDismissNewWordOverlay,
this.onFlagTokenInfo,
@ -164,7 +163,7 @@ class WordZoomWidget extends StatelessWidget {
),
),
),
wordIsNew
TokensUtil.isRecentlyCollected(token)
? NewWordOverlay(
key: ValueKey(transformTargetId),
overlayColor: overlayColor,