ConfigActivity: Listen for name changes
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
parent
2f310f0fad
commit
74a6526695
@ -18,7 +18,7 @@ public class AddActivity extends BaseConfigActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCurrentConfigChanged(final Config config) {
|
protected void onCurrentConfigChanged(final Config oldConfig, final Config newConfig) {
|
||||||
// Do nothing (this never happens).
|
// Do nothing (this never happens).
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ abstract class BaseConfigActivity extends Activity {
|
|||||||
Context.BIND_AUTO_CREATE);
|
Context.BIND_AUTO_CREATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract void onCurrentConfigChanged(Config config);
|
protected abstract void onCurrentConfigChanged(Config oldCconfig, Config newConfig);
|
||||||
|
|
||||||
protected abstract void onEditingStateChanged(boolean isEditing);
|
protected abstract void onEditingStateChanged(boolean isEditing);
|
||||||
|
|
||||||
@ -74,8 +74,9 @@ abstract class BaseConfigActivity extends Activity {
|
|||||||
public void setCurrentConfig(final Config config) {
|
public void setCurrentConfig(final Config config) {
|
||||||
if (currentConfig == config)
|
if (currentConfig == config)
|
||||||
return;
|
return;
|
||||||
|
final Config oldConfig = currentConfig;
|
||||||
currentConfig = config;
|
currentConfig = config;
|
||||||
onCurrentConfigChanged(config);
|
onCurrentConfigChanged(oldConfig, config);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setIsEditing(final boolean isEditing) {
|
public void setIsEditing(final boolean isEditing) {
|
||||||
|
@ -4,6 +4,7 @@ import android.app.Fragment;
|
|||||||
import android.app.FragmentManager;
|
import android.app.FragmentManager;
|
||||||
import android.app.FragmentTransaction;
|
import android.app.FragmentTransaction;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.databinding.Observable;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
@ -30,6 +31,7 @@ public class ConfigActivity extends BaseConfigActivity {
|
|||||||
private boolean isSingleLayout;
|
private boolean isSingleLayout;
|
||||||
private boolean isStateSaved;
|
private boolean isStateSaved;
|
||||||
private int mainContainer;
|
private int mainContainer;
|
||||||
|
private Observable.OnPropertyChangedCallback nameChangeCallback;
|
||||||
private String visibleFragmentTag;
|
private String visibleFragmentTag;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -161,6 +163,11 @@ public class ConfigActivity extends BaseConfigActivity {
|
|||||||
setContentView(R.layout.config_activity);
|
setContentView(R.layout.config_activity);
|
||||||
isSingleLayout = findViewById(R.id.detail_fragment) == null;
|
isSingleLayout = findViewById(R.id.detail_fragment) == null;
|
||||||
mainContainer = isSingleLayout ? R.id.master_fragment : R.id.detail_fragment;
|
mainContainer = isSingleLayout ? R.id.master_fragment : R.id.detail_fragment;
|
||||||
|
if (isSingleLayout) {
|
||||||
|
nameChangeCallback = new ConfigNameChangeCallback();
|
||||||
|
if (getCurrentConfig() != null)
|
||||||
|
getCurrentConfig().addOnPropertyChangedCallback(nameChangeCallback);
|
||||||
|
}
|
||||||
isLayoutFinished = true;
|
isLayoutFinished = true;
|
||||||
moveToState(getCurrentConfig(), isEditing());
|
moveToState(getCurrentConfig(), isEditing());
|
||||||
}
|
}
|
||||||
@ -172,9 +179,13 @@ public class ConfigActivity extends BaseConfigActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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.
|
// Abandon editing a config when the current config changes.
|
||||||
moveToState(config, false);
|
moveToState(newConfig, false);
|
||||||
|
if (nameChangeCallback != null && newConfig != null)
|
||||||
|
newConfig.addOnPropertyChangedCallback(nameChangeCallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -247,6 +258,17 @@ public class ConfigActivity extends BaseConfigActivity {
|
|||||||
moveToState(getCurrentConfig(), isEditing());
|
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 static class FragmentCache {
|
||||||
private ConfigDetailFragment detailFragment;
|
private ConfigDetailFragment detailFragment;
|
||||||
private ConfigEditFragment editFragment;
|
private ConfigEditFragment editFragment;
|
||||||
|
Loading…
Reference in New Issue
Block a user