mirror of
https://github.com/zjs81/meshcore-open.git
synced 2026-06-22 02:14:28 +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>
86 lines
3.2 KiB
Dart
86 lines
3.2 KiB
Dart
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../models/message.dart';
|
|
|
|
class MessageStore {
|
|
static const String _keyPrefix = 'messages_';
|
|
|
|
Future<void> saveMessages(String contactKeyHex, List<Message> messages) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final key = '$_keyPrefix$contactKeyHex';
|
|
final jsonList = messages.map(_messageToJson).toList();
|
|
await prefs.setString(key, jsonEncode(jsonList));
|
|
}
|
|
|
|
Future<List<Message>> loadMessages(String contactKeyHex) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final key = '$_keyPrefix$contactKeyHex';
|
|
final jsonString = prefs.getString(key);
|
|
if (jsonString == null) return [];
|
|
|
|
try {
|
|
final jsonList = jsonDecode(jsonString) as List<dynamic>;
|
|
return jsonList.map((json) => _messageFromJson(json)).toList();
|
|
} catch (e) {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future<void> clearMessages(String contactKeyHex) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final key = '$_keyPrefix$contactKeyHex';
|
|
await prefs.remove(key);
|
|
}
|
|
|
|
Map<String, dynamic> _messageToJson(Message msg) {
|
|
return {
|
|
'senderKey': base64Encode(msg.senderKey),
|
|
'text': msg.text,
|
|
'timestamp': msg.timestamp.millisecondsSinceEpoch,
|
|
'isOutgoing': msg.isOutgoing,
|
|
'isCli': msg.isCli,
|
|
'status': msg.status.index,
|
|
'messageId': msg.messageId,
|
|
'retryCount': msg.retryCount,
|
|
'estimatedTimeoutMs': msg.estimatedTimeoutMs,
|
|
'expectedAckHash': msg.expectedAckHash != null ? base64Encode(msg.expectedAckHash!) : null,
|
|
'sentAt': msg.sentAt?.millisecondsSinceEpoch,
|
|
'deliveredAt': msg.deliveredAt?.millisecondsSinceEpoch,
|
|
'tripTimeMs': msg.tripTimeMs,
|
|
'forceFlood': msg.forceFlood,
|
|
'pathLength': msg.pathLength,
|
|
'pathBytes': msg.pathBytes.isNotEmpty ? base64Encode(msg.pathBytes) : null,
|
|
};
|
|
}
|
|
|
|
Message _messageFromJson(Map<String, dynamic> json) {
|
|
return Message(
|
|
senderKey: Uint8List.fromList(base64Decode(json['senderKey'] as String)),
|
|
text: json['text'] as String,
|
|
timestamp: DateTime.fromMillisecondsSinceEpoch(json['timestamp'] as int),
|
|
isOutgoing: json['isOutgoing'] as bool,
|
|
isCli: json['isCli'] as bool? ?? false,
|
|
status: MessageStatus.values[json['status'] as int],
|
|
messageId: json['messageId'] as String?,
|
|
retryCount: json['retryCount'] as int? ?? 0,
|
|
estimatedTimeoutMs: json['estimatedTimeoutMs'] as int?,
|
|
expectedAckHash: json['expectedAckHash'] != null
|
|
? Uint8List.fromList(base64Decode(json['expectedAckHash'] as String))
|
|
: null,
|
|
sentAt: json['sentAt'] != null
|
|
? DateTime.fromMillisecondsSinceEpoch(json['sentAt'] as int)
|
|
: null,
|
|
deliveredAt: json['deliveredAt'] != null
|
|
? DateTime.fromMillisecondsSinceEpoch(json['deliveredAt'] as int)
|
|
: null,
|
|
tripTimeMs: json['tripTimeMs'] as int?,
|
|
forceFlood: json['forceFlood'] as bool? ?? false,
|
|
pathLength: json['pathLength'] as int?,
|
|
pathBytes: json['pathBytes'] != null
|
|
? Uint8List.fromList(base64Decode(json['pathBytes'] as String))
|
|
: Uint8List(0),
|
|
);
|
|
}
|
|
}
|