Skip to content

Commit

Permalink
Restoring state. Clearing autocomplete states when switching transaci…
Browse files Browse the repository at this point in the history
…on type
  • Loading branch information
mvarnagiris committed Dec 19, 2014
1 parent 7028439 commit 20c150c
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public static void start(Context context, String transactionServerId) {
transactionController.onPause();
}

@Override public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
transactionController.onSaveInstanceState(outState);
}

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
transactionController.handleActivityResult(requestCode, resultCode, data);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
package com.code44.finance.ui.transactions.autocomplete;

import android.os.Parcel;
import android.os.Parcelable;

import com.code44.finance.data.model.Account;
import com.code44.finance.data.model.Category;
import com.code44.finance.data.model.Tag;

import java.util.ArrayList;
import java.util.List;

public final class AutoCompleteResult {
public final class AutoCompleteResult implements Parcelable {
public static final Parcelable.Creator<AutoCompleteResult> CREATOR = new Parcelable.Creator<AutoCompleteResult>() {
public AutoCompleteResult createFromParcel(Parcel in) {
return new AutoCompleteResult(in);
}

public AutoCompleteResult[] newArray(int size) {
return new AutoCompleteResult[size];
}
};

private Long amount;
private Account accountFrom;
private Account accountTo;
Expand All @@ -20,6 +34,49 @@ public final class AutoCompleteResult {
private List<Tag> otherTags;
private List<String> otherNotes;

public AutoCompleteResult() {
}

private AutoCompleteResult(Parcel in) {
amount = (Long) in.readValue(Long.class.getClassLoader());
accountFrom = in.readParcelable(Account.class.getClassLoader());
accountTo = in.readParcelable(Account.class.getClassLoader());
category = in.readParcelable(Category.class.getClassLoader());
in.readTypedList(tags, Tag.CREATOR);
note = in.readString();
otherAmounts = new ArrayList<>();
in.readList(otherAmounts, Long.class.getClassLoader());
otherAccountsFrom = new ArrayList<>();
in.readTypedList(otherAccountsFrom, Account.CREATOR);
otherAccountsTo = new ArrayList<>();
in.readTypedList(otherAccountsTo, Account.CREATOR);
otherCategories = new ArrayList<>();
in.readTypedList(otherCategories, Category.CREATOR);
otherTags = new ArrayList<>();
in.readTypedList(otherTags, Tag.CREATOR);
otherNotes = new ArrayList<>();
in.readStringList(otherNotes);
}

@Override public int describeContents() {
return 0;
}

@Override public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(amount);
dest.writeParcelable(accountFrom, flags);
dest.writeParcelable(accountTo, flags);
dest.writeParcelable(category, flags);
dest.writeTypedList(tags);
dest.writeString(note);
dest.writeList(otherAmounts);
dest.writeTypedList(otherAccountsFrom);
dest.writeTypedList(otherAccountsTo);
dest.writeTypedList(otherCategories);
dest.writeTypedList(otherTags);
dest.writeStringList(otherNotes);
}

public Long getAmount() {
return amount;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,16 @@ public class TransactionController implements TransactionAutoComplete.Transactio
private static final int REQUEST_EXCHANGE_RATE = 8;
private static final int REQUEST_AMOUNT_TO = 9;

private static final String STATE_TRANSACTION_EDIT_DATA = "STATE_TRANSACTION_EDIT_DATA";

private static final boolean LOG_AUTO_COMPLETE = true;

private final BaseActivity activity;
private final EventBus eventBus;
private final Executor autoCompleteExecutor;
private final CurrenciesApi currenciesApi;
private final OnTransactionUpdatedListener listener;

private final TransactionEditData transactionEditData;

private final TransactionTypeViewController transactionTypeViewController;
Expand All @@ -80,7 +83,12 @@ public TransactionController(BaseActivity activity, Bundle savedInstanceState, E
this.currenciesApi = currenciesApi;
this.listener = listener;

transactionEditData = new TransactionEditData();
if (savedInstanceState == null) {
transactionEditData = new TransactionEditData();
} else {
transactionEditData = savedInstanceState.getParcelable(STATE_TRANSACTION_EDIT_DATA);
}

transactionTypeViewController = new TransactionTypeViewController(activity, this);
amountViewController = new AmountViewController(activity, this, this, mainCurrency);
dateTimeViewController = new DateTimeViewController(activity, this, this);
Expand Down Expand Up @@ -264,6 +272,10 @@ public void onPause() {
eventBus.unregister(this);
}

public void onSaveInstanceState(Bundle outState) {
outState.putParcelable(STATE_TRANSACTION_EDIT_DATA, transactionEditData);
}

public void handleActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.code44.finance.ui.transactions.controllers;

import android.os.Parcel;
import android.os.Parcelable;

import com.code44.finance.common.model.TransactionState;
import com.code44.finance.common.model.TransactionType;
import com.code44.finance.data.model.Account;
Expand All @@ -11,7 +14,17 @@

import java.util.List;

public class TransactionEditData {
public class TransactionEditData implements Parcelable {
public static final Parcelable.Creator<TransactionEditData> CREATOR = new Parcelable.Creator<TransactionEditData>() {
public TransactionEditData createFromParcel(Parcel in) {
return new TransactionEditData(in);
}

public TransactionEditData[] newArray(int size) {
return new TransactionEditData[size];
}
};

private Transaction storedTransaction;
private AutoCompleteResult autoCompleteResult;

Expand Down Expand Up @@ -39,6 +52,67 @@ public class TransactionEditData {
private boolean isIncludeInReportsSet = false;
private boolean isExchangeRateSet = false;

public TransactionEditData() {
}

private TransactionEditData(Parcel in) {
storedTransaction = in.readParcelable(Transaction.class.getClassLoader());
autoCompleteResult = in.readParcelable(AutoCompleteResult.class.getClassLoader());
transactionType = (TransactionType) in.readSerializable();
amount = (Long) in.readValue(Long.class.getClassLoader());
date = (Long) in.readValue(Long.class.getClassLoader());
accountFrom = in.readParcelable(Account.class.getClassLoader());
accountTo = in.readParcelable(Account.class.getClassLoader());
category = in.readParcelable(Category.class.getClassLoader());
in.readTypedList(tags, Tag.CREATOR);
note = in.readString();
transactionState = (TransactionState) in.readSerializable();
includeInReports = (Boolean) in.readValue(Boolean.class.getClassLoader());
exchangeRate = (Double) in.readValue(Double.class.getClassLoader());
isTransactionTypeSet = in.readInt() == 1;
isAmountSet = in.readInt() == 1;
isDateSet = in.readInt() == 1;
isAccountFromSet = in.readInt() == 1;
isAccountToSet = in.readInt() == 1;
isCategorySet = in.readInt() == 1;
isTagsSet = in.readInt() == 1;
isNoteSet = in.readInt() == 1;
isTransactionStateSet = in.readInt() == 1;
isIncludeInReportsSet = in.readInt() == 1;
isExchangeRateSet = in.readInt() == 1;
}

@Override public int describeContents() {
return 0;
}

@Override public void writeToParcel(Parcel dest, int flags) {
dest.writeParcelable(storedTransaction, flags);
dest.writeParcelable(autoCompleteResult, flags);
dest.writeSerializable(transactionType);
dest.writeValue(amount);
dest.writeValue(date);
dest.writeParcelable(accountFrom, flags);
dest.writeParcelable(accountTo, flags);
dest.writeParcelable(category, flags);
dest.writeTypedList(tags);
dest.writeString(note);
dest.writeSerializable(transactionState);
dest.writeValue(includeInReports);
dest.writeValue(exchangeRate);
dest.writeInt(isTransactionTypeSet ? 1 : 0);
dest.writeInt(isAmountSet ? 1 : 0);
dest.writeInt(isDateSet ? 1 : 0);
dest.writeInt(isAccountFromSet ? 1 : 0);
dest.writeInt(isAccountToSet ? 1 : 0);
dest.writeInt(isCategorySet ? 1 : 0);
dest.writeInt(isTagsSet ? 1 : 0);
dest.writeInt(isNoteSet ? 1 : 0);
dest.writeInt(isTransactionStateSet ? 1 : 0);
dest.writeInt(isIncludeInReportsSet ? 1 : 0);
dest.writeInt(isExchangeRateSet ? 1 : 0);
}

public Transaction getStoredTransaction() {
return storedTransaction;
}
Expand Down Expand Up @@ -66,6 +140,7 @@ public TransactionType getTransactionType() {
public void setTransactionType(TransactionType transactionType) {
this.transactionType = transactionType;
isTransactionTypeSet = true;
onTransactionTypeChanged();
}

public long getAmount() {
Expand Down Expand Up @@ -402,4 +477,20 @@ public boolean validateAccountTo(ViewController controller) {

return true;
}

private void onTransactionTypeChanged() {
autoCompleteResult = null;

accountFrom = null;
accountTo = null;
category = null;
tags = null;
note = null;

isAccountFromSet = false;
isAccountToSet = false;
isCategorySet = false;
isTagsSet = false;
isNoteSet = false;
}
}

0 comments on commit 20c150c

Please sign in to comment.