mirror of
https://github.com/zjs81/meshcore-open.git
synced 2026-04-20 22:13:48 +00:00
Open-source Flutter client for MeshCore LoRa mesh networking devices. Features: - BLE device scanning and connection - Nordic UART Service (NUS) integration - Material 3 design with system theme support - Provider-based state management - Placeholder screens for chat, contacts, and settings 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
120 lines
4 KiB
Dart
120 lines
4 KiB
Dart
import 'dart:convert';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../models/path_history.dart';
|
|
|
|
class StorageService {
|
|
static const String _pathHistoryPrefix = 'path_history_';
|
|
static const String _pendingMessagesKey = 'pending_messages';
|
|
static const String _repeaterPasswordsKey = 'repeater_passwords';
|
|
|
|
Future<void> savePathHistory(
|
|
String contactPubKeyHex, ContactPathHistory history) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final key = '$_pathHistoryPrefix$contactPubKeyHex';
|
|
final jsonStr = jsonEncode(history.toJson());
|
|
await prefs.setString(key, jsonStr);
|
|
}
|
|
|
|
Future<ContactPathHistory?> loadPathHistory(String contactPubKeyHex) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final key = '$_pathHistoryPrefix$contactPubKeyHex';
|
|
final jsonStr = prefs.getString(key);
|
|
|
|
if (jsonStr == null) return null;
|
|
|
|
try {
|
|
final json = jsonDecode(jsonStr) as Map<String, dynamic>;
|
|
return ContactPathHistory.fromJson(contactPubKeyHex, json);
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
Future<void> clearPathHistory(String contactPubKeyHex) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final key = '$_pathHistoryPrefix$contactPubKeyHex';
|
|
await prefs.remove(key);
|
|
}
|
|
|
|
Future<void> clearAllPathHistories() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final keys = prefs.getKeys();
|
|
final pathHistoryKeys =
|
|
keys.where((key) => key.startsWith(_pathHistoryPrefix));
|
|
|
|
for (final key in pathHistoryKeys) {
|
|
await prefs.remove(key);
|
|
}
|
|
}
|
|
|
|
Future<Map<String, String>> loadPendingMessages() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final jsonStr = prefs.getString(_pendingMessagesKey);
|
|
|
|
if (jsonStr == null) return {};
|
|
|
|
try {
|
|
final json = jsonDecode(jsonStr) as Map<String, dynamic>;
|
|
return json.map((key, value) => MapEntry(key, value as String));
|
|
} catch (e) {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
Future<void> savePendingMessages(Map<String, String> pending) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final jsonStr = jsonEncode(pending);
|
|
await prefs.setString(_pendingMessagesKey, jsonStr);
|
|
}
|
|
|
|
Future<void> clearPendingMessages() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.remove(_pendingMessagesKey);
|
|
}
|
|
|
|
/// Save a repeater password by public key hex
|
|
Future<void> saveRepeaterPassword(
|
|
String repeaterPubKeyHex, String password) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final passwords = await loadRepeaterPasswords();
|
|
passwords[repeaterPubKeyHex] = password;
|
|
final jsonStr = jsonEncode(passwords);
|
|
await prefs.setString(_repeaterPasswordsKey, jsonStr);
|
|
}
|
|
|
|
/// Load all saved repeater passwords (map of pubKeyHex -> password)
|
|
Future<Map<String, String>> loadRepeaterPasswords() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final jsonStr = prefs.getString(_repeaterPasswordsKey);
|
|
|
|
if (jsonStr == null) return {};
|
|
|
|
try {
|
|
final json = jsonDecode(jsonStr) as Map<String, dynamic>;
|
|
return json.map((key, value) => MapEntry(key, value as String));
|
|
} catch (e) {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
/// Get a specific repeater's saved password
|
|
Future<String?> getRepeaterPassword(String repeaterPubKeyHex) async {
|
|
final passwords = await loadRepeaterPasswords();
|
|
return passwords[repeaterPubKeyHex];
|
|
}
|
|
|
|
/// Remove a saved repeater password
|
|
Future<void> removeRepeaterPassword(String repeaterPubKeyHex) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final passwords = await loadRepeaterPasswords();
|
|
passwords.remove(repeaterPubKeyHex);
|
|
final jsonStr = jsonEncode(passwords);
|
|
await prefs.setString(_repeaterPasswordsKey, jsonStr);
|
|
}
|
|
|
|
/// Clear all saved repeater passwords
|
|
Future<void> clearAllRepeaterPasswords() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.remove(_repeaterPasswordsKey);
|
|
}
|
|
}
|