From 702b0d119cb923c80317116b6e6a356ff1a802d6 Mon Sep 17 00:00:00 2001 From: Ashita Prasad Date: Mon, 23 Jun 2025 03:04:24 +0530 Subject: [PATCH] sharedprefs to hive --- lib/dashbot/providers/dashbot_providers.dart | 11 ++++------- lib/services/hive_services.dart | 9 +++++++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/dashbot/providers/dashbot_providers.dart b/lib/dashbot/providers/dashbot_providers.dart index 16616bfd..e49c1c1e 100644 --- a/lib/dashbot/providers/dashbot_providers.dart +++ b/lib/dashbot/providers/dashbot_providers.dart @@ -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>> { _loadMessages(); } - static const _storageKey = 'chatMessages'; - Future _loadMessages() async { - final prefs = await SharedPreferences.getInstance(); - final messages = prefs.getString(_storageKey); + final messages = await hiveHandler.getDashbotMessages(); if (messages != null) { state = List>.from(json.decode(messages)); } } Future _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 message) { diff --git a/lib/services/hive_services.dart b/lib/services/hive_services.dart index c2554722..dbfc66aa 100644 --- a/lib/services/hive_services.dart +++ b/lib/services/hive_services.dart @@ -147,11 +147,16 @@ class HiveHandler { Future getHistoryRequest(String id) async => await historyLazyBox.get(id); Future setHistoryRequest( - String id, Map? historyRequestJsoon) => - historyLazyBox.put(id, historyRequestJsoon); + String id, Map? historyRequestJson) => + historyLazyBox.put(id, historyRequestJson); Future deleteHistoryRequest(String id) => historyLazyBox.delete(id); + Future getDashbotMessages() async => + await dashBotBox.get(kKeyDashBotBoxIds); + Future saveDashbotMessages(String messages) => + dashBotBox.put(kKeyDashBotBoxIds, messages); + Future clearAllHistory() async { await historyMetaBox.clear(); await historyLazyBox.clear();