-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Catalog dark theme toggle to be a menu with System default option
PiperOrigin-RevId: 246841272
- Loading branch information
Showing
12 changed files
with
168 additions
and
80 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
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
File renamed without changes.
24 changes: 24 additions & 0 deletions
24
catalog/java/io/material/catalog/assets/res/drawable/ic_theme_default_vd_theme_24px.xml
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,24 @@ | ||
<!-- | ||
Copyright 2019 The Android Open Source Project | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
<vector xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:width="24dp" | ||
android:height="24dp" | ||
android:viewportHeight="24.0" | ||
android:viewportWidth="24.0"> | ||
<path | ||
android:fillColor="?attr/colorControlNormal" | ||
android:pathData="M6,18c0,0.55 0.45,1 1,1h1v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L11,19h2v3.5c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5L16,19h1c0.55,0 1,-0.45 1,-1L18,8L6,8v10zM3.5,8C2.67,8 2,8.67 2,9.5v7c0,0.83 0.67,1.5 1.5,1.5S5,17.33 5,16.5v-7C5,8.67 4.33,8 3.5,8zM20.5,8c-0.83,0 -1.5,0.67 -1.5,1.5v7c0,0.83 0.67,1.5 1.5,1.5s1.5,-0.67 1.5,-1.5v-7c0,-0.83 -0.67,-1.5 -1.5,-1.5zM15.53,2.16l1.3,-1.3c0.2,-0.2 0.2,-0.51 0,-0.71 -0.2,-0.2 -0.51,-0.2 -0.71,0l-1.48,1.48C13.85,1.23 12.95,1 12,1c-0.96,0 -1.86,0.23 -2.66,0.63L7.85,0.15c-0.2,-0.2 -0.51,-0.2 -0.71,0 -0.2,0.2 -0.2,0.51 0,0.71l1.31,1.31C6.97,3.26 6,5.01 6,7h12c0,-1.99 -0.97,-3.75 -2.47,-4.84zM10,5L9,5L9,4h1v1zM15,5h-1L14,4h1v1z"/> | ||
</vector> |
File renamed without changes.
44 changes: 0 additions & 44 deletions
44
catalog/java/io/material/catalog/tableofcontents/DarkThemePreferencesRepository.java
This file was deleted.
Oops, something went wrong.
77 changes: 77 additions & 0 deletions
77
catalog/java/io/material/catalog/tableofcontents/ThemePreferencesRepository.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,77 @@ | ||
/* | ||
* Copyright 2019 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.material.catalog.tableofcontents; | ||
|
||
import io.material.catalog.R; | ||
|
||
import android.content.Context; | ||
import android.content.SharedPreferences; | ||
import androidx.annotation.IdRes; | ||
import androidx.appcompat.app.AppCompatDelegate; | ||
import android.util.SparseIntArray; | ||
|
||
class ThemePreferencesRepository { | ||
|
||
private static final String PREFERENCES_NAME = "night_mode_preferences"; | ||
private static final String KEY_NIGHT_MODE = "night_mode"; | ||
private static final SparseIntArray THEME_NIGHT_MODE_MAP = new SparseIntArray(); | ||
|
||
static { | ||
THEME_NIGHT_MODE_MAP.append(R.id.theme_light, AppCompatDelegate.MODE_NIGHT_NO); | ||
THEME_NIGHT_MODE_MAP.append(R.id.theme_dark, AppCompatDelegate.MODE_NIGHT_YES); | ||
THEME_NIGHT_MODE_MAP.append(R.id.theme_default, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); | ||
} | ||
|
||
private final Context context; | ||
|
||
ThemePreferencesRepository(Context context) { | ||
this.context = context; | ||
} | ||
|
||
void saveAndApplyTheme(@IdRes int id) { | ||
int nightMode = convertToNightMode(id); | ||
saveNightMode(nightMode); | ||
AppCompatDelegate.setDefaultNightMode(nightMode); | ||
} | ||
|
||
void applyTheme() { | ||
ensureDefaultNightMode(getNightMode()); | ||
} | ||
|
||
private void ensureDefaultNightMode(int mode) { | ||
if (AppCompatDelegate.getDefaultNightMode() != mode) { | ||
AppCompatDelegate.setDefaultNightMode(mode); | ||
} | ||
} | ||
|
||
private int getNightMode() { | ||
return getSharedPreferences() | ||
.getInt(KEY_NIGHT_MODE, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); | ||
} | ||
|
||
private void saveNightMode(int nightMode) { | ||
getSharedPreferences().edit().putInt(KEY_NIGHT_MODE, nightMode).commit(); | ||
} | ||
|
||
private SharedPreferences getSharedPreferences() { | ||
return context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_PRIVATE); | ||
} | ||
|
||
private int convertToNightMode(@IdRes int id) { | ||
return THEME_NIGHT_MODE_MAP.get(id, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM); | ||
} | ||
} |
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
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
35 changes: 35 additions & 0 deletions
35
catalog/java/io/material/catalog/tableofcontents/res/menu/theme_menu.xml
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,35 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
Copyright 2019 The Android Open Source Project | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
<menu xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<item | ||
android:id="@+id/theme_light" | ||
android:icon="@drawable/ic_theme_light_vd_theme_24px" | ||
android:title="@string/cat_choose_theme_light"/> | ||
|
||
<item | ||
android:id="@+id/theme_dark" | ||
android:icon="@drawable/ic_theme_dark_vd_theme_24px" | ||
android:title="@string/cat_choose_theme_dark"/> | ||
|
||
<item | ||
android:id="@+id/theme_default" | ||
android:icon="@drawable/ic_theme_default_vd_theme_24px" | ||
android:title="@string/cat_choose_theme_default"/> | ||
|
||
</menu> |
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