add force, quiet to pullAb, no force pull while switch tab, this may be

not good, because data not updated, have to do with refresh button, we
may change to quiet pull in the future
This commit is contained in:
rustdesk 2023-06-23 15:10:10 +08:00
parent 977ac2a3b2
commit 58e4d66b44
4 changed files with 24 additions and 29 deletions

View File

@ -43,9 +43,6 @@ class _AddressBookState extends State<AddressBook> {
if (gFFI.abModel.abError.isNotEmpty) {
return _buildShowError(gFFI.abModel.abError.value);
}
if (gFFI.abModel.fromServer.isFalse) {
return Offstage();
}
return isDesktop
? _buildAddressBookDesktop()
: _buildAddressBookMobile();

View File

@ -22,7 +22,7 @@ class PeerTabPage extends StatefulWidget {
class _TabEntry {
final Widget widget;
final Function() load;
final Function({dynamic hint}) load;
_TabEntry(this.widget, this.load);
}
@ -52,12 +52,12 @@ class _PeerTabPageState extends State<PeerTabPage>
AddressBook(
menuPadding: _menuPadding(),
),
() => gFFI.abModel.pullAb()),
({dynamic hint}) => gFFI.abModel.pullAb(force: hint == null)),
_TabEntry(
MyGroup(
menuPadding: _menuPadding(),
),
() => gFFI.groupModel.pull(),
({dynamic hint}) => gFFI.groupModel.pull(force: hint == null),
),
];
@ -75,7 +75,7 @@ class _PeerTabPageState extends State<PeerTabPage>
Future<void> handleTabSelection(int tabIndex) async {
if (tabIndex < entries.length) {
gFFI.peerTabModel.setCurrentTab(tabIndex);
entries[tabIndex].load();
entries[tabIndex].load(hint: false);
}
}

View File

@ -5,6 +5,7 @@ import 'package:flutter_hbb/models/model.dart';
import 'package:flutter_hbb/models/peer_model.dart';
import 'package:flutter_hbb/models/platform_model.dart';
import 'package:get/get.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:http/http.dart' as http;
import '../common.dart';
@ -13,19 +14,23 @@ class AbModel {
final abLoading = false.obs;
final abError = "".obs;
final tags = [].obs;
final RxBool fromServer = false.obs;
final peers = List<Peer>.empty(growable: true).obs;
final selectedTags = List<String>.empty(growable: true).obs;
var initialized = false;
WeakReference<FFI> parent;
AbModel(this.parent);
Future<dynamic> pullAb() async {
Future<void> pullAb({force = true, quiet = false}) async {
if (gFFI.userModel.userName.isEmpty) return;
abLoading.value = true;
abError.value = "";
if (abLoading.value) return;
if (!force && initialized) return;
if (!quiet) {
abLoading.value = true;
abError.value = "";
}
final api = "${await bind.mainGetApiServer()}/api/ab/get";
try {
var authHeaders = getHttpHeaders();
@ -50,25 +55,20 @@ class AbModel {
}
}
}
fromServer.value = true;
return resp.body;
} else {
fromServer.value = true;
return "";
}
} catch (err) {
err.printError();
abError.value = err.toString();
} finally {
abLoading.value = false;
initialized = true;
}
return null;
}
Future<void> reset() async {
await bind.mainSetLocalOption(key: "selected-tags", value: '');
tags.clear();
peers.clear();
initialized = false;
}
void addId(String id, String alias, List<dynamic> tags) {
@ -104,7 +104,6 @@ class AbModel {
}
Future<void> pushAb() async {
abLoading.value = true;
final api = "${await bind.mainGetApiServer()}/api/ab";
var authHeaders = getHttpHeaders();
authHeaders['Content-Type'] = "application/json";
@ -113,16 +112,11 @@ class AbModel {
"data": jsonEncode({"tags": tags, "peers": peersJsonData})
});
try {
final resp =
await http.post(Uri.parse(api), headers: authHeaders, body: body);
abError.value = "";
await pullAb();
debugPrint("resp: ${resp.body}");
await http.post(Uri.parse(api), headers: authHeaders, body: body);
await pullAb(quiet: true);
} catch (e) {
abError.value = e.toString();
} finally {
abLoading.value = false;
}
BotToast.showText(contentColor: Colors.red, text: e.toString());
} finally {}
}
Peer? find(String id) {

View File

@ -18,6 +18,7 @@ class GroupModel {
final RxString selectedUser = ''.obs;
final RxString searchUserText = ''.obs;
WeakReference<FFI> parent;
var initialized = false;
GroupModel(this.parent);
@ -26,13 +27,16 @@ class GroupModel {
groupId.value = '';
users.clear();
peersShow.clear();
initialized = false;
}
Future<void> pull() async {
Future<void> pull({force = true}) async {
if (!force && initialized) return;
groupLoading.value = true;
groupLoadError.value = "";
await _pull();
groupLoading.value = false;
initialized = true;
}
Future<void> _pull() async {