|
24 | 24 | import com.rockthevote.grommet.ui.misc.BetterSpinner; |
25 | 25 | import com.rockthevote.grommet.ui.misc.ChildrenViewStateHelper; |
26 | 26 | import com.rockthevote.grommet.ui.misc.ObservableValidator; |
| 27 | +import com.rockthevote.grommet.util.Strings; |
27 | 28 | import com.squareup.sqlbrite.BriteDatabase; |
28 | 29 |
|
29 | 30 | import java.util.concurrent.TimeUnit; |
|
41 | 42 | public class AddressView extends FrameLayout { |
42 | 43 | private static final String PA_ABREV = "PA"; |
43 | 44 |
|
| 45 | + private static final String COUNTY_ENABLED_KEY = "county_enabled_key"; |
| 46 | + |
| 47 | + private String childrenStateKey; |
| 48 | + private String superStateKey; |
| 49 | + |
44 | 50 | @NotEmpty |
45 | 51 | @BindView(R.id.til_street_address) TextInputLayout streetTIL; |
46 | 52 | @BindView(R.id.street) EditText streetEditText; |
@@ -112,10 +118,14 @@ public AddressView(Context context, AttributeSet attrs, int defStyleAttr) { |
112 | 118 | break; |
113 | 119 | case 4: |
114 | 120 | type = Address.Type.ASSISTANT_ADDRESS; |
| 121 | + break; |
115 | 122 | } |
116 | 123 | } finally { |
117 | 124 | typedArray.recycle(); |
118 | 125 | } |
| 126 | + |
| 127 | + superStateKey = AddressView.class.getSimpleName() + ".superState." + type.toString(); |
| 128 | + childrenStateKey = AddressView.class.getSimpleName() + ".childState." + type.toString(); |
119 | 129 | } |
120 | 130 | } |
121 | 131 |
|
@@ -163,16 +173,15 @@ protected void onFinishInflate() { |
163 | 173 | if (!PA_ABREV.equals(stateAdapter.getItem(i))) { |
164 | 174 | countySpinner.setErrorEnabled(false); |
165 | 175 | countySpinner.setEnabled(false); |
166 | | - countySpinner.getEditText().setText(""); |
167 | | - countySpinner.getEditText().setEnabled(false); |
168 | 176 | } else { |
169 | | - countySpinner.getEditText().setEnabled(true); |
170 | 177 | countySpinner.setEnabled(true); |
171 | 178 | } |
172 | 179 |
|
173 | 180 | stateSpinner.dismiss(); |
174 | 181 | }); |
175 | | - stateSpinner.getEditText().setText(stateAdapter.getItem(stateAdapter.getPosition(PA_ABREV))); |
| 182 | + if (Strings.isBlank(stateSpinner.getEditText().getEditableText().toString())) { |
| 183 | + stateSpinner.getEditText().setText(stateAdapter.getItem(stateAdapter.getPosition(PA_ABREV))); |
| 184 | + } |
176 | 185 | } |
177 | 186 | } |
178 | 187 |
|
@@ -211,19 +220,21 @@ protected void onDetachedFromWindow() { |
211 | 220 | @Override |
212 | 221 | protected Parcelable onSaveInstanceState() { |
213 | 222 | final Bundle state = new Bundle(); |
214 | | - state.putParcelable("superState", super.onSaveInstanceState()); |
215 | | - state.putSparseParcelableArray(ChildrenViewStateHelper.DEFAULT_CHILDREN_STATE_KEY, |
216 | | - ChildrenViewStateHelper.newInstance(this).saveChildrenState()); |
| 223 | + state.putParcelable(superStateKey, super.onSaveInstanceState()); |
| 224 | + state.putBoolean(COUNTY_ENABLED_KEY, countySpinner.isEnabled()); |
| 225 | + state.putSparseParcelableArray(childrenStateKey, |
| 226 | + ChildrenViewStateHelper.newInstance(this).saveChildrenState(childrenStateKey)); |
217 | 227 | return state; |
218 | 228 | } |
219 | 229 |
|
220 | 230 | @Override |
221 | 231 | protected void onRestoreInstanceState(final Parcelable state) { |
222 | 232 | if (state instanceof Bundle) { |
223 | 233 | final Bundle localState = (Bundle) state; |
224 | | - super.onRestoreInstanceState(localState.getParcelable("superState")); |
| 234 | + super.onRestoreInstanceState(localState.getParcelable(superStateKey)); |
225 | 235 | ChildrenViewStateHelper.newInstance(this).restoreChildrenState(localState |
226 | | - .getSparseParcelableArray(ChildrenViewStateHelper.DEFAULT_CHILDREN_STATE_KEY)); |
| 236 | + .getSparseParcelableArray(childrenStateKey), childrenStateKey); |
| 237 | + countySpinner.setEnabled(localState.getBoolean(COUNTY_ENABLED_KEY, true)); |
227 | 238 | } else { |
228 | 239 | super.onRestoreInstanceState(state); |
229 | 240 | } |
|
0 commit comments