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