mirror of
https://github.com/zjs81/meshcore-open.git
synced 2026-06-15 07:04:26 +10:00
e7a5b9e209
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>
58 lines
1.8 KiB
Dart
58 lines
1.8 KiB
Dart
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import '../models/contact.dart';
|
|
|
|
class ContactStore {
|
|
static const String _key = 'contacts';
|
|
|
|
Future<List<Contact>> loadContacts() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final jsonStr = prefs.getString(_key);
|
|
if (jsonStr == null) return [];
|
|
|
|
try {
|
|
final jsonList = jsonDecode(jsonStr) as List<dynamic>;
|
|
return jsonList.map((entry) => _fromJson(entry as Map<String, dynamic>)).toList();
|
|
} catch (_) {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future<void> saveContacts(List<Contact> contacts) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final jsonList = contacts.map(_toJson).toList();
|
|
await prefs.setString(_key, jsonEncode(jsonList));
|
|
}
|
|
|
|
Map<String, dynamic> _toJson(Contact contact) {
|
|
return {
|
|
'publicKey': base64Encode(contact.publicKey),
|
|
'name': contact.name,
|
|
'type': contact.type,
|
|
'pathLength': contact.pathLength,
|
|
'path': base64Encode(contact.path),
|
|
'latitude': contact.latitude,
|
|
'longitude': contact.longitude,
|
|
'lastSeen': contact.lastSeen.millisecondsSinceEpoch,
|
|
};
|
|
}
|
|
|
|
Contact _fromJson(Map<String, dynamic> json) {
|
|
return Contact(
|
|
publicKey: Uint8List.fromList(base64Decode(json['publicKey'] as String)),
|
|
name: json['name'] as String? ?? 'Unknown',
|
|
type: json['type'] as int? ?? 0,
|
|
pathLength: json['pathLength'] as int? ?? -1,
|
|
path: json['path'] != null
|
|
? Uint8List.fromList(base64Decode(json['path'] as String))
|
|
: Uint8List(0),
|
|
latitude: (json['latitude'] as num?)?.toDouble(),
|
|
longitude: (json['longitude'] as num?)?.toDouble(),
|
|
lastSeen: DateTime.fromMillisecondsSinceEpoch(json['lastSeen'] as int? ?? 0),
|
|
);
|
|
}
|
|
}
|