forked from plusonelabs/calendar-widget
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plusonelabs#28 Show, which Theme is being configured. Allow to switch…
… "Dark theme" during changes of settings.
- Loading branch information
Showing
8 changed files
with
196 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
app/src/main/java/org/andstatus/todoagenda/prefs/ColorThemeType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.andstatus.todoagenda.prefs; | ||
|
||
import android.app.Activity; | ||
import android.content.Context; | ||
import android.os.Build; | ||
|
||
import androidx.annotation.StringRes; | ||
|
||
import org.andstatus.todoagenda.R; | ||
|
||
import static org.andstatus.todoagenda.prefs.InstanceSettings.isDarkThemeOn; | ||
|
||
/** | ||
* See https://github.com/andstatus/todoagenda/issues/48 | ||
* @author [email protected] | ||
*/ | ||
public enum ColorThemeType { | ||
SINGLE("single", R.string.appearance_group_color_title), | ||
DARK("dark", R.string.colors_dark), | ||
LIGHT("light", R.string.colors_light), | ||
NONE("none", R.string.no_title); | ||
|
||
private final static ColorThemeType defaultValue = SINGLE; | ||
|
||
public final String value; | ||
@StringRes | ||
public final int titleResId; | ||
|
||
ColorThemeType(String value, int titleResId) { | ||
this.value = value; | ||
this.titleResId = titleResId; | ||
} | ||
|
||
public boolean isValid() { | ||
return this != NONE; | ||
} | ||
|
||
public static ColorThemeType fromValue(String value) { | ||
for (ColorThemeType item : ColorThemeType.values()) { | ||
if (item.value.equals(value)) { | ||
return item; | ||
} | ||
} | ||
return defaultValue; | ||
} | ||
|
||
/** See https://developer.android.com/guide/topics/ui/look-and-feel/darktheme */ | ||
public static boolean canHaveDifferentColorsForDark() { | ||
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; | ||
} | ||
|
||
public ColorThemeType fromEditor(Context context, boolean differentColorsForDark) { | ||
if (differentColorsForDark && canHaveDifferentColorsForDark()) { | ||
if (this == ColorThemeType.DARK || this == ColorThemeType.SINGLE && isDarkThemeOn(context)) { | ||
return ColorThemeType.DARK; | ||
} else { | ||
return ColorThemeType.LIGHT; | ||
} | ||
} else { | ||
if (this == ColorThemeType.LIGHT || this == ColorThemeType.SINGLE) { | ||
return ColorThemeType.SINGLE; | ||
} else { | ||
return ColorThemeType.NONE; | ||
} | ||
} | ||
} | ||
|
||
public ColorThemeType setTitle(Activity activity) { | ||
if (activity != null) { | ||
activity.setTitle(titleResId); | ||
} | ||
return this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package org.andstatus.todoagenda.prefs; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import android.os.Bundle; | ||
|
||
import androidx.fragment.app.DialogFragment; | ||
import androidx.fragment.app.FragmentActivity; | ||
import androidx.preference.ListPreference; | ||
import androidx.preference.Preference; | ||
import androidx.preference.PreferenceFragmentCompat; | ||
|
@@ -12,11 +14,13 @@ | |
import com.rarepebble.colorpicker.ColorPreference; | ||
import com.rarepebble.colorpicker.ColorPreferenceDialog; | ||
|
||
import org.andstatus.todoagenda.MainActivity; | ||
import org.andstatus.todoagenda.R; | ||
import org.andstatus.todoagenda.TextShading; | ||
import org.andstatus.todoagenda.widget.TimeSection; | ||
|
||
import static org.andstatus.todoagenda.WidgetConfigurationActivity.FRAGMENT_TAG; | ||
import static org.andstatus.todoagenda.prefs.ApplicationPreferences.PREF_DIFFERENT_COLORS_FOR_DARK; | ||
|
||
/** AndroidX version created by [email protected] | ||
* based on this answer: https://stackoverflow.com/a/53290775/297710 | ||
|
@@ -27,27 +31,39 @@ public class ColorsPreferencesFragment extends PreferenceFragmentCompat | |
|
||
@Override | ||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { | ||
setTitle(); | ||
addPreferencesFromResource(R.xml.preferences_colors); | ||
removeUnavailablePreferences(); | ||
} | ||
|
||
private void setTitle() { | ||
ApplicationPreferences.getEditingColorThemeType(getActivity()).setTitle(getActivity()); | ||
} | ||
|
||
@Override | ||
public void onResume() { | ||
super.onResume(); | ||
setTitle(); | ||
removeUnavailablePreferences(); | ||
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this); | ||
showShadings(); | ||
} | ||
|
||
private void removeUnavailablePreferences() { | ||
if (!InstanceSettings.canHaveDifferentColorsForDark()) { | ||
Context context = getActivity(); | ||
if (context == null) return; | ||
|
||
ColorThemeType colorThemeType = ApplicationPreferences.getColorThemeType(context); | ||
if (!ColorThemeType.canHaveDifferentColorsForDark() || | ||
colorThemeType == ColorThemeType.LIGHT || | ||
colorThemeType == ColorThemeType.SINGLE && !InstanceSettings.isDarkThemeOn(context)) { | ||
PreferenceScreen screen = getPreferenceScreen(); | ||
Preference preference = findPreference(InstanceSettings.PREF_DIFFERENT_COLORS_FOR_DARK); | ||
Preference preference = findPreference(PREF_DIFFERENT_COLORS_FOR_DARK); | ||
if (screen != null && preference != null) { | ||
screen.removePreference(preference); | ||
} | ||
} | ||
if (ApplicationPreferences.noPastEvents(getActivity())) { | ||
if (ApplicationPreferences.noPastEvents(context)) { | ||
PreferenceScreen screen = getPreferenceScreen(); | ||
Preference preference = findPreference(TimeSection.PAST.preferenceCategoryKey); | ||
if (screen != null && preference != null) { | ||
|
@@ -70,6 +86,17 @@ public void onPause() { | |
|
||
@Override | ||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { | ||
if (PREF_DIFFERENT_COLORS_FOR_DARK.equals(key)){ | ||
FragmentActivity activity = getActivity(); | ||
if (activity != null) { | ||
if (ApplicationPreferences.getEditingColorThemeType(activity) == ColorThemeType.NONE) { | ||
activity.startActivity(MainActivity.intentToConfigure(activity, ApplicationPreferences.getWidgetId(activity))); | ||
activity.finish(); | ||
return; | ||
}; | ||
setTitle(); | ||
} | ||
} | ||
showShadings(); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.