| 
									
										
										
										
											2022-07-25 16:26:51 +08:00
										 |  |  | import 'dart:convert'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import 'package:flutter/material.dart'; | 
					
						
							|  |  |  | import 'package:flutter_hbb/models/model.dart'; | 
					
						
							| 
									
										
										
										
											2022-08-03 22:03:31 +08:00
										 |  |  | import 'package:flutter_hbb/models/platform_model.dart'; | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  | import 'package:get/get.dart'; | 
					
						
							| 
									
										
										
										
											2022-07-25 16:26:51 +08:00
										 |  |  | import 'package:http/http.dart' as http; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class AbModel with ChangeNotifier { | 
					
						
							|  |  |  |   var abLoading = false; | 
					
						
							|  |  |  |   var abError = ""; | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  |   var tags = [].obs; | 
					
						
							|  |  |  |   var peers = [].obs; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   var selectedTags = List<String>.empty(growable: true).obs; | 
					
						
							| 
									
										
										
										
											2022-07-25 16:26:51 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   WeakReference<FFI> parent; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   AbModel(this.parent); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   FFI? get _ffi => parent.target; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<dynamic> getAb() async { | 
					
						
							|  |  |  |     abLoading = true; | 
					
						
							|  |  |  |     notifyListeners(); | 
					
						
							|  |  |  |     // request
 | 
					
						
							|  |  |  |     final api = "${await getApiServer()}/api/ab/get"; | 
					
						
							| 
									
										
										
										
											2022-07-26 17:14:52 +08:00
										 |  |  |     try { | 
					
						
							|  |  |  |       final resp = | 
					
						
							|  |  |  |           await http.post(Uri.parse(api), headers: await _getHeaders()); | 
					
						
							| 
									
										
										
										
											2022-09-15 11:06:44 +08:00
										 |  |  |       if (resp.body.isNotEmpty && resp.body.toLowerCase() != "null") { | 
					
						
							|  |  |  |         Map<String, dynamic> 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']; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         notifyListeners(); | 
					
						
							|  |  |  |         return resp.body; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         return ""; | 
					
						
							| 
									
										
										
										
											2022-07-26 17:14:52 +08:00
										 |  |  |       } | 
					
						
							|  |  |  |     } catch (err) { | 
					
						
							|  |  |  |       abError = err.toString(); | 
					
						
							|  |  |  |     } finally { | 
					
						
							| 
									
										
										
										
											2022-09-15 11:06:44 +08:00
										 |  |  |       notifyListeners(); | 
					
						
							| 
									
										
										
										
											2022-07-26 17:14:52 +08:00
										 |  |  |       abLoading = false; | 
					
						
							| 
									
										
										
										
											2022-07-25 16:26:51 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2022-07-26 17:14:52 +08:00
										 |  |  |     return null; | 
					
						
							| 
									
										
										
										
											2022-07-25 16:26:51 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<String> getApiServer() async { | 
					
						
							| 
									
										
										
										
											2022-08-03 22:13:40 +08:00
										 |  |  |     return await bind.mainGetApiServer(); | 
					
						
							| 
									
										
										
										
											2022-07-25 16:26:51 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void reset() { | 
					
						
							|  |  |  |     tags.clear(); | 
					
						
							|  |  |  |     peers.clear(); | 
					
						
							|  |  |  |     notifyListeners(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<Map<String, String>>? _getHeaders() { | 
					
						
							|  |  |  |     return _ffi?.getHttpHeaders(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-07-26 17:03:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   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<dynamic> tags) { | 
					
						
							|  |  |  |     final it = peers.where((element) => element['id'] == id); | 
					
						
							|  |  |  |     if (it.isEmpty) { | 
					
						
							|  |  |  |       return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     it.first['tags'] = tags; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   Future<void> updateAb() async { | 
					
						
							|  |  |  |     abLoading = true; | 
					
						
							|  |  |  |     notifyListeners(); | 
					
						
							|  |  |  |     final api = "${await getApiServer()}/api/ab"; | 
					
						
							|  |  |  |     var authHeaders = await _getHeaders() ?? Map<String, String>(); | 
					
						
							|  |  |  |     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<dynamic>).contains(tag)) { | 
					
						
							|  |  |  |         ((peer['tags']) as List<dynamic>).remove(tag); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     notifyListeners(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   void unsetSelectedTags() { | 
					
						
							|  |  |  |     selectedTags.clear(); | 
					
						
							|  |  |  |     notifyListeners(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   List<dynamic> getPeerTags(String id) { | 
					
						
							|  |  |  |     final it = peers.where((p0) => p0['id'] == id); | 
					
						
							|  |  |  |     if (it.isEmpty) { | 
					
						
							|  |  |  |       return []; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return it.first['tags'] ?? []; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-07-27 14:29:47 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-29 12:03:24 +08:00
										 |  |  |   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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-27 14:29:47 +08:00
										 |  |  |   void clear() { | 
					
						
							|  |  |  |     peers.clear(); | 
					
						
							|  |  |  |     tags.clear(); | 
					
						
							|  |  |  |     notifyListeners(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-07-25 16:26:51 +08:00
										 |  |  | } |