mirror of
https://github.com/foss42/apidash.git
synced 2025-05-30 21:41:50 +08:00
feat: auto clear history
This commit is contained in:
@ -9,6 +9,7 @@ void main() async {
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
GoogleFonts.config.allowRuntimeFetching = false;
|
||||
await openBoxes();
|
||||
await autoClearHistory();
|
||||
if (kIsLinux) {
|
||||
await setupInitialWindow();
|
||||
}
|
||||
|
51
lib/services/history_service.dart
Normal file
51
lib/services/history_service.dart
Normal file
@ -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<void> 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<String>? historyIds = hiveHandler.getHistoryIds();
|
||||
List<String> toRemoveIds = [];
|
||||
|
||||
if (historyIds == null || historyIds.isEmpty) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (var historyId in historyIds) {
|
||||
var jsonModel = hiveHandler.getHistoryMeta(historyId);
|
||||
if (jsonModel != null) {
|
||||
var jsonMap = Map<String, Object?>.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)));
|
||||
}
|
||||
}
|
@ -97,7 +97,7 @@ class HiveHandler {
|
||||
String id, Map<String, dynamic>? historyRequestJsoon) =>
|
||||
historyLazyBox.put(id, historyRequestJsoon);
|
||||
|
||||
Future<void> deleteHistoryReqyest(String id) => historyLazyBox.delete(id);
|
||||
Future<void> deleteHistoryRequest(String id) => historyLazyBox.delete(id);
|
||||
|
||||
Future clear() async {
|
||||
await dataBox.clear();
|
||||
|
@ -1,3 +1,4 @@
|
||||
export 'http_service.dart';
|
||||
export 'hive_services.dart';
|
||||
export 'history_service.dart';
|
||||
export 'window_services.dart';
|
||||
|
@ -113,3 +113,19 @@ List<HistoryMetaModel> 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;
|
||||
}
|
||||
}
|
||||
|
@ -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';
|
||||
|
Reference in New Issue
Block a user