AbModel use getx only

This commit is contained in:
csf 2022-10-08 17:13:24 +09:00
parent ee1a7fb791
commit a13c4c5907
6 changed files with 127 additions and 158 deletions

View File

@ -1,9 +1,7 @@
import 'package:contextmenu/contextmenu.dart'; import 'package:contextmenu/contextmenu.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_hbb/common/widgets/peers_view.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:get/get.dart';
import 'package:provider/provider.dart';
import '../../common.dart'; import '../../common.dart';
import '../../desktop/pages/desktop_home_page.dart'; import '../../desktop/pages/desktop_home_page.dart';
@ -24,7 +22,7 @@ class _AddressBookState extends State<AddressBook> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) => gFFI.abModel.getAb()); WidgetsBinding.instance.addPostFrameCallback((_) => gFFI.abModel.pullAb());
} }
@override @override
@ -66,63 +64,57 @@ class _AddressBookState extends State<AddressBook> {
} }
final model = gFFI.abModel; final model = gFFI.abModel;
return FutureBuilder( return FutureBuilder(
future: model.getAb(), future: model.pullAb(),
builder: (context, snapshot) { builder: (context, snapshot) {
if (snapshot.hasData) { if (snapshot.hasData) {
return _buildAddressBook(context); return _buildAddressBook(context);
} else if (snapshot.hasError) { } else if (snapshot.hasError) {
return Column( return _buildShowError(snapshot.error.toString());
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(translate("${snapshot.error}")),
TextButton(
onPressed: () {
setState(() {});
},
child: Text(translate("Retry")))
],
);
} else { } else {
if (model.abLoading) { return Obx(() {
if (model.abLoading.value) {
return const Center( return const Center(
child: CircularProgressIndicator(), child: CircularProgressIndicator(),
); );
} else if (model.abError.isNotEmpty) { } else if (model.abError.isNotEmpty) {
return _buildShowError(model.abError.value);
} else {
return const Offstage();
}
});
}
});
}
Widget _buildShowError(String error) {
return Center( return Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: [ children: [
Text(translate(model.abError)), Text(translate(error)),
TextButton( TextButton(
onPressed: () { onPressed: () {
setState(() {}); setState(() {});
}, },
child: Text(translate("Retry"))) child: Text(translate("Retry")))
], ],
), ));
);
} else {
return const Offstage();
}
}
});
} }
Widget _buildAddressBook(BuildContext context) { Widget _buildAddressBook(BuildContext context) {
return Consumer<AbModel>( return Row(
builder: (context, model, child) => Row(
children: [ children: [
Card( Card(
margin: EdgeInsets.symmetric(horizontal: 4.0), margin: EdgeInsets.symmetric(horizontal: 4.0),
shape: RoundedRectangleBorder( shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12), borderRadius: BorderRadius.circular(12),
side: BorderSide( side:
color: Theme.of(context).scaffoldBackgroundColor)), BorderSide(color: Theme.of(context).scaffoldBackgroundColor)),
child: Container( child: Container(
width: 200, width: 200,
height: double.infinity, height: double.infinity,
padding: const EdgeInsets.symmetric( padding:
horizontal: 12.0, vertical: 8.0), const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
child: Column( child: Column(
children: [ children: [
Row( Row(
@ -143,8 +135,7 @@ class _AddressBookState extends State<AddressBook> {
), ),
PopupMenuItem( PopupMenuItem(
value: 'unset-all-tag', value: 'unset-all-tag',
child: Text( child: Text(translate("Unselect all tags")),
translate("Unselect all tags")),
), ),
], ],
onSelected: handleAbOp, onSelected: handleAbOp,
@ -162,12 +153,10 @@ class _AddressBookState extends State<AddressBook> {
child: Obx( child: Obx(
() => Wrap( () => Wrap(
children: gFFI.abModel.tags children: gFFI.abModel.tags
.map((e) => .map((e) => buildTag(e, gFFI.abModel.selectedTags,
buildTag(e, gFFI.abModel.selectedTags,
onTap: () { onTap: () {
// //
if (gFFI.abModel.selectedTags if (gFFI.abModel.selectedTags.contains(e)) {
.contains(e)) {
gFFI.abModel.selectedTags.remove(e); gFFI.abModel.selectedTags.remove(e);
} else { } else {
gFFI.abModel.selectedTags.add(e); gFFI.abModel.selectedTags.add(e);
@ -191,7 +180,7 @@ class _AddressBookState extends State<AddressBook> {
))), ))),
) )
], ],
)); );
} }
Widget buildTag(String tagName, RxList<dynamic> rxTags, {Function()? onTap}) { Widget buildTag(String tagName, RxList<dynamic> rxTags, {Function()? onTap}) {
@ -202,7 +191,7 @@ class _AddressBookState extends State<AddressBook> {
title: Text(translate("Delete")), title: Text(translate("Delete")),
onTap: () { onTap: () {
gFFI.abModel.deleteTag(tagName); gFFI.abModel.deleteTag(tagName);
gFFI.abModel.updateAb(); gFFI.abModel.pushAb();
Future.delayed(Duration.zero, () => Get.back()); Future.delayed(Duration.zero, () => Get.back());
}, },
) )
@ -264,7 +253,7 @@ class _AddressBookState extends State<AddressBook> {
} }
gFFI.abModel.addId(newId); gFFI.abModel.addId(newId);
} }
await gFFI.abModel.updateAb(); await gFFI.abModel.pushAb();
this.setState(() {}); this.setState(() {});
// final currentPeers // final currentPeers
} }
@ -331,7 +320,7 @@ class _AddressBookState extends State<AddressBook> {
for (final tag in tags) { for (final tag in tags) {
gFFI.abModel.addTag(tag); gFFI.abModel.addTag(tag);
} }
await gFFI.abModel.updateAb(); await gFFI.abModel.pushAb();
// final currentPeers // final currentPeers
} }
close(); close();
@ -390,7 +379,7 @@ class _AddressBookState extends State<AddressBook> {
isInProgress = true; isInProgress = true;
}); });
gFFI.abModel.changeTagForPeer(id, selectedTag); gFFI.abModel.changeTagForPeer(id, selectedTag);
await gFFI.abModel.updateAb(); await gFFI.abModel.pushAb();
close(); close();
} }

View File

@ -615,10 +615,10 @@ abstract class BasePeerCard extends StatelessWidget {
await bind.mainSetPeerOption(id: id, key: 'alias', value: name); await bind.mainSetPeerOption(id: id, key: 'alias', value: name);
if (isAddressBook) { if (isAddressBook) {
gFFI.abModel.setPeerAlias(id, name); gFFI.abModel.setPeerAlias(id, name);
await gFFI.abModel.updateAb(); await gFFI.abModel.pushAb();
} }
if (isAddressBook) { if (isAddressBook) {
gFFI.abModel.getAb(); gFFI.abModel.pullAb();
} else { } else {
bind.mainLoadRecentPeers(); bind.mainLoadRecentPeers();
bind.mainLoadFavPeers(); bind.mainLoadFavPeers();
@ -791,7 +791,7 @@ class AddressBookPeerCard extends BasePeerCard {
proc: () { proc: () {
() async { () async {
gFFI.abModel.deletePeer(id); gFFI.abModel.deletePeer(id);
await gFFI.abModel.updateAb(); await gFFI.abModel.pushAb();
}(); }();
}, },
padding: super.menuPadding, padding: super.menuPadding,
@ -826,7 +826,7 @@ class AddressBookPeerCard extends BasePeerCard {
isInProgress = true; isInProgress = true;
}); });
gFFI.abModel.changeTagForPeer(id, selectedTag); gFFI.abModel.changeTagForPeer(id, selectedTag);
await gFFI.abModel.updateAb(); await gFFI.abModel.pushAb();
close(); close();
} }
@ -873,7 +873,7 @@ class AddressBookPeerCard extends BasePeerCard {
title: Text(translate("Delete")), title: Text(translate("Delete")),
onTap: () { onTap: () {
gFFI.abModel.deleteTag(tagName); gFFI.abModel.deleteTag(tagName);
gFFI.abModel.updateAb(); gFFI.abModel.pushAb();
Future.delayed(Duration.zero, () => Get.back()); Future.delayed(Duration.zero, () => Get.back());
}, },
) )

View File

@ -54,7 +54,7 @@ class _PeerTabPageState extends State<PeerTabPage>
bind.mainDiscover(); bind.mainDiscover();
break; break;
case 3: case 3:
gFFI.abModel.getAb(); gFFI.abModel.pullAb();
break; break;
} }
} }

View File

@ -297,11 +297,4 @@ class AddressBookPeersView extends BasePeersView {
} }
return true; return true;
} }
@override
Widget build(BuildContext context) {
final widget = super.build(context);
// gFFI.abModel.updateAb();
return widget;
}
} }

View File

@ -267,7 +267,6 @@ class _AppState extends State<App> {
ChangeNotifierProvider.value(value: gFFI.imageModel), ChangeNotifierProvider.value(value: gFFI.imageModel),
ChangeNotifierProvider.value(value: gFFI.cursorModel), ChangeNotifierProvider.value(value: gFFI.cursorModel),
ChangeNotifierProvider.value(value: gFFI.canvasModel), ChangeNotifierProvider.value(value: gFFI.canvasModel),
ChangeNotifierProvider.value(value: gFFI.abModel),
ChangeNotifierProvider.value(value: gFFI.userModel), ChangeNotifierProvider.value(value: gFFI.userModel),
], ],
child: GetMaterialApp( child: GetMaterialApp(

View File

@ -9,9 +9,9 @@ import 'package:http/http.dart' as http;
import '../common.dart'; import '../common.dart';
class AbModel with ChangeNotifier { class AbModel {
var abLoading = false; var abLoading = false.obs;
var abError = ""; var abError = "".obs;
var tags = [].obs; var tags = [].obs;
var peers = List<Peer>.empty(growable: true).obs; var peers = List<Peer>.empty(growable: true).obs;
@ -23,9 +23,8 @@ class AbModel with ChangeNotifier {
FFI? get _ffi => parent.target; FFI? get _ffi => parent.target;
Future<dynamic> getAb() async { Future<dynamic> pullAb() async {
abLoading = true; abLoading.value = true;
notifyListeners();
// request // request
final api = "${await bind.mainGetApiServer()}/api/ab/get"; final api = "${await bind.mainGetApiServer()}/api/ab/get";
try { try {
@ -43,17 +42,15 @@ class AbModel with ChangeNotifier {
peers.add(Peer.fromJson(peer)); peers.add(Peer.fromJson(peer));
} }
} }
notifyListeners();
return resp.body; return resp.body;
} else { } else {
return ""; return "";
} }
} catch (err) { } catch (err) {
err.printError(); err.printError();
abError = err.toString(); abError.value = err.toString();
} finally { } finally {
abLoading = false; abLoading.value = false;
notifyListeners();
} }
return null; return null;
} }
@ -61,7 +58,6 @@ class AbModel with ChangeNotifier {
void reset() { void reset() {
tags.clear(); tags.clear();
peers.clear(); peers.clear();
notifyListeners();
} }
void addId(String id) async { void addId(String id) async {
@ -69,7 +65,6 @@ class AbModel with ChangeNotifier {
return; return;
} }
peers.add(Peer.fromJson({"id": id})); peers.add(Peer.fromJson({"id": id}));
notifyListeners();
} }
void addTag(String tag) async { void addTag(String tag) async {
@ -77,7 +72,6 @@ class AbModel with ChangeNotifier {
return; return;
} }
tags.add(tag); tags.add(tag);
notifyListeners();
} }
void changeTagForPeer(String id, List<dynamic> tags) { void changeTagForPeer(String id, List<dynamic> tags) {
@ -88,9 +82,8 @@ class AbModel with ChangeNotifier {
it.first.tags = tags; it.first.tags = tags;
} }
Future<void> updateAb() async { Future<void> pushAb() async {
abLoading = true; abLoading.value = true;
notifyListeners();
final api = "${await bind.mainGetApiServer()}/api/ab"; final api = "${await bind.mainGetApiServer()}/api/ab";
var authHeaders = await getHttpHeaders(); var authHeaders = await getHttpHeaders();
authHeaders['Content-Type'] = "application/json"; authHeaders['Content-Type'] = "application/json";
@ -101,15 +94,14 @@ class AbModel with ChangeNotifier {
try { try {
final resp = final resp =
await http.post(Uri.parse(api), headers: authHeaders, body: body); await http.post(Uri.parse(api), headers: authHeaders, body: body);
abError = ""; abError.value = "";
await getAb(); await pullAb();
debugPrint("resp: ${resp.body}"); debugPrint("resp: ${resp.body}");
} catch (e) { } catch (e) {
abError = e.toString(); abError.value = e.toString();
} finally { } finally {
abLoading = false; abLoading.value = false;
} }
notifyListeners();
} }
bool idContainBy(String id) { bool idContainBy(String id) {
@ -122,7 +114,6 @@ class AbModel with ChangeNotifier {
void deletePeer(String id) { void deletePeer(String id) {
peers.removeWhere((element) => element.id == id); peers.removeWhere((element) => element.id == id);
notifyListeners();
} }
void deleteTag(String tag) { void deleteTag(String tag) {
@ -135,12 +126,10 @@ class AbModel with ChangeNotifier {
((peer.tags)).remove(tag); ((peer.tags)).remove(tag);
} }
} }
notifyListeners();
} }
void unsetSelectedTags() { void unsetSelectedTags() {
selectedTags.clear(); selectedTags.clear();
notifyListeners();
} }
List<dynamic> getPeerTags(String id) { List<dynamic> getPeerTags(String id) {
@ -165,6 +154,5 @@ class AbModel with ChangeNotifier {
void clear() { void clear() {
peers.clear(); peers.clear();
tags.clear(); tags.clear();
notifyListeners();
} }
} }