package ru.orangesoftware.financisto2.activity;
import android.content.Intent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OptionsMenu;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ru.orangesoftware.financisto2.R;
import ru.orangesoftware.financisto2.model.Category;
import ru.orangesoftware.financisto2.model.Currency;
import ru.orangesoftware.financisto2.model.TransactionAttribute;
import ru.orangesoftware.financisto2.widget.AmountInput;
import ru.orangesoftware.financisto2.widget.AmountInput_;
/**
* Created by IntelliJ IDEA.
* User: Denis Solonenko
* Date: 4/21/11 7:17 PM
*/
@EActivity(R.layout.split_fixed)
@OptionsMenu(R.menu.split_menu)
public class SplitTransactionActivity extends AbstractSplitActivity implements CategorySelector.CategorySelectorListener {
private TextView amountTitle;
private AmountInput amountInput;
private CategorySelector categorySelector;
@Override
protected void createUI(LinearLayout layout) {
categorySelector.createNode(layout, false);
amountInput = AmountInput_.build(this);
amountInput.setOwner(this);
amountInput.setOnAmountChangedListener(new AmountInput.OnAmountChangedListener() {
@Override
public void onAmountChanged(long oldAmount, long newAmount) {
setUnsplitAmount(split.unsplitAmount - newAmount);
}
});
View v = x.addEditNode(layout, R.string.amount, amountInput);
amountTitle = (TextView) v.findViewById(R.id.label);
categorySelector.createAttributesLayout(layout);
}
@Override
protected void fetchData() {
categorySelector = new CategorySelector(this, x, false);
categorySelector.setListener(this);
categorySelector.doNotShowSplitCategory();
categorySelector.fetchCategories();
}
@Override
protected void updateUI() {
super.updateUI();
categorySelector.selectCategory(split.categoryId);
setAmount(split.fromAmount);
}
@Override
protected boolean updateFromUI() {
super.updateFromUI();
split.fromAmount = amountInput.getAmount();
split.categoryAttributes = getAttributes();
return true;
}
private Map<Long, String> getAttributes() {
List<TransactionAttribute> attributeList = categorySelector.getAttributes();
Map<Long, String> attributes = new HashMap<Long, String>();
for (TransactionAttribute ta : attributeList) {
attributes.put(ta.attributeId, ta.value);
}
return attributes;
}
@Override
public void onCategorySelected(Category category, boolean selectLast) {
if (category.isIncome()) {
amountInput.setIncome();
} else {
amountInput.setExpense();
}
split.categoryId = category.id;
categorySelector.addAttributes(split);
}
private void setAmount(long amount) {
amountInput.setAmount(amount);
Currency c = getCurrency();
amountInput.setCurrency(c);
amountTitle.setText(getString(R.string.amount)+" ("+c.name+")");
}
@Override
protected void onClick(View v, int id) {
super.onClick(v, id);
categorySelector.onClick(id);
}
@Override
public void onSelectedId(int id, long selectedId) {
categorySelector.onSelectedId(id, selectedId);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
categorySelector.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
amountInput.processActivityResult(requestCode, data);
}
}
}