|
|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
import 'dart:async';
|
|
|
|
|
import 'dart:collection';
|
|
|
|
|
import 'dart:developer';
|
|
|
|
|
|
|
|
|
|
import 'package:fluffychat/pangea/constants/local.key.dart';
|
|
|
|
|
import 'package:fluffychat/pangea/constants/pangea_event_types.dart';
|
|
|
|
|
import 'package:fluffychat/pangea/controllers/pangea_controller.dart';
|
|
|
|
|
import 'package:fluffychat/pangea/extensions/pangea_room_extension/pangea_room_extension.dart';
|
|
|
|
|
@ -21,6 +23,7 @@ class _RecordCacheItem {
|
|
|
|
|
|
|
|
|
|
/// Controller for handling activity completions.
|
|
|
|
|
class PracticeActivityRecordController {
|
|
|
|
|
static const int maxStoredEvents = 100;
|
|
|
|
|
static final Map<int, _RecordCacheItem> _cache = {};
|
|
|
|
|
late final PangeaController _pangeaController;
|
|
|
|
|
Timer? _cacheClearTimer;
|
|
|
|
|
@ -29,6 +32,49 @@ class PracticeActivityRecordController {
|
|
|
|
|
_initializeCacheClearing();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LinkedHashMap<String, int> get completedActivities {
|
|
|
|
|
try {
|
|
|
|
|
final dynamic locallySaved = _pangeaController.pStoreService.read(
|
|
|
|
|
PLocalKey.completedActivities,
|
|
|
|
|
);
|
|
|
|
|
if (locallySaved == null) return LinkedHashMap<String, int>();
|
|
|
|
|
try {
|
|
|
|
|
final LinkedHashMap<String, int> cache =
|
|
|
|
|
LinkedHashMap<String, int>.from(locallySaved);
|
|
|
|
|
return cache;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
_pangeaController.pStoreService.delete(
|
|
|
|
|
PLocalKey.completedActivities,
|
|
|
|
|
);
|
|
|
|
|
return LinkedHashMap<String, int>();
|
|
|
|
|
}
|
|
|
|
|
} catch (exception, stackTrace) {
|
|
|
|
|
ErrorHandler.logError(
|
|
|
|
|
e: PangeaWarningError(
|
|
|
|
|
"Failed to get completed activities from cache: $exception",
|
|
|
|
|
),
|
|
|
|
|
s: stackTrace,
|
|
|
|
|
m: 'Failed to get completed activities from cache',
|
|
|
|
|
);
|
|
|
|
|
return LinkedHashMap<String, int>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Future<void> completeActivity(String messageID) async {
|
|
|
|
|
final LinkedHashMap<String, int> currentCache = completedActivities;
|
|
|
|
|
final numCompleted = currentCache[messageID] ?? 0;
|
|
|
|
|
currentCache[messageID] = numCompleted + 1;
|
|
|
|
|
|
|
|
|
|
if (currentCache.length > maxStoredEvents) {
|
|
|
|
|
currentCache.remove(currentCache.keys.first);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await _pangeaController.pStoreService.save(
|
|
|
|
|
PLocalKey.completedActivities,
|
|
|
|
|
currentCache,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _initializeCacheClearing() {
|
|
|
|
|
const duration = Duration(minutes: 2);
|
|
|
|
|
_cacheClearTimer = Timer.periodic(duration, (Timer t) => _clearCache());
|
|
|
|
|
|