]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / ProfileThemedActivity.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.ui.activity;
19
20 import android.annotation.SuppressLint;
21 import android.os.Bundle;
22
23 import androidx.annotation.Nullable;
24
25 import net.ktnx.mobileledger.App;
26 import net.ktnx.mobileledger.dao.BaseDAO;
27 import net.ktnx.mobileledger.dao.ProfileDAO;
28 import net.ktnx.mobileledger.db.DB;
29 import net.ktnx.mobileledger.db.Profile;
30 import net.ktnx.mobileledger.model.Data;
31 import net.ktnx.mobileledger.utils.Colors;
32 import net.ktnx.mobileledger.utils.Logger;
33
34 import java.util.Locale;
35
36 @SuppressLint("Registered")
37 public class ProfileThemedActivity extends CrashReportingActivity {
38     public static final String TAG = "prf-thm-act";
39     protected static final String PARAM_PROFILE_ID = "profile-id";
40     protected static final String PARAM_THEME = "theme";
41     protected Profile mProfile;
42     private boolean themeSetUp = false;
43     private boolean mIgnoreProfileChange;
44     private int mThemeHue;
45     protected void setupProfileColors(int newHue) {
46         if (themeSetUp && newHue == mThemeHue) {
47             Logger.debug(TAG,
48                     String.format(Locale.ROOT, "Ignore request to set theme to the same value (%d)",
49                             newHue));
50             return;
51         }
52
53         Logger.debug(TAG,
54                 String.format(Locale.ROOT, "Changing theme from %d to %d", mThemeHue, newHue));
55
56         mThemeHue = newHue;
57         Colors.setupTheme(this, mThemeHue);
58
59         if (themeSetUp) {
60             Logger.debug(TAG,
61                     "setupProfileColors(): theme already set up, supposedly the activity will be " +
62                     "recreated");
63 //            this.recreate();
64             return;
65         }
66         themeSetUp = true;
67
68         Colors.profileThemeId = mThemeHue;
69     }
70     @Override
71     protected void onStart() {
72         super.onStart();
73         Colors.refreshColors(getTheme());
74     }
75     protected void onCreate(@Nullable Bundle savedInstanceState) {
76         initProfile();
77
78         Data.observeProfile(this, profile -> {
79             if (profile == null) {
80                 Logger.debug(TAG, "No current profile, leaving");
81                 return;
82             }
83
84             mProfile = profile;
85             storeProfilePref(profile);
86             int hue = profile.getTheme();
87
88             if (hue != mThemeHue) {
89                 Logger.debug(TAG,
90                         String.format(Locale.US, "profile observer calling setupProfileColors(%d)",
91                                 hue));
92                 setupProfileColors(hue);
93             }
94         });
95
96         super.onCreate(savedInstanceState);
97     }
98     public void storeProfilePref(Profile profile) {
99         App.storeStartupProfileAndTheme(profile.getId(), profile.getTheme());
100     }
101     protected void initProfile() {
102         long profileId = App.getStartupProfile();
103         int hue = App.getStartupTheme();
104         if (profileId == -1)
105             mThemeHue = Colors.DEFAULT_HUE_DEG;
106
107         Logger.debug(TAG,
108                 String.format(Locale.US, "initProfile() calling setupProfileColors(%d)", hue));
109         setupProfileColors(hue);
110
111         initProfile(profileId);
112     }
113     protected void initProfile(long profileId) {
114         BaseDAO.runAsync(() -> initProfileSync(profileId));
115     }
116     private void initProfileSync(long profileId) {
117         Logger.debug(TAG, String.format(Locale.US, "Loading profile %d", profileId));
118         ProfileDAO dao = DB.get()
119                            .getProfileDAO();
120         Profile profile = dao.getByIdSync(profileId);
121
122         if (profile == null) {
123             Logger.debug(TAG, String.format(Locale.ROOT, "Profile %d not found. Trying any other",
124                     profileId));
125
126             profile = dao.getAnySync();
127         }
128
129         if (profile == null)
130             Logger.debug(TAG, "No profile could be loaded");
131         else
132             Logger.debug(TAG, String.format(Locale.ROOT, "Profile %d loaded. posting", profileId));
133         Data.postCurrentProfile(profile);
134     }
135 }