]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/Data.java
replace assertions with good old if()
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / Data.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.model;
19
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22 import android.os.AsyncTask;
23
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;
33
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;
40
41 import androidx.lifecycle.MutableLiveData;
42
43 import static net.ktnx.mobileledger.utils.Logger.debug;
44
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();
61         debug("data",
62                 String.format(Locale.ENGLISH, "background task count is %d after incrementing",
63                         cnt));
64         backgroundTasksRunning.postValue(cnt > 0);
65     }
66     public static void backgroundTaskFinished() {
67         int cnt = backgroundTaskCount.decrementAndGet();
68         debug("data",
69                 String.format(Locale.ENGLISH, "background task count is %d after decrementing",
70                         cnt));
71         backgroundTasksRunning.postValue(cnt > 0);
72     }
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);
77     }
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;
85             }
86
87             return -1;
88         }
89     }
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;
98             }
99
100             return -1;
101         }
102     }
103     public static int retrieveCurrentThemeIdFromDb() {
104         String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
105         if (profileUUID == null) return -1;
106
107         SQLiteDatabase db = App.getDatabase();
108         try (Cursor c = db
109                 .rawQuery("SELECT theme from profiles where uuid=?", new String[]{profileUUID}))
110         {
111             if (c.moveToNext()) return c.getInt(0);
112         }
113
114         return -1;
115     }
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()) {
121                 readLock.close();
122                 try (LockHolder ignored = profilesLocker.lockForWriting()) {
123                     profile = MobileLedgerProfile.loadAllFromDB(profileUUID);
124                 }
125             }
126             else {
127                 int i = getProfileIndex(profileUUID);
128                 if (i == -1) i = 0;
129                 profile = prList.get(i);
130             }
131         }
132         return profile;
133     }
134     public synchronized static void scheduleTransactionListRetrieval(MainActivity activity) {
135         if (retrieveTransactionsTask != null) {
136             Logger.debug("db", "Ignoring request for transaction retrieval - already active");
137             return;
138         }
139         MobileLedgerProfile pr = profile.getValue();
140         if (pr == null) throw new IllegalStateException("No current profile");
141
142         retrieveTransactionsTask =
143                 new RetrieveTransactionsTask(new WeakReference<>(activity), profile.getValue());
144         Logger.debug("db", "Created a background transaction retrieval task");
145
146         retrieveTransactionsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
147     }
148     public static synchronized void stopTransactionsRetrieval() {
149         if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
150     }
151     public static void transactionRetrievalDone() {
152         retrieveTransactionsTask = null;
153     }
154 }