fix: improve transfer recognition and local data safety
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
@@ -28,6 +29,13 @@ class LocalDatabase {
|
||||
String? _namespace;
|
||||
|
||||
String? get namespace => _namespace;
|
||||
|
||||
Future<bool> namespaceExists(String namespace) async {
|
||||
final safeNamespace = namespace.replaceAll(RegExp(r'[^a-zA-Z0-9_-]'), '_');
|
||||
final directory = await getApplicationSupportDirectory();
|
||||
return File(p.join(directory.path, 'jizhi_$safeNamespace.db')).exists();
|
||||
}
|
||||
|
||||
Database get _db {
|
||||
final database = _database;
|
||||
if (database == null) throw StateError('本地数据库尚未初始化');
|
||||
@@ -766,9 +774,16 @@ class LocalDatabase {
|
||||
|
||||
void replaceLocalTransaction(int localId, Map<String, dynamic> remote) {
|
||||
final remoteId = (remote['id'] as num).toInt();
|
||||
_db.execute('DELETE FROM transactions WHERE id = ?', [localId]);
|
||||
_saveIdMap('transaction', localId, remoteId);
|
||||
cacheTransaction(remote);
|
||||
_db.execute('BEGIN');
|
||||
try {
|
||||
cacheTransaction(remote);
|
||||
_saveIdMap('transaction', localId, remoteId);
|
||||
_db.execute('DELETE FROM transactions WHERE id = ?', [localId]);
|
||||
_db.execute('COMMIT');
|
||||
} catch (_) {
|
||||
_db.execute('ROLLBACK');
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
void enqueueSync(
|
||||
|
||||
Reference in New Issue
Block a user