]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/Data.java
stop any running back-end data retrieval when the profile is changed
[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.async.RetrieveTransactionsTask;
25 import net.ktnx.mobileledger.ui.activity.MainActivity;
26 import net.ktnx.mobileledger.utils.LockHolder;
27 import net.ktnx.mobileledger.utils.Locker;
28 import net.ktnx.mobileledger.utils.Logger;
29 import net.ktnx.mobileledger.utils.MLDB;
30 import net.ktnx.mobileledger.utils.ObservableList;
31 import net.ktnx.mobileledger.utils.ObservableValue;
32
33 import java.lang.ref.WeakReference;
34 import java.util.ArrayList;
35 import java.util.Date;
36 import java.util.List;
37 import java.util.Locale;
38 import java.util.concurrent.atomic.AtomicInteger;
39
40 import androidx.lifecycle.MutableLiveData;
41
42 import static net.ktnx.mobileledger.utils.Logger.debug;
43
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<>(new ArrayList<>());
53     public static ObservableValue<Boolean> optShowOnlyStarred = new ObservableValue<>();
54     public static MutableLiveData<String> accountFilter = new MutableLiveData<>();
55     private static AtomicInteger backgroundTaskCount = new AtomicInteger(0);
56     private static Locker profilesLocker = new Locker();
57     private static RetrieveTransactionsTask retrieveTransactionsTask;
58     public static void backgroundTaskStarted() {
59         int cnt = backgroundTaskCount.incrementAndGet();
60         debug("data",
61                 String.format(Locale.ENGLISH, "background task count is %d after incrementing",
62                         cnt));
63         backgroundTasksRunning.postValue(cnt > 0);
64     }
65     public static void backgroundTaskFinished() {
66         int cnt = backgroundTaskCount.decrementAndGet();
67         debug("data",
68                 String.format(Locale.ENGLISH, "background task count is %d after decrementing",
69                         cnt));
70         backgroundTasksRunning.postValue(cnt > 0);
71     }
72     public static void setCurrentProfile(MobileLedgerProfile newProfile) {
73         MLDB.setOption(MLDB.OPT_PROFILE_UUID, newProfile.getUuid());
74         stopTransactionsRetrieval();
75         profile.postValue(newProfile);
76     }
77     public static int getProfileIndex(MobileLedgerProfile profile) {
78         try (LockHolder ignored = profilesLocker.lockForReading()) {
79             List<MobileLedgerProfile> prList = profiles.getValue();
80             assert prList != null;
81             for (int i = 0; i < prList.size(); i++) {
82                 MobileLedgerProfile p = prList.get(i);
83                 if (p.equals(profile)) return i;
84             }
85
86             return -1;
87         }
88     }
89     @SuppressWarnings("WeakerAccess")
90     public static int getProfileIndex(String profileUUID) {
91         try (LockHolder ignored = profilesLocker.lockForReading()) {
92             List<MobileLedgerProfile> prList = profiles.getValue();
93             assert prList != null;
94             for (int i = 0; i < prList.size(); i++) {
95                 MobileLedgerProfile p = prList.get(i);
96                 if (p.getUuid().equals(profileUUID)) return i;
97             }
98
99             return -1;
100         }
101     }
102     public static int retrieveCurrentThemeIdFromDb() {
103         String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
104         if (profileUUID == null) return -1;
105
106         SQLiteDatabase db = MLDB.getDatabase();
107         try (Cursor c = db
108                 .rawQuery("SELECT theme from profiles where uuid=?", new String[]{profileUUID}))
109         {
110             if (c.moveToNext()) return c.getInt(0);
111         }
112
113         return -1;
114     }
115     public static MobileLedgerProfile getProfile(String profileUUID) {
116         MobileLedgerProfile profile;
117         try (LockHolder readLock = profilesLocker.lockForReading()) {
118             List<MobileLedgerProfile> prList = profiles.getValue();
119             assert prList != null;
120             if (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         retrieveTransactionsTask =
140                 new RetrieveTransactionsTask(new WeakReference<>(activity), profile.getValue());
141         Logger.debug("db", "Created a background transaction retrieval task");
142
143         retrieveTransactionsTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
144     }
145     public static synchronized void stopTransactionsRetrieval() {
146         if (retrieveTransactionsTask != null) retrieveTransactionsTask.cancel(false);
147     }
148     public static void transactionRetrievalDone() {
149         retrieveTransactionsTask = null;
150     }
151 }