fix ab peers state

This commit is contained in:
csf 2022-10-08 16:53:03 +09:00
parent 9449e50ba4
commit ee1a7fb791
5 changed files with 50 additions and 40 deletions

View File

@ -185,9 +185,10 @@ class _AddressBookState extends State<AddressBook> {
Expanded(
child: Align(
alignment: Alignment.topLeft,
child: AddressBookPeersView(
menuPadding: widget.menuPadding,
)),
child: Obx(() => AddressBookPeersView(
menuPadding: widget.menuPadding,
initPeers: gFFI.abModel.peers.value,
))),
)
],
));

View File

@ -601,11 +601,11 @@ abstract class BasePeerCard extends StatelessWidget {
var name = peer.alias;
var controller = TextEditingController(text: name);
if (isAddressBook) {
final peer = gFFI.abModel.peers.firstWhere((p) => id == p['id']);
final peer = gFFI.abModel.peers.firstWhereOrNull((p) => id == p.id);
if (peer == null) {
// this should not happen
} else {
name = peer['alias'] ?? '';
name = peer.alias;
}
}
gFFI.dialogManager.show((setState, close) {
@ -614,7 +614,7 @@ abstract class BasePeerCard extends StatelessWidget {
name = controller.text;
await bind.mainSetPeerOption(id: id, key: 'alias', value: name);
if (isAddressBook) {
gFFI.abModel.setPeerOption(id, 'alias', name);
gFFI.abModel.setPeerAlias(id, name);
await gFFI.abModel.updateAb();
}
if (isAddressBook) {

View File

@ -265,7 +265,10 @@ class DiscoveredPeersView extends BasePeersView {
class AddressBookPeersView extends BasePeersView {
AddressBookPeersView(
{Key? key, EdgeInsets? menuPadding, ScrollController? scrollController})
{Key? key,
EdgeInsets? menuPadding,
ScrollController? scrollController,
required List<Peer> initPeers})
: super(
key: key,
name: 'address book peer',
@ -277,16 +280,9 @@ class AddressBookPeersView extends BasePeersView {
peer: peer,
menuPadding: menuPadding,
))),
initPeers: _loadPeers(),
initPeers: initPeers,
);
static List<Peer> _loadPeers() {
debugPrint("_loadPeers : ${gFFI.abModel.peers.toString()}");
return gFFI.abModel.peers.map((e) {
return Peer.fromJson(e);
}).toList();
}
static bool _hitTag(List<dynamic> selectedTags, List<dynamic> idents) {
if (selectedTags.isEmpty) {
return true;

View File

@ -2,6 +2,7 @@ import 'dart:convert';
import 'package:flutter/material.dart';
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:http/http.dart' as http;
@ -12,7 +13,7 @@ class AbModel with ChangeNotifier {
var abLoading = false;
var abError = "";
var tags = [].obs;
var peers = [].obs;
var peers = List<Peer>.empty(growable: true).obs;
var selectedTags = List<String>.empty(growable: true).obs;
@ -26,7 +27,7 @@ class AbModel with ChangeNotifier {
abLoading = true;
notifyListeners();
// request
final api = "${await getApiServer()}/api/ab/get";
final api = "${await bind.mainGetApiServer()}/api/ab/get";
try {
final resp =
await http.post(Uri.parse(api), headers: await getHttpHeaders());
@ -37,7 +38,10 @@ class AbModel with ChangeNotifier {
} else if (json.containsKey('data')) {
final data = jsonDecode(json['data']);
tags.value = data['tags'];
peers.value = data['peers'];
peers.clear();
for (final peer in data['peers']) {
peers.add(Peer.fromJson(peer));
}
}
notifyListeners();
return resp.body;
@ -45,6 +49,7 @@ class AbModel with ChangeNotifier {
return "";
}
} catch (err) {
err.printError();
abError = err.toString();
} finally {
abLoading = false;
@ -53,10 +58,6 @@ class AbModel with ChangeNotifier {
return null;
}
Future<String> getApiServer() async {
return await bind.mainGetApiServer();
}
void reset() {
tags.clear();
peers.clear();
@ -67,7 +68,7 @@ class AbModel with ChangeNotifier {
if (idContainBy(id)) {
return;
}
peers.add({"id": id});
peers.add(Peer.fromJson({"id": id}));
notifyListeners();
}
@ -80,21 +81,22 @@ class AbModel with ChangeNotifier {
}
void changeTagForPeer(String id, List<dynamic> tags) {
final it = peers.where((element) => element['id'] == id);
final it = peers.where((element) => element.id == id);
if (it.isEmpty) {
return;
}
it.first['tags'] = tags;
it.first.tags = tags;
}
Future<void> updateAb() async {
abLoading = true;
notifyListeners();
final api = "${await getApiServer()}/api/ab";
final api = "${await bind.mainGetApiServer()}/api/ab";
var authHeaders = await getHttpHeaders();
authHeaders['Content-Type'] = "application/json";
final peersJsonData = peers.map((e) => e.toJson()).toList();
final body = jsonEncode({
"data": jsonEncode({"tags": tags, "peers": peers})
"data": jsonEncode({"tags": tags, "peers": peersJsonData})
});
try {
final resp =
@ -111,7 +113,7 @@ class AbModel with ChangeNotifier {
}
bool idContainBy(String id) {
return peers.where((element) => element['id'] == id).isNotEmpty;
return peers.where((element) => element.id == id).isNotEmpty;
}
bool tagContainBy(String tag) {
@ -119,18 +121,18 @@ class AbModel with ChangeNotifier {
}
void deletePeer(String id) {
peers.removeWhere((element) => element['id'] == id);
peers.removeWhere((element) => element.id == id);
notifyListeners();
}
void deleteTag(String tag) {
tags.removeWhere((element) => element == tag);
for (var peer in peers) {
if (peer['tags'] == null) {
if (peer.tags.isEmpty) {
continue;
}
if (((peer['tags']) as List<dynamic>).contains(tag)) {
((peer['tags']) as List<dynamic>).remove(tag);
if (peer.tags.contains(tag)) {
((peer.tags)).remove(tag);
}
}
notifyListeners();
@ -142,21 +144,21 @@ class AbModel with ChangeNotifier {
}
List<dynamic> getPeerTags(String id) {
final it = peers.where((p0) => p0['id'] == id);
final it = peers.where((p0) => p0.id == id);
if (it.isEmpty) {
return [];
} else {
return it.first['tags'] ?? [];
return it.first.tags;
}
}
void setPeerOption(String id, String key, String value) {
final it = peers.where((p0) => p0['id'] == id);
void setPeerAlias(String id, String value) {
final it = peers.where((p0) => p0.id == id);
if (it.isEmpty) {
debugPrint("${id} is not exists");
debugPrint("$id is not exists");
return;
} else {
it.first[key] = value;
it.first.alias = value;
}
}

View File

@ -7,8 +7,8 @@ class Peer {
final String username;
final String hostname;
final String platform;
final String alias;
final List<dynamic> tags;
String alias;
List<dynamic> tags;
bool online = false;
Peer.fromJson(Map<String, dynamic> json)
@ -19,6 +19,17 @@ class Peer {
alias = json['alias'] ?? '',
tags = json['tags'] ?? [];
Map<String, dynamic> toJson() {
return <String, dynamic>{
"id": id,
"username": username,
"hostname": hostname,
"platform": platform,
"alias": alias,
"tags": tags,
};
}
Peer({
required this.id,
required this.username,