X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Factivity%2FProfileThemedActivity.java;h=b9726492844cfd959cb208ae6258a423fb1ebd52;hp=f4d482a5b7a93b356684115fb3f0c0c822d0c26d;hb=751aaa5b5283931ca7a221127a3737402d3c6b84;hpb=5e1dd65e12ff7a6c7dc3fece1238459dd0fc3efe diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java index f4d482a5..b9726492 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/ProfileThemedActivity.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Damyan Ivanov. + * Copyright © 2021 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -22,16 +22,50 @@ import android.os.Bundle; import androidx.annotation.Nullable; +import net.ktnx.mobileledger.App; +import net.ktnx.mobileledger.dao.BaseDAO; +import net.ktnx.mobileledger.dao.ProfileDAO; +import net.ktnx.mobileledger.db.DB; +import net.ktnx.mobileledger.db.Profile; import net.ktnx.mobileledger.model.Data; -import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.utils.Colors; -import net.ktnx.mobileledger.utils.MLDB; +import net.ktnx.mobileledger.utils.Logger; + +import java.util.Locale; @SuppressLint("Registered") public class ProfileThemedActivity extends CrashReportingActivity { - protected MobileLedgerProfile mProfile; - protected void setupProfileColors() { - Colors.setupTheme(this, mProfile); + public static final String TAG = "prf-thm-act"; + protected static final String PARAM_PROFILE_ID = "profile-id"; + protected static final String PARAM_THEME = "theme"; + protected Profile mProfile; + private boolean themeSetUp = false; + private boolean mIgnoreProfileChange; + private int mThemeHue; + protected void setupProfileColors(int newHue) { + if (themeSetUp && newHue == mThemeHue) { + Logger.debug(TAG, + String.format(Locale.ROOT, "Ignore request to set theme to the same value (%d)", + newHue)); + return; + } + + Logger.debug(TAG, + String.format(Locale.ROOT, "Changing theme from %d to %d", mThemeHue, newHue)); + + mThemeHue = newHue; + Colors.setupTheme(this, mThemeHue); + + if (themeSetUp) { + Logger.debug(TAG, + "setupProfileColors(): theme already set up, supposedly the activity will be " + + "recreated"); +// this.recreate(); + return; + } + themeSetUp = true; + + Colors.profileThemeId = mThemeHue; } @Override protected void onStart() { @@ -40,19 +74,62 @@ public class ProfileThemedActivity extends CrashReportingActivity { } protected void onCreate(@Nullable Bundle savedInstanceState) { initProfile(); - setupProfileColors(); + + Data.observeProfile(this, profile -> { + if (profile == null) { + Logger.debug(TAG, "No current profile, leaving"); + return; + } + + mProfile = profile; + storeProfilePref(profile); + int hue = profile.getTheme(); + + if (hue != mThemeHue) { + Logger.debug(TAG, + String.format(Locale.US, "profile observer calling setupProfileColors(%d)", + hue)); + setupProfileColors(hue); + } + }); + super.onCreate(savedInstanceState); } + public void storeProfilePref(Profile profile) { + App.storeStartupProfileAndTheme(profile.getId(), profile.getTheme()); + } protected void initProfile() { - mProfile = Data.profile.getValue(); - if (mProfile == null) { - String profileUUID = MLDB.getOption(MLDB.OPT_PROFILE_UUID, null); - MobileLedgerProfile startupProfile; + long profileId = App.getStartupProfile(); + int hue = App.getStartupTheme(); + if (profileId == -1) + mThemeHue = Colors.DEFAULT_HUE_DEG; + + Logger.debug(TAG, + String.format(Locale.US, "initProfile() calling setupProfileColors(%d)", hue)); + setupProfileColors(hue); + initProfile(profileId); + } + protected void initProfile(long profileId) { + BaseDAO.runAsync(() -> initProfileSync(profileId)); + } + private void initProfileSync(long profileId) { + Logger.debug(TAG, String.format(Locale.US, "Loading profile %d", profileId)); + ProfileDAO dao = DB.get() + .getProfileDAO(); + Profile profile = dao.getByIdSync(profileId); - startupProfile = Data.getProfile(profileUUID); - Data.setCurrentProfile(startupProfile); - mProfile = startupProfile; + if (profile == null) { + Logger.debug(TAG, String.format(Locale.ROOT, "Profile %d not found. Trying any other", + profileId)); + + profile = dao.getAnySync(); } + + if (profile == null) + Logger.debug(TAG, "No profile could be loaded"); + else + Logger.debug(TAG, String.format(Locale.ROOT, "Profile %d loaded. posting", profileId)); + Data.postCurrentProfile(profile); } }