Fix theme and icon color on startup

This commit is contained in:
CappielloAntonio 2021-04-12 17:56:09 +02:00
parent ecc941dc11
commit b3ae8b1fff
7 changed files with 20 additions and 4 deletions

View file

@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:ignore="ScopedStorage" />
<application <application
android:name="App"
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"

View file

@ -2,6 +2,9 @@ package com.cappielloantonio.play;
import android.app.Application; import android.app.Application;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import androidx.preference.PreferenceManager;
import com.cappielloantonio.play.helper.ThemeHelper; import com.cappielloantonio.play.helper.ThemeHelper;
import com.cappielloantonio.play.util.PreferenceUtil; import com.cappielloantonio.play.util.PreferenceUtil;
@ -24,7 +27,9 @@ public class App extends Application {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
ThemeHelper.applyTheme(PreferenceUtil.getInstance(getApplicationContext()).getTheme()); SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
String themePref = sharedPreferences.getString("themePref", ThemeHelper.DEFAULT_MODE);
ThemeHelper.applyTheme(themePref);
} }
public static App getInstance() { public static App getInstance() {

View file

@ -16,6 +16,8 @@ import com.cappielloantonio.play.helper.MusicPlayerRemote;
import com.cappielloantonio.play.model.Song; import com.cappielloantonio.play.model.Song;
import com.cappielloantonio.play.repository.SongRepository; import com.cappielloantonio.play.repository.SongRepository;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;

View file

@ -1,11 +1,14 @@
package com.cappielloantonio.play.helper; package com.cappielloantonio.play.helper;
import android.os.Build; import android.os.Build;
import android.util.Log;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatDelegate; import androidx.appcompat.app.AppCompatDelegate;
public class ThemeHelper { public class ThemeHelper {
private static final String TAG = "ThemeHelper";
public static final String LIGHT_MODE = "light"; public static final String LIGHT_MODE = "light";
public static final String DARK_MODE = "dark"; public static final String DARK_MODE = "dark";
public static final String DEFAULT_MODE = "default"; public static final String DEFAULT_MODE = "default";
@ -13,14 +16,19 @@ public class ThemeHelper {
public static void applyTheme(@NonNull String themePref) { public static void applyTheme(@NonNull String themePref) {
switch (themePref) { switch (themePref) {
case LIGHT_MODE: { case LIGHT_MODE: {
Log.d(TAG, "applyTheme: LIGHT");
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
break; break;
} }
case DARK_MODE: { case DARK_MODE: {
Log.d(TAG, "applyTheme: DARK");
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
break; break;
} }
default: { default: {
Log.d(TAG, "applyTheme: SYSTEM");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
} else { } else {

View file

@ -45,7 +45,7 @@ public class PreferenceUtil {
public static PreferenceUtil getInstance(final Context context) { public static PreferenceUtil getInstance(final Context context) {
if (sInstance == null) { if (sInstance == null) {
sInstance = new PreferenceUtil(context.getApplicationContext()); sInstance = new PreferenceUtil(context);
} }
return sInstance; return sInstance;

View file

@ -4,6 +4,6 @@
android:viewportWidth="24" android:viewportWidth="24"
android:viewportHeight="24"> android:viewportHeight="24">
<path <path
android:fillColor="#FF000000" android:fillColor="@color/titleTextColor"
android:pathData="M10,16c0.55,0 1,-0.45 1,-1L11,9c0,-0.55 -0.45,-1 -1,-1s-1,0.45 -1,1v6c0,0.55 0.45,1 1,1zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8zM14,16c0.55,0 1,-0.45 1,-1L15,9c0,-0.55 -0.45,-1 -1,-1s-1,0.45 -1,1v6c0,0.55 0.45,1 1,1z"/> android:pathData="M10,16c0.55,0 1,-0.45 1,-1L11,9c0,-0.55 -0.45,-1 -1,-1s-1,0.45 -1,1v6c0,0.55 0.45,1 1,1zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8zM14,16c0.55,0 1,-0.45 1,-1L15,9c0,-0.55 -0.45,-1 -1,-1s-1,0.45 -1,1v6c0,0.55 0.45,1 1,1z"/>
</vector> </vector>

View file

@ -4,6 +4,6 @@
android:viewportWidth="24" android:viewportWidth="24"
android:viewportHeight="24"> android:viewportHeight="24">
<path <path
android:fillColor="#FF000000" android:fillColor="@color/titleTextColor"
android:pathData="M10.8,15.9l4.67,-3.5c0.27,-0.2 0.27,-0.6 0,-0.8L10.8,8.1c-0.33,-0.25 -0.8,-0.01 -0.8,0.4v7c0,0.41 0.47,0.65 0.8,0.4zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8z"/> android:pathData="M10.8,15.9l4.67,-3.5c0.27,-0.2 0.27,-0.6 0,-0.8L10.8,8.1c-0.33,-0.25 -0.8,-0.01 -0.8,0.4v7c0,0.41 0.47,0.65 0.8,0.4zM12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM12,20c-4.41,0 -8,-3.59 -8,-8s3.59,-8 8,-8 8,3.59 8,8 -3.59,8 -8,8z"/>
</vector> </vector>