mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Replace InterfaceUtils theme methods by ThemeSwitcher
This commit is contained in:
@@ -114,7 +114,6 @@ public class BaseAndroidTest
|
|||||||
|
|
||||||
protected void setTheme(@StyleRes int themeId)
|
protected void setTheme(@StyleRes int themeId)
|
||||||
{
|
{
|
||||||
InterfaceUtils.setFixedTheme(themeId);
|
|
||||||
targetContext.setTheme(themeId);
|
targetContext.setTheme(themeId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ public class CheckmarkWidgetViewTest extends BaseViewTest
|
|||||||
public void setUp()
|
public void setUp()
|
||||||
{
|
{
|
||||||
super.setUp();
|
super.setUp();
|
||||||
InterfaceUtils.setFixedTheme(R.style.TransparentWidgetTheme);
|
|
||||||
|
|
||||||
habit = fixtures.createShortHabit();
|
habit = fixtures.createShortHabit();
|
||||||
view = new CheckmarkWidgetView(targetContext);
|
view = new CheckmarkWidgetView(targetContext);
|
||||||
|
|||||||
@@ -30,4 +30,6 @@ import dagger.*;
|
|||||||
public interface ActivityComponent
|
public interface ActivityComponent
|
||||||
{
|
{
|
||||||
ColorPickerDialogFactory getColorPickerDialogFactory();
|
ColorPickerDialogFactory getColorPickerDialogFactory();
|
||||||
|
|
||||||
|
ThemeSwitcher getThemeSwitcher();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ import android.support.v7.app.*;
|
|||||||
import android.view.*;
|
import android.view.*;
|
||||||
|
|
||||||
import org.isoron.uhabits.*;
|
import org.isoron.uhabits.*;
|
||||||
import org.isoron.uhabits.utils.*;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base class for all activities in the application.
|
* Base class for all activities in the application.
|
||||||
@@ -128,7 +127,7 @@ abstract public class BaseActivity extends AppCompatActivity
|
|||||||
protected void onCreate(Bundle savedInstanceState)
|
protected void onCreate(Bundle savedInstanceState)
|
||||||
{
|
{
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
InterfaceUtils.applyCurrentTheme(this);
|
|
||||||
androidExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
|
androidExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
|
||||||
Thread.setDefaultUncaughtExceptionHandler(this);
|
Thread.setDefaultUncaughtExceptionHandler(this);
|
||||||
|
|
||||||
@@ -139,5 +138,7 @@ abstract public class BaseActivity extends AppCompatActivity
|
|||||||
.activityModule(new ActivityModule(this))
|
.activityModule(new ActivityModule(this))
|
||||||
.appComponent(app.getComponent())
|
.appComponent(app.getComponent())
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
component.getThemeSwitcher().apply();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,10 +43,16 @@ public abstract class BaseRootView extends FrameLayout
|
|||||||
{
|
{
|
||||||
private final Context context;
|
private final Context context;
|
||||||
|
|
||||||
|
private final BaseActivity activity;
|
||||||
|
|
||||||
|
private final ThemeSwitcher themeSwitcher;
|
||||||
|
|
||||||
public BaseRootView(Context context)
|
public BaseRootView(Context context)
|
||||||
{
|
{
|
||||||
super(context);
|
super(context);
|
||||||
this.context = context;
|
this.context = context;
|
||||||
|
activity = (BaseActivity) context;
|
||||||
|
themeSwitcher = activity.getComponent().getThemeSwitcher();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getDisplayHomeAsUp()
|
public boolean getDisplayHomeAsUp()
|
||||||
@@ -59,7 +65,7 @@ public abstract class BaseRootView extends FrameLayout
|
|||||||
|
|
||||||
public int getToolbarColor()
|
public int getToolbarColor()
|
||||||
{
|
{
|
||||||
if (SDK_INT < LOLLIPOP && !InterfaceUtils.isNightMode())
|
if (SDK_INT < LOLLIPOP && !themeSwitcher.isNightMode())
|
||||||
{
|
{
|
||||||
return context
|
return context
|
||||||
.getResources()
|
.getResources()
|
||||||
|
|||||||
@@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -44,16 +44,20 @@ public class ListHabitsMenu extends BaseMenu
|
|||||||
|
|
||||||
private final Preferences preferences;
|
private final Preferences preferences;
|
||||||
|
|
||||||
|
private ThemeSwitcher themeSwitcher;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public ListHabitsMenu(@NonNull BaseActivity activity,
|
public ListHabitsMenu(@NonNull BaseActivity activity,
|
||||||
@NonNull ListHabitsScreen screen,
|
@NonNull ListHabitsScreen screen,
|
||||||
@NonNull HabitCardListAdapter adapter,
|
@NonNull HabitCardListAdapter adapter,
|
||||||
@NonNull Preferences preferences)
|
@NonNull Preferences preferences,
|
||||||
|
@NonNull ThemeSwitcher themeSwitcher)
|
||||||
{
|
{
|
||||||
super(activity);
|
super(activity);
|
||||||
this.screen = screen;
|
this.screen = screen;
|
||||||
this.adapter = adapter;
|
this.adapter = adapter;
|
||||||
this.preferences = preferences;
|
this.preferences = preferences;
|
||||||
|
this.themeSwitcher = themeSwitcher;
|
||||||
|
|
||||||
showCompleted = preferences.getShowCompleted();
|
showCompleted = preferences.getShowCompleted();
|
||||||
showArchived = preferences.getShowArchived();
|
showArchived = preferences.getShowArchived();
|
||||||
@@ -64,7 +68,7 @@ public class ListHabitsMenu extends BaseMenu
|
|||||||
public void onCreate(@NonNull Menu menu)
|
public void onCreate(@NonNull Menu menu)
|
||||||
{
|
{
|
||||||
MenuItem nightModeItem = menu.findItem(R.id.action_night_mode);
|
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);
|
MenuItem showArchivedItem = menu.findItem(R.id.action_show_archived);
|
||||||
showArchivedItem.setChecked(showArchived);
|
showArchivedItem.setChecked(showArchived);
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ import org.isoron.uhabits.commands.*;
|
|||||||
import org.isoron.uhabits.intents.*;
|
import org.isoron.uhabits.intents.*;
|
||||||
import org.isoron.uhabits.io.*;
|
import org.isoron.uhabits.io.*;
|
||||||
import org.isoron.uhabits.models.*;
|
import org.isoron.uhabits.models.*;
|
||||||
import org.isoron.uhabits.utils.*;
|
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
|
||||||
@@ -75,7 +74,10 @@ public class ListHabitsScreen extends BaseScreen
|
|||||||
private final ColorPickerDialogFactory colorPickerFactory;
|
private final ColorPickerDialogFactory colorPickerFactory;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
private EditHabitDialogFactory editHabitDialogFactory;
|
private final EditHabitDialogFactory editHabitDialogFactory;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private final ThemeSwitcher themeSwitcher;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
public ListHabitsScreen(@NonNull BaseActivity activity,
|
public ListHabitsScreen(@NonNull BaseActivity activity,
|
||||||
@@ -83,6 +85,7 @@ public class ListHabitsScreen extends BaseScreen
|
|||||||
@NonNull DirFinder dirFinder,
|
@NonNull DirFinder dirFinder,
|
||||||
@NonNull ListHabitsRootView rootView,
|
@NonNull ListHabitsRootView rootView,
|
||||||
@NonNull IntentFactory intentFactory,
|
@NonNull IntentFactory intentFactory,
|
||||||
|
@NonNull ThemeSwitcher themeSwitcher,
|
||||||
@NonNull ConfirmDeleteDialogFactory confirmDeleteDialogFactory,
|
@NonNull ConfirmDeleteDialogFactory confirmDeleteDialogFactory,
|
||||||
@NonNull CreateHabitDialogFactory createHabitDialogFactory,
|
@NonNull CreateHabitDialogFactory createHabitDialogFactory,
|
||||||
@NonNull FilePickerDialogFactory filePickerDialogFactory,
|
@NonNull FilePickerDialogFactory filePickerDialogFactory,
|
||||||
@@ -99,6 +102,7 @@ public class ListHabitsScreen extends BaseScreen
|
|||||||
this.dirFinder = dirFinder;
|
this.dirFinder = dirFinder;
|
||||||
this.filePickerDialogFactory = filePickerDialogFactory;
|
this.filePickerDialogFactory = filePickerDialogFactory;
|
||||||
this.intentFactory = intentFactory;
|
this.intentFactory = intentFactory;
|
||||||
|
this.themeSwitcher = themeSwitcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onAttached()
|
public void onAttached()
|
||||||
@@ -229,9 +233,9 @@ public class ListHabitsScreen extends BaseScreen
|
|||||||
|
|
||||||
public void toggleNightMode()
|
public void toggleNightMode()
|
||||||
{
|
{
|
||||||
if (InterfaceUtils.isNightMode())
|
if (themeSwitcher.isNightMode())
|
||||||
InterfaceUtils.setCurrentTheme(InterfaceUtils.THEME_LIGHT);
|
themeSwitcher.setTheme(ThemeSwitcher.THEME_LIGHT);
|
||||||
else InterfaceUtils.setCurrentTheme(InterfaceUtils.THEME_DARK);
|
else themeSwitcher.setTheme(ThemeSwitcher.THEME_DARK);
|
||||||
|
|
||||||
refreshTheme();
|
refreshTheme();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,15 +19,11 @@
|
|||||||
|
|
||||||
package org.isoron.uhabits.utils;
|
package org.isoron.uhabits.utils;
|
||||||
|
|
||||||
import android.app.*;
|
|
||||||
import android.content.*;
|
import android.content.*;
|
||||||
import android.content.res.*;
|
import android.content.res.*;
|
||||||
import android.graphics.*;
|
import android.graphics.*;
|
||||||
import android.preference.*;
|
|
||||||
import android.util.*;
|
import android.util.*;
|
||||||
|
|
||||||
import org.isoron.uhabits.*;
|
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public abstract class InterfaceUtils
|
public abstract class InterfaceUtils
|
||||||
@@ -39,19 +35,8 @@ public abstract class InterfaceUtils
|
|||||||
"ja", "fr", "hr", "sl"
|
"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;
|
private static Typeface fontAwesome;
|
||||||
|
|
||||||
public static void setFixedTheme(Integer fixedTheme)
|
|
||||||
{
|
|
||||||
InterfaceUtils.fixedTheme = fixedTheme;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Typeface getFontAwesome(Context context)
|
public static Typeface getFontAwesome(Context context)
|
||||||
{
|
{
|
||||||
if(fontAwesome == null)
|
if(fontAwesome == null)
|
||||||
@@ -84,46 +69,4 @@ public abstract class InterfaceUtils
|
|||||||
return false;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ import android.content.*;
|
|||||||
import android.preference.*;
|
import android.preference.*;
|
||||||
|
|
||||||
import org.isoron.uhabits.*;
|
import org.isoron.uhabits.*;
|
||||||
|
import org.isoron.uhabits.activities.*;
|
||||||
|
|
||||||
import javax.inject.*;
|
import javax.inject.*;
|
||||||
|
|
||||||
@@ -58,6 +59,16 @@ public class Preferences
|
|||||||
return defaultScoreInterval;
|
return defaultScoreInterval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getTheme()
|
||||||
|
{
|
||||||
|
return prefs.getInt("pref_theme", ThemeSwitcher.THEME_LIGHT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isPureBlackEnabled()
|
||||||
|
{
|
||||||
|
return prefs.getBoolean("pref_pure_black", false);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
|
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
|
||||||
String key)
|
String key)
|
||||||
@@ -156,6 +167,11 @@ public class Preferences
|
|||||||
.apply();
|
.apply();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setTheme(int theme)
|
||||||
|
{
|
||||||
|
prefs.edit().putInt("pref_theme", theme).apply();
|
||||||
|
}
|
||||||
|
|
||||||
public boolean shouldReverseCheckmarks()
|
public boolean shouldReverseCheckmarks()
|
||||||
{
|
{
|
||||||
if (shouldReverseCheckmarks == null) shouldReverseCheckmarks =
|
if (shouldReverseCheckmarks == null) shouldReverseCheckmarks =
|
||||||
|
|||||||
@@ -92,9 +92,9 @@ public class StyledResources
|
|||||||
{
|
{
|
||||||
int[] attrs = new int[]{ attrId };
|
int[] attrs = new int[]{ attrId };
|
||||||
|
|
||||||
Integer fixedTheme = InterfaceUtils.fixedTheme;
|
// Integer fixedTheme = ThemeSwitcher.fixedTheme;
|
||||||
if (fixedTheme != null)
|
// if (fixedTheme != null)
|
||||||
return context.getTheme().obtainStyledAttributes(fixedTheme, attrs);
|
// return context.getTheme().obtainStyledAttributes(fixedTheme, attrs);
|
||||||
|
|
||||||
return context.obtainStyledAttributes(attrs);
|
return context.obtainStyledAttributes(attrs);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user