]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/utils/Globals.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / utils / Globals.java
1 /*
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.
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.utils;
19
20 import android.app.Activity;
21 import android.content.Context;
22 import android.view.View;
23 import android.view.inputmethod.InputMethodManager;
24
25 import java.text.ParseException;
26 import java.text.SimpleDateFormat;
27 import java.util.Calendar;
28 import java.util.Locale;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 public final class Globals {
33     private static final ThreadLocal<SimpleDateFormat> dateFormatter =
34             new ThreadLocal<SimpleDateFormat>() {
35                 @Override
36                 protected SimpleDateFormat initialValue() {
37                     return new SimpleDateFormat("yyyy/MM/dd", Locale.US);
38                 }
39             };
40     private static final ThreadLocal<SimpleDateFormat> isoDateFormatter =
41             new ThreadLocal<SimpleDateFormat>() {
42                 @Override
43                 protected SimpleDateFormat initialValue() {
44                     return new SimpleDateFormat("yyyy-MM-dd", Locale.US);
45                 }
46             };
47     public static String[] monthNames;
48     public static final String developerEmail = "dam+mole-crash@ktnx.net";
49     private static final Pattern reLedgerDate =
50             Pattern.compile("^(?:(?:(\\d+)/)??(\\d\\d?)/)?(\\d\\d?)$");
51     public static void hideSoftKeyboard(Activity act) {
52         // hide the keyboard
53         View v = act.getCurrentFocus();
54         if (v != null) {
55             InputMethodManager imm =
56                     (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
57             imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
58         }
59     }
60     public static SimpleDate parseLedgerDate(String dateString) throws ParseException {
61         Matcher m = reLedgerDate.matcher(dateString);
62         if (!m.matches()) throw new ParseException(
63                 String.format("'%s' does not match expected pattern '%s'", dateString,
64                         reLedgerDate.toString()), 0);
65
66         String yearStr = m.group(1);
67         String monthStr = m.group(2);
68         String dayStr = m.group(3);
69
70         int year, month, day;
71
72         String toParse;
73         if (yearStr == null) {
74             SimpleDate today = SimpleDate.today();
75             year = today.year;
76             if (monthStr == null) {
77                 month = today.month;
78             }
79             else month = Integer.parseInt(monthStr);
80         }
81         else {
82             year = Integer.parseInt(yearStr);
83             assert monthStr != null;
84             month = Integer.parseInt(monthStr);
85         }
86
87         assert dayStr != null;
88         day = Integer.parseInt(dayStr);
89
90         return new SimpleDate(year, month, day);
91     }
92     public static Calendar parseLedgerDateAsCalendar(String dateString) throws ParseException {
93         return parseLedgerDate(dateString).toCalendar();
94     }
95     public static SimpleDate parseIsoDate(String dateString) throws ParseException {
96         return SimpleDate.fromDate(isoDateFormatter.get().parse(dateString));
97     }
98     public static String formatLedgerDate(SimpleDate date) {
99         return dateFormatter.get().format(date.toDate());
100     }
101     public static String formatIsoDate(SimpleDate date) {
102         return isoDateFormatter.get().format(date.toDate());
103     }
104 }