X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;ds=sidebyside;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Factivity%2FProfileThemedActivity.java;h=a1beb94be914565384fd5dfa33b8b003586412a6;hb=fe70111bba0edb9256275958223c2b561f7340e0;hp=1301a85fc6ce4b0132c23b944ac49d498e23ce79;hpb=e975cffd3885678f30f69d09fbc99905c3118588;p=mobile-ledger.git
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 1301a85f..a1beb94b 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,31 +1,68 @@
/*
- * Copyright © 2019 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
- * the Free Software Foundation, either version 3 of the License, or
- * (at your opinion), any later version.
+ * 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
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your opinion), any later version.
*
- * MoLe is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License terms for details.
+ * MoLe is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License terms for details.
*
- * You should have received a copy of the GNU General Public License
- * along with Mobile-Ledger. If not, see .
+ * You should have received a copy of the GNU General Public License
+ * along with MoLe. If not, see .
*/
package net.ktnx.mobileledger.ui.activity;
+import android.annotation.SuppressLint;
+import android.os.AsyncTask;
import android.os.Bundle;
+import androidx.annotation.Nullable;
+
+import net.ktnx.mobileledger.App;
+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.utils.Colors;
+import net.ktnx.mobileledger.utils.Logger;
-import androidx.annotation.Nullable;
+import java.util.Locale;
+@SuppressLint("Registered")
public class ProfileThemedActivity extends CrashReportingActivity {
- protected void setupProfileColors() {
- Colors.setupTheme(this);
+ 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, recreating activity");
+ this.recreate();
+ }
+ themeSetUp = true;
+
+ Colors.profileThemeId = mThemeHue;
}
@Override
protected void onStart() {
@@ -33,7 +70,55 @@ public class ProfileThemedActivity extends CrashReportingActivity {
Colors.refreshColors(getTheme());
}
protected void onCreate(@Nullable Bundle savedInstanceState) {
+ initProfile();
+
+ Data.observeProfile(this, profile -> {
+ if (profile == null) {
+ Logger.debug(TAG, "No current profile, leaving");
+ finish();
+ return;
+ }
+
+ mProfile = profile;
+ int hue = profile.getTheme();
+
+ if (hue != mThemeHue) {
+ storeProfilePref(profile);
+ setupProfileColors(hue);
+ }
+ });
+
super.onCreate(savedInstanceState);
- Colors.setupTheme(this);
+ }
+ public void storeProfilePref(Profile profile) {
+ App.storeStartupProfileAndTheme(profile.getId(), profile.getTheme());
+ }
+ protected void initProfile() {
+ long profileId = App.getStartupProfile();
+ int hue = App.getStartupTheme();
+ if (profileId == -1)
+ mThemeHue = Colors.DEFAULT_HUE_DEG;
+
+ setupProfileColors(hue);
+
+ initProfile(profileId);
+ }
+ protected void initProfile(long profileId) {
+ AsyncTask.execute(() -> initProfileAsync(profileId));
+ }
+ private void initProfileAsync(long profileId) {
+ ProfileDAO dao = DB.get()
+ .getProfileDAO();
+ Profile profile = dao.getByIdSync(profileId);
+
+ if (profile == null) {
+ Logger.debug(TAG, String.format(Locale.ROOT, "Profile %d not found. Trying any other",
+ profileId));
+
+ profile = dao.getAnySync();
+ }
+
+ Logger.debug(TAG, String.format(Locale.ROOT, "Profile %d loaded. posting", profileId));
+ Data.postCurrentProfile(profile);
}
}