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.
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 androidx.annotation.NonNull;
21 import androidx.annotation.Nullable;
22 import androidx.lifecycle.LifecycleOwner;
23 import androidx.lifecycle.LiveData;
24 import androidx.lifecycle.MutableLiveData;
25 import androidx.lifecycle.Observer;
27 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
28 import net.ktnx.mobileledger.db.DB;
29 import net.ktnx.mobileledger.db.Profile;
30 import net.ktnx.mobileledger.utils.Locker;
31 import net.ktnx.mobileledger.utils.Logger;
33 import java.text.NumberFormat;
34 import java.text.ParseException;
35 import java.text.ParsePosition;
36 import java.util.Date;
37 import java.util.List;
38 import java.util.Locale;
39 import java.util.concurrent.atomic.AtomicInteger;
41 import static net.ktnx.mobileledger.utils.Logger.debug;
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()
51 public static final MutableLiveData<Currency.Position> currencySymbolPosition =
52 new MutableLiveData<>();
53 public static final MutableLiveData<Boolean> currencyGap = new MutableLiveData<>(true);
54 public static final MutableLiveData<Locale> locale = new MutableLiveData<>();
55 public static final MutableLiveData<Boolean> drawerOpen = new MutableLiveData<>(false);
56 public static final MutableLiveData<Date> lastUpdateDate = new MutableLiveData<>(null);
57 public static final MutableLiveData<Integer> lastUpdateTransactionCount =
58 new MutableLiveData<>(0);
59 public static final MutableLiveData<Integer> lastUpdateAccountCount = new MutableLiveData<>(0);
60 public static final MutableLiveData<String> lastTransactionsUpdateText =
61 new MutableLiveData<>();
62 public static final MutableLiveData<String> lastAccountsUpdateText = new MutableLiveData<>();
63 private static final MutableLiveData<Profile> profile = new MutableLiveData<>();
64 private static final AtomicInteger backgroundTaskCount = new AtomicInteger(0);
65 private static final Locker profilesLocker = new Locker();
66 private static NumberFormat numberFormatter;
69 locale.setValue(Locale.getDefault());
73 public static Profile getProfile() {
74 return profile.getValue();
76 public static void backgroundTaskStarted() {
77 int cnt = backgroundTaskCount.incrementAndGet();
79 String.format(Locale.ENGLISH, "background task count is %d after incrementing",
81 backgroundTasksRunning.postValue(cnt > 0);
83 public static void backgroundTaskFinished() {
84 int cnt = backgroundTaskCount.decrementAndGet();
86 String.format(Locale.ENGLISH, "background task count is %d after decrementing",
88 backgroundTasksRunning.postValue(cnt > 0);
90 public static void setCurrentProfile(@NonNull Profile newProfile) {
91 profile.setValue(newProfile);
93 public static void postCurrentProfile(@NonNull Profile newProfile) {
94 profile.postValue(newProfile);
96 public static void refreshCurrencyData(Locale locale) {
97 NumberFormat formatter = NumberFormat.getCurrencyInstance(locale);
98 java.util.Currency currency = formatter.getCurrency();
99 String symbol = currency != null ? currency.getSymbol() : "";
100 Logger.debug("locale", String.format(
101 "Discovering currency symbol position for locale %s (currency is %s; symbol is %s)",
102 locale.toString(), currency != null ? currency.toString() : "<none>", symbol));
103 String formatted = formatter.format(1234.56f);
104 Logger.debug("locale", String.format("1234.56 formats as '%s'", formatted));
106 if (formatted.startsWith(symbol)) {
107 currencySymbolPosition.setValue(Currency.Position.before);
109 // is the currency symbol directly followed by the first formatted digit?
110 final char canary = formatted.charAt(symbol.length());
111 currencyGap.setValue(canary != '1');
113 else if (formatted.endsWith(symbol)) {
114 currencySymbolPosition.setValue(Currency.Position.after);
116 // is the currency symbol directly preceded bu the last formatted digit?
117 final char canary = formatted.charAt(formatted.length() - symbol.length() - 1);
118 currencyGap.setValue(canary != '6');
121 currencySymbolPosition.setValue(Currency.Position.none);
123 NumberFormat newNumberFormatter = NumberFormat.getNumberInstance();
124 newNumberFormatter.setParseIntegerOnly(false);
125 newNumberFormatter.setGroupingUsed(true);
126 newNumberFormatter.setGroupingUsed(true);
127 newNumberFormatter.setMinimumIntegerDigits(1);
128 newNumberFormatter.setMinimumFractionDigits(2);
130 numberFormatter = newNumberFormatter;
132 public static String formatCurrency(float number) {
133 NumberFormat formatter = NumberFormat.getCurrencyInstance(locale.getValue());
134 return formatter.format(number);
136 public static String formatNumber(float number) {
137 return numberFormatter.format(number);
139 public static void observeProfile(LifecycleOwner lifecycleOwner, Observer<Profile> observer) {
140 profile.observe(lifecycleOwner, observer);
142 public static void removeProfileObservers(LifecycleOwner owner) {
143 profile.removeObservers(owner);
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());
151 return parsed.floatValue();