cwtch-ui/lib/models/profileservers.dart

37 lines
1.1 KiB
Dart
Raw Permalink Normal View History

2021-06-24 23:10:45 +00:00
import 'package:flutter/material.dart';
2021-10-29 23:37:02 +00:00
class ProfileServerListState extends ChangeNotifier {
List<RemoteServerInfoState> _servers = [];
2021-06-24 23:10:45 +00:00
2021-10-29 23:37:02 +00:00
void replace(Iterable<RemoteServerInfoState> newServers) {
2021-06-24 23:10:45 +00:00
_servers.clear();
_servers.addAll(newServers);
notifyListeners();
}
2021-10-29 23:37:02 +00:00
RemoteServerInfoState? getServer(String onion) {
2021-06-24 23:10:45 +00:00
int idx = _servers.indexWhere((element) => element.onion == onion);
return idx >= 0 ? _servers[idx] : null;
}
void updateServerCache(String onion, String status) {
int idx = _servers.indexWhere((element) => element.onion == onion);
if (idx >= 0) {
2021-10-29 23:37:02 +00:00
_servers[idx] = RemoteServerInfoState(onion: onion, status: status);
2021-06-24 23:10:45 +00:00
} else {
print("Tried to update server cache without a starting state...this is probably an error");
}
notifyListeners();
}
2021-10-29 23:37:02 +00:00
List<RemoteServerInfoState> get servers => _servers.sublist(0); //todo: copy?? dont want caller able to bypass changenotifier
2021-06-24 23:10:45 +00:00
}
2021-10-29 23:37:02 +00:00
class RemoteServerInfoState extends ChangeNotifier {
2021-06-24 23:10:45 +00:00
final String onion;
final String status;
2021-10-29 23:37:02 +00:00
RemoteServerInfoState({required this.onion, required this.status});
2021-06-24 23:10:45 +00:00
}