Merge pull request #5348 from 21pages/ab

ab: use cache for display and display peers while loading
This commit is contained in:
RustDesk
2023-08-11 15:59:12 +08:00
committed by GitHub
6 changed files with 160 additions and 17 deletions

View File

@@ -28,6 +28,7 @@ class AbModel {
final tags = [].obs;
final peers = List<Peer>.empty(growable: true).obs;
final sortTags = shouldSortTags().obs;
bool get emtpy => peers.isEmpty && tags.isEmpty;
final selectedTags = List<String>.empty(growable: true).obs;
var initialized = false;
@@ -46,10 +47,13 @@ class AbModel {
}
Future<void> pullAb({force = true, quiet = false}) async {
debugPrint("pullAb, force:$force, quite:$quiet");
debugPrint("pullAb, force:$force, quiet:$quiet");
if (gFFI.userModel.userName.isEmpty) return;
if (abLoading.value) return;
if (!force && initialized) return;
if (!initialized) {
await _loadCache();
}
if (!quiet) {
abLoading.value = true;
abError.value = "";
@@ -92,14 +96,21 @@ class AbModel {
peers.add(Peer.fromJson(peer));
}
}
save(); // save on success
_saveCache(); // save on success
}
}
}
} catch (err) {
abError.value = err.toString();
} finally {
abLoading.value = false;
if (initialized) {
// make loading effect obvious
Future.delayed(Duration(milliseconds: 300), () {
abLoading.value = false;
});
} else {
abLoading.value = false;
}
initialized = true;
sync_all_from_recent = true;
_timerCounter = 0;
@@ -197,7 +208,7 @@ class AbModel {
try {
await http.Client().send(request);
// await pullAb(quiet: true);
save(); // save on success
_saveCache(); // save on success
} catch (e) {
BotToast.showText(contentColor: Colors.red, text: e.toString());
} finally {
@@ -369,21 +380,39 @@ class AbModel {
}
}
save() {
_saveCache() {
try {
final infos = peers
.map((e) => (<String, dynamic>{
"id": e.id,
"hash": e.hash,
}))
.toList();
final peersJsonData = peers.map((e) => e.toAbUploadJson()).toList();
final m = <String, dynamic>{
"access_token": bind.mainGetLocalOption(key: 'access_token'),
"peers": infos,
"peers": peersJsonData,
"tags": tags.map((e) => e.toString()).toList(),
};
bind.mainSaveAb(json: jsonEncode(m));
} catch (e) {
debugPrint('ab save:$e');
}
}
_loadCache() async {
try {
final access_token = bind.mainGetLocalOption(key: 'access_token');
if (access_token.isEmpty) return;
final cache = await bind.mainLoadAb();
final data = jsonDecode(cache);
if (data == null || data['access_token'] != access_token) return;
tags.clear();
peers.clear();
if (data['tags'] is List) {
tags.value = data['tags'];
}
if (data['peers'] is List) {
for (final peer in data['peers']) {
peers.add(Peer.fromJson(peer));
}
}
} catch (e) {
debugPrint("load ab cache: $e");
}
}
}