2 * Copyright © 2020 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;
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 import androidx.lifecycle.LifecycleOwner;
26 import androidx.lifecycle.MutableLiveData;
27 import androidx.lifecycle.Observer;
29 import net.ktnx.mobileledger.App;
30 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
31 import net.ktnx.mobileledger.utils.LockHolder;
32 import net.ktnx.mobileledger.utils.Locker;
33 import net.ktnx.mobileledger.utils.Logger;
34 import net.ktnx.mobileledger.utils.MLDB;
35 import net.ktnx.mobileledger.utils.ObservableValue;
37 import java.text.NumberFormat;
38 import java.util.ArrayList;
39 import java.util.Date;
40 import java.util.List;
41 import java.util.Locale;
42 import java.util.Objects;
43 import java.util.concurrent.atomic.AtomicInteger;
45 import static net.ktnx.mobileledger.utils.Logger.debug;
47 public final class Data {
48 public static final MutableLiveData<Boolean> backgroundTasksRunning =
49 new MutableLiveData<>(false);
50 public static final MutableLiveData<RetrieveTransactionsTask.Progress> backgroundTaskProgress =
51 new MutableLiveData<>();
52 public static final MutableLiveData<ArrayList<MobileLedgerProfile>> profiles =
53 new MutableLiveData<>(null);
54 public static final MutableLiveData<Currency.Position> currencySymbolPosition =
55 new MutableLiveData<>();
56 public static final MutableLiveData<Boolean> currencyGap = new MutableLiveData<>(true);
57 public static final MutableLiveData<Locale> locale = new MutableLiveData<>();
58 public static final MutableLiveData<Boolean> drawerOpen = new MutableLiveData<>(false);
59 public static final MutableLiveData<Date> lastUpdateDate = new MutableLiveData<>(null);
60 public static final MutableLiveData<Integer> lastUpdateTransactionCount =
61 new MutableLiveData<>(0);
62 public static final MutableLiveData<Integer> lastUpdateAccountCount = new MutableLiveData<>(0);
63 public static final ObservableValue<String> lastTransactionsUpdateText =
64 new ObservableValue<>();
65 public static final ObservableValue<String> lastAccountsUpdateText = new ObservableValue<>();
66 private static final MutableLiveData<MobileLedgerProfile> profile =
67 new InertMutableLiveData<>();
68 private static final AtomicInteger backgroundTaskCount = new AtomicInteger(0);
69 private static final Locker profilesLocker = new Locker();
72 locale.setValue(Locale.getDefault());
76 public static MobileLedgerProfile getProfile() {
77 return Objects.requireNonNull(profile.getValue());
79 public static void backgroundTaskStarted() {
80 int cnt = backgroundTaskCount.incrementAndGet();
82 String.format(Locale.ENGLISH, "background task count is %d after incrementing",
84 backgroundTasksRunning.postValue(cnt > 0);
86 public static void backgroundTaskFinished() {
87 int cnt = backgroundTaskCount.decrementAndGet();
89 String.format(Locale.ENGLISH, "background task count is %d after decrementing",
91 backgroundTasksRunning.postValue(cnt > 0);
93 public static void setCurrentProfile(@NonNull MobileLedgerProfile newProfile) {
94 MLDB.setOption(MLDB.OPT_PROFILE_UUID, newProfile.getUuid());
95 profile.setValue(newProfile);
97 public static int getProfileIndex(MobileLedgerProfile profile) {
98 try (LockHolder ignored = profilesLocker.lockForReading()) {
99 List<MobileLedgerProfile> prList = profiles.getValue();
101 throw new AssertionError();
102 for (int i = 0; i < prList.size(); i++) {
103 MobileLedgerProfile p = prList.get(i);
104 if (p.equals(profile))
111 @SuppressWarnings("WeakerAccess")
112 public static int getProfileIndex(String profileUUID) {
113 try (LockHolder ignored = profilesLocker.lockForReading()) {
114 List<MobileLedgerProfile> prList = profiles.getValue();
116 throw new AssertionError();
117 for (int i = 0; i < prList.size(); i++) {
118 MobileLedgerProfile p = prList.get(i);
120 .equals(profileUUID))
127 public static int retrieveCurrentThemeIdFromDb() {
128 String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
129 if (profileUUID == null)
132 SQLiteDatabase db = App.getDatabase();
133 try (Cursor c = db.rawQuery("SELECT theme from profiles where uuid=?",
134 new String[]{profileUUID}))
143 public static MobileLedgerProfile getProfile(String profileUUID) {
144 MobileLedgerProfile profile;
145 try (LockHolder readLock = profilesLocker.lockForReading()) {
146 List<MobileLedgerProfile> prList = profiles.getValue();
147 if ((prList == null) || prList.isEmpty()) {
149 try (LockHolder ignored = profilesLocker.lockForWriting()) {
150 profile = MobileLedgerProfile.loadAllFromDB(profileUUID);
154 int i = getProfileIndex(profileUUID);
157 profile = prList.get(i);
162 public static void refreshCurrencyData(Locale locale) {
163 NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);
164 java.util.Currency currency = formatter.getCurrency();
165 String symbol = currency != null ? currency.getSymbol() : "";
166 Logger.debug("locale", String.format(
167 "Discovering currency symbol position for locale %s (currency is %s; symbol is %s)",
168 locale.toString(), currency != null ? currency.toString() : "<none>", symbol));
169 String formatted = formatter.format(1234.56f);
170 Logger.debug("locale", String.format("1234.56 formats as '%s'", formatted));
172 if (formatted.startsWith(symbol)) {
173 currencySymbolPosition.setValue(Currency.Position.before);
175 // is the currency symbol directly followed by the first formatted digit?
176 final char canary = formatted.charAt(symbol.length());
177 currencyGap.setValue(canary != '1');
179 else if (formatted.endsWith(symbol)) {
180 currencySymbolPosition.setValue(Currency.Position.after);
182 // is the currency symbol directly preceded bu the last formatted digit?
183 final char canary = formatted.charAt(formatted.length() - symbol.length() - 1);
184 currencyGap.setValue(canary != '6');
187 currencySymbolPosition.setValue(Currency.Position.none);
190 public static void observeProfile(LifecycleOwner lifecycleOwner,
191 Observer<MobileLedgerProfile> observer) {
192 profile.observe(lifecycleOwner, observer);
194 public synchronized static MobileLedgerProfile initProfile() {
195 MobileLedgerProfile currentProfile = profile.getValue();
196 if (currentProfile != null)
197 return currentProfile;
199 String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null);
200 MobileLedgerProfile startupProfile = getProfile(profileUUID);
201 if (startupProfile != null)
202 setCurrentProfile(startupProfile);
203 return startupProfile;
206 public static void removeProfileObservers(LifecycleOwner owner) {
207 profile.removeObservers(owner);