From bed7ad347c4fd77e13b2e11e69b6b47e421647b7 Mon Sep 17 00:00:00 2001 From: DenserMeerkat Date: Mon, 22 Jul 2024 21:19:33 +0530 Subject: [PATCH] feat: auto clear history --- lib/main.dart | 1 + lib/services/history_service.dart | 51 +++++++++++++++++++++++++++ lib/services/hive_services.dart | 2 +- lib/services/services.dart | 1 + lib/utils/history_utils.dart | 16 +++++++++ test/providers/ui_providers_test.dart | 3 ++ 6 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 lib/services/history_service.dart diff --git a/lib/main.dart b/lib/main.dart index 5ac878aa..d9730a03 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -9,6 +9,7 @@ void main() async { WidgetsFlutterBinding.ensureInitialized(); GoogleFonts.config.allowRuntimeFetching = false; await openBoxes(); + await autoClearHistory(); if (kIsLinux) { await setupInitialWindow(); } diff --git a/lib/services/history_service.dart b/lib/services/history_service.dart new file mode 100644 index 00000000..d34d17fb --- /dev/null +++ b/lib/services/history_service.dart @@ -0,0 +1,51 @@ +import 'package:apidash/models/models.dart'; +import 'package:apidash/utils/utils.dart'; +import 'package:apidash/consts.dart'; +import 'hive_services.dart'; + +Future autoClearHistory() async { + final settingsMap = hiveHandler.settings; + final retentionPeriod = settingsMap['historyRetentionPeriod']; + + HistoryRetentionPeriod historyRetentionPeriod = + HistoryRetentionPeriod.oneWeek; + if (retentionPeriod != null) { + historyRetentionPeriod = + HistoryRetentionPeriod.values.byName(retentionPeriod); + } + DateTime? retentionDate = getRetentionDate(historyRetentionPeriod); + + if (retentionDate == null) { + return; + } else { + List? historyIds = hiveHandler.getHistoryIds(); + List toRemoveIds = []; + + if (historyIds == null || historyIds.isEmpty) { + return; + } + + for (var historyId in historyIds) { + var jsonModel = hiveHandler.getHistoryMeta(historyId); + if (jsonModel != null) { + var jsonMap = Map.from(jsonModel); + HistoryMetaModel historyMetaModelFromJson = + HistoryMetaModel.fromJson(jsonMap); + if (historyMetaModelFromJson.timeStamp.isBefore(retentionDate)) { + toRemoveIds.add(historyId); + } + } + } + + if (toRemoveIds.isEmpty) { + return; + } + + for (var id in toRemoveIds) { + await hiveHandler.deleteHistoryRequest(id); + hiveHandler.deleteHistoryMeta(id); + } + hiveHandler.setHistoryIds( + historyIds..removeWhere((id) => toRemoveIds.contains(id))); + } +} diff --git a/lib/services/hive_services.dart b/lib/services/hive_services.dart index c6a3cc40..1a0f7ac0 100644 --- a/lib/services/hive_services.dart +++ b/lib/services/hive_services.dart @@ -97,7 +97,7 @@ class HiveHandler { String id, Map? historyRequestJsoon) => historyLazyBox.put(id, historyRequestJsoon); - Future deleteHistoryReqyest(String id) => historyLazyBox.delete(id); + Future deleteHistoryRequest(String id) => historyLazyBox.delete(id); Future clear() async { await dataBox.clear(); diff --git a/lib/services/services.dart b/lib/services/services.dart index a7cf03fd..7551de9b 100644 --- a/lib/services/services.dart +++ b/lib/services/services.dart @@ -1,3 +1,4 @@ export 'http_service.dart'; export 'hive_services.dart'; +export 'history_service.dart'; export 'window_services.dart'; diff --git a/lib/utils/history_utils.dart b/lib/utils/history_utils.dart index acbea853..c91a5e68 100644 --- a/lib/utils/history_utils.dart +++ b/lib/utils/history_utils.dart @@ -113,3 +113,19 @@ List getRequestGroup( requestGroup.sort((a, b) => b.timeStamp.compareTo(a.timeStamp)); return requestGroup; } + +DateTime? getRetentionDate(HistoryRetentionPeriod retentionPeriod) { + DateTime now = DateTime.now(); + DateTime today = DateTime(now.year, now.month, now.day); + + switch (retentionPeriod) { + case HistoryRetentionPeriod.oneWeek: + return today.subtract(const Duration(days: 7)); + case HistoryRetentionPeriod.oneMonth: + return today.subtract(const Duration(days: 30)); + case HistoryRetentionPeriod.threeMonths: + return today.subtract(const Duration(days: 90)); + default: + return null; + } +} diff --git a/test/providers/ui_providers_test.dart b/test/providers/ui_providers_test.dart index 5854f768..1bd76c4d 100644 --- a/test/providers/ui_providers_test.dart +++ b/test/providers/ui_providers_test.dart @@ -1,4 +1,7 @@ import 'dart:io'; + +import 'package:spot/spot.dart'; +import 'package:apidash/consts.dart'; import 'package:apidash/providers/providers.dart'; import 'package:apidash/screens/common_widgets/common_widgets.dart'; import 'package:apidash/screens/dashboard.dart';