ConfigActivity: Listen for name changes

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Samuel Holland 2017-11-28 17:25:49 -06:00
parent 2f310f0fad
commit 74a6526695
3 changed files with 28 additions and 5 deletions

View File

@ -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).
}

View File

@ -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) {

View File

@ -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;