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: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,63 +64,57 @@ 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 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(model.abError)),
Text(translate(error)),
TextButton(
onPressed: () {
setState(() {});
},
child: Text(translate("Retry")))
],
),
);
} else {
return const Offstage();
}
}
});
));
}
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)),
side:
BorderSide(color: Theme.of(context).scaffoldBackgroundColor)),
child: Container(
width: 200,
height: double.infinity,
padding: const EdgeInsets.symmetric(
horizontal: 12.0, vertical: 8.0),
padding:
const EdgeInsets.symmetric(horizontal: 12.0, vertical: 8.0),
child: Column(
children: [
Row(
@ -143,8 +135,7 @@ class _AddressBookState extends State<AddressBook> {
),
PopupMenuItem(
value: 'unset-all-tag',
child: Text(
translate("Unselect all tags")),
child: Text(translate("Unselect all tags")),
),
],
onSelected: handleAbOp,
@ -162,12 +153,10 @@ class _AddressBookState extends State<AddressBook> {
child: Obx(
() => Wrap(
children: gFFI.abModel.tags
.map((e) =>
buildTag(e, gFFI.abModel.selectedTags,
.map((e) => buildTag(e, gFFI.abModel.selectedTags,
onTap: () {
//
if (gFFI.abModel.selectedTags
.contains(e)) {
if (gFFI.abModel.selectedTags.contains(e)) {
gFFI.abModel.selectedTags.remove(e);
} else {
gFFI.abModel.selectedTags.add(e);
@ -191,7 +180,7 @@ class _AddressBookState extends State<AddressBook> {
))),
)
],
));
);
}
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();
}

View File

@ -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());
},
)

View File

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

View File

@ -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;
}
}

View File

@ -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(

View File

@ -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();
}
}