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