import 'dart:convert'; import 'package:flutter/material.dart'; import 'package:flutter_hbb/models/model.dart'; import 'package:flutter_hbb/models/platform_model.dart'; import 'package:get/get.dart'; import 'package:http/http.dart' as http; class AbModel with ChangeNotifier { var abLoading = false; var abError = ""; var tags = [].obs; var peers = [].obs; var selectedTags = List.empty(growable: true).obs; WeakReference parent; AbModel(this.parent); FFI? get _ffi => parent.target; Future getAb() async { abLoading = true; notifyListeners(); // request final api = "${await getApiServer()}/api/ab/get"; try { final resp = await http.post(Uri.parse(api), headers: await _getHeaders()); Map json = jsonDecode(resp.body); if (json.containsKey('error')) { abError = json['error']; } else if (json.containsKey('data')) { final data = jsonDecode(json['data']); tags.value = data['tags']; peers.value = data['peers']; } return resp.body; } catch (err) { abError = err.toString(); } finally { abLoading = false; } notifyListeners(); return null; } Future getApiServer() async { return await bind.mainGetApiServer(); } void reset() { tags.clear(); peers.clear(); notifyListeners(); } Future>? _getHeaders() { return _ffi?.getHttpHeaders(); } /// void addId(String id) async { if (idContainBy(id)) { return; } peers.add({"id": id}); notifyListeners(); } void addTag(String tag) async { if (tagContainBy(tag)) { return; } tags.add(tag); notifyListeners(); } void changeTagForPeer(String id, List tags) { final it = peers.where((element) => element['id'] == id); if (it.isEmpty) { return; } it.first['tags'] = tags; } Future updateAb() async { abLoading = true; notifyListeners(); final api = "${await getApiServer()}/api/ab"; var authHeaders = await _getHeaders() ?? Map(); authHeaders['Content-Type'] = "application/json"; final body = jsonEncode({ "data": jsonEncode({"tags": tags, "peers": peers}) }); final resp = await http.post(Uri.parse(api), headers: authHeaders, body: body); abLoading = false; await getAb(); notifyListeners(); debugPrint("resp: ${resp.body}"); } bool idContainBy(String id) { return peers.where((element) => element['id'] == id).isNotEmpty; } bool tagContainBy(String tag) { return tags.where((element) => element == tag).isNotEmpty; } void deletePeer(String 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) { continue; } if (((peer['tags']) as List).contains(tag)) { ((peer['tags']) as List).remove(tag); } } notifyListeners(); } void unsetSelectedTags() { selectedTags.clear(); notifyListeners(); } List getPeerTags(String id) { final it = peers.where((p0) => p0['id'] == id); if (it.isEmpty) { return []; } else { return it.first['tags'] ?? []; } } void setPeerOption(String id, String key, String value) { final it = peers.where((p0) => p0['id'] == id); if (it.isEmpty) { debugPrint("${id} is not exists"); return; } else { it.first[key] = value; } } void clear() { peers.clear(); tags.clear(); notifyListeners(); } }