mirror of
https://github.com/foss42/apidash.git
synced 2025-07-02 05:57:55 +08:00
sharedprefs to hive
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
import 'dart:convert';
|
||||
import 'package:apidash/services/services.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../services/services.dart';
|
||||
|
||||
final chatMessagesProvider =
|
||||
@ -17,19 +17,16 @@ class ChatMessagesNotifier extends StateNotifier<List<Map<String, dynamic>>> {
|
||||
_loadMessages();
|
||||
}
|
||||
|
||||
static const _storageKey = 'chatMessages';
|
||||
|
||||
Future<void> _loadMessages() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final messages = prefs.getString(_storageKey);
|
||||
final messages = await hiveHandler.getDashbotMessages();
|
||||
if (messages != null) {
|
||||
state = List<Map<String, dynamic>>.from(json.decode(messages));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _saveMessages() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
await prefs.setString(_storageKey, json.encode(state));
|
||||
final messages = json.encode(state);
|
||||
await hiveHandler.saveDashbotMessages(messages);
|
||||
}
|
||||
|
||||
void addMessage(Map<String, dynamic> message) {
|
||||
|
@ -147,11 +147,16 @@ class HiveHandler {
|
||||
Future<dynamic> getHistoryRequest(String id) async =>
|
||||
await historyLazyBox.get(id);
|
||||
Future<void> setHistoryRequest(
|
||||
String id, Map<String, dynamic>? historyRequestJsoon) =>
|
||||
historyLazyBox.put(id, historyRequestJsoon);
|
||||
String id, Map<String, dynamic>? historyRequestJson) =>
|
||||
historyLazyBox.put(id, historyRequestJson);
|
||||
|
||||
Future<void> deleteHistoryRequest(String id) => historyLazyBox.delete(id);
|
||||
|
||||
Future<dynamic> getDashbotMessages() async =>
|
||||
await dashBotBox.get(kKeyDashBotBoxIds);
|
||||
Future<void> saveDashbotMessages(String messages) =>
|
||||
dashBotBox.put(kKeyDashBotBoxIds, messages);
|
||||
|
||||
Future clearAllHistory() async {
|
||||
await historyMetaBox.clear();
|
||||
await historyLazyBox.clear();
|
||||
|
Reference in New Issue
Block a user