update to zjs81/dev 9334d55

This commit is contained in:
ericz committed 2026-09-28 20:02:32 +02:00
commit 38eef0970e
175 files changed
+23224 -6470

No files matched your search

+23 -6
View File
@@ -68,13 +68,24 @@ class ChannelMessageStore {
if (jsonString == null || jsonString.isEmpty) {
return [];
}
final List<dynamic> jsonList;
try {
final jsonList = jsonDecode(jsonString) as List<dynamic>;
return jsonList.map((json) => _messageFromJson(json)).toList();
jsonList = jsonDecode(jsonString) as List<dynamic>;
} catch (e) {
// If parsing fails, return empty list
appLogger.warn(
'Stored messages for channel $channelIndex are unreadable: $e',
);
return [];
}
final messages = <ChannelMessage>[];
for (final json in jsonList) {
try {
messages.add(_messageFromJson(json as Map<String, dynamic>));
} catch (e) {
appLogger.warn('Skipping malformed stored channel message: $e');
}
}
return messages;
}
/// Clear messages for a specific channel
@@ -108,6 +119,7 @@ class ChannelMessageStore {
'isOutgoing': msg.isOutgoing,
'status': msg.status.index,
'channelIndex': msg.channelIndex,
'region': msg.region,
'repeatCount': msg.repeatCount,
'pathLength': msg.pathLength,
'pathHashWidth': msg.pathHashWidth,
@@ -163,7 +175,7 @@ class ChannelMessageStore {
senderKey: json['senderKey'] != null
? Uint8List.fromList(base64Decode(json['senderKey']))
: null,
senderName: json['senderName'] as String,
senderName: json['senderName'] as String? ?? 'Unknown',
text: decodedText,
originalText: json['originalText'] as String?,
translatedText: json['translatedText'] as String?,
@@ -173,8 +185,12 @@ class ChannelMessageStore {
),
translationModelId: json['translationModelId'] as String?,
timestamp: DateTime.fromMillisecondsSinceEpoch(json['timestamp'] as int),
isOutgoing: json['isOutgoing'] as bool,
status: ChannelMessageStatus.values[json['status'] as int],
isOutgoing: json['isOutgoing'] as bool? ?? false,
status:
ChannelMessageStatus.values.elementAtOrNull(
json['status'] as int? ?? -1,
) ??
ChannelMessageStatus.failed,
repeatCount: (json['repeatCount'] as int?) ?? 0,
pathLength: decodedPathLength,
pathHashWidth: decodedPathHashWidth,
@@ -188,6 +204,7 @@ class ChannelMessageStore {
.toList() ??
const [],
channelIndex: json['channelIndex'] as int?,
region: json['region'] as String?,
messageId: json['messageId'] as String?,
packetHash: json['packetHash'] as String?,
replyToMessageId: json['replyToMessageId'] as String?,
+12
View File
@@ -10,6 +10,18 @@ class ChannelRegionStore {
String get keyFor => '$_keyPrefix$publicKeyHex';
String get _defaultKey => 'default_region_$publicKeyHex';
String loadDefaultRegion() {
if (publicKeyHex.isEmpty) return '';
return PrefsManager.instance.getString(_defaultKey) ?? '';
}
Future<void> saveDefaultRegion(String region) async {
if (publicKeyHex.isEmpty) return;
await PrefsManager.instance.setString(_defaultKey, region);
}
Future<String> loadRegion(int channelIndex) async {
if (publicKeyHex.isEmpty) {
appLogger.warn(
+12 -5
View File
@@ -45,15 +45,22 @@ class CommunityStore {
return [];
}
final List<dynamic> jsonList;
try {
final jsonList = jsonDecode(jsonString) as List<dynamic>;
return jsonList
.map((json) => Community.fromJson(json as Map<String, dynamic>))
.toList();
jsonList = jsonDecode(jsonString) as List<dynamic>;
} catch (e) {
// If JSON is corrupted, return empty list
appLogger.warn('Stored communities are unreadable: $e');
return [];
}
final communities = <Community>[];
for (final json in jsonList) {
try {
communities.add(Community.fromJson(json as Map<String, dynamic>));
} catch (e) {
appLogger.warn('Skipping malformed stored community: $e');
}
}
return communities;
}
/// Save all communities to storage
+37 -7
View File
@@ -2,30 +2,54 @@ import 'dart:convert';
import 'dart:typed_data';
import '../models/contact.dart';
import '../utils/app_logger.dart';
import 'prefs_manager.dart';
class ContactDiscoveryStore {
static const String _keyPrefix = 'discovered_contacts';
String publicKeyHex = '';
set setPublicKeyHex(String value) =>
publicKeyHex = value.length > 10 ? value.substring(0, 10) : '';
String get keyFor => '$_keyPrefix$publicKeyHex';
Future<List<Contact>> loadContacts() async {
if (publicKeyHex.isEmpty) return [];
final prefs = PrefsManager.instance;
final jsonStr = prefs.getString(_keyPrefix);
var jsonStr = prefs.getString(keyFor);
if (jsonStr == null) {
jsonStr = prefs.getString(_keyPrefix);
if (jsonStr != null) {
await prefs.setString(keyFor, jsonStr);
await prefs.remove(_keyPrefix);
}
}
if (jsonStr == null) return [];
final List<dynamic> jsonList;
try {
final jsonList = jsonDecode(jsonStr) as List<dynamic>;
return jsonList
.map((entry) => _fromJson(entry as Map<String, dynamic>))
.toList();
} catch (_) {
jsonList = jsonDecode(jsonStr) as List<dynamic>;
} catch (e) {
appLogger.warn('Stored discovered contacts are unreadable: $e');
return [];
}
final contacts = <Contact>[];
for (final entry in jsonList) {
try {
contacts.add(_fromJson(entry as Map<String, dynamic>));
} catch (e) {
appLogger.warn('Skipping malformed discovered contact: $e');
}
}
return contacts;
}
Future<void> saveContacts(List<Contact> contacts) async {
if (publicKeyHex.isEmpty) return;
final prefs = PrefsManager.instance;
final jsonList = contacts.map(_toJson).toList();
await prefs.setString(_keyPrefix, jsonEncode(jsonList));
await prefs.setString(keyFor, jsonEncode(jsonList));
}
String exportContactsJson(List<Contact> contacts) {
@@ -85,6 +109,7 @@ class ContactDiscoveryStore {
'flags': contact.flags,
'pathLength': contact.pathLength,
'path': base64Encode(contact.path),
'pathHashWidth': contact.pathHashWidth,
'pathOverride': contact.pathOverride,
'pathOverrideBytes': contact.pathOverrideBytes != null
? base64Encode(contact.pathOverrideBytes!)
@@ -113,6 +138,7 @@ class ContactDiscoveryStore {
int decodedPathLength = rawPathLength;
Uint8List decodedPath = rawPath;
int? decodedPathHashWidth = json['pathHashWidth'] as int?;
if (rawPathLength == 0xFF || rawPathLength < 0) {
decodedPathLength = -1;
@@ -123,6 +149,7 @@ class ContactDiscoveryStore {
final width = mode + 1;
final byteLen = hopCount * width;
decodedPathLength = hopCount;
decodedPathHashWidth = width;
if (byteLen <= rawPath.length) {
decodedPath = rawPath.sublist(0, byteLen);
} else {
@@ -139,6 +166,9 @@ class ContactDiscoveryStore {
flags: json['flags'] as int? ?? 0,
pathLength: decodedPathLength,
path: decodedPath,
pathHashWidth:
decodedPathHashWidth ??
Contact.inferPathHashWidth(decodedPathLength, decodedPath.length),
pathOverride: json['pathOverride'] as int?,
pathOverrideBytes: json['pathOverrideBytes'] != null
? Uint8List.fromList(
+19 -5
View File
@@ -40,14 +40,22 @@ class ContactStore {
return [];
}
final List<dynamic> jsonList;
try {
final jsonList = jsonDecode(jsonString) as List<dynamic>;
return jsonList
.map((entry) => _fromJson(entry as Map<String, dynamic>))
.toList();
} catch (_) {
jsonList = jsonDecode(jsonString) as List<dynamic>;
} catch (e) {
appLogger.warn('Stored contacts are unreadable: $e');
return [];
}
final contacts = <Contact>[];
for (final entry in jsonList) {
try {
contacts.add(_fromJson(entry as Map<String, dynamic>));
} catch (e) {
appLogger.warn('Skipping malformed stored contact: $e');
}
}
return contacts;
}
Future<void> saveContacts(List<Contact> contacts) async {
@@ -68,6 +76,7 @@ class ContactStore {
'flags': contact.flags,
'pathLength': contact.pathLength,
'path': base64Encode(contact.path),
'pathHashWidth': contact.pathHashWidth,
'pathOverride': contact.pathOverride,
'pathOverrideBytes': contact.pathOverrideBytes != null
? base64Encode(contact.pathOverrideBytes!)
@@ -96,6 +105,7 @@ class ContactStore {
int decodedPathLength = rawPathLength;
Uint8List decodedPath = rawPath;
int? decodedPathHashWidth = json['pathHashWidth'] as int?;
if (rawPathLength == 0xFF || rawPathLength < 0) {
decodedPathLength = -1;
@@ -106,6 +116,7 @@ class ContactStore {
final width = mode + 1;
final byteLen = hopCount * width;
decodedPathLength = hopCount;
decodedPathHashWidth = width;
if (byteLen <= rawPath.length) {
decodedPath = rawPath.sublist(0, byteLen);
} else {
@@ -122,6 +133,9 @@ class ContactStore {
flags: json['flags'] as int? ?? 0,
pathLength: decodedPathLength,
path: decodedPath,
pathHashWidth:
decodedPathHashWidth ??
Contact.inferPathHashWidth(decodedPathLength, decodedPath.length),
pathOverride: json['pathOverride'] as int?,
pathOverrideBytes: json['pathOverrideBytes'] != null
? Uint8List.fromList(
+16 -4
View File
@@ -57,12 +57,22 @@ class MessageStore {
return [];
}
final List<dynamic> jsonList;
try {
final jsonList = jsonDecode(jsonString) as List<dynamic>;
return jsonList.map((json) => _messageFromJson(json)).toList();
jsonList = jsonDecode(jsonString) as List<dynamic>;
} catch (e) {
appLogger.warn('Stored messages for $contactKeyHex are unreadable: $e');
return [];
}
final messages = <Message>[];
for (final json in jsonList) {
try {
messages.add(_messageFromJson(json as Map<String, dynamic>));
} catch (e) {
appLogger.warn('Skipping malformed stored message: $e');
}
}
return messages;
}
Future<void> clearMessages(String contactKeyHex) async {
@@ -146,9 +156,11 @@ class MessageStore {
senderKey: Uint8List.fromList(base64Decode(json['senderKey'] as String)),
text: decodedText,
timestamp: DateTime.fromMillisecondsSinceEpoch(json['timestamp'] as int),
isOutgoing: json['isOutgoing'] as bool,
isOutgoing: json['isOutgoing'] as bool? ?? false,
isCli: isCli,
status: MessageStatus.values[json['status'] as int],
status:
MessageStatus.values.elementAtOrNull(json['status'] as int? ?? -1) ??
MessageStatus.failed,
messageId: json['messageId'] as String?,
originalText: json['originalText'] as String?,
translatedText: json['translatedText'] as String?,
+3
View File
@@ -47,6 +47,9 @@ class RegionStore {
await channelRegionStore.saveRegion(channel.index, '');
}
}
if (channelRegionStore.loadDefaultRegion() == region) {
await channelRegionStore.saveDefaultRegion('');
}
regions.remove(region);
saveRegions(regions);
}