Convert the list of tunnels to a KeyedObservableList

Signed-off-by: Samuel Holland <samuel@sholland.org>
This commit is contained in:
Samuel Holland
2018-01-06 04:09:30 -06:00
parent c73287f64b
commit ff0bb081a0
8 changed files with 73 additions and 45 deletions
@@ -9,6 +9,7 @@ import com.wireguard.android.BR;
import com.wireguard.android.backend.Backend;
import com.wireguard.android.configStore.ConfigStore;
import com.wireguard.android.util.ExceptionLoggers;
import com.wireguard.android.util.Keyed;
import com.wireguard.config.Config;
import org.threeten.bp.Instant;
@@ -23,7 +24,7 @@ import java9.util.concurrent.CompletionStage;
* Encapsulates the volatile and nonvolatile state of a WireGuard tunnel.
*/
public class Tunnel extends BaseObservable implements Comparable<Tunnel> {
public class Tunnel extends BaseObservable implements Keyed<String> {
public static final int NAME_MAX_LENGTH = 16;
private static final Pattern NAME_PATTERN = Pattern.compile("[a-zA-Z0-9_=+.-]{1,16}");
private static final String TAG = Tunnel.class.getSimpleName();
@@ -48,11 +49,6 @@ public class Tunnel extends BaseObservable implements Comparable<Tunnel> {
return name != null && NAME_PATTERN.matcher(name).matches();
}
@Override
public int compareTo(@NonNull final Tunnel tunnel) {
return name.compareTo(tunnel.name);
}
@Bindable
public Config getConfig() {
if (config == null)
@@ -66,6 +62,11 @@ public class Tunnel extends BaseObservable implements Comparable<Tunnel> {
return CompletableFuture.completedFuture(config);
}
@Override
public String getKey() {
return name;
}
@Bindable
public Instant getLastStateChange() {
return lastStateChange;
@@ -1,10 +0,0 @@
package com.wireguard.android.model;
import com.wireguard.android.databinding.ObservableTreeMap;
/**
* Created by samuel on 12/19/17.
*/
public class TunnelCollection extends ObservableTreeMap<String, Tunnel> {
}
@@ -8,6 +8,8 @@ import com.wireguard.android.backend.Backend;
import com.wireguard.android.configStore.ConfigStore;
import com.wireguard.android.model.Tunnel.State;
import com.wireguard.android.util.ExceptionLoggers;
import com.wireguard.android.util.KeyedObservableList;
import com.wireguard.android.util.SortedKeyedObservableArrayList;
import com.wireguard.config.Config;
import java.util.Collections;
@@ -35,7 +37,8 @@ public final class TunnelManager {
private final Backend backend;
private final ConfigStore configStore;
private final SharedPreferences preferences;
private final TunnelCollection tunnels = new TunnelCollection();
private final KeyedObservableList<String, Tunnel> tunnels =
new SortedKeyedObservableArrayList<>();
@Inject
public TunnelManager(final Backend backend, final ConfigStore configStore,
@@ -47,7 +50,7 @@ public final class TunnelManager {
private Tunnel add(final String name, final Config config) {
final Tunnel tunnel = new Tunnel(backend, configStore, name, config);
tunnels.put(name, tunnel);
tunnels.add(tunnel);
return tunnel;
}
@@ -71,13 +74,13 @@ public final class TunnelManager {
return backend.setState(tunnel, State.DOWN)
.thenCompose(x -> configStore.delete(tunnel.getName()))
.thenAccept(x -> {
tunnels.remove(tunnel.getName());
tunnels.remove(tunnel);
if (tunnel.getName().equals(preferences.getString(KEY_PRIMARY_TUNNEL, null)))
preferences.edit().remove(KEY_PRIMARY_TUNNEL).apply();
});
}
public TunnelCollection getTunnels() {
public KeyedObservableList<String, Tunnel> getTunnels() {
return tunnels;
}
@@ -105,7 +108,7 @@ public final class TunnelManager {
}
public CompletionStage<Void> saveState() {
final Set<String> runningTunnels = StreamSupport.stream(tunnels.values())
final Set<String> runningTunnels = StreamSupport.stream(tunnels)
.filter(tunnel -> tunnel.getState() == State.UP)
.map(Tunnel::getName)
.collect(Collectors.toUnmodifiableSet());