Replace InterfaceUtils theme methods by ThemeSwitcher

pull/157/head
Alinson S. Xavier 9 years ago
parent dd3d78b82c
commit 05aa5b1172

@ -114,7 +114,6 @@ public class BaseAndroidTest
protected void setTheme(@StyleRes int themeId)
{
InterfaceUtils.setFixedTheme(themeId);
targetContext.setTheme(themeId);
}

@ -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);

@ -30,4 +30,6 @@ import dagger.*;
public interface ActivityComponent
{
ColorPickerDialogFactory getColorPickerDialogFactory();
ThemeSwitcher getThemeSwitcher();
}

@ -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();
}
}

@ -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()

@ -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 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);

@ -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();
}

@ -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;
}
}

@ -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 =

@ -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);
}

Loading…
Cancel
Save