Sync update: 2026-05-18 15:45:06

This commit is contained in:
Hamza-Ayed
2026-05-18 15:45:07 +03:00
parent 30d32df1c0
commit 82650b8c1f
141 changed files with 5255 additions and 24 deletions

View File

@@ -1,5 +1,7 @@
import 'dart:async';
import 'dart:convert';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../services/whatsapp_service.dart';
import '../models/conversation_model.dart';
@@ -18,6 +20,9 @@ class ConversationsController extends GetxController {
void onInit() {
super.onInit();
// Load local cached conversations first for instant UI response
_loadCachedConversations();
// Load conversations initially if already ready
if (_svc.isWaReady.value) {
loadConversations();
@@ -44,9 +49,34 @@ class ConversationsController extends GetxController {
super.onClose();
}
// ── Local Caching ────────────────────────────────────────────────────────
Future<void> _loadCachedConversations() async {
try {
final prefs = await SharedPreferences.getInstance();
final cached = prefs.getString('cached_conversations');
if (cached != null) {
final List<dynamic> decoded = jsonDecode(cached);
conversations.assignAll(decoded.map((c) => ConversationModel.fromJson(c as Map<String, dynamic>)));
print('[CACHE] Loaded ${conversations.length} conversations instantly.');
}
} catch (e) {
print('[CACHE ERROR] $e');
}
}
Future<void> _saveConversationsToCache(List<dynamic> rawData) async {
try {
final prefs = await SharedPreferences.getInstance();
await prefs.setString('cached_conversations', jsonEncode(rawData));
print('[CACHE] Saved ${rawData.length} conversations to local cache.');
} catch (e) {
print('[CACHE SAVE ERROR] $e');
}
}
// ── Load Conversations ───────────────────────────────────────────────────
Future<void> loadConversations() async {
if (!_svc.isWaReady.value) return;
if (!_svc.isWaReady.value || isLoading.value) return;
isLoading.value = true;
errorMessage.value = null;
@@ -56,6 +86,7 @@ class ConversationsController extends GetxController {
if (res['type'] == 'conversations') {
final List<dynamic> data = res['data'] ?? [];
conversations.assignAll(data.map((c) => ConversationModel.fromJson(c as Map<String, dynamic>)));
_saveConversationsToCache(data);
} else {
errorMessage.value = res['message'] ?? 'Failed to load conversations';
}
@@ -159,6 +190,7 @@ class ConversationsController extends GetxController {
if (res['type'] == 'conversations') {
final List<dynamic> data = res['data'] ?? [];
conversations.assignAll(data.map((c) => ConversationModel.fromJson(c as Map<String, dynamic>)));
_saveConversationsToCache(data);
}
} catch (_) {}
}