From 74a65266950268469471bc82721578baca5e9df5 Mon Sep 17 00:00:00 2001 From: Samuel Holland Date: Tue, 28 Nov 2017 17:25:49 -0600 Subject: [PATCH] ConfigActivity: Listen for name changes Signed-off-by: Jason A. Donenfeld --- .../com/wireguard/android/AddActivity.java | 2 +- .../wireguard/android/BaseConfigActivity.java | 5 ++-- .../com/wireguard/android/ConfigActivity.java | 26 +++++++++++++++++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/wireguard/android/AddActivity.java b/app/src/main/java/com/wireguard/android/AddActivity.java index 080eecae..5b57e634 100644 --- a/app/src/main/java/com/wireguard/android/AddActivity.java +++ b/app/src/main/java/com/wireguard/android/AddActivity.java @@ -18,7 +18,7 @@ public class AddActivity extends BaseConfigActivity { } @Override - protected void onCurrentConfigChanged(final Config config) { + protected void onCurrentConfigChanged(final Config oldConfig, final Config newConfig) { // Do nothing (this never happens). } diff --git a/app/src/main/java/com/wireguard/android/BaseConfigActivity.java b/app/src/main/java/com/wireguard/android/BaseConfigActivity.java index 14a9384c..1bd5c70d 100644 --- a/app/src/main/java/com/wireguard/android/BaseConfigActivity.java +++ b/app/src/main/java/com/wireguard/android/BaseConfigActivity.java @@ -52,7 +52,7 @@ abstract class BaseConfigActivity extends Activity { Context.BIND_AUTO_CREATE); } - protected abstract void onCurrentConfigChanged(Config config); + protected abstract void onCurrentConfigChanged(Config oldCconfig, Config newConfig); protected abstract void onEditingStateChanged(boolean isEditing); @@ -74,8 +74,9 @@ abstract class BaseConfigActivity extends Activity { public void setCurrentConfig(final Config config) { if (currentConfig == config) return; + final Config oldConfig = currentConfig; currentConfig = config; - onCurrentConfigChanged(config); + onCurrentConfigChanged(oldConfig, config); } public void setIsEditing(final boolean isEditing) { diff --git a/app/src/main/java/com/wireguard/android/ConfigActivity.java b/app/src/main/java/com/wireguard/android/ConfigActivity.java index 439f77da..24284eb1 100644 --- a/app/src/main/java/com/wireguard/android/ConfigActivity.java +++ b/app/src/main/java/com/wireguard/android/ConfigActivity.java @@ -4,6 +4,7 @@ import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.Intent; +import android.databinding.Observable; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; @@ -30,6 +31,7 @@ public class ConfigActivity extends BaseConfigActivity { private boolean isSingleLayout; private boolean isStateSaved; private int mainContainer; + private Observable.OnPropertyChangedCallback nameChangeCallback; private String visibleFragmentTag; /** @@ -161,6 +163,11 @@ public class ConfigActivity extends BaseConfigActivity { setContentView(R.layout.config_activity); isSingleLayout = findViewById(R.id.detail_fragment) == null; mainContainer = isSingleLayout ? R.id.master_fragment : R.id.detail_fragment; + if (isSingleLayout) { + nameChangeCallback = new ConfigNameChangeCallback(); + if (getCurrentConfig() != null) + getCurrentConfig().addOnPropertyChangedCallback(nameChangeCallback); + } isLayoutFinished = true; moveToState(getCurrentConfig(), isEditing()); } @@ -172,9 +179,13 @@ public class ConfigActivity extends BaseConfigActivity { } @Override - protected void onCurrentConfigChanged(final Config config) { + protected void onCurrentConfigChanged(final Config oldConfig, final Config newConfig) { + if (nameChangeCallback != null && oldConfig != null) + oldConfig.removeOnPropertyChangedCallback(nameChangeCallback); // Abandon editing a config when the current config changes. - moveToState(config, false); + moveToState(newConfig, false); + if (nameChangeCallback != null && newConfig != null) + newConfig.addOnPropertyChangedCallback(nameChangeCallback); } @Override @@ -247,6 +258,17 @@ public class ConfigActivity extends BaseConfigActivity { moveToState(getCurrentConfig(), isEditing()); } + private class ConfigNameChangeCallback extends Observable.OnPropertyChangedCallback { + @Override + public void onPropertyChanged(final Observable sender, final int propertyId) { + if (sender != getCurrentConfig()) + sender.removeOnPropertyChangedCallback(this); + if (propertyId != 0 && propertyId != BR.name) + return; + setTitle(getCurrentConfig().getName()); + } + } + private static class FragmentCache { private ConfigDetailFragment detailFragment; private ConfigEditFragment editFragment;