2 * Copyright © 2019 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.model;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.AsyncTask;
24 import net.ktnx.mobileledger.App;
25 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
26 import net.ktnx.mobileledger.ui.activity.MainActivity;
27 import net.ktnx.mobileledger.utils.LockHolder;
28 import net.ktnx.mobileledger.utils.Locker;
29 import net.ktnx.mobileledger.utils.Logger;
30 import net.ktnx.mobileledger.utils.MLDB;
31 import net.ktnx.mobileledger.utils.ObservableList;
32 import net.ktnx.mobileledger.utils.ObservableValue;
34 import java.lang.ref.WeakReference;
35 import java.util.ArrayList;
36 import java.util.Date;
37 import java.util.List;
38 import java.util.Locale;
39 import java.util.concurrent.atomic.AtomicInteger;
41 import androidx.lifecycle.MutableLiveData;
43 import static net.ktnx.mobileledger.utils.Logger.debug;
45 public final class Data {
46 public static ObservableList<TransactionListItem> transactions =
47 new ObservableList<>(new ArrayList<>());
48 public static ObservableList<LedgerAccount> accounts = new ObservableList<>(new ArrayList<>());
49 public static MutableLiveData<Boolean> backgroundTasksRunning = new MutableLiveData<>(false);
50 public static MutableLiveData<Date> lastUpdateDate = new MutableLiveData<>();
51 public static MutableLiveData<MobileLedgerProfile> profile = new MutableLiveData<>();
52 public static MutableLiveData<ArrayList<MobileLedgerProfile>> profiles =
53 new MutableLiveData<>(null);
54 public static ObservableValue<Boolean> optShowOnlyStarred = new ObservableValue<>();
55 public static MutableLiveData<String> accountFilter = new MutableLiveData<>();
56 private static AtomicInteger backgroundTaskCount = new AtomicInteger(0);
57 private static Locker profilesLocker = new Locker();
58 private static RetrieveTransactionsTask retrieveTransactionsTask;
59 public static void backgroundTaskStarted() {
60 int cnt = backgroundTaskCount.incrementAndGet();
62 String.format(Locale.ENGLISH, "background task count is %d after incrementing",
64 backgroundTasksRunning.postValue(cnt > 0);
66 public static void backgroundTaskFinished() {
67 int cnt = backgroundTaskCount.decrementAndGet();
69 String.format(Locale.ENGLISH, "background task count is %d after decrementing",
71 backgroundTasksRunning.postValue(cnt > 0);
73 public static void setCurrentProfile(MobileLedgerProfile newProfile) {
74 MLDB.setOption(MLDB.OPT_PROFILE_UUID, (newProfile == null) ? null : newProfile.getUuid());
75 stopTransactionsRetrieval();
76 profile.postValue(newProfile);
78 public static int getProfileIndex(MobileLedgerProfile profile) {
79 try (LockHolder ignored = profilesLocker.lockForReading()) {
80 List<MobileLedgerProfile> prList = profiles.getValue();
81 if (prList == null) throw new AssertionError();
82 for (int i = 0; i < prList.size(); i++) {
83 MobileLedgerProfile p = prList.get(i);
84 if (p.equals(profile)) return i;
90 @SuppressWarnings("WeakerAccess")
91 public static int getProfileIndex(String profileUUID) {
92 try (LockHolder ignored = profilesLocker.lockForReading()) {
93 List<MobileLedgerProfile> prList = profiles.getValue();
94 if (prList == null) throw new AssertionError();
95 for (int i = 0; i < prList.size(); i++) {
96 MobileLedgerProfile p = prList.get(i);
97 if (p.getUuid().equals(profileUUID)) return i;
103 public static int retrieveCurrentThemeIdFromDb() {
104 String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
105 if (profileUUID == null) return -1;
107 SQLiteDatabase db = App.getDatabase();
109 .rawQuery("SELECT theme from profiles where uuid=?", new String[]{profileUUID}))
111 if (c.moveToNext()) return c.getInt(0);
116 public static MobileLedgerProfile getProfile(String profileUUID) {
117 MobileLedgerProfile profile;
118 try (LockHolder readLock = profilesLocker.lockForReading()) {
119 List<MobileLedgerProfile> prList = profiles.getValue();
120 if ((prList == null) || prList.isEmpty()) {
122 try (LockHolder ignored = profilesLocker.lockForWriting()) {
123 profile = MobileLedgerProfile.loadAllFromDB(profileUUID);
127 int i = getProfileIndex(profileUUID);
129 profile = prList.get(i);
134 public synchronized static void scheduleTransactionListRetrieval(MainActivity activity) {
135 if (retrieveTransactionsTask != null) {
136 Logger.debug("db", "Ignoring request for transaction retrieval - already active");
139 MobileLedgerProfile pr = profile.getValue();
140 if (pr == null) throw new IllegalStateException("No current profile");
142 retrieveTransactionsTask =
143 new RetrieveTransactionsTask(new WeakReference<>(activity), profile.getValue());
144 Logger.debug("db", "Created a background transaction retrieval task");
146 retrieveTransactionsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
148 public static synchronized void stopTransactionsRetrieval() {
149 if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
151 public static void transactionRetrievalDone() {
152 retrieveTransactionsTask = null;