sharedprefs to hive

This commit is contained in:
Ashita Prasad
2025-06-23 03:04:24 +05:30
parent 9089179a1a
commit 702b0d119c
2 changed files with 11 additions and 9 deletions

View File

@ -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) {

View File

@ -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();