Persist quick send history safely

This commit is contained in:
Misaka
2026-05-17 17:10:01 +08:00
parent 19441399d0
commit 5f6e045e98
2 changed files with 78 additions and 7 deletions

View File

@@ -221,7 +221,7 @@ class _InputSyncPageState extends State<InputSyncPage>
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) {
unawaited(_syncAppendEnterFromPrefs());
unawaited(_syncSharedPrefsFromDisk());
}
}
@@ -239,7 +239,7 @@ class _InputSyncPageState extends State<InputSyncPage>
prefs.getString(_portKey) ?? nativeState.port.toString();
clearAfterSend = prefs.getBool(_clearAfterSendKey) ?? false;
appendEnter = prefs.getBool(_appendEnterKey) ?? false;
sendHistory = prefs.getStringList(_historyKey) ?? [];
sendHistory = _readHistory(prefs);
connection = nativeState;
headerExpanded = !nativeState.connected;
});
@@ -459,17 +459,44 @@ class _InputSyncPageState extends State<InputSyncPage>
});
}
Future<void> _syncAppendEnterFromPrefs() async {
Future<void> _syncSharedPrefsFromDisk() async {
final prefs = await SharedPreferences.getInstance();
await prefs.reload();
final nextAppendEnter = prefs.getBool(_appendEnterKey) ?? false;
if (!mounted || nextAppendEnter == appendEnter) {
final nextHistory = _readHistory(prefs);
if (!mounted) {
return;
}
setState(() {
appendEnter = nextAppendEnter;
});
if (nextAppendEnter != appendEnter ||
!_stringListsEqual(nextHistory, sendHistory)) {
setState(() {
appendEnter = nextAppendEnter;
sendHistory = nextHistory;
});
}
}
bool _stringListsEqual(List<String> left, List<String> right) {
if (left.length != right.length) {
return false;
}
for (var index = 0; index < left.length; index += 1) {
if (left[index] != right[index]) {
return false;
}
}
return true;
}
List<String> _readHistory(SharedPreferences prefs) {
try {
return prefs.getStringList(_historyKey) ?? [];
} catch (_) {
return [];
}
}
void _restoreHistory(String text) {