]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/Data.java
35f39358c46efa6d0634bea443c6a5bb01501af5
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / Data.java
1 /*
2  * Copyright © 2021 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 androidx.annotation.NonNull;
21 import androidx.lifecycle.LifecycleOwner;
22 import androidx.lifecycle.LiveData;
23 import androidx.lifecycle.MutableLiveData;
24 import androidx.lifecycle.Observer;
25
26 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
27 import net.ktnx.mobileledger.db.DB;
28 import net.ktnx.mobileledger.db.Profile;
29 import net.ktnx.mobileledger.utils.Locker;
30 import net.ktnx.mobileledger.utils.Logger;
31
32 import java.text.NumberFormat;
33 import java.text.ParseException;
34 import java.text.ParsePosition;
35 import java.util.Date;
36 import java.util.List;
37 import java.util.Locale;
38 import java.util.Objects;
39 import java.util.concurrent.atomic.AtomicInteger;
40
41 import static net.ktnx.mobileledger.utils.Logger.debug;
42
43 public final class Data {
44     public static final MutableLiveData<Boolean> backgroundTasksRunning =
45             new MutableLiveData<>(false);
46     public static final MutableLiveData<RetrieveTransactionsTask.Progress> backgroundTaskProgress =
47             new MutableLiveData<>();
48     public static final LiveData<List<Profile>> profiles = DB.get().getProfileDAO().getAllOrdered();
49     public static final MutableLiveData<Currency.Position> currencySymbolPosition =
50             new MutableLiveData<>();
51     public static final MutableLiveData<Boolean> currencyGap = new MutableLiveData<>(true);
52     public static final MutableLiveData<Locale> locale = new MutableLiveData<>();
53     public static final MutableLiveData<Boolean> drawerOpen = new MutableLiveData<>(false);
54     public static final MutableLiveData<Date> lastUpdateDate = new MutableLiveData<>(null);
55     public static final MutableLiveData<Integer> lastUpdateTransactionCount =
56             new MutableLiveData<>(0);
57     public static final MutableLiveData<Integer> lastUpdateAccountCount = new MutableLiveData<>(0);
58     public static final MutableLiveData<String> lastTransactionsUpdateText =
59             new MutableLiveData<>();
60     public static final MutableLiveData<String> lastAccountsUpdateText = new MutableLiveData<>();
61     private static final MutableLiveData<Profile> profile =
62             new InertMutableLiveData<>();
63     private static final AtomicInteger backgroundTaskCount = new AtomicInteger(0);
64     private static final Locker profilesLocker = new Locker();
65     private static NumberFormat numberFormatter;
66
67     static {
68         locale.setValue(Locale.getDefault());
69     }
70
71     @NonNull
72     public static Profile getProfile() {
73         return Objects.requireNonNull(profile.getValue());
74     }
75     public static void backgroundTaskStarted() {
76         int cnt = backgroundTaskCount.incrementAndGet();
77         debug("data",
78                 String.format(Locale.ENGLISH, "background task count is %d after incrementing",
79                         cnt));
80         backgroundTasksRunning.postValue(cnt > 0);
81     }
82     public static void backgroundTaskFinished() {
83         int cnt = backgroundTaskCount.decrementAndGet();
84         debug("data",
85                 String.format(Locale.ENGLISH, "background task count is %d after decrementing",
86                         cnt));
87         backgroundTasksRunning.postValue(cnt > 0);
88     }
89     public static void setCurrentProfile(@NonNull Profile newProfile) {
90         profile.setValue(newProfile);
91     }
92     public static void postCurrentProfile(@NonNull Profile newProfile) {
93         profile.postValue(newProfile);
94     }
95     public static void refreshCurrencyData(Locale locale) {
96         NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);
97         java.util.Currency currency = formatter.getCurrency();
98         String symbol = currency != null ? currency.getSymbol() : "";
99         Logger.debug("locale", String.format(
100                 "Discovering currency symbol position for locale %s (currency is %s; symbol is %s)",
101                 locale.toString(), currency != null ? currency.toString() : "<none>", symbol));
102         String formatted = formatter.format(1234.56f);
103         Logger.debug("locale", String.format("1234.56 formats as '%s'", formatted));
104
105         if (formatted.startsWith(symbol)) {
106             currencySymbolPosition.setValue(Currency.Position.before);
107
108             // is the currency symbol directly followed by the first formatted digit?
109             final char canary = formatted.charAt(symbol.length());
110             currencyGap.setValue(canary != '1');
111         }
112         else if (formatted.endsWith(symbol)) {
113             currencySymbolPosition.setValue(Currency.Position.after);
114
115             // is the currency symbol directly preceded bu the last formatted digit?
116             final char canary = formatted.charAt(formatted.length() - symbol.length() - 1);
117             currencyGap.setValue(canary != '6');
118         }
119         else
120             currencySymbolPosition.setValue(Currency.Position.none);
121
122         NumberFormat newNumberFormatter = NumberFormat.getNumberInstance();
123         newNumberFormatter.setParseIntegerOnly(false);
124         newNumberFormatter.setGroupingUsed(true);
125         newNumberFormatter.setGroupingUsed(true);
126         newNumberFormatter.setMinimumIntegerDigits(1);
127         newNumberFormatter.setMinimumFractionDigits(2);
128
129         numberFormatter = newNumberFormatter;
130     }
131     public static String formatCurrency(float number) {
132         NumberFormat formatter = NumberFormat.getCurrencyInstance(locale.getValue());
133         return formatter.format(number);
134     }
135     public static String formatNumber(float number) {
136         return numberFormatter.format(number);
137     }
138     public static void observeProfile(LifecycleOwner lifecycleOwner,
139                                       Observer<Profile> observer) {
140         profile.observe(lifecycleOwner, observer);
141     }
142     public static void removeProfileObservers(LifecycleOwner owner) {
143         profile.removeObservers(owner);
144     }
145     public static float parseNumber(String str) throws ParseException {
146         ParsePosition pos = new ParsePosition(0);
147         Number parsed = numberFormatter.parse(str);
148         if (parsed == null || pos.getErrorIndex() > -1)
149             throw new ParseException("Error parsing '" + str + "'", pos.getErrorIndex());
150
151         return parsed.floatValue();
152     }
153 }