mirror of
https://github.com/rustdesk/rustdesk.git
synced 2024-11-24 20:59:37 +08:00
AbModel use getx only
This commit is contained in:
parent
ee1a7fb791
commit
a13c4c5907
@ -1,9 +1,7 @@
|
||||
import 'package:contextmenu/contextmenu.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_hbb/common/widgets/peers_view.dart';
|
||||
import 'package:flutter_hbb/models/ab_model.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../../common.dart';
|
||||
import '../../desktop/pages/desktop_home_page.dart';
|
||||
@ -24,7 +22,7 @@ class _AddressBookState extends State<AddressBook> {
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => gFFI.abModel.getAb());
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => gFFI.abModel.pullAb());
|
||||
}
|
||||
|
||||
@override
|
||||
@ -66,132 +64,123 @@ class _AddressBookState extends State<AddressBook> {
|
||||
}
|
||||
final model = gFFI.abModel;
|
||||
return FutureBuilder(
|
||||
future: model.getAb(),
|
||||
future: model.pullAb(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
return _buildAddressBook(context);
|
||||
} else if (snapshot.hasError) {
|
||||
return Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(translate("${snapshot.error}")),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
setState(() {});
|
||||
},
|
||||
child: Text(translate("Retry")))
|
||||
],
|
||||
);
|
||||
return _buildShowError(snapshot.error.toString());
|
||||
} else {
|
||||
if (model.abLoading) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
} else if (model.abError.isNotEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(translate(model.abError)),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
setState(() {});
|
||||
},
|
||||
child: Text(translate("Retry")))
|
||||
],
|
||||
),
|
||||
);
|
||||
} else {
|
||||
return const Offstage();
|
||||
}
|
||||
return Obx(() {
|
||||
if (model.abLoading.value) {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
} else if (model.abError.isNotEmpty) {
|
||||
return _buildShowError(model.abError.value);
|
||||
} else {
|
||||
return const Offstage();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Widget _buildShowError(String error) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(translate(error)),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
setState(() {});
|
||||
},
|
||||
child: Text(translate("Retry")))
|
||||
],
|
||||
));
|
||||
}
|
||||
|
||||
Widget _buildAddressBook(BuildContext context) {
|
||||
return Consumer<AbModel>(
|
||||
builder: (context, model, child) => Row(
|
||||
return Row(
|
||||
children: [
|
||||
Card(
|
||||
margin: EdgeInsets.symmetric(horizontal: 4.0),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
side:
|
||||
BorderSide(color: Theme.of(context).scaffoldBackgroundColor)),
|
||||
child: Container(
|
||||
width: 200,
|
||||
height: double.infinity,
|
||||
padding:
|
||||
const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
|
||||
child: Column(
|
||||
children: [
|
||||
Card(
|
||||
margin: EdgeInsets.symmetric(horizontal: 4.0),
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
side: BorderSide(
|
||||
color: Theme.of(context).scaffoldBackgroundColor)),
|
||||
child: Container(
|
||||
width: 200,
|
||||
height: double.infinity,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 12.0, vertical: 8.0),
|
||||
child: Column(
|
||||
children: [
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
// TODO same style as peer
|
||||
Text(translate('Tags')),
|
||||
InkWell(
|
||||
child: PopupMenuButton(
|
||||
itemBuilder: (context) => [
|
||||
PopupMenuItem(
|
||||
value: 'add-id',
|
||||
child: Text(translate("Add ID")),
|
||||
),
|
||||
PopupMenuItem(
|
||||
value: 'add-tag',
|
||||
child: Text(translate("Add Tag")),
|
||||
),
|
||||
PopupMenuItem(
|
||||
value: 'unset-all-tag',
|
||||
child: Text(
|
||||
translate("Unselect all tags")),
|
||||
),
|
||||
],
|
||||
onSelected: handleAbOp,
|
||||
child: const Icon(Icons.more_vert_outlined)),
|
||||
)
|
||||
],
|
||||
),
|
||||
Expanded(
|
||||
child: Container(
|
||||
width: double.infinity,
|
||||
height: double.infinity,
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: MyTheme.darkGray),
|
||||
borderRadius: BorderRadius.circular(2)),
|
||||
child: Obx(
|
||||
() => Wrap(
|
||||
children: gFFI.abModel.tags
|
||||
.map((e) =>
|
||||
buildTag(e, gFFI.abModel.selectedTags,
|
||||
onTap: () {
|
||||
//
|
||||
if (gFFI.abModel.selectedTags
|
||||
.contains(e)) {
|
||||
gFFI.abModel.selectedTags.remove(e);
|
||||
} else {
|
||||
gFFI.abModel.selectedTags.add(e);
|
||||
}
|
||||
}))
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
).marginSymmetric(vertical: 8.0),
|
||||
)
|
||||
],
|
||||
),
|
||||
),
|
||||
).marginOnly(right: 8.0),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
// TODO same style as peer
|
||||
Text(translate('Tags')),
|
||||
InkWell(
|
||||
child: PopupMenuButton(
|
||||
itemBuilder: (context) => [
|
||||
PopupMenuItem(
|
||||
value: 'add-id',
|
||||
child: Text(translate("Add ID")),
|
||||
),
|
||||
PopupMenuItem(
|
||||
value: 'add-tag',
|
||||
child: Text(translate("Add Tag")),
|
||||
),
|
||||
PopupMenuItem(
|
||||
value: 'unset-all-tag',
|
||||
child: Text(translate("Unselect all tags")),
|
||||
),
|
||||
],
|
||||
onSelected: handleAbOp,
|
||||
child: const Icon(Icons.more_vert_outlined)),
|
||||
)
|
||||
],
|
||||
),
|
||||
Expanded(
|
||||
child: Align(
|
||||
alignment: Alignment.topLeft,
|
||||
child: Obx(() => AddressBookPeersView(
|
||||
menuPadding: widget.menuPadding,
|
||||
initPeers: gFFI.abModel.peers.value,
|
||||
))),
|
||||
child: Container(
|
||||
width: double.infinity,
|
||||
height: double.infinity,
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: MyTheme.darkGray),
|
||||
borderRadius: BorderRadius.circular(2)),
|
||||
child: Obx(
|
||||
() => Wrap(
|
||||
children: gFFI.abModel.tags
|
||||
.map((e) => buildTag(e, gFFI.abModel.selectedTags,
|
||||
onTap: () {
|
||||
//
|
||||
if (gFFI.abModel.selectedTags.contains(e)) {
|
||||
gFFI.abModel.selectedTags.remove(e);
|
||||
} else {
|
||||
gFFI.abModel.selectedTags.add(e);
|
||||
}
|
||||
}))
|
||||
.toList(),
|
||||
),
|
||||
),
|
||||
).marginSymmetric(vertical: 8.0),
|
||||
)
|
||||
],
|
||||
));
|
||||
),
|
||||
),
|
||||
).marginOnly(right: 8.0),
|
||||
Expanded(
|
||||
child: Align(
|
||||
alignment: Alignment.topLeft,
|
||||
child: Obx(() => AddressBookPeersView(
|
||||
menuPadding: widget.menuPadding,
|
||||
initPeers: gFFI.abModel.peers.value,
|
||||
))),
|
||||
)
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget buildTag(String tagName, RxList<dynamic> rxTags, {Function()? onTap}) {
|
||||
@ -202,7 +191,7 @@ class _AddressBookState extends State<AddressBook> {
|
||||
title: Text(translate("Delete")),
|
||||
onTap: () {
|
||||
gFFI.abModel.deleteTag(tagName);
|
||||
gFFI.abModel.updateAb();
|
||||
gFFI.abModel.pushAb();
|
||||
Future.delayed(Duration.zero, () => Get.back());
|
||||
},
|
||||
)
|
||||
@ -264,7 +253,7 @@ class _AddressBookState extends State<AddressBook> {
|
||||
}
|
||||
gFFI.abModel.addId(newId);
|
||||
}
|
||||
await gFFI.abModel.updateAb();
|
||||
await gFFI.abModel.pushAb();
|
||||
this.setState(() {});
|
||||
// final currentPeers
|
||||
}
|
||||
@ -331,7 +320,7 @@ class _AddressBookState extends State<AddressBook> {
|
||||
for (final tag in tags) {
|
||||
gFFI.abModel.addTag(tag);
|
||||
}
|
||||
await gFFI.abModel.updateAb();
|
||||
await gFFI.abModel.pushAb();
|
||||
// final currentPeers
|
||||
}
|
||||
close();
|
||||
@ -390,7 +379,7 @@ class _AddressBookState extends State<AddressBook> {
|
||||
isInProgress = true;
|
||||
});
|
||||
gFFI.abModel.changeTagForPeer(id, selectedTag);
|
||||
await gFFI.abModel.updateAb();
|
||||
await gFFI.abModel.pushAb();
|
||||
close();
|
||||
}
|
||||
|
||||
|
@ -615,10 +615,10 @@ abstract class BasePeerCard extends StatelessWidget {
|
||||
await bind.mainSetPeerOption(id: id, key: 'alias', value: name);
|
||||
if (isAddressBook) {
|
||||
gFFI.abModel.setPeerAlias(id, name);
|
||||
await gFFI.abModel.updateAb();
|
||||
await gFFI.abModel.pushAb();
|
||||
}
|
||||
if (isAddressBook) {
|
||||
gFFI.abModel.getAb();
|
||||
gFFI.abModel.pullAb();
|
||||
} else {
|
||||
bind.mainLoadRecentPeers();
|
||||
bind.mainLoadFavPeers();
|
||||
@ -791,7 +791,7 @@ class AddressBookPeerCard extends BasePeerCard {
|
||||
proc: () {
|
||||
() async {
|
||||
gFFI.abModel.deletePeer(id);
|
||||
await gFFI.abModel.updateAb();
|
||||
await gFFI.abModel.pushAb();
|
||||
}();
|
||||
},
|
||||
padding: super.menuPadding,
|
||||
@ -826,7 +826,7 @@ class AddressBookPeerCard extends BasePeerCard {
|
||||
isInProgress = true;
|
||||
});
|
||||
gFFI.abModel.changeTagForPeer(id, selectedTag);
|
||||
await gFFI.abModel.updateAb();
|
||||
await gFFI.abModel.pushAb();
|
||||
close();
|
||||
}
|
||||
|
||||
@ -873,7 +873,7 @@ class AddressBookPeerCard extends BasePeerCard {
|
||||
title: Text(translate("Delete")),
|
||||
onTap: () {
|
||||
gFFI.abModel.deleteTag(tagName);
|
||||
gFFI.abModel.updateAb();
|
||||
gFFI.abModel.pushAb();
|
||||
Future.delayed(Duration.zero, () => Get.back());
|
||||
},
|
||||
)
|
||||
|
@ -54,7 +54,7 @@ class _PeerTabPageState extends State<PeerTabPage>
|
||||
bind.mainDiscover();
|
||||
break;
|
||||
case 3:
|
||||
gFFI.abModel.getAb();
|
||||
gFFI.abModel.pullAb();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -297,11 +297,4 @@ class AddressBookPeersView extends BasePeersView {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final widget = super.build(context);
|
||||
// gFFI.abModel.updateAb();
|
||||
return widget;
|
||||
}
|
||||
}
|
||||
|
@ -267,7 +267,6 @@ class _AppState extends State<App> {
|
||||
ChangeNotifierProvider.value(value: gFFI.imageModel),
|
||||
ChangeNotifierProvider.value(value: gFFI.cursorModel),
|
||||
ChangeNotifierProvider.value(value: gFFI.canvasModel),
|
||||
ChangeNotifierProvider.value(value: gFFI.abModel),
|
||||
ChangeNotifierProvider.value(value: gFFI.userModel),
|
||||
],
|
||||
child: GetMaterialApp(
|
||||
|
@ -9,9 +9,9 @@ import 'package:http/http.dart' as http;
|
||||
|
||||
import '../common.dart';
|
||||
|
||||
class AbModel with ChangeNotifier {
|
||||
var abLoading = false;
|
||||
var abError = "";
|
||||
class AbModel {
|
||||
var abLoading = false.obs;
|
||||
var abError = "".obs;
|
||||
var tags = [].obs;
|
||||
var peers = List<Peer>.empty(growable: true).obs;
|
||||
|
||||
@ -23,9 +23,8 @@ class AbModel with ChangeNotifier {
|
||||
|
||||
FFI? get _ffi => parent.target;
|
||||
|
||||
Future<dynamic> getAb() async {
|
||||
abLoading = true;
|
||||
notifyListeners();
|
||||
Future<dynamic> pullAb() async {
|
||||
abLoading.value = true;
|
||||
// request
|
||||
final api = "${await bind.mainGetApiServer()}/api/ab/get";
|
||||
try {
|
||||
@ -43,17 +42,15 @@ class AbModel with ChangeNotifier {
|
||||
peers.add(Peer.fromJson(peer));
|
||||
}
|
||||
}
|
||||
notifyListeners();
|
||||
return resp.body;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
} catch (err) {
|
||||
err.printError();
|
||||
abError = err.toString();
|
||||
abError.value = err.toString();
|
||||
} finally {
|
||||
abLoading = false;
|
||||
notifyListeners();
|
||||
abLoading.value = false;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -61,7 +58,6 @@ class AbModel with ChangeNotifier {
|
||||
void reset() {
|
||||
tags.clear();
|
||||
peers.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void addId(String id) async {
|
||||
@ -69,7 +65,6 @@ class AbModel with ChangeNotifier {
|
||||
return;
|
||||
}
|
||||
peers.add(Peer.fromJson({"id": id}));
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void addTag(String tag) async {
|
||||
@ -77,7 +72,6 @@ class AbModel with ChangeNotifier {
|
||||
return;
|
||||
}
|
||||
tags.add(tag);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void changeTagForPeer(String id, List<dynamic> tags) {
|
||||
@ -88,9 +82,8 @@ class AbModel with ChangeNotifier {
|
||||
it.first.tags = tags;
|
||||
}
|
||||
|
||||
Future<void> updateAb() async {
|
||||
abLoading = true;
|
||||
notifyListeners();
|
||||
Future<void> pushAb() async {
|
||||
abLoading.value = true;
|
||||
final api = "${await bind.mainGetApiServer()}/api/ab";
|
||||
var authHeaders = await getHttpHeaders();
|
||||
authHeaders['Content-Type'] = "application/json";
|
||||
@ -101,15 +94,14 @@ class AbModel with ChangeNotifier {
|
||||
try {
|
||||
final resp =
|
||||
await http.post(Uri.parse(api), headers: authHeaders, body: body);
|
||||
abError = "";
|
||||
await getAb();
|
||||
abError.value = "";
|
||||
await pullAb();
|
||||
debugPrint("resp: ${resp.body}");
|
||||
} catch (e) {
|
||||
abError = e.toString();
|
||||
abError.value = e.toString();
|
||||
} finally {
|
||||
abLoading = false;
|
||||
abLoading.value = false;
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
bool idContainBy(String id) {
|
||||
@ -122,7 +114,6 @@ class AbModel with ChangeNotifier {
|
||||
|
||||
void deletePeer(String id) {
|
||||
peers.removeWhere((element) => element.id == id);
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void deleteTag(String tag) {
|
||||
@ -135,12 +126,10 @@ class AbModel with ChangeNotifier {
|
||||
((peer.tags)).remove(tag);
|
||||
}
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void unsetSelectedTags() {
|
||||
selectedTags.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
List<dynamic> getPeerTags(String id) {
|
||||
@ -165,6 +154,5 @@ class AbModel with ChangeNotifier {
|
||||
void clear() {
|
||||
peers.clear();
|
||||
tags.clear();
|
||||
notifyListeners();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user