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.ui.activity;
20 import android.annotation.SuppressLint;
21 import android.os.AsyncTask;
22 import android.os.Bundle;
24 import androidx.annotation.Nullable;
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.utils.Colors;
32 import net.ktnx.mobileledger.utils.Logger;
34 import java.util.Locale;
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) {
48 String.format(Locale.ROOT, "Ignore request to set theme to the same value (%d)",
54 String.format(Locale.ROOT, "Changing theme from %d to %d", mThemeHue, newHue));
57 Colors.setupTheme(this, mThemeHue);
60 Logger.debug(TAG, "setupProfileColors(): theme already set up, recreating activity");
65 Colors.profileThemeId = mThemeHue;
68 protected void onStart() {
70 Colors.refreshColors(getTheme());
72 protected void onCreate(@Nullable Bundle savedInstanceState) {
75 Data.observeProfile(this, profile -> {
76 if (profile == null) {
77 Logger.debug(TAG, "No current profile, leaving");
82 int hue = profile.getTheme();
84 if (hue != mThemeHue) {
85 storeProfilePref(profile);
86 setupProfileColors(hue);
90 super.onCreate(savedInstanceState);
92 public void storeProfilePref(Profile profile) {
93 App.storeStartupProfileAndTheme(profile.getId(), profile.getTheme());
95 protected void initProfile() {
96 long profileId = App.getStartupProfile();
97 int hue = App.getStartupTheme();
99 mThemeHue = Colors.DEFAULT_HUE_DEG;
101 setupProfileColors(hue);
103 initProfile(profileId);
105 protected void initProfile(long profileId) {
106 AsyncTask.execute(() -> initProfileAsync(profileId));
108 private void initProfileAsync(long profileId) {
109 ProfileDAO dao = DB.get()
111 Profile profile = dao.getByIdSync(profileId);
113 if (profile == null) {
114 Logger.debug(TAG, String.format(Locale.ROOT, "Profile %d not found. Trying any other",
117 profile = dao.getAnySync();
121 Logger.debug(TAG, "No profile could be loaded");
123 Logger.debug(TAG, String.format(Locale.ROOT, "Profile %d loaded. posting", profileId));
124 Data.postCurrentProfile(profile);