2 * Copyright © 2021 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.patterns;
20 import android.database.Cursor;
21 import android.os.AsyncTask;
23 import androidx.annotation.NonNull;
24 import androidx.lifecycle.LiveData;
25 import androidx.lifecycle.MutableLiveData;
26 import androidx.lifecycle.ViewModel;
28 import net.ktnx.mobileledger.App;
29 import net.ktnx.mobileledger.dao.PatternAccountDAO;
30 import net.ktnx.mobileledger.dao.PatternHeaderDAO;
31 import net.ktnx.mobileledger.db.DB;
32 import net.ktnx.mobileledger.db.PatternAccount;
33 import net.ktnx.mobileledger.db.PatternHeader;
34 import net.ktnx.mobileledger.model.Currency;
35 import net.ktnx.mobileledger.model.PatternDetailsItem;
36 import net.ktnx.mobileledger.utils.Logger;
37 import net.ktnx.mobileledger.utils.MLDB;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.List;
42 import java.util.Objects;
44 public class PatternDetailsViewModel extends ViewModel {
45 static final int NEW_PATTERN = -1;
46 private final MutableLiveData<List<PatternDetailsItem>> items = new MutableLiveData<>();
47 private long mPatternId;
48 private String mDefaultPatternName;
49 public String getDefaultPatternName() {
50 return mDefaultPatternName;
52 public void setDefaultPatternName(String name) {
53 mDefaultPatternName = name;
55 public LiveData<List<PatternDetailsItem>> getItems() {
59 public void resetItems() {
60 items.setValue(Collections.emptyList());
61 checkItemConsistency();
63 private void checkItemConsistency() {
64 ArrayList<PatternDetailsItem> newList = new ArrayList<>(items.getValue());
65 boolean changes = false;
66 if (newList.size() < 1) {
67 final PatternDetailsItem.Header header = PatternDetailsItem.createHeader();
68 header.setName(mDefaultPatternName);
73 while (newList.size() < 3) {
74 newList.add(PatternDetailsItem.createAccountRow(newList.size() - 1));
79 items.setValue(newList);
81 public void loadItems(long patternId) {
82 DB db = App.getRoomDB();
83 LiveData<PatternHeader> ph = db.getPatternDAO()
84 .getPattern(patternId);
85 ArrayList<PatternDetailsItem> list = new ArrayList<>();
87 MLDB.queryInBackground(
88 "SELECT name, regular_expression, transaction_description, transaction_comment, " +
89 "date_year_match_group, date_month_match_group, date_day_match_group FROM " +
90 "patterns WHERE id=?", new String[]{String.valueOf(patternId)},
91 new MLDB.CallbackHelper() {
93 public void onDone() {
96 MLDB.queryInBackground(
97 "SELECT id, position, acc, acc_match_group, currency, " +
98 "currency_match_group, amount, amount_match_group," +
99 " comment, comment_match_group FROM " +
100 "pattern_accounts WHERE pattern_id=? ORDER BY " + "position ASC",
101 new String[]{String.valueOf(patternId)}, new MLDB.CallbackHelper() {
103 public void onDone() {
105 items.postValue(list);
108 public boolean onRow(@NonNull Cursor cursor) {
109 PatternDetailsItem.AccountRow item =
110 PatternDetailsItem.createAccountRow(
114 item.setId(cursor.getInt(0));
116 if (cursor.isNull(3)) {
117 item.setAccountName(cursor.getString(2));
120 item.setAccountNameMatchGroup(cursor.getShort(3));
123 if (cursor.isNull(5)) {
124 final int currId = cursor.getInt(4);
126 item.setCurrency(Currency.loadById(currId));
129 item.setCurrencyMatchGroup(cursor.getShort(5));
132 if (cursor.isNull(7)) {
133 item.setAmount(cursor.getFloat(6));
136 item.setAmountMatchGroup(cursor.getShort(7));
139 if (cursor.isNull(9)) {
140 item.setAccountComment(cursor.getString(8));
143 item.setAccountCommentMatchGroup(cursor.getShort(9));
151 public boolean onRow(@NonNull Cursor cursor) {
152 PatternDetailsItem.Header header = PatternDetailsItem.createHeader();
153 header.setName(cursor.getString(0));
154 header.setPattern(cursor.getString(1));
155 header.setTransactionDescription(cursor.getString(2));
156 header.setTransactionComment(cursor.getString(3));
157 header.setDateYearMatchGroup(cursor.getShort(4));
158 header.setDateMonthMatchGroup(cursor.getShort(5));
159 header.setDateDayMatchGroup(cursor.getShort(6));
167 public void setTestText(String text) {
168 List<PatternDetailsItem> list = new ArrayList<>(items.getValue());
169 PatternDetailsItem.Header header = new PatternDetailsItem.Header(list.get(0)
171 header.setTestText(text);
174 items.setValue(list);
176 public void setPatternId(int patternId) {
177 if (mPatternId != patternId) {
178 if (patternId == NEW_PATTERN) {
182 loadItems(patternId);
184 mPatternId = patternId;
188 public void onSavePattern() {
189 Logger.debug("flow", "PatternDetailsViewModel.onSavePattern(); model=" + this);
190 final List<PatternDetailsItem> list = Objects.requireNonNull(items.getValue());
192 AsyncTask.execute(() -> {
193 PatternDetailsItem.Header modelHeader = list.get(0)
195 PatternHeaderDAO headerDAO = App.getRoomDB()
197 PatternHeader dbHeader = modelHeader.toDBO();
198 if (mPatternId <= 0) {
199 dbHeader.setId(mPatternId = headerDAO.insert(dbHeader));
202 headerDAO.update(dbHeader);
205 PatternAccountDAO paDAO = App.getRoomDB()
206 .getPatternAccountDAO();
207 for (int i = 1; i < list.size(); i++) {
208 PatternAccount dbAccount = list.get(i)
210 .toDBO(dbHeader.getId());
211 dbAccount.setPatternId(mPatternId);
212 if (dbAccount.getId() == null || dbAccount.getId() <= 0)
213 dbAccount.setId(paDAO.insert(dbAccount));
215 paDAO.update(dbAccount);