aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/app/src/main/java/com/wireguard/config/Config.java
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-04-28 04:45:17 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-04-28 04:51:43 +0200
commit63071f57b7b501c23843f502a5d2a1ac83338bc7 (patch)
tree21a9a0f4de77b67669949534c27e033d1ee896c3 /app/src/main/java/com/wireguard/config/Config.java
parentDo not do DNS lookups for IPs (diff)
downloadwireguard-android-63071f57b7b501c23843f502a5d2a1ac83338bc7.tar.xz
wireguard-android-63071f57b7b501c23843f502a5d2a1ac83338bc7.zip
Use validation instead of two-way binding
This is insane, but it appears to be working. We essentially store things in a separate class for editing, and then commit it back at a given time. This business with onViewStateRestored in both TunnelEditorFragment and in TunnelDetailFragment is buggy and likely wrong. In general TunnelEditorFragment should probably be rewritten. The relationship with the changed name is not clear. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'app/src/main/java/com/wireguard/config/Config.java')
-rw-r--r--app/src/main/java/com/wireguard/config/Config.java62
1 files changed, 59 insertions, 3 deletions
diff --git a/app/src/main/java/com/wireguard/config/Config.java b/app/src/main/java/com/wireguard/config/Config.java
index a3341d5f..e4731a0c 100644
--- a/app/src/main/java/com/wireguard/config/Config.java
+++ b/app/src/main/java/com/wireguard/config/Config.java
@@ -1,6 +1,9 @@
package com.wireguard.config;
+import com.android.databinding.library.baseAdapters.BR;
+
import android.databinding.BaseObservable;
+import android.databinding.Bindable;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableList;
import android.os.Parcel;
@@ -11,12 +14,14 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.List;
/**
* Represents a wg-quick configuration file, its name, and its connection state.
*/
-public class Config extends BaseObservable implements Parcelable {
+public class Config implements Parcelable {
public static final Creator<Config> CREATOR = new Creator<Config>() {
@Override
public Config createFromParcel(final Parcel in) {
@@ -29,8 +34,59 @@ public class Config extends BaseObservable implements Parcelable {
}
};
+ public static class Observable extends BaseObservable {
+ private String name;
+ private Interface.Observable observableInterface;
+ private ObservableList<Peer.Observable> observablePeers;
+
+
+ public Observable(Config parent, String name) {
+ this.name = name;
+ loadData(parent);
+ }
+
+ public void loadData(Config parent) {
+ this.observableInterface = new Interface.Observable(parent.interfaceSection);
+ this.observablePeers = new ObservableArrayList<>();
+ for (Peer peer : parent.getPeers())
+ this.observablePeers.add(new Peer.Observable(peer));
+ }
+
+ public void commitData(Config parent) {
+ this.observableInterface.commitData(parent.interfaceSection);
+ List<Peer> newPeers = new ArrayList<>(this.observablePeers.size());
+ for (Peer.Observable observablePeer : this.observablePeers) {
+ Peer peer = new Peer();
+ observablePeer.commitData(peer);
+ newPeers.add(peer);
+ }
+ parent.peers = newPeers;
+ notifyChange();
+ }
+
+ @Bindable
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ notifyPropertyChanged(BR.name);
+ }
+
+ @Bindable
+ public Interface.Observable getInterfaceSection() {
+ return observableInterface;
+ }
+
+ @Bindable
+ public ObservableList<Peer.Observable> getPeers() {
+ return observablePeers;
+ }
+ }
+
private final Interface interfaceSection;
- private final ObservableList<Peer> peers = new ObservableArrayList<>();
+ private List<Peer> peers = new ArrayList<>();
public Config() {
interfaceSection = new Interface();
@@ -83,7 +139,7 @@ public class Config extends BaseObservable implements Parcelable {
return interfaceSection;
}
- public ObservableList<Peer> getPeers() {
+ public List<Peer> getPeers() {
return peers;
}