From e90291be594b00148992edbdb2bb552bd7ff0286 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 21 Nov 2022 16:12:21 +0100 Subject: [PATCH 1/2] add app icon preview bottom sheet --- .../ui/adapters/IconsSheetAdapter.kt | 68 +++++++++++++++++++ .../ui/preferences/AppearanceSettings.kt | 8 +-- .../libretube/ui/sheets/IconsBottomSheet.kt | 28 ++++++++ .../ui/viewholders/IconsSheetViewHolder.kt | 8 +++ .../com/github/libretube/util/ThemeHelper.kt | 18 ++--- app/src/main/res/layout/app_icon_item.xml | 25 +++++++ app/src/main/res/values/array.xml | 23 ------- app/src/main/res/xml/appearance_settings.xml | 8 +-- 8 files changed, 139 insertions(+), 47 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/ui/adapters/IconsSheetAdapter.kt create mode 100644 app/src/main/java/com/github/libretube/ui/sheets/IconsBottomSheet.kt create mode 100644 app/src/main/java/com/github/libretube/ui/viewholders/IconsSheetViewHolder.kt create mode 100644 app/src/main/res/layout/app_icon_item.xml diff --git a/app/src/main/java/com/github/libretube/ui/adapters/IconsSheetAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/IconsSheetAdapter.kt new file mode 100644 index 000000000..be50fa334 --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/adapters/IconsSheetAdapter.kt @@ -0,0 +1,68 @@ +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.databinding.AppIconItemBinding +import com.github.libretube.ui.viewholders.IconsSheetViewHolder +import com.github.libretube.util.ThemeHelper + +class IconsSheetAdapter : RecyclerView.Adapter() { + 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 { + 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 + ) + } +} diff --git a/app/src/main/java/com/github/libretube/ui/preferences/AppearanceSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/AppearanceSettings.kt index 67ec84f5c..70aa98df5 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/AppearanceSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/AppearanceSettings.kt @@ -10,7 +10,7 @@ import com.github.libretube.ui.activities.SettingsActivity import com.github.libretube.ui.base.BasePreferenceFragment import com.github.libretube.ui.dialogs.NavBarOptionsDialog import com.github.libretube.ui.dialogs.RequireRestartDialog -import com.github.libretube.util.ThemeHelper +import com.github.libretube.ui.sheets.IconsBottomSheet import com.google.android.material.color.DynamicColors class AppearanceSettings : BasePreferenceFragment() { @@ -42,9 +42,9 @@ class AppearanceSettings : BasePreferenceFragment() { true } - val iconChange = findPreference(PreferenceKeys.APP_ICON) - iconChange?.setOnPreferenceChangeListener { _, newValue -> - ThemeHelper.changeIcon(requireContext(), newValue.toString()) + val iconChange = findPreference(PreferenceKeys.APP_ICON) + iconChange?.setOnPreferenceClickListener { + IconsBottomSheet().show(childFragmentManager) true } diff --git a/app/src/main/java/com/github/libretube/ui/sheets/IconsBottomSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/IconsBottomSheet.kt new file mode 100644 index 000000000..3c3d0186c --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/sheets/IconsBottomSheet.kt @@ -0,0 +1,28 @@ +package com.github.libretube.ui.sheets + +import android.app.Dialog +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, 2) + binding.optionsRecycler.adapter = IconsSheetAdapter() + } +} diff --git a/app/src/main/java/com/github/libretube/ui/viewholders/IconsSheetViewHolder.kt b/app/src/main/java/com/github/libretube/ui/viewholders/IconsSheetViewHolder.kt new file mode 100644 index 000000000..78d21a6f8 --- /dev/null +++ b/app/src/main/java/com/github/libretube/ui/viewholders/IconsSheetViewHolder.kt @@ -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) diff --git a/app/src/main/java/com/github/libretube/util/ThemeHelper.kt b/app/src/main/java/com/github/libretube/util/ThemeHelper.kt index 524ffabc6..35b26befd 100644 --- a/app/src/main/java/com/github/libretube/util/ThemeHelper.kt +++ b/app/src/main/java/com/github/libretube/util/ThemeHelper.kt @@ -11,6 +11,7 @@ import androidx.appcompat.app.AppCompatDelegate import androidx.core.text.HtmlCompat import com.github.libretube.R import com.github.libretube.constants.PreferenceKeys +import com.github.libretube.ui.adapters.IconsSheetAdapter import com.google.android.material.color.DynamicColors object ThemeHelper { @@ -93,15 +94,9 @@ object ThemeHelper { * change the app icon */ fun changeIcon(context: Context, newLogoActivityAlias: String) { - val activityAliases = context.resources.getStringArray(R.array.iconsValue) // Disable Old Icon(s) - for (activityAlias in activityAliases) { - val activityClass = "com.github.libretube." + - if (activityAlias == activityAliases[0]) { - "ui.activities.MainActivity" // default icon/activity - } else { - activityAlias - } + for (appIcon in IconsSheetAdapter.availableIcons) { + val activityClass = "com.github.libretube." + appIcon.activityAlias // remove old icons context.packageManager.setComponentEnabledSetting( @@ -112,12 +107,7 @@ object ThemeHelper { } // set the class name for the activity alias - val newLogoActivityClass = "com.github.libretube." + - if (newLogoActivityAlias == activityAliases[0]) { - "ui.activities.MainActivity" // default icon/activity - } else { - newLogoActivityAlias - } + val newLogoActivityClass = "com.github.libretube." + newLogoActivityAlias // Enable New Icon context.packageManager.setComponentEnabledSetting( ComponentName(context.packageName, newLogoActivityClass), diff --git a/app/src/main/res/layout/app_icon_item.xml b/app/src/main/res/layout/app_icon_item.xml new file mode 100644 index 000000000..2e48f3de4 --- /dev/null +++ b/app/src/main/res/layout/app_icon_item.xml @@ -0,0 +1,25 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/array.xml b/app/src/main/res/values/array.xml index 5b5f4eb28..32fec8860 100644 --- a/app/src/main/res/values/array.xml +++ b/app/src/main/res/values/array.xml @@ -217,29 +217,6 @@ 5 - - @string/defaultIcon - @string/defaultIconLight - @string/legacyIcon - @string/gradientIcon - @string/fireIcon - @string/torchIcon - @string/shapedIcon - @string/flameIcon - @string/birdIcon - - - MainActivity - DefaultLight - IconLegacy - IconGradient - IconFire - IconTorch - IconShaped - IconFlame - IconBird - - MP4 MKV diff --git a/app/src/main/res/xml/appearance_settings.xml b/app/src/main/res/xml/appearance_settings.xml index c89505e52..a1311543b 100644 --- a/app/src/main/res/xml/appearance_settings.xml +++ b/app/src/main/res/xml/appearance_settings.xml @@ -29,14 +29,10 @@ app:title="@string/color_accent" app:useSimpleSummaryProvider="true" /> - + app:title="@string/app_icon" /> From 0dccb05a547b772308bd87ce7ae88536d0e861d2 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 21 Nov 2022 16:23:59 +0100 Subject: [PATCH 2/2] add a summary --- .../github/libretube/ui/adapters/IconsSheetAdapter.kt | 3 +++ .../libretube/ui/preferences/AppearanceSettings.kt | 10 ++++++++-- .../github/libretube/ui/sheets/IconsBottomSheet.kt | 3 +-- app/src/main/res/layout/app_icon_item.xml | 11 +++++++---- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/adapters/IconsSheetAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/IconsSheetAdapter.kt index be50fa334..aea7258b8 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/IconsSheetAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/IconsSheetAdapter.kt @@ -6,8 +6,10 @@ 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() { @@ -25,6 +27,7 @@ class IconsSheetAdapter : RecyclerView.Adapter() { 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) } } diff --git a/app/src/main/java/com/github/libretube/ui/preferences/AppearanceSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/AppearanceSettings.kt index 70aa98df5..583e3235d 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/AppearanceSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/AppearanceSettings.kt @@ -7,10 +7,12 @@ import androidx.preference.SwitchPreferenceCompat import com.github.libretube.R import com.github.libretube.constants.PreferenceKeys 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.dialogs.NavBarOptionsDialog import com.github.libretube.ui.dialogs.RequireRestartDialog import com.github.libretube.ui.sheets.IconsBottomSheet +import com.github.libretube.util.PreferenceHelper import com.google.android.material.color.DynamicColors class AppearanceSettings : BasePreferenceFragment() { @@ -42,8 +44,12 @@ class AppearanceSettings : BasePreferenceFragment() { true } - val iconChange = findPreference(PreferenceKeys.APP_ICON) - iconChange?.setOnPreferenceClickListener { + val changeIcon = findPreference(PreferenceKeys.APP_ICON) + val iconPref = PreferenceHelper.getString(PreferenceKeys.APP_ICON, IconsSheetAdapter.Companion.AppIcon.Default.activityAlias) + IconsSheetAdapter.availableIcons.firstOrNull { it.activityAlias == iconPref }?.let { + changeIcon?.summary = getString(it.nameResource) + } + changeIcon?.setOnPreferenceClickListener { IconsBottomSheet().show(childFragmentManager) true } diff --git a/app/src/main/java/com/github/libretube/ui/sheets/IconsBottomSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/IconsBottomSheet.kt index 3c3d0186c..57f9a0656 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/IconsBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/IconsBottomSheet.kt @@ -1,6 +1,5 @@ package com.github.libretube.ui.sheets -import android.app.Dialog import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -22,7 +21,7 @@ class IconsBottomSheet : ExpandedBottomSheet() { } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - binding.optionsRecycler.layoutManager = GridLayoutManager(context, 2) + binding.optionsRecycler.layoutManager = GridLayoutManager(context, 3) binding.optionsRecycler.adapter = IconsSheetAdapter() } } diff --git a/app/src/main/res/layout/app_icon_item.xml b/app/src/main/res/layout/app_icon_item.xml index 2e48f3de4..14eb9abcd 100644 --- a/app/src/main/res/layout/app_icon_item.xml +++ b/app/src/main/res/layout/app_icon_item.xml @@ -3,15 +3,17 @@ 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="5dp"> + android:paddingVertical="10dp"> + android:maxLines="1" + android:textSize="11sp" /> \ No newline at end of file