switch between themes

This commit is contained in:
Thomas 2022-11-29 15:18:46 +01:00
parent c8a4ffa922
commit 9a10a7aaf6
6 changed files with 71 additions and 71 deletions

View file

@ -30,6 +30,7 @@ import org.acra.config.DialogConfigurationBuilder;
import org.acra.config.MailSenderConfigurationBuilder;
import org.acra.data.StringFormat;
import app.fedilab.android.helper.ThemeHelper;
import es.dmoral.toasty.Toasty;
@ -68,6 +69,8 @@ public class MainApplication extends MultiDexApplication {
MultiDex.install(MainApplication.this);
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(MainApplication.this);
boolean send_crash_reports = sharedpreferences.getBoolean(getString(R.string.SET_SEND_CRASH_REPORTS), false);
String currentTheme = sharedpreferences.getString(getString(R.string.SET_THEME_BASE), getString(R.string.SET_DEFAULT_THEME));
ThemeHelper.switchTo(currentTheme);
if (send_crash_reports) {
ACRA.init(this, new CoreConfigurationBuilder()
//core configuration:

View file

@ -23,6 +23,7 @@ import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
@ -32,6 +33,7 @@ import android.view.animation.TranslateAnimation;
import androidx.annotation.AttrRes;
import androidx.annotation.ColorInt;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;
@ -264,6 +266,25 @@ public class ThemeHelper {
activity.getBaseContext().getResources().updateConfiguration(configuration, metrics);
}
public static void switchTo(String themePref) {
if (themes.LIGHT.name().equals(themePref)) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
} else if (themes.DARK.name().equals(themePref)) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
}
}
}
public enum themes {
LIGHT,
DARK,
SYSTEM
}
public interface SlideAnimation {
void onAnimationEnded();

View file

@ -60,6 +60,7 @@ import java.util.List;
import app.fedilab.android.R;
import app.fedilab.android.activities.ComposeActivity;
import app.fedilab.android.databinding.ActivityThemeSettingsBinding;
import app.fedilab.android.databinding.PopupStatusThemeBinding;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.helper.ThemeHelper;
@ -72,7 +73,6 @@ public class FragmentThemingSettings extends PreferenceFragmentCompat implements
private List<LinkedHashMap<String, String>> listOfThemes;
private SharedPreferences appPref;
private SharedPreferences cyneaPref;
private boolean shouldRestart;
@Override
public void onCreatePreferences(Bundle bundle, String s) {
@ -80,7 +80,6 @@ public class FragmentThemingSettings extends PreferenceFragmentCompat implements
appPref = PreferenceManager.getDefaultSharedPreferences(requireActivity());
createPref();
listOfThemes = ThemeHelper.getContributorsTheme(requireActivity());
shouldRestart = false;
}
@ -109,7 +108,12 @@ public class FragmentThemingSettings extends PreferenceFragmentCompat implements
if (key.equals("use_custom_theme")) {
createPref();
}
shouldRestart = true;
if (key.compareTo(getString(R.string.SET_THEME_BASE)) == 0) {
ListPreference SET_THEME_BASE = findPreference(getString(R.string.SET_THEME_BASE));
if (SET_THEME_BASE != null) {
ThemeHelper.switchTo(SET_THEME_BASE.getValue());
}
}
Helper.recreateMainActivity(requireActivity());
}
@ -300,8 +304,7 @@ public class FragmentThemingSettings extends PreferenceFragmentCompat implements
Preference launch_custom_theme = findPreference("launch_custom_theme");
if (launch_custom_theme != null) {
launch_custom_theme.setOnPreferenceClickListener(preference -> {
shouldRestart = true;
startActivity(new Intent(requireActivity(), ActivityThemeSettingsBinding.class));
return false;
});
@ -418,7 +421,6 @@ public class FragmentThemingSettings extends PreferenceFragmentCompat implements
ComposeActivity.MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
return true;
}
shouldRestart = true;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
@ -439,7 +441,6 @@ public class FragmentThemingSettings extends PreferenceFragmentCompat implements
createPref();
});
shouldRestart = true;
dialogBuilder.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.setCancelable(false);

View file

@ -55,7 +55,11 @@
<action
android:id="@+id/categories_to_theming"
app:destination="@id/ThemeSettingsActivity" />
app:destination="@id/FragmentThemingSettings"
app:enterAnim="@anim/enter"
app:exitAnim="@anim/exit"
app:popEnterAnim="@anim/pop_enter"
app:popExitAnim="@anim/pop_exit" />
<action
android:id="@+id/categories_to_language"
@ -91,6 +95,10 @@
android:name="app.fedilab.android.ui.fragment.settings.FragmentPrivacySettings"
android:label="@string/action_privacy" />
<fragment
android:id="@+id/FragmentThemingSettings"
android:name="app.fedilab.android.ui.fragment.settings.FragmentThemingSettings"
android:label="@string/theming" />
<fragment
android:id="@+id/FragmentLanguageSettings"
@ -101,8 +109,4 @@
android:id="@+id/EditProfileActivity"
android:name="app.fedilab.android.activities.EditProfileActivity" />
<activity
android:id="@+id/ThemeSettingsActivity"
android:name="app.fedilab.android.activities.ThemeSettingsActivity" />
</navigation>

View file

@ -695,6 +695,18 @@
<string name="not_valid_list_name">List name is not valid!</string>
<string name="no_account_in_list">No accounts found for this list!</string>
<string name="scheduled">Scheduled</string>
<string-array name="set_theme_mode_value">
<item>Light</item>
<item>Dark</item>
<item>System default</item>
</string-array>
<string-array name="SET_THEME_MODE_VALUE" translatable="false">
<item>LIGHT</item>
<item>DARK</item>
<item>SYSTEM</item>
</string-array>
<string-array name="set_notification_type_value">
<item>Push notifications</item>
<item>Fetch at fixed times</item>
@ -1252,6 +1264,9 @@
<string name="SET_SHOW_REPLIES" translatable="false">SET_SHOW_REPLIES</string>
<string name="SET_DISABLE_ANIMATED_EMOJI" translatable="false">SET_DISABLE_ANIMATED_EMOJI</string>
<string name="SET_CAPITALIZE" translatable="false">SET_CAPITALIZE</string>
<string name="SET_THEME_BASE" translatable="false">SET_THEME_BASE</string>
<string name="SET_DEFAULT_THEME" translatable="false">SYSTEM</string>
<string name="SET_FULL_PREVIEW" translatable="false">SET_FULL_PREVIEW</string>
<string name="SET_SHARE_DETAILS" translatable="false">SET_SHARE_DETAILS</string>
<string name="SET_CUSTOM_SHARING" translatable="false">SET_CUSTOM_SHARING</string>
@ -1957,4 +1972,7 @@
<string name="saved_changes">Changes have been saved!</string>
<string name="create_domain_block">Create domain block</string>
<string name="mute_tag">Are you sure to mute the tag %1$s?</string>
<string name="type_of_theme">Pickup a mode for the theme</string>
<string name="pref_customize">Customize colors</string>
<string name="pref_customize_summary">Allow to set your custom colors for themes.</string>
</resources>

View file

@ -5,19 +5,22 @@
android:layout_height="match_parent">
<ListPreference
app:defaultValue="SYSTEM"
app:dialogTitle="@string/type_of_theme"
app:entries="@array/set_theme_mode_value"
app:entryValues="@array/SET_THEME_MODE_VALUE"
app:iconSpaceReserved="false"
app:key="@string/SET_THEME_BASE"
app:title="@string/type_of_theme"
app:useSimpleSummaryProvider="true" />
<Preference
app:iconSpaceReserved="false"
app:key="launch_custom_theme"
app:summary="@string/pref_theme_base_summary"
app:title="@string/pref_theme_base" />
app:summary="@string/pref_customize_summary"
app:title="@string/pref_customize" />
<SwitchPreferenceCompat
app:defaultValue="false"
app:iconSpaceReserved="false"
app:key="use_custom_theme"
app:singleLineTitle="false"
app:summary="@string/pref_custom_theme_new_summary"
app:title="@string/pref_custom_theme" />
<Preference
app:iconSpaceReserved="false"
@ -29,57 +32,7 @@
android:key="cyanea_preference_category"
android:title="@string/customize_timelines"
app:iconSpaceReserved="false">
<!--
<com.jaredrummler.android.colorpicker.ColorPreferenceCompat
app:defaultValue="-1"
app:iconSpaceReserved="false"
app:key="theme_link_color"
app:summary="@string/link_color"
app:title="@string/link_color_title" />
<com.jaredrummler.android.colorpicker.ColorPreferenceCompat
app:defaultValue="-1"
app:iconSpaceReserved="false"
app:key="theme_boost_header_color"
app:summary="@string/boost_header_color"
app:title="@string/boost_header_color_title" />
<com.jaredrummler.android.colorpicker.ColorPreferenceCompat
app:defaultValue="-1"
app:iconSpaceReserved="false"
app:key="theme_text_header_1_line"
app:summary="@string/displayname_title"
app:title="@string/display_name" />
<com.jaredrummler.android.colorpicker.ColorPreferenceCompat
app:defaultValue="-1"
app:iconSpaceReserved="false"
app:key="theme_text_header_2_line"
app:summary="@string/username_title"
app:title="@string/username" />
<com.jaredrummler.android.colorpicker.ColorPreferenceCompat
app:defaultValue="-1"
app:iconSpaceReserved="false"
app:key="theme_statuses_color"
app:summary="@string/background_status"
app:title="@string/background_status_title" />
<com.jaredrummler.android.colorpicker.ColorPreferenceCompat
app:defaultValue="-1"
app:iconSpaceReserved="false"
app:key="theme_icons_color"
app:summary="@string/icons_color"
app:title="@string/icons_color_title" />
<com.jaredrummler.android.colorpicker.ColorPreferenceCompat
app:defaultValue="-1"
app:iconSpaceReserved="false"
app:key="theme_text_color"
app:summary="@string/text_color"
app:title="@string/text_color_title" />
-->
<Preference
app:iconSpaceReserved="false"
app:key="reset_pref"