Line data Source code
1 : import 'package:flutter/material.dart'; 2 : 3 : class ServerListState extends ChangeNotifier { 4 : List<ServerInfoState> _servers = []; 5 : 6 0 : void replace(Iterable<ServerInfoState> newServers) { 7 0 : _servers.clear(); 8 0 : _servers.addAll(newServers); 9 0 : notifyListeners(); 10 : } 11 : 12 0 : void clear() { 13 0 : _servers.clear(); 14 : } 15 : 16 0 : ServerInfoState? getServer(String onion) { 17 0 : int idx = _servers.indexWhere((element) => element.onion == onion); 18 0 : return idx >= 0 ? _servers[idx] : null; 19 : } 20 : 21 0 : void add(String onion, String serverBundle, bool running, String description, bool autoStart, bool isEncrypted) { 22 0 : var sis = ServerInfoState(onion: onion, serverBundle: serverBundle, running: running, description: description, autoStart: autoStart, isEncrypted: isEncrypted); 23 0 : int idx = _servers.indexWhere((element) => element.onion == onion); 24 0 : if (idx >= 0) { 25 0 : _servers[idx] = sis; 26 : } else { 27 0 : _servers.add(ServerInfoState(onion: onion, serverBundle: serverBundle, running: running, description: description, autoStart: autoStart, isEncrypted: isEncrypted)); 28 : } 29 0 : notifyListeners(); 30 : } 31 : 32 0 : void updateServer(String onion, String serverBundle, bool running, String description, bool autoStart, bool isEncrypted) { 33 0 : int idx = _servers.indexWhere((element) => element.onion == onion); 34 0 : if (idx >= 0) { 35 0 : _servers[idx] = ServerInfoState(onion: onion, serverBundle: serverBundle, running: running, description: description, autoStart: autoStart, isEncrypted: isEncrypted); 36 : } else { 37 0 : print("Tried to update server list without a starting state...this is probably an error"); 38 : } 39 0 : notifyListeners(); 40 : } 41 : 42 0 : void updateServerStats(String onion, int newTotalMessages, int newConnections) { 43 0 : var server = getServer(onion); 44 : if (server != null) { 45 0 : server.setStats(newTotalMessages, newConnections); 46 0 : resort(); 47 0 : notifyListeners(); 48 : } 49 : } 50 : 51 0 : void delete(String onion) { 52 0 : _servers.removeWhere((element) => element.onion == onion); 53 0 : notifyListeners(); 54 : } 55 : 56 0 : void resort() { 57 0 : _servers.sort((ServerInfoState a, ServerInfoState b) { 58 : // return -1 = a first in list 59 : // return 1 = b first in list 60 0 : if (a.totalMessages > b.totalMessages) { 61 0 : return -1; 62 0 : } else if (b.totalMessages > a.totalMessages) { 63 : return 1; 64 : } 65 : 66 : return 0; 67 : }); 68 : } 69 : 70 0 : List<ServerInfoState> get servers => _servers.sublist(0); //todo: copy?? dont want caller able to bypass changenotifier 71 : } 72 : 73 : class ServerInfoState extends ChangeNotifier { 74 : String onion; 75 : String serverBundle; 76 : String description; 77 : bool running; 78 : bool autoStart; 79 : bool isEncrypted; 80 : int totalMessages = 0; 81 : int connections = 0; 82 : 83 0 : ServerInfoState({required this.onion, required this.serverBundle, required this.running, required this.description, required this.autoStart, required this.isEncrypted}); 84 : 85 0 : void setAutostart(bool val) { 86 0 : autoStart = val; 87 0 : notifyListeners(); 88 : } 89 : 90 0 : void setRunning(bool val) { 91 0 : running = val; 92 0 : notifyListeners(); 93 : } 94 : 95 0 : void setDescription(String val) { 96 0 : description = val; 97 0 : notifyListeners(); 98 : } 99 : 100 0 : void setStats(int newTotalMessages, int newConnections) { 101 0 : totalMessages = newTotalMessages; 102 0 : connections = newConnections; 103 0 : notifyListeners(); 104 : } 105 : }