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 androidx.lifecycle.MutableLiveData;
26 import net.ktnx.mobileledger.App;
27 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
28 import net.ktnx.mobileledger.ui.activity.MainActivity;
29 import net.ktnx.mobileledger.utils.LockHolder;
30 import net.ktnx.mobileledger.utils.Locker;
31 import net.ktnx.mobileledger.utils.Logger;
32 import net.ktnx.mobileledger.utils.MLDB;
33 import net.ktnx.mobileledger.utils.ObservableList;
35 import java.lang.ref.WeakReference;
36 import java.util.ArrayList;
37 import java.util.Date;
38 import java.util.List;
39 import java.util.Locale;
40 import java.util.concurrent.atomic.AtomicInteger;
42 import static net.ktnx.mobileledger.utils.Logger.debug;
44 public final class Data {
45 public static ObservableList<TransactionListItem> transactions =
46 new ObservableList<>(new ArrayList<>());
47 public static ObservableList<LedgerAccount> accounts = new ObservableList<>(new ArrayList<>());
48 public static MutableLiveData<Boolean> backgroundTasksRunning = new MutableLiveData<>(false);
49 public static MutableLiveData<Date> lastUpdateDate = new MutableLiveData<>();
50 public static MutableLiveData<MobileLedgerProfile> profile = new MutableLiveData<>();
51 public static MutableLiveData<ArrayList<MobileLedgerProfile>> profiles =
52 new MutableLiveData<>(null);
53 public static MutableLiveData<String> accountFilter = new MutableLiveData<>();
54 private static AtomicInteger backgroundTaskCount = new AtomicInteger(0);
55 private static Locker profilesLocker = new Locker();
56 private static RetrieveTransactionsTask retrieveTransactionsTask;
57 public static void backgroundTaskStarted() {
58 int cnt = backgroundTaskCount.incrementAndGet();
60 String.format(Locale.ENGLISH, "background task count is %d after incrementing",
62 backgroundTasksRunning.postValue(cnt > 0);
64 public static void backgroundTaskFinished() {
65 int cnt = backgroundTaskCount.decrementAndGet();
67 String.format(Locale.ENGLISH, "background task count is %d after decrementing",
69 backgroundTasksRunning.postValue(cnt > 0);
71 public static void setCurrentProfile(MobileLedgerProfile newProfile) {
72 MLDB.setOption(MLDB.OPT_PROFILE_UUID, (newProfile == null) ? null : newProfile.getUuid());
73 stopTransactionsRetrieval();
74 profile.postValue(newProfile);
76 public static int getProfileIndex(MobileLedgerProfile profile) {
77 try (LockHolder ignored = profilesLocker.lockForReading()) {
78 List<MobileLedgerProfile> prList = profiles.getValue();
79 if (prList == null) throw new AssertionError();
80 for (int i = 0; i < prList.size(); i++) {
81 MobileLedgerProfile p = prList.get(i);
82 if (p.equals(profile)) return i;
88 @SuppressWarnings("WeakerAccess")
89 public static int getProfileIndex(String profileUUID) {
90 try (LockHolder ignored = profilesLocker.lockForReading()) {
91 List<MobileLedgerProfile> prList = profiles.getValue();
92 if (prList == null) throw new AssertionError();
93 for (int i = 0; i < prList.size(); i++) {
94 MobileLedgerProfile p = prList.get(i);
95 if (p.getUuid().equals(profileUUID)) return i;
101 public static int retrieveCurrentThemeIdFromDb() {
102 String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
103 if (profileUUID == null) return -1;
105 SQLiteDatabase db = App.getDatabase();
107 .rawQuery("SELECT theme from profiles where uuid=?", new String[]{profileUUID}))
109 if (c.moveToNext()) return c.getInt(0);
114 public static MobileLedgerProfile getProfile(String profileUUID) {
115 MobileLedgerProfile profile;
116 try (LockHolder readLock = profilesLocker.lockForReading()) {
117 List<MobileLedgerProfile> prList = profiles.getValue();
118 if ((prList == null) || prList.isEmpty()) {
120 try (LockHolder ignored = profilesLocker.lockForWriting()) {
121 profile = MobileLedgerProfile.loadAllFromDB(profileUUID);
125 int i = getProfileIndex(profileUUID);
127 profile = prList.get(i);
132 public synchronized static void scheduleTransactionListRetrieval(MainActivity activity) {
133 if (retrieveTransactionsTask != null) {
134 Logger.debug("db", "Ignoring request for transaction retrieval - already active");
137 MobileLedgerProfile pr = profile.getValue();
138 if (pr == null) throw new IllegalStateException("No current profile");
140 retrieveTransactionsTask =
141 new RetrieveTransactionsTask(new WeakReference<>(activity), profile.getValue());
142 Logger.debug("db", "Created a background transaction retrieval task");
144 retrieveTransactionsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
146 public static synchronized void stopTransactionsRetrieval() {
147 if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
149 public static void transactionRetrievalDone() {
150 retrieveTransactionsTask = null;