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