Skip to content

Commit

Permalink
Merge pull request #84 from huttneab/bug_fixes_3
Browse files Browse the repository at this point in the history
Fix a big UI bug for save instance state.
  • Loading branch information
huttneab authored Aug 26, 2016
2 parents 1f4d5a8 + f0894cc commit 6ca57f5
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Manifest version information!
def versionMajor = 1
def versionMinor = 0
def versionPatch = 7
def versionPatch = 8
def versionBuild = 0 // bump for dogfood builds, public betas, etc.

apply plugin: 'com.android.application'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public void toJson(JsonWriter writer, ApiAddress value) throws IOException {
writer.beginObject();
writer.name("sub_address_type");
writer.value("APT");
writer.name("subAddress");
writer.name("sub_address");
subAddressAdapter.toJson(writer, value.subAddress());
writer.endObject();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package com.rockthevote.grommet.ui.misc;

import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.ListPopupWindow;
import android.util.AttributeSet;
import android.util.SparseArray;
import android.widget.AdapterView;
import android.widget.ListAdapter;

import com.rockthevote.grommet.R;


public class BetterSpinner extends TextInputLayout {
private String childrenStateKey;
private String superStateKey;

ListPopupWindow listPopupWindow;
ListAdapter listAdapter;
Expand All @@ -29,6 +34,10 @@ public BetterSpinner(Context context, AttributeSet attrs) {
public BetterSpinner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

superStateKey = BetterSpinner.class.getSimpleName() + ".superState";
childrenStateKey = BetterSpinner.class.getSimpleName() + ".childState";


editText = new TextInputEditText(context);
editText.setId(R.id.titleId);
editText.setHeight((int) getResources().getDimension(R.dimen.list_item_height));
Expand Down Expand Up @@ -64,6 +73,46 @@ public void setHeight(int height) {
listPopupWindow.setHeight(height);
}

@Override
public Parcelable onSaveInstanceState() {
final Bundle state = new Bundle();
state.putParcelable(superStateKey, super.onSaveInstanceState());
state.putSparseParcelableArray(childrenStateKey,
ChildrenViewStateHelper.newInstance(this).saveChildrenState(childrenStateKey));
return state;
}

@Override
protected void onRestoreInstanceState(final Parcelable state) {
if (state instanceof Bundle) {
final Bundle localState = (Bundle) state;
super.onRestoreInstanceState(localState.getParcelable(superStateKey));
ChildrenViewStateHelper.newInstance(this).restoreChildrenState(localState
.getSparseParcelableArray(childrenStateKey), childrenStateKey);
} else {
super.onRestoreInstanceState(state);
}
}

@Override
protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
dispatchFreezeSelfOnly(container);
}

@Override
protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
dispatchThawSelfOnly(container);
}

@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
if (!enabled) {
getEditText().setText("");
}
getEditText().setEnabled(enabled);
}

@NonNull
@Override
public TextInputEditText getEditText() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ private ChildrenViewStateHelper() {
* Also you need override {@link ViewGroup#dispatchSaveInstanceState(SparseArray)} and call {@link ViewGroup#dispatchFreezeSelfOnly(SparseArray)}.
*/
public SparseArray<Parcelable> saveChildrenState() {
return saveChildrenState(DEFAULT_CHILDREN_STATE_KEY);
}

public SparseArray<Parcelable> saveChildrenState(String stateKey) {
final SparseArray<Parcelable> array = new SparseArray<>();
for (int i = 0; i < mClientViewGroup.getChildCount(); i++) {
final Bundle bundle = new Bundle();
final SparseArray<Parcelable> childArray = new SparseArray<>(); //create independent SparseArray for each child (View or ViewGroup)
mClientViewGroup.getChildAt(i).saveHierarchyState(childArray);
bundle.putSparseParcelableArray(DEFAULT_CHILDREN_STATE_KEY, childArray);
bundle.putSparseParcelableArray(stateKey, childArray);
array.append(i, bundle);
}
return array;
Expand All @@ -41,12 +45,16 @@ public SparseArray<Parcelable> saveChildrenState() {
* Also you need override {@link ViewGroup#dispatchRestoreInstanceState(SparseArray)} and call {@link ViewGroup#dispatchThawSelfOnly(SparseArray)}.
*/
public void restoreChildrenState(@Nullable final SparseArray<Parcelable> childrenState) {
restoreChildrenState(childrenState, DEFAULT_CHILDREN_STATE_KEY);
}

public void restoreChildrenState(@Nullable final SparseArray<Parcelable> childrenState, String stateKey) {
if (null == childrenState) {
return;
}
for (int i = 0; i < mClientViewGroup.getChildCount(); i++) {
final Bundle bundle = (Bundle) childrenState.get(i);
final SparseArray<Parcelable> childState = bundle.getSparseParcelableArray(DEFAULT_CHILDREN_STATE_KEY);
final SparseArray<Parcelable> childState = bundle.getSparseParcelableArray(stateKey);
mClientViewGroup.getChildAt(i).restoreHierarchyState(childState);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,25 @@ public void enableTabAtPosition(int pos) {
@Override
protected Parcelable onSaveInstanceState() {
final Bundle state = new Bundle();
state.putParcelable("superState", super.onSaveInstanceState());
state.putParcelable("stepperSuperState", super.onSaveInstanceState());
state.putParcelable(STEPPER_STATE_KEY, new SparseBooleanArrayParcelable(enabled));
return state;
}

@Override
@SuppressWarnings("ConstantConditions")
protected void onRestoreInstanceState(final Parcelable state) {
if (state instanceof Bundle) {
final Bundle localState = (Bundle) state;
super.onRestoreInstanceState(localState.getParcelable("superState"));
super.onRestoreInstanceState(localState.getParcelable("stepperSuperState"));
enabled = localState.getParcelable(STEPPER_STATE_KEY);
for (int i = 0; i < getTabCount(); i++) {
getTabAt(i).getCustomView().setEnabled(enabled.get(i));
}
} else {
super.onRestoreInstanceState(state);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import static com.rockthevote.grommet.data.db.model.VoterId.Type.SSN_LAST_FOUR;

public class AdditionalInfoFragment extends BaseRegistrationFragment {
private static final String OTHER_PARTY_VISIBILITY_KEY = "other_party_visibility_key";

@BindView(R.id.spinner_race) BetterSpinner raceSpinner;

Expand Down Expand Up @@ -131,15 +132,13 @@ public void onViewCreated(View view, Bundle savedInstanceState) {

validator = new ObservableValidator(this, getActivity());


// Setup Race Spinner
raceEnumAdapter = new EnumAdapter<>(getActivity(), Race.class);
raceSpinner.setAdapter(raceEnumAdapter);
raceSpinner.setOnItemClickListener((adapterView, view1, i, l) -> {
raceSpinner.getEditText().setText(raceEnumAdapter.getItem(i).toString());
raceSpinner.dismiss();
});
raceSpinner.getEditText().setText(Race.OTHER.toString());


// Setup Party Spinner
Expand Down Expand Up @@ -168,12 +167,24 @@ public void onViewCreated(View view, Bundle savedInstanceState) {
phoneTypeSpinner.getEditText().setText(phoneTypeEnumAdapter.getItem(i).toString());
phoneTypeSpinner.dismiss();
});
phoneTypeSpinner.getEditText().setText(phoneTypeEnumAdapter.getItem(0).toString());

if (null != savedInstanceState) {
otherPartyTIL.setVisibility(savedInstanceState.getBoolean(OTHER_PARTY_VISIBILITY_KEY) ?
View.VISIBLE : View.GONE);
}

}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

// set up defaults
if (null == savedInstanceState) {
raceSpinner.getEditText().setText(Race.OTHER.toString());
phoneTypeSpinner.getEditText().setText(phoneTypeEnumAdapter.getItem(0).toString());
}

phoneOptIn.setText(getString(R.string.label_receive_text, partnerNamePref.get()));
emailOptIn.setText(getString(R.string.label_receive_email, partnerNamePref.get()));
}
Expand Down Expand Up @@ -222,7 +233,6 @@ public void onResume() {
subscriptions.add(RxTextView.afterTextChangeEvents(preferredLanguage)
.observeOn(Schedulers.io())
.debounce(DEBOUNCE, TimeUnit.MILLISECONDS)
.skip(1)
.subscribe(event -> {
AdditionalInfo.insertOrUpdate(db, rockyRequestRowId.get(),
LANGUAGE_PREF, new AdditionalInfo.Builder()
Expand Down Expand Up @@ -356,6 +366,12 @@ public void onSSNChecked(boolean checked) {
doesNotHaveSSN.onNext(checked);
}

@Override
public void onSaveInstanceState(Bundle outState) {
outState.putBoolean(OTHER_PARTY_VISIBILITY_KEY, View.VISIBLE == otherPartyTIL.getVisibility());
super.onSaveInstanceState(outState);
}

@Override
public Observable<Boolean> verify() {
return validator.validate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,15 @@ public void onAcceptClick() {
.subscribe(rockyRequest -> {
birthday.setText(Dates.formatAsISO8601_ShortDate(rockyRequest.dateOfBirth()));
race.setText(rockyRequest.race().toString());
party.setText(rockyRequest.party().toString());

String polParty;
if (RockyRequest.Party.OTHER_PARTY == rockyRequest.party()) {
polParty = rockyRequest.otherParty();
} else {
polParty = rockyRequest.party().toString();
}

party.setText(polParty);
phoneLabel.setText(rockyRequest.phoneType().toString());
// show/hide registration address views
for (View v : mailingAddressViews) {
Expand Down Expand Up @@ -187,6 +195,8 @@ public void onClearSignatureClick(View v) {
public void onCheckChanged(boolean checked) {
if (checked) {
dialog.show(getFragmentManager(), "disclosure_dialog");
} else {
buttonRegister.setEnabled(false);
}
}

Expand All @@ -198,7 +208,8 @@ public void onStartSigning() {
@Override
public void onSigned() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap image = (Images.aspectSafeScale(Images.transformAspectRatio(signaturePad.getSignatureBitmap(), 3, 1), 180, 60));
Bitmap image = Images.transformAspectRatio(signaturePad.getSignatureBitmap(), 3, 1);
image = Images.aspectSafeScale(image, 180, 60);
image.compress(Bitmap.CompressFormat.PNG, 100, baos);

db.update(RockyRequest.TABLE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.rockthevote.grommet.ui.misc.BetterSpinner;
import com.rockthevote.grommet.ui.misc.ChildrenViewStateHelper;
import com.rockthevote.grommet.ui.misc.ObservableValidator;
import com.rockthevote.grommet.util.Strings;
import com.squareup.sqlbrite.BriteDatabase;

import java.util.concurrent.TimeUnit;
Expand All @@ -41,6 +42,11 @@
public class AddressView extends FrameLayout {
private static final String PA_ABREV = "PA";

private static final String COUNTY_ENABLED_KEY = "county_enabled_key";

private String childrenStateKey;
private String superStateKey;

@NotEmpty
@BindView(R.id.til_street_address) TextInputLayout streetTIL;
@BindView(R.id.street) EditText streetEditText;
Expand Down Expand Up @@ -112,10 +118,14 @@ public AddressView(Context context, AttributeSet attrs, int defStyleAttr) {
break;
case 4:
type = Address.Type.ASSISTANT_ADDRESS;
break;
}
} finally {
typedArray.recycle();
}

superStateKey = AddressView.class.getSimpleName() + ".superState." + type.toString();
childrenStateKey = AddressView.class.getSimpleName() + ".childState." + type.toString();
}
}

Expand Down Expand Up @@ -163,16 +173,15 @@ protected void onFinishInflate() {
if (!PA_ABREV.equals(stateAdapter.getItem(i))) {
countySpinner.setErrorEnabled(false);
countySpinner.setEnabled(false);
countySpinner.getEditText().setText("");
countySpinner.getEditText().setEnabled(false);
} else {
countySpinner.getEditText().setEnabled(true);
countySpinner.setEnabled(true);
}

stateSpinner.dismiss();
});
stateSpinner.getEditText().setText(stateAdapter.getItem(stateAdapter.getPosition(PA_ABREV)));
if (Strings.isBlank(stateSpinner.getEditText().getEditableText().toString())) {
stateSpinner.getEditText().setText(stateAdapter.getItem(stateAdapter.getPosition(PA_ABREV)));
}
}
}

Expand Down Expand Up @@ -211,19 +220,21 @@ protected void onDetachedFromWindow() {
@Override
protected Parcelable onSaveInstanceState() {
final Bundle state = new Bundle();
state.putParcelable("superState", super.onSaveInstanceState());
state.putSparseParcelableArray(ChildrenViewStateHelper.DEFAULT_CHILDREN_STATE_KEY,
ChildrenViewStateHelper.newInstance(this).saveChildrenState());
state.putParcelable(superStateKey, super.onSaveInstanceState());
state.putBoolean(COUNTY_ENABLED_KEY, countySpinner.isEnabled());
state.putSparseParcelableArray(childrenStateKey,
ChildrenViewStateHelper.newInstance(this).saveChildrenState(childrenStateKey));
return state;
}

@Override
protected void onRestoreInstanceState(final Parcelable state) {
if (state instanceof Bundle) {
final Bundle localState = (Bundle) state;
super.onRestoreInstanceState(localState.getParcelable("superState"));
super.onRestoreInstanceState(localState.getParcelable(superStateKey));
ChildrenViewStateHelper.newInstance(this).restoreChildrenState(localState
.getSparseParcelableArray(ChildrenViewStateHelper.DEFAULT_CHILDREN_STATE_KEY));
.getSparseParcelableArray(childrenStateKey), childrenStateKey);
countySpinner.setEnabled(localState.getBoolean(COUNTY_ENABLED_KEY, true));
} else {
super.onRestoreInstanceState(state);
}
Expand Down
16 changes: 10 additions & 6 deletions app/src/main/java/com/rockthevote/grommet/ui/views/NameView.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@

public class NameView extends FrameLayout {

private String childrenStateKey;
private String superStateKey;

@BindView(R.id.name_section_title) TextView sectionTitle;

@NotEmpty
Expand Down Expand Up @@ -108,7 +111,8 @@ public NameView(Context context, AttributeSet attrs, int defStyleAttr) {
} finally {
typedArray.recycle();
}

superStateKey = NameView.class.getSimpleName() + ".superState." + type.toString();
childrenStateKey = NameView.class.getSimpleName() + ".childState." + type.toString();
}
}

Expand Down Expand Up @@ -182,19 +186,19 @@ protected void onDetachedFromWindow() {
@Override
protected Parcelable onSaveInstanceState() {
final Bundle state = new Bundle();
state.putParcelable("superState", super.onSaveInstanceState());
state.putSparseParcelableArray(ChildrenViewStateHelper.DEFAULT_CHILDREN_STATE_KEY,
ChildrenViewStateHelper.newInstance(this).saveChildrenState());
state.putParcelable(superStateKey, super.onSaveInstanceState());
state.putSparseParcelableArray(childrenStateKey,
ChildrenViewStateHelper.newInstance(this).saveChildrenState(childrenStateKey));
return state;
}

@Override
protected void onRestoreInstanceState(final Parcelable state) {
if (state instanceof Bundle) {
final Bundle localState = (Bundle) state;
super.onRestoreInstanceState(localState.getParcelable("superState"));
super.onRestoreInstanceState(localState.getParcelable(superStateKey));
ChildrenViewStateHelper.newInstance(this).restoreChildrenState(localState
.getSparseParcelableArray(ChildrenViewStateHelper.DEFAULT_CHILDREN_STATE_KEY));
.getSparseParcelableArray(childrenStateKey), childrenStateKey);
} else {
super.onRestoreInstanceState(state);
}
Expand Down
Loading

0 comments on commit 6ca57f5

Please sign in to comment.