manage network state for both user info and user group

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou 2023-06-21 16:08:45 +08:00
parent 581e84d806
commit 10508a41b1
6 changed files with 64 additions and 33 deletions

View File

@ -3,6 +3,7 @@ import 'package:flutter_hbb/common/formatter/id_formatter.dart';
import 'package:flutter_hbb/common/widgets/peer_card.dart';
import 'package:flutter_hbb/common/widgets/peers_view.dart';
import 'package:flutter_hbb/desktop/widgets/popup_menu.dart';
import 'package:flutter_hbb/models/state_model.dart';
import '../../consts.dart';
import '../../desktop/widgets/material_mod_popup_menu.dart' as mod_menu;
import 'package:get/get.dart';
@ -29,15 +30,18 @@ class _AddressBookState extends State<AddressBook> {
}
@override
Widget build(BuildContext context) => FutureBuilder<Widget>(
future: buildBody(context),
builder: (context, snapshot) {
if (snapshot.hasData) {
return snapshot.data!;
} else {
return const Offstage();
}
});
Widget build(BuildContext context) => Obx(() => Offstage(
offstage: stateGlobal.svcStatus.value != SvcStatus.ready,
child: FutureBuilder<Widget>(
future: buildBody(context),
builder: (context, snapshot) {
if (snapshot.hasData) {
return snapshot.data!;
} else {
return const Offstage();
}
}),
));
Future<Widget> buildBody(BuildContext context) async {
return Obx(() {
@ -54,7 +58,7 @@ class _AddressBookState extends State<AddressBook> {
if (gFFI.abModel.abError.isNotEmpty) {
return _buildShowError(gFFI.abModel.abError.value);
}
if (gFFI.userModel.fromServer.isFalse) {
if (gFFI.abModel.fromServer.isFalse) {
return Offstage();
}
return isDesktop

View File

@ -1,6 +1,7 @@
import 'package:flutter/material.dart';
import 'package:flutter_hbb/common/hbbs/hbbs.dart';
import 'package:flutter_hbb/common/widgets/peers_view.dart';
import 'package:flutter_hbb/models/state_model.dart';
import 'package:get/get.dart';
import '../../common.dart';
@ -26,15 +27,20 @@ class _MyGroupState extends State<MyGroup> {
}
@override
Widget build(BuildContext context) => FutureBuilder<Widget>(
future: buildBody(context),
builder: (context, snapshot) {
if (snapshot.hasData) {
return snapshot.data!;
} else {
return const Offstage();
}
});
Widget build(BuildContext context) {
return Obx(() => Offstage(
offstage: stateGlobal.svcStatus.value != SvcStatus.ready,
child: FutureBuilder<Widget>(
future: buildBody(context),
builder: (context, snapshot) {
if (snapshot.hasData) {
return snapshot.data!;
} else {
return const Offstage();
}
}),
));
}
Future<Widget> buildBody(BuildContext context) async {
return Obx(() {

View File

@ -42,7 +42,6 @@ class _ConnectionPageState extends State<ConnectionPage>
final FocusNode _idFocusNode = FocusNode();
var svcStopped = Get.find<RxBool>(tag: 'stop-service');
var svcStatusCode = 0.obs;
var svcIsUsingPublicServer = true.obs;
bool isWindowMinimized = false;
@ -253,9 +252,9 @@ class _ConnectionPageState extends State<ConnectionPage>
width: 8,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(4),
color: svcStopped.value || svcStatusCode.value == 0
color: svcStopped.value || stateGlobal.svcStatus.value == SvcStatus.connecting
? kColorWarn
: (svcStatusCode.value == 1
: (stateGlobal.svcStatus.value == SvcStatus.ready
? Color.fromARGB(255, 50, 190, 166)
: Color.fromARGB(255, 224, 79, 95)),
),
@ -263,9 +262,9 @@ class _ConnectionPageState extends State<ConnectionPage>
Text(
svcStopped.value
? translate("Service is not running")
: svcStatusCode.value == 0
: stateGlobal.svcStatus.value == SvcStatus.connecting
? translate("connecting_status")
: svcStatusCode.value == -1
: stateGlobal.svcStatus.value == SvcStatus.notReady
? translate("not_ready_status")
: translate('Ready'),
style: TextStyle(fontSize: em)),
@ -286,7 +285,7 @@ class _ConnectionPageState extends State<ConnectionPage>
Flexible(
child: Offstage(
offstage: !(!svcStopped.value &&
svcStatusCode.value == 1 &&
stateGlobal.svcStatus.value == SvcStatus.ready &&
svcIsUsingPublicServer.value),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
@ -330,7 +329,25 @@ class _ConnectionPageState extends State<ConnectionPage>
updateStatus() async {
final status =
jsonDecode(await bind.mainGetConnectStatus()) as Map<String, dynamic>;
svcStatusCode.value = status["status_num"];
final statusNum = status['status_num'] as int;
final preStatus = stateGlobal.svcStatus.value;
if (statusNum == 0) {
stateGlobal.svcStatus.value = SvcStatus.connecting;
} else if (statusNum == -1) {
stateGlobal.svcStatus.value = SvcStatus.notReady;
} else if (statusNum == 1) {
stateGlobal.svcStatus.value = SvcStatus.ready;
if (preStatus != SvcStatus.ready) {
gFFI.userModel.refreshCurrentUser();
gFFI.groupModel.pull();
}
} else {
stateGlobal.svcStatus.value = SvcStatus.notReady;
}
if (stateGlobal.svcStatus.value != SvcStatus.ready) {
gFFI.userModel.isAdmin.value = false;
gFFI.groupModel.reset();
}
svcIsUsingPublicServer.value = await bind.mainIsUsingPublicServer();
}
}

View File

@ -10,12 +10,13 @@ import 'package:http/http.dart' as http;
import '../common.dart';
class AbModel {
var abLoading = false.obs;
var abError = "".obs;
var tags = [].obs;
var peers = List<Peer>.empty(growable: true).obs;
final abLoading = false.obs;
final abError = "".obs;
final tags = [].obs;
final RxBool fromServer = false.obs;
final peers = List<Peer>.empty(growable: true).obs;
var selectedTags = List<String>.empty(growable: true).obs;
final selectedTags = List<String>.empty(growable: true).obs;
WeakReference<FFI> parent;
@ -49,8 +50,10 @@ class AbModel {
}
}
}
fromServer.value = true;
return resp.body;
} else {
fromServer.value = true;
return "";
}
} catch (err) {

View File

@ -6,6 +6,8 @@ import 'package:get/get.dart';
import '../consts.dart';
enum SvcStatus { notReady, connecting, ready }
class StateGlobal {
int _windowId = -1;
bool _fullscreen = false;
@ -16,6 +18,7 @@ class StateGlobal {
final RxDouble _windowBorderWidth = RxDouble(kWindowBorderWidth);
final RxBool showRemoteToolBar = false.obs;
final RxInt displaysCount = 0.obs;
final svcStatus = SvcStatus.notReady.obs;
// Use for desktop -> remote toolbar -> resolution
final Map<String, Map<int, String?>> _lastResolutionGroupValues = {};

View File

@ -14,7 +14,6 @@ import 'platform_model.dart';
class UserModel {
final RxString userName = ''.obs;
final RxBool isAdmin = false.obs;
final RxBool fromServer = false.obs;
WeakReference<FFI> parent;
UserModel(this.parent);
@ -85,7 +84,6 @@ class UserModel {
_parseAndUpdateUser(UserPayload user) {
userName.value = user.name;
isAdmin.value = user.isAdmin;
fromServer.value = true;
}
Future<void> _updateOtherModels() async {