make word meaning activity refetch new definition if cache expires (#1779)

* make word meaning activity refetch new definition if cache expires

* generated

* bake expire at into response json

* generated

* add expire at in to and from json method

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: ggurdin <46800240+ggurdin@users.noreply.github.com>
pull/1688/head
Wilson 9 months ago committed by GitHub
parent 91ab045365
commit 6af450efb2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,39 +1,50 @@
import 'dart:convert';
import 'dart:developer';
import 'package:flutter/foundation.dart';
import 'package:get_storage/get_storage.dart';
import 'package:http/http.dart';
import 'package:fluffychat/pangea/common/config/environment.dart';
import 'package:fluffychat/pangea/common/network/requests.dart';
import 'package:fluffychat/pangea/common/network/urls.dart';
import 'package:fluffychat/pangea/common/utils/error_handler.dart';
import 'package:fluffychat/pangea/events/models/content_feedback.dart';
import 'package:fluffychat/pangea/lemmas/lemma_info_request.dart';
import 'package:fluffychat/pangea/lemmas/lemma_info_response.dart';
import 'package:fluffychat/widgets/matrix.dart';
import '../common/config/environment.dart';
import '../common/network/requests.dart';
class LemmaInfoRepo {
static final GetStorage _lemmaStorage = GetStorage('lemma_storage');
static void set(LemmaInfoRequest request, LemmaInfoResponse response) {
// set expireAt if not set
response.expireAt ??= DateTime.now().add(const Duration(minutes: 1));
_lemmaStorage.write(request.storageKey, response.toJson());
}
static Future<LemmaInfoResponse> get(
LemmaInfoRequest request, [
String? feedback,
bool useExpireAt = false,
]) async {
final cachedJson = _lemmaStorage.read(request.storageKey);
if (cachedJson != null) {
final cached = LemmaInfoResponse.fromJson(cachedJson);
final cached =
cachedJson == null ? null : LemmaInfoResponse.fromJson(cachedJson);
if (cached != null) {
if (feedback == null) {
// in this case, we just return the cached response
return cached;
// at this point we have a cache without feedback
if (!useExpireAt) {
// return cache as is if we're not using expireAt
return cached;
} else if (cached.expireAt != null) {
if (DateTime.now().isBefore(cached.expireAt!)) {
// return cache as is if we're using expireAt and it's set but not expired
return cached;
}
}
// we intentionally do not handle the case of expired at not set because
// old caches won't have them set, and we want to trigger a new
// choreo call
} else {
// we're adding this within the service to avoid needing to have the widgets
// save state including the bad response
@ -42,16 +53,6 @@ class LemmaInfoRepo {
feedback,
);
}
} else if (feedback != null) {
// the cache should have the request in order for the user to provide feedback
// this would be a strange situation and indicate some error in our logic
debugger(when: kDebugMode);
ErrorHandler.logError(
m: 'Feedback provided for a non-cached request',
data: request.toJson(),
);
} else {
debugPrint('No cached response for lemma ${request.lemma}, calling API');
}
final Requests req = Requests(

@ -3,16 +3,21 @@ import 'package:fluffychat/pangea/events/models/content_feedback.dart';
class LemmaInfoResponse implements JsonSerializable {
final List<String> emoji;
final String meaning;
DateTime? expireAt;
LemmaInfoResponse({
required this.emoji,
required this.meaning,
this.expireAt,
});
factory LemmaInfoResponse.fromJson(Map<String, dynamic> json) {
return LemmaInfoResponse(
emoji: (json['emoji'] as List<dynamic>).map((e) => e as String).toList(),
meaning: json['meaning'] as String,
expireAt: json['expireAt'] == null
? null
: DateTime.parse(json['expireAt'] as String),
);
}
@ -21,6 +26,7 @@ class LemmaInfoResponse implements JsonSerializable {
return {
'emoji': emoji,
'meaning': meaning,
'expireAt': expireAt?.toIso8601String(),
};
}

@ -38,7 +38,7 @@ class LemmaMeaningActivityGenerator {
userL1: req.userL1,
);
final res = await LemmaInfoRepo.get(lemmaDefReq);
final res = await LemmaInfoRepo.get(lemmaDefReq, null, true);
final choices = await getDistractorMeanings(lemmaDefReq, 3);

Loading…
Cancel
Save