From 562c05183b812fa5fe19389d4554682419b4c877 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Mon, 1 Nov 2021 19:45:17 -0700 Subject: [PATCH] server list, add edit --- lib/models/servers.dart | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 lib/models/servers.dart diff --git a/lib/models/servers.dart b/lib/models/servers.dart new file mode 100644 index 00000000..2cafbe60 --- /dev/null +++ b/lib/models/servers.dart @@ -0,0 +1,65 @@ +import 'package:flutter/material.dart'; + +class ServerListState extends ChangeNotifier { + List _servers = []; + + void replace(Iterable newServers) { + _servers.clear(); + _servers.addAll(newServers); + notifyListeners(); + } + + void clear() { + _servers.clear(); + } + + ServerInfoState? getServer(String onion) { + int idx = _servers.indexWhere((element) => element.onion == onion); + return idx >= 0 ? _servers[idx] : null; + } + + void add(String onion, String serverBundle, bool running, String description, bool autoStart, bool isEncrypted) { + print("servers.add desc:$description autostart: $autoStart"); + _servers.add(ServerInfoState(onion: onion, serverBundle: serverBundle, running: running, description: description, autoStart: autoStart, isEncrypted: isEncrypted)); + notifyListeners(); + } + + void updateServer(String onion, String serverBundle, bool running, String description, bool autoStart, bool isEncrypted) { + int idx = _servers.indexWhere((element) => element.onion == onion); + if (idx >= 0) { + _servers[idx] = ServerInfoState(onion: onion, serverBundle: serverBundle, running: running, description: description, autoStart: autoStart, isEncrypted: isEncrypted); + } else { + print("Tried to update server list without a starting state...this is probably an error"); + } + notifyListeners(); + } + + List get servers => _servers.sublist(0); //todo: copy?? dont want caller able to bypass changenotifier + +} + +class ServerInfoState extends ChangeNotifier { + String onion; + String serverBundle; + String description; + bool running; + bool autoStart; + bool isEncrypted; + + ServerInfoState({required this.onion, required this.serverBundle, required this.running, required this.description, required this.autoStart, required this.isEncrypted}); + + void setAutostart(bool val) { + autoStart = val; + notifyListeners(); + } + + void setRunning(bool val) { + running = val; + notifyListeners(); + } + + void setDescription(String val) { + description = val; + notifyListeners(); + } +}