From 05aa5b11729e907ae480d8a9cbdadc09e76f0ec9 Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Sun, 31 Jul 2016 08:42:40 -0400 Subject: [PATCH] Replace InterfaceUtils theme methods by ThemeSwitcher --- .../org/isoron/uhabits/BaseAndroidTest.java | 1 - .../views/CheckmarkWidgetViewTest.java | 1 - .../uhabits/activities/ActivityComponent.java | 2 + .../uhabits/activities/BaseActivity.java | 5 +- .../uhabits/activities/BaseRootView.java | 8 +- .../uhabits/activities/ThemeSwitcher.java | 90 +++++++++++++++++++ .../habits/list/ListHabitsMenu.java | 8 +- .../habits/list/ListHabitsScreen.java | 14 +-- .../isoron/uhabits/utils/InterfaceUtils.java | 57 ------------ .../org/isoron/uhabits/utils/Preferences.java | 16 ++++ .../isoron/uhabits/utils/StyledResources.java | 6 +- 11 files changed, 136 insertions(+), 72 deletions(-) create mode 100644 app/src/main/java/org/isoron/uhabits/activities/ThemeSwitcher.java diff --git a/app/src/androidTest/java/org/isoron/uhabits/BaseAndroidTest.java b/app/src/androidTest/java/org/isoron/uhabits/BaseAndroidTest.java index 6c7829ae7..aca8bc5ba 100644 --- a/app/src/androidTest/java/org/isoron/uhabits/BaseAndroidTest.java +++ b/app/src/androidTest/java/org/isoron/uhabits/BaseAndroidTest.java @@ -114,7 +114,6 @@ public class BaseAndroidTest protected void setTheme(@StyleRes int themeId) { - InterfaceUtils.setFixedTheme(themeId); targetContext.setTheme(themeId); } diff --git a/app/src/androidTest/java/org/isoron/uhabits/widgets/views/CheckmarkWidgetViewTest.java b/app/src/androidTest/java/org/isoron/uhabits/widgets/views/CheckmarkWidgetViewTest.java index 7f7a955ea..726e8bdf1 100644 --- a/app/src/androidTest/java/org/isoron/uhabits/widgets/views/CheckmarkWidgetViewTest.java +++ b/app/src/androidTest/java/org/isoron/uhabits/widgets/views/CheckmarkWidgetViewTest.java @@ -45,7 +45,6 @@ public class CheckmarkWidgetViewTest extends BaseViewTest public void setUp() { super.setUp(); - InterfaceUtils.setFixedTheme(R.style.TransparentWidgetTheme); habit = fixtures.createShortHabit(); view = new CheckmarkWidgetView(targetContext); diff --git a/app/src/main/java/org/isoron/uhabits/activities/ActivityComponent.java b/app/src/main/java/org/isoron/uhabits/activities/ActivityComponent.java index e187556e3..e80a9f540 100644 --- a/app/src/main/java/org/isoron/uhabits/activities/ActivityComponent.java +++ b/app/src/main/java/org/isoron/uhabits/activities/ActivityComponent.java @@ -30,4 +30,6 @@ import dagger.*; public interface ActivityComponent { ColorPickerDialogFactory getColorPickerDialogFactory(); + + ThemeSwitcher getThemeSwitcher(); } diff --git a/app/src/main/java/org/isoron/uhabits/activities/BaseActivity.java b/app/src/main/java/org/isoron/uhabits/activities/BaseActivity.java index 83181fae6..8640c722e 100644 --- a/app/src/main/java/org/isoron/uhabits/activities/BaseActivity.java +++ b/app/src/main/java/org/isoron/uhabits/activities/BaseActivity.java @@ -26,7 +26,6 @@ import android.support.v7.app.*; import android.view.*; import org.isoron.uhabits.*; -import org.isoron.uhabits.utils.*; /** * Base class for all activities in the application. @@ -128,7 +127,7 @@ abstract public class BaseActivity extends AppCompatActivity protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - InterfaceUtils.applyCurrentTheme(this); + androidExceptionHandler = Thread.getDefaultUncaughtExceptionHandler(); Thread.setDefaultUncaughtExceptionHandler(this); @@ -139,5 +138,7 @@ abstract public class BaseActivity extends AppCompatActivity .activityModule(new ActivityModule(this)) .appComponent(app.getComponent()) .build(); + + component.getThemeSwitcher().apply(); } } diff --git a/app/src/main/java/org/isoron/uhabits/activities/BaseRootView.java b/app/src/main/java/org/isoron/uhabits/activities/BaseRootView.java index e523bbd06..176065757 100644 --- a/app/src/main/java/org/isoron/uhabits/activities/BaseRootView.java +++ b/app/src/main/java/org/isoron/uhabits/activities/BaseRootView.java @@ -43,10 +43,16 @@ public abstract class BaseRootView extends FrameLayout { private final Context context; + private final BaseActivity activity; + + private final ThemeSwitcher themeSwitcher; + public BaseRootView(Context context) { super(context); this.context = context; + activity = (BaseActivity) context; + themeSwitcher = activity.getComponent().getThemeSwitcher(); } public boolean getDisplayHomeAsUp() @@ -59,7 +65,7 @@ public abstract class BaseRootView extends FrameLayout public int getToolbarColor() { - if (SDK_INT < LOLLIPOP && !InterfaceUtils.isNightMode()) + if (SDK_INT < LOLLIPOP && !themeSwitcher.isNightMode()) { return context .getResources() diff --git a/app/src/main/java/org/isoron/uhabits/activities/ThemeSwitcher.java b/app/src/main/java/org/isoron/uhabits/activities/ThemeSwitcher.java new file mode 100644 index 000000000..d0744108d --- /dev/null +++ b/app/src/main/java/org/isoron/uhabits/activities/ThemeSwitcher.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker 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 for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +package org.isoron.uhabits.activities; + +import android.support.annotation.*; + +import org.isoron.uhabits.*; +import org.isoron.uhabits.utils.*; + +import javax.inject.*; + +@ActivityScope +public class ThemeSwitcher +{ + public static final int THEME_DARK = 1; + + public static final int THEME_LIGHT = 0; + + @NonNull + private final BaseActivity activity; + + private Preferences preferences; + + @Inject + public ThemeSwitcher(@NonNull BaseActivity activity, + @NonNull Preferences preferences) + { + this.activity = activity; + this.preferences = preferences; + } + + public void apply() + { + switch (getTheme()) + { + case THEME_DARK: + applyDarkTheme(); + break; + + case THEME_LIGHT: + default: + applyLightTheme(); + break; + } + } + + public boolean isNightMode() + { + return getTheme() == THEME_DARK; + } + + private void applyDarkTheme() + { + if (preferences.isPureBlackEnabled()) + activity.setTheme(R.style.AppBaseThemeDark_PureBlack); + else activity.setTheme(R.style.AppBaseThemeDark); + } + + private void applyLightTheme() + { + activity.setTheme(R.style.AppBaseTheme); + } + + private int getTheme() + { + return preferences.getTheme(); + } + + public void setTheme(int theme) + { + preferences.setTheme(theme); + } +} diff --git a/app/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsMenu.java b/app/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsMenu.java index 60694c68f..66832e5bc 100644 --- a/app/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsMenu.java +++ b/app/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsMenu.java @@ -44,16 +44,20 @@ public class ListHabitsMenu extends BaseMenu private final Preferences preferences; + private ThemeSwitcher themeSwitcher; + @Inject public ListHabitsMenu(@NonNull BaseActivity activity, @NonNull ListHabitsScreen screen, @NonNull HabitCardListAdapter adapter, - @NonNull Preferences preferences) + @NonNull Preferences preferences, + @NonNull ThemeSwitcher themeSwitcher) { super(activity); this.screen = screen; this.adapter = adapter; this.preferences = preferences; + this.themeSwitcher = themeSwitcher; showCompleted = preferences.getShowCompleted(); showArchived = preferences.getShowArchived(); @@ -64,7 +68,7 @@ public class ListHabitsMenu extends BaseMenu public void onCreate(@NonNull Menu menu) { MenuItem nightModeItem = menu.findItem(R.id.action_night_mode); - nightModeItem.setChecked(InterfaceUtils.isNightMode()); + nightModeItem.setChecked(themeSwitcher.isNightMode()); MenuItem showArchivedItem = menu.findItem(R.id.action_show_archived); showArchivedItem.setChecked(showArchived); diff --git a/app/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.java b/app/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.java index e5af5b9f7..dd8553acf 100644 --- a/app/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.java +++ b/app/src/main/java/org/isoron/uhabits/activities/habits/list/ListHabitsScreen.java @@ -32,7 +32,6 @@ import org.isoron.uhabits.commands.*; import org.isoron.uhabits.intents.*; import org.isoron.uhabits.io.*; import org.isoron.uhabits.models.*; -import org.isoron.uhabits.utils.*; import java.io.*; @@ -75,7 +74,10 @@ public class ListHabitsScreen extends BaseScreen private final ColorPickerDialogFactory colorPickerFactory; @NonNull - private EditHabitDialogFactory editHabitDialogFactory; + private final EditHabitDialogFactory editHabitDialogFactory; + + @NonNull + private final ThemeSwitcher themeSwitcher; @Inject public ListHabitsScreen(@NonNull BaseActivity activity, @@ -83,6 +85,7 @@ public class ListHabitsScreen extends BaseScreen @NonNull DirFinder dirFinder, @NonNull ListHabitsRootView rootView, @NonNull IntentFactory intentFactory, + @NonNull ThemeSwitcher themeSwitcher, @NonNull ConfirmDeleteDialogFactory confirmDeleteDialogFactory, @NonNull CreateHabitDialogFactory createHabitDialogFactory, @NonNull FilePickerDialogFactory filePickerDialogFactory, @@ -99,6 +102,7 @@ public class ListHabitsScreen extends BaseScreen this.dirFinder = dirFinder; this.filePickerDialogFactory = filePickerDialogFactory; this.intentFactory = intentFactory; + this.themeSwitcher = themeSwitcher; } public void onAttached() @@ -229,9 +233,9 @@ public class ListHabitsScreen extends BaseScreen public void toggleNightMode() { - if (InterfaceUtils.isNightMode()) - InterfaceUtils.setCurrentTheme(InterfaceUtils.THEME_LIGHT); - else InterfaceUtils.setCurrentTheme(InterfaceUtils.THEME_DARK); + if (themeSwitcher.isNightMode()) + themeSwitcher.setTheme(ThemeSwitcher.THEME_LIGHT); + else themeSwitcher.setTheme(ThemeSwitcher.THEME_DARK); refreshTheme(); } diff --git a/app/src/main/java/org/isoron/uhabits/utils/InterfaceUtils.java b/app/src/main/java/org/isoron/uhabits/utils/InterfaceUtils.java index da7d11e2a..5db2a875a 100644 --- a/app/src/main/java/org/isoron/uhabits/utils/InterfaceUtils.java +++ b/app/src/main/java/org/isoron/uhabits/utils/InterfaceUtils.java @@ -19,15 +19,11 @@ package org.isoron.uhabits.utils; -import android.app.*; import android.content.*; import android.content.res.*; import android.graphics.*; -import android.preference.*; import android.util.*; -import org.isoron.uhabits.*; - import java.util.*; public abstract class InterfaceUtils @@ -39,19 +35,8 @@ public abstract class InterfaceUtils "ja", "fr", "hr", "sl" }; - public static final int THEME_DARK = 1; - - public static final int THEME_LIGHT = 0; - - public static Integer fixedTheme; - private static Typeface fontAwesome; - public static void setFixedTheme(Integer fixedTheme) - { - InterfaceUtils.fixedTheme = fixedTheme; - } - public static Typeface getFontAwesome(Context context) { if(fontAwesome == null) @@ -84,46 +69,4 @@ public abstract class InterfaceUtils return false; } - public static void applyCurrentTheme(Activity activity) - { - switch(getCurrentTheme()) - { - case THEME_DARK: - { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity); - boolean pureBlackEnabled = prefs.getBoolean("pref_pure_black", false); - - if(pureBlackEnabled) - activity.setTheme(R.style.AppBaseThemeDark_PureBlack); - else - activity.setTheme(R.style.AppBaseThemeDark); - - break; - } - - case THEME_LIGHT: - default: - activity.setTheme(R.style.AppBaseTheme); - break; - } - } - - private static int getCurrentTheme() - { - Context appContext = HabitsApplication.getContext(); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext); - return prefs.getInt("pref_theme", THEME_LIGHT); - } - - public static void setCurrentTheme(int theme) - { - Context appContext = HabitsApplication.getContext(); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(appContext); - prefs.edit().putInt("pref_theme", theme).apply(); - } - - public static boolean isNightMode() - { - return getCurrentTheme() == THEME_DARK; - } } diff --git a/app/src/main/java/org/isoron/uhabits/utils/Preferences.java b/app/src/main/java/org/isoron/uhabits/utils/Preferences.java index 5e1882ad7..653141e32 100644 --- a/app/src/main/java/org/isoron/uhabits/utils/Preferences.java +++ b/app/src/main/java/org/isoron/uhabits/utils/Preferences.java @@ -23,6 +23,7 @@ import android.content.*; import android.preference.*; import org.isoron.uhabits.*; +import org.isoron.uhabits.activities.*; import javax.inject.*; @@ -58,6 +59,16 @@ public class Preferences return defaultScoreInterval; } + public int getTheme() + { + return prefs.getInt("pref_theme", ThemeSwitcher.THEME_LIGHT); + } + + public boolean isPureBlackEnabled() + { + return prefs.getBoolean("pref_pure_black", false); + } + @Override public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) @@ -156,6 +167,11 @@ public class Preferences .apply(); } + public void setTheme(int theme) + { + prefs.edit().putInt("pref_theme", theme).apply(); + } + public boolean shouldReverseCheckmarks() { if (shouldReverseCheckmarks == null) shouldReverseCheckmarks = diff --git a/app/src/main/java/org/isoron/uhabits/utils/StyledResources.java b/app/src/main/java/org/isoron/uhabits/utils/StyledResources.java index 9ff9211cc..2cf9f2032 100644 --- a/app/src/main/java/org/isoron/uhabits/utils/StyledResources.java +++ b/app/src/main/java/org/isoron/uhabits/utils/StyledResources.java @@ -92,9 +92,9 @@ public class StyledResources { int[] attrs = new int[]{ attrId }; - Integer fixedTheme = InterfaceUtils.fixedTheme; - if (fixedTheme != null) - return context.getTheme().obtainStyledAttributes(fixedTheme, attrs); +// Integer fixedTheme = ThemeSwitcher.fixedTheme; +// if (fixedTheme != null) +// return context.getTheme().obtainStyledAttributes(fixedTheme, attrs); return context.obtainStyledAttributes(attrs); }