Merge pull request #1963 from Bnyro/master

Preview App icons in Appearance Settings
This commit is contained in:
Bnyro 2022-11-21 16:25:05 +01:00 committed by GitHub
commit 440d2554ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 150 additions and 47 deletions

View File

@ -0,0 +1,71 @@
package com.github.libretube.ui.adapters
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.AppIconItemBinding
import com.github.libretube.ui.viewholders.IconsSheetViewHolder
import com.github.libretube.util.PreferenceHelper
import com.github.libretube.util.ThemeHelper
class IconsSheetAdapter : RecyclerView.Adapter<IconsSheetViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IconsSheetViewHolder {
val binding = AppIconItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return IconsSheetViewHolder(binding)
}
override fun getItemCount(): Int {
return availableIcons.size
}
override fun onBindViewHolder(holder: IconsSheetViewHolder, position: Int) {
val appIcon = availableIcons[position]
holder.binding.apply { iconIV.setImageResource(appIcon.iconResource)
iconName.text = root.context.getString(appIcon.nameResource)
root.setOnClickListener {
PreferenceHelper.putString(PreferenceKeys.APP_ICON, appIcon.activityAlias)
ThemeHelper.changeIcon(root.context, appIcon.activityAlias)
}
}
}
companion object {
sealed class AppIcon(
@StringRes val nameResource: Int,
@DrawableRes val iconResource: Int,
val activityAlias: String
) {
object Default :
AppIcon(R.string.defaultIcon, R.mipmap.ic_launcher, "ui.activities.MainActivity")
object DefaultLight :
AppIcon(R.string.defaultIconLight, R.mipmap.ic_launcher_light, "DefaultLight")
object Legacy : AppIcon(R.string.legacyIcon, R.mipmap.ic_legacy, "IconLegacy")
object Gradient :
AppIcon(R.string.gradientIcon, R.mipmap.ic_gradient, "IconGradient")
object Fire : AppIcon(R.string.fireIcon, R.mipmap.ic_fire, "IconFire")
object Torch : AppIcon(R.string.torchIcon, R.mipmap.ic_torch, "IconTorch")
object Shaped : AppIcon(R.string.shapedIcon, R.mipmap.ic_shaped, "IconShaped")
object Flame : AppIcon(R.string.flameIcon, R.mipmap.ic_flame, "IconFlame")
object Bird : AppIcon(R.string.birdIcon, R.mipmap.ic_bird, "IconBird")
}
val availableIcons = listOf(
AppIcon.Default,
AppIcon.DefaultLight,
AppIcon.Legacy,
AppIcon.Gradient,
AppIcon.Fire,
AppIcon.Torch,
AppIcon.Shaped,
AppIcon.Flame,
AppIcon.Bird
)
}
}

View File

@ -7,10 +7,12 @@ import androidx.preference.SwitchPreferenceCompat
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.ui.activities.SettingsActivity import com.github.libretube.ui.activities.SettingsActivity
import com.github.libretube.ui.adapters.IconsSheetAdapter
import com.github.libretube.ui.base.BasePreferenceFragment import com.github.libretube.ui.base.BasePreferenceFragment
import com.github.libretube.ui.dialogs.NavBarOptionsDialog import com.github.libretube.ui.dialogs.NavBarOptionsDialog
import com.github.libretube.ui.dialogs.RequireRestartDialog import com.github.libretube.ui.dialogs.RequireRestartDialog
import com.github.libretube.util.ThemeHelper import com.github.libretube.ui.sheets.IconsBottomSheet
import com.github.libretube.util.PreferenceHelper
import com.google.android.material.color.DynamicColors import com.google.android.material.color.DynamicColors
class AppearanceSettings : BasePreferenceFragment() { class AppearanceSettings : BasePreferenceFragment() {
@ -42,9 +44,13 @@ class AppearanceSettings : BasePreferenceFragment() {
true true
} }
val iconChange = findPreference<ListPreference>(PreferenceKeys.APP_ICON) val changeIcon = findPreference<Preference>(PreferenceKeys.APP_ICON)
iconChange?.setOnPreferenceChangeListener { _, newValue -> val iconPref = PreferenceHelper.getString(PreferenceKeys.APP_ICON, IconsSheetAdapter.Companion.AppIcon.Default.activityAlias)
ThemeHelper.changeIcon(requireContext(), newValue.toString()) IconsSheetAdapter.availableIcons.firstOrNull { it.activityAlias == iconPref }?.let {
changeIcon?.summary = getString(it.nameResource)
}
changeIcon?.setOnPreferenceClickListener {
IconsBottomSheet().show(childFragmentManager)
true true
} }

View File

@ -0,0 +1,27 @@
package com.github.libretube.ui.sheets
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.github.libretube.databinding.BottomSheetBinding
import com.github.libretube.ui.adapters.IconsSheetAdapter
class IconsBottomSheet : ExpandedBottomSheet() {
private lateinit var binding: BottomSheetBinding
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = BottomSheetBinding.inflate(layoutInflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
binding.optionsRecycler.layoutManager = GridLayoutManager(context, 3)
binding.optionsRecycler.adapter = IconsSheetAdapter()
}
}

View File

@ -0,0 +1,8 @@
package com.github.libretube.ui.viewholders
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.AppIconItemBinding
class IconsSheetViewHolder(
val binding: AppIconItemBinding
) : RecyclerView.ViewHolder(binding.root)

View File

@ -11,6 +11,7 @@ import androidx.appcompat.app.AppCompatDelegate
import androidx.core.text.HtmlCompat import androidx.core.text.HtmlCompat
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.ui.adapters.IconsSheetAdapter
import com.google.android.material.color.DynamicColors import com.google.android.material.color.DynamicColors
object ThemeHelper { object ThemeHelper {
@ -93,15 +94,9 @@ object ThemeHelper {
* change the app icon * change the app icon
*/ */
fun changeIcon(context: Context, newLogoActivityAlias: String) { fun changeIcon(context: Context, newLogoActivityAlias: String) {
val activityAliases = context.resources.getStringArray(R.array.iconsValue)
// Disable Old Icon(s) // Disable Old Icon(s)
for (activityAlias in activityAliases) { for (appIcon in IconsSheetAdapter.availableIcons) {
val activityClass = "com.github.libretube." + val activityClass = "com.github.libretube." + appIcon.activityAlias
if (activityAlias == activityAliases[0]) {
"ui.activities.MainActivity" // default icon/activity
} else {
activityAlias
}
// remove old icons // remove old icons
context.packageManager.setComponentEnabledSetting( context.packageManager.setComponentEnabledSetting(
@ -112,12 +107,7 @@ object ThemeHelper {
} }
// set the class name for the activity alias // set the class name for the activity alias
val newLogoActivityClass = "com.github.libretube." + val newLogoActivityClass = "com.github.libretube." + newLogoActivityAlias
if (newLogoActivityAlias == activityAliases[0]) {
"ui.activities.MainActivity" // default icon/activity
} else {
newLogoActivityAlias
}
// Enable New Icon // Enable New Icon
context.packageManager.setComponentEnabledSetting( context.packageManager.setComponentEnabledSetting(
ComponentName(context.packageName, newLogoActivityClass), ComponentName(context.packageName, newLogoActivityClass),

View File

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="10dp"
android:background="@drawable/rounded_ripple"
android:orientation="vertical"
android:paddingVertical="10dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/iconIV"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_gravity="center"
android:padding="10dp"
tools:src="@mipmap/ic_gradient" />
<TextView
android:id="@+id/iconName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ellipsize="end"
android:maxLines="1"
android:textSize="11sp" />
</LinearLayout>

View File

@ -217,29 +217,6 @@
<item>5</item> <item>5</item>
</string-array> </string-array>
<string-array name="icons">
<item>@string/defaultIcon</item>
<item>@string/defaultIconLight</item>
<item>@string/legacyIcon</item>
<item>@string/gradientIcon</item>
<item>@string/fireIcon</item>
<item>@string/torchIcon</item>
<item>@string/shapedIcon</item>
<item>@string/flameIcon</item>
<item>@string/birdIcon</item>
</string-array>
<string-array name="iconsValue">
<item>MainActivity</item>
<item>DefaultLight</item>
<item>IconLegacy</item>
<item>IconGradient</item>
<item>IconFire</item>
<item>IconTorch</item>
<item>IconShaped</item>
<item>IconFlame</item>
<item>IconBird</item>
</string-array>
<string-array name="videoFormats"> <string-array name="videoFormats">
<item>MP4</item> <item>MP4</item>
<item>MKV</item> <item>MKV</item>

View File

@ -29,14 +29,10 @@
app:title="@string/color_accent" app:title="@string/color_accent"
app:useSimpleSummaryProvider="true" /> app:useSimpleSummaryProvider="true" />
<ListPreference <Preference
android:icon="@drawable/ic_frame" android:icon="@drawable/ic_frame"
app:defaultValue="MainActivity"
app:entries="@array/icons"
app:entryValues="@array/iconsValue"
app:key="icon_change" app:key="icon_change"
app:title="@string/app_icon" app:title="@string/app_icon" />
app:useSimpleSummaryProvider="true" />
</PreferenceCategory> </PreferenceCategory>