import java.util.ArrayList;
import java.util.Calendar;
+import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
private final Item trailer = new Item(this);
private final ArrayList<Item> items = new ArrayList<>();
private final MutableLiveData<Boolean> isSubmittable = new MutableLiveData<>(false);
- private final MutableLiveData<Integer> focusedItem = new MutableLiveData<>(null);
+ private final MutableLiveData<Integer> focusedItem = new MutableLiveData<>(0);
private final MutableLiveData<Integer> accountCount = new MutableLiveData<>(0);
+ private final MutableLiveData<Boolean> simulateSave = new MutableLiveData<>(false);
+ public boolean getSimulateSave() {
+ return simulateSave.getValue();
+ }
+ public void setSimulateSave(boolean simulateSave) {
+ this.simulateSave.setValue(simulateSave);
+ }
+ public void toggleSimulateSave() {
+ simulateSave.setValue(!simulateSave.getValue());
+ }
+ public void observeSimulateSave(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
+ @NonNull
+ androidx.lifecycle.Observer<? super Boolean> observer) {
+ this.simulateSave.observe(owner, observer);
+ }
public int getAccountCount() {
return items.size();
}
@NonNull androidx.lifecycle.Observer<? super Integer> observer) {
this.accountCount.removeObserver(observer);
}
+ public int getFocusedItem() { return focusedItem.getValue(); }
public void setFocusedItem(int position) {
focusedItem.setValue(position);
}
for (int i = 0; i < items.size(); i++) {
Item item = items.get(i);
LedgerTransactionAccount acc = item.getAccount();
- debug("submittable", String.format("Item %2d: [%4.2f] %s", i,
- acc.isAmountSet() ? acc.getAmount() : 0, acc.getAccountName()));
+ debug("submittable", String.format("Item %2d: [%4.2f] %s (%s)", i,
+ acc.isAmountSet() ? acc.getAmount() : 0, acc.getAccountName(),
+ acc.getComment()));
}
}
}
public void sendCountNotifications() {
accountCount.setValue(getAccountCount());
}
+ public void sendFocusedNotification() {
+ focusedItem.setValue(focusedItem.getValue());
+ }
+ public void updateFocusedItem(int position) {
+ focusedItem.setValue(position);
+ }
+ public void noteFocusChanged(int position, FocusedElement element) {
+ getItem(position).setFocusedElement(element);
+ }
+ public void swapItems(int one, int two) {
+ Collections.swap(items, one - 1, two - 1);
+ }
+ public void toggleComment(int position) {
+ final MutableLiveData<Boolean> commentVisible = getItem(position).commentVisible;
+ commentVisible.postValue(!commentVisible.getValue());
+ }
+ public void moveItemLast(int index) {
+ /* 0
+ 1 <-- index
+ 2
+ 3 <-- desired position
+ */
+ int itemCount = items.size();
+
+ if (index < itemCount - 1) {
+ Item acc = items.remove(index);
+ items.add(itemCount - 1, acc);
+ }
+ }
enum ItemType {generalData, transactionRow, bottomFiller}
//==========================================================================================
- class Item extends Object {
+ enum FocusedElement {Account, Comment, Amount}
+
+ class Item {
private ItemType type;
private MutableLiveData<Date> date = new MutableLiveData<>();
private MutableLiveData<String> description = new MutableLiveData<>();
private MutableLiveData<String> amountHint = new MutableLiveData<>(null);
private NewTransactionModel model;
private MutableLiveData<Boolean> editable = new MutableLiveData<>(true);
+ private FocusedElement focusedElement = FocusedElement.Account;
+ private MutableLiveData<String> comment = new MutableLiveData<>(null);
+ private MutableLiveData<Boolean> commentVisible = new MutableLiveData<>(false);
public Item(NewTransactionModel model) {
this.model = model;
type = ItemType.bottomFiller;
this.account = account;
this.editable.setValue(true);
}
+ public FocusedElement getFocusedElement() {
+ return focusedElement;
+ }
+ public void setFocusedElement(FocusedElement focusedElement) {
+ this.focusedElement = focusedElement;
+ }
public NewTransactionModel getModel() {
return model;
}
this.date.setValue(date);
}
public void setDate(String text) {
+ if ((text == null) || text.trim()
+ .isEmpty())
+ {
+ setDate((Date) null);
+ return;
+ }
+
int year, month, day;
final Calendar c = GregorianCalendar.getInstance();
Matcher m = reYMD.matcher(text);
final int myDay = c.get(Calendar.DAY_OF_MONTH);
if (today.get(Calendar.YEAR) != myYear) {
- return String.format(Locale.US, "%d/%02d/%02d", myYear, myMonth, myDay);
+ return String.format(Locale.US, "%d/%02d/%02d", myYear, myMonth + 1, myDay);
}
if (today.get(Calendar.MONTH) != myMonth) {
- return String.format(Locale.US, "%d/%02d", myMonth, myDay);
+ return String.format(Locale.US, "%d/%02d", myMonth + 1, myDay);
}
return String.valueOf(myDay);
public void stopObservingEditableFlag(Observer<Boolean> observer) {
editable.removeObserver(observer);
}
+ public void observeCommentVisible(NewTransactionActivity activity,
+ Observer<Boolean> observer) {
+ commentVisible.observe(activity, observer);
+ }
+ public void stopObservingCommentVisible(Observer<Boolean> observer) {
+ commentVisible.removeObserver(observer);
+ }
+ public void observeComment(NewTransactionActivity activity,
+ Observer<String> observer) {
+ comment.observe(activity, observer);
+ }
+ public void stopObservingComment(Observer<String> observer) {
+ comment.removeObserver(observer);
+ }
+ public void setComment(String comment) {
+ getAccount().setComment(comment);
+ this.comment.postValue(comment);
+ }
}
}