From 975833b3d85320c361fc2e17b2a0aa933e009b3d Mon Sep 17 00:00:00 2001 From: Naveen Date: Fri, 11 Nov 2022 05:54:30 +0530 Subject: [PATCH 01/11] Add selected state, create abstract bottom sheet dialog class --- .../commons/adapters/SimpleListItemAdapter.kt | 19 +-- .../dialogs/BottomSheetChooserDialog.kt | 74 ++++++++++++ .../dialogs/SimpleBottomSheetChooserDialog.kt | 110 ------------------ .../BaseBottomSheetDialogFragment.kt | 50 ++++++++ .../commons/models/SimpleListItem.kt | 4 +- .../res/drawable/bottom_sheet_bg_black.xml | 16 +++ .../res/drawable/ic_check_circle_vector.xml | 3 + ...et_chooser.xml => dialog_bottom_sheet.xml} | 27 +---- .../src/main/res/layout/item_simple_list.xml | 18 ++- .../layout/layout_simple_recycler_view.xml | 12 ++ .../samples/activities/MainActivity.kt | 11 +- 11 files changed, 195 insertions(+), 149 deletions(-) create mode 100644 commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/BottomSheetChooserDialog.kt delete mode 100644 commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/SimpleBottomSheetChooserDialog.kt create mode 100644 commons/src/main/kotlin/com/simplemobiletools/commons/fragments/BaseBottomSheetDialogFragment.kt create mode 100644 commons/src/main/res/drawable/bottom_sheet_bg_black.xml create mode 100644 commons/src/main/res/drawable/ic_check_circle_vector.xml rename commons/src/main/res/layout/{dialog_bottom_sheet_chooser.xml => dialog_bottom_sheet.xml} (59%) create mode 100644 commons/src/main/res/layout/layout_simple_recycler_view.xml diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/SimpleListItemAdapter.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/SimpleListItemAdapter.kt index 138fc1e94..fd5c2896d 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/SimpleListItemAdapter.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/adapters/SimpleListItemAdapter.kt @@ -7,9 +7,7 @@ import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView import com.simplemobiletools.commons.R -import com.simplemobiletools.commons.extensions.applyColorFilter -import com.simplemobiletools.commons.extensions.getProperTextColor -import com.simplemobiletools.commons.extensions.setImageResourceOrBeGone +import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.models.SimpleListItem import kotlinx.android.synthetic.main.item_simple_list.view.* @@ -30,12 +28,19 @@ open class SimpleListItemAdapter(val activity: Activity, val onItemClicked: (Sim fun bindView(item: SimpleListItem) { itemView.apply { - val textColor = context.getProperTextColor() - bottom_sheet_item_title.setText(item.textRes) - bottom_sheet_item_title.setTextColor(textColor) + val color = if (item.selected) { + val primaryColor = context.getProperPrimaryColor() + bottom_sheet_selected_icon.beVisible() + bottom_sheet_selected_icon.applyColorFilter(primaryColor) + primaryColor + } else { + context.getProperTextColor() + } + bottom_sheet_item_title.setText(item.textRes) + bottom_sheet_item_title.setTextColor(color) bottom_sheet_item_icon.setImageResourceOrBeGone(item.imageRes) - bottom_sheet_item_icon.applyColorFilter(textColor) + bottom_sheet_item_icon.applyColorFilter(color) setOnClickListener { onItemClicked(item) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/BottomSheetChooserDialog.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/BottomSheetChooserDialog.kt new file mode 100644 index 000000000..3b821aaa4 --- /dev/null +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/BottomSheetChooserDialog.kt @@ -0,0 +1,74 @@ +package com.simplemobiletools.commons.dialogs + +import android.os.Bundle +import android.view.ViewGroup +import androidx.fragment.app.FragmentManager +import com.simplemobiletools.commons.R +import com.simplemobiletools.commons.adapters.SimpleListItemAdapter +import com.simplemobiletools.commons.fragments.BaseBottomSheetDialogFragment +import com.simplemobiletools.commons.models.SimpleListItem +import kotlinx.android.synthetic.main.layout_simple_recycler_view.* + +class BottomSheetChooserDialog : BaseBottomSheetDialogFragment() { + + var onItemClick: ((SimpleListItem) -> Unit)? = null + + override fun setupContentView(parent: ViewGroup) { + val child = layoutInflater.inflate(R.layout.layout_simple_recycler_view, parent, false) + parent.addView(child) + setupRecyclerView() + } + + private fun setupRecyclerView() { + @Suppress("UNCHECKED_CAST") + val listItems = arguments?.getParcelableArray(DATA) as Array + getRecyclerViewAdapter().submitList(listItems.toList()) + } + + private fun getRecyclerViewAdapter(): SimpleListItemAdapter { + var adapter = recycler_view.adapter as? SimpleListItemAdapter + if (adapter == null) { + adapter = SimpleListItemAdapter(requireActivity()) { + onItemClick?.invoke(it) + dismissAllowingStateLoss() + } + recycler_view.adapter = adapter + } + return adapter + } + + fun updateChooserItems(newItems: Array) { + if (isAdded) { + getRecyclerViewAdapter().submitList(newItems.toList()) + } + } + + override fun onDestroy() { + super.onDestroy() + onItemClick = null + } + + companion object { + private const val TAG = "BottomSheetChooserDialog" + private const val DATA = "data" + + fun createChooser( + fragmentManager: FragmentManager, + title: Int?, + data: Array, + callback: (SimpleListItem) -> Unit + ): BottomSheetChooserDialog { + val extras = Bundle().apply { + if (title != null) { + putInt(BOTTOM_SHEET_TITLE, title) + } + putParcelableArray(DATA, data) + } + return BottomSheetChooserDialog().apply { + arguments = extras + onItemClick = callback + show(fragmentManager, TAG) + } + } + } +} diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/SimpleBottomSheetChooserDialog.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/SimpleBottomSheetChooserDialog.kt deleted file mode 100644 index bd48ad9e4..000000000 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/dialogs/SimpleBottomSheetChooserDialog.kt +++ /dev/null @@ -1,110 +0,0 @@ -package com.simplemobiletools.commons.dialogs - -import android.graphics.drawable.LayerDrawable -import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import androidx.core.content.res.ResourcesCompat -import androidx.fragment.app.FragmentManager -import com.google.android.material.bottomsheet.BottomSheetDialogFragment -import com.simplemobiletools.commons.R -import com.simplemobiletools.commons.adapters.SimpleListItemAdapter -import com.simplemobiletools.commons.extensions.* -import com.simplemobiletools.commons.models.SimpleListItem -import kotlinx.android.synthetic.main.dialog_bottom_sheet_chooser.* - -class SimpleBottomSheetChooserDialog : BottomSheetDialogFragment() { - - var onItemClick: ((SimpleListItem) -> Unit)? = null - - override fun onCreateView( - inflater: LayoutInflater, - container: ViewGroup?, - savedInstanceState: Bundle? - ): View? { - val view = inflater.inflate(R.layout.dialog_bottom_sheet_chooser, container, false) - val context = requireContext() - val config = context.baseConfig - if (!config.isUsingSystemTheme) { - val background = ResourcesCompat.getDrawable(context.resources, R.drawable.bottom_sheet_bg, context.theme) - val backgroundColor = context.getProperBackgroundColor() - (background as LayerDrawable).findDrawableByLayerId(R.id.bottom_sheet_background).applyColorFilter(backgroundColor) - view.background = background - } - return view - } - - override fun onViewCreated(view: View, savedInstanceState: Bundle?) { - super.onViewCreated(view, savedInstanceState) - val title = arguments?.getInt(TITLE).takeIf { it != 0 } - val subtitle = arguments?.getInt(SUBTITLE).takeIf { it != 0 } - view.apply { - bottom_sheet_title.setTextColor(context.getProperTextColor()) - bottom_sheet_title.setTextOrBeGone(title) - bottom_sheet_subtitle.setTextColor(context.getProperTextColor()) - bottom_sheet_subtitle.setTextOrBeGone(subtitle) - setupRecyclerView() - } - } - - @Suppress("UNCHECKED_CAST") - private fun setupRecyclerView() { - val listItems = arguments?.getParcelableArray(DATA) as Array - getAudioRouteAdapter().submitList(listItems.toList()) - } - - private fun getAudioRouteAdapter(): SimpleListItemAdapter { - var adapter = bottom_sheet_recycler_view.adapter as? SimpleListItemAdapter - if (adapter == null) { - adapter = SimpleListItemAdapter(requireActivity()) { - onItemClick?.invoke(it) - dismissAllowingStateLoss() - } - bottom_sheet_recycler_view.adapter = adapter - } - return adapter - } - - fun updateChooserItems(newItems: Array) { - if (isAdded) { - getAudioRouteAdapter().submitList(newItems.toList()) - } - } - - override fun onDestroy() { - super.onDestroy() - onItemClick = null - } - - companion object { - private const val TAG = "BottomSheetChooser" - private const val TITLE = "title_string" - private const val SUBTITLE = "subtitle_string" - private const val DATA = "data" - - fun createChooser( - fragmentManager: FragmentManager, - title: Int?, - subtitle: Int?, - data: Array, - callback: (SimpleListItem) -> Unit - ): SimpleBottomSheetChooserDialog { - val extras = Bundle().apply { - if (title != null) { - putInt(TITLE, title) - } - if (subtitle != null) { - putInt(SUBTITLE, subtitle) - } - - putParcelableArray(DATA, data) - } - return SimpleBottomSheetChooserDialog().apply { - arguments = extras - onItemClick = callback - show(fragmentManager, TAG) - } - } - } -} diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/fragments/BaseBottomSheetDialogFragment.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/fragments/BaseBottomSheetDialogFragment.kt new file mode 100644 index 000000000..ea2bb840c --- /dev/null +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/fragments/BaseBottomSheetDialogFragment.kt @@ -0,0 +1,50 @@ +package com.simplemobiletools.commons.fragments + +import android.graphics.drawable.LayerDrawable +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.core.content.res.ResourcesCompat +import com.google.android.material.bottomsheet.BottomSheetDialogFragment +import com.simplemobiletools.commons.R +import com.simplemobiletools.commons.extensions.* +import kotlinx.android.synthetic.main.dialog_bottom_sheet.view.* + +abstract class BaseBottomSheetDialogFragment : BottomSheetDialogFragment() { + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View? { + val view = inflater.inflate(R.layout.dialog_bottom_sheet, container, false) + val context = requireContext() + val config = context.baseConfig + + if (requireContext().isBlackAndWhiteTheme()) { + view.background = ResourcesCompat.getDrawable(context.resources, R.drawable.bottom_sheet_bg_black, context.theme) + } else if (!config.isUsingSystemTheme) { + view.background = ResourcesCompat.getDrawable(context.resources, R.drawable.bottom_sheet_bg, context.theme).apply { + (this as LayerDrawable).findDrawableByLayerId(R.id.bottom_sheet_background).applyColorFilter(context.getProperBackgroundColor()) + } + } + return view + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + val title = arguments?.getInt(BOTTOM_SHEET_TITLE).takeIf { it != 0 } + view.apply { + bottom_sheet_title.setTextColor(context.getProperTextColor()) + bottom_sheet_title.setTextOrBeGone(title) + setupContentView(bottom_sheet_content_holder) + } + } + + abstract fun setupContentView(parent: ViewGroup) + + companion object { + const val BOTTOM_SHEET_TITLE = "title_string" + } +} diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/models/SimpleListItem.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/models/SimpleListItem.kt index b6ab849a5..69dba355a 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/models/SimpleListItem.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/models/SimpleListItem.kt @@ -4,7 +4,7 @@ import android.os.Parcelable import kotlinx.android.parcel.Parcelize @Parcelize -data class SimpleListItem(val id: Int, val imageRes: Int?, val textRes: Int) : Parcelable { +data class SimpleListItem(val id: Int, val imageRes: Int?, val textRes: Int, val selected: Boolean = false) : Parcelable { companion object { fun areItemsTheSame(old: SimpleListItem, new: SimpleListItem): Boolean { @@ -12,7 +12,7 @@ data class SimpleListItem(val id: Int, val imageRes: Int?, val textRes: Int) : P } fun areContentsTheSame(old: SimpleListItem, new: SimpleListItem): Boolean { - return old.imageRes == new.imageRes && old.textRes == new.textRes + return old.imageRes == new.imageRes && old.textRes == new.textRes && old.selected == new.selected } } } diff --git a/commons/src/main/res/drawable/bottom_sheet_bg_black.xml b/commons/src/main/res/drawable/bottom_sheet_bg_black.xml new file mode 100644 index 000000000..a89b9e5f8 --- /dev/null +++ b/commons/src/main/res/drawable/bottom_sheet_bg_black.xml @@ -0,0 +1,16 @@ + + + + + + + + + + diff --git a/commons/src/main/res/drawable/ic_check_circle_vector.xml b/commons/src/main/res/drawable/ic_check_circle_vector.xml new file mode 100644 index 000000000..a1c599dda --- /dev/null +++ b/commons/src/main/res/drawable/ic_check_circle_vector.xml @@ -0,0 +1,3 @@ + + + diff --git a/commons/src/main/res/layout/dialog_bottom_sheet_chooser.xml b/commons/src/main/res/layout/dialog_bottom_sheet.xml similarity index 59% rename from commons/src/main/res/layout/dialog_bottom_sheet_chooser.xml rename to commons/src/main/res/layout/dialog_bottom_sheet.xml index b9304683c..bf8723a9f 100644 --- a/commons/src/main/res/layout/dialog_bottom_sheet_chooser.xml +++ b/commons/src/main/res/layout/dialog_bottom_sheet.xml @@ -21,32 +21,15 @@ tools:text="Title" tools:visibility="visible" /> - - - + app:layout_constraintTop_toBottomOf="@id/bottom_sheet_title" + app:layout_constraintVertical_bias="0" /> diff --git a/commons/src/main/res/layout/item_simple_list.xml b/commons/src/main/res/layout/item_simple_list.xml index 54ca9d962..39fadb38a 100644 --- a/commons/src/main/res/layout/item_simple_list.xml +++ b/commons/src/main/res/layout/item_simple_list.xml @@ -23,11 +23,25 @@ android:id="@+id/bottom_sheet_item_title" android:layout_width="0dp" android:layout_height="wrap_content" - android:layout_marginStart="@dimen/small_margin" + android:layout_marginHorizontal="@dimen/small_margin" app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintEnd_toStartOf="@id/bottom_sheet_selected_icon" app:layout_constraintStart_toEndOf="@id/bottom_sheet_item_icon" app:layout_constraintTop_toTopOf="parent" tools:text="ReadMe.md" /> + + diff --git a/commons/src/main/res/layout/layout_simple_recycler_view.xml b/commons/src/main/res/layout/layout_simple_recycler_view.xml new file mode 100644 index 000000000..4dc87308b --- /dev/null +++ b/commons/src/main/res/layout/layout_simple_recycler_view.xml @@ -0,0 +1,12 @@ + diff --git a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt index cab9d0d19..52406b19d 100644 --- a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt +++ b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt @@ -2,7 +2,7 @@ package com.simplemobiletools.commons.samples.activities import android.os.Bundle import com.simplemobiletools.commons.activities.BaseSimpleActivity -import com.simplemobiletools.commons.dialogs.SimpleBottomSheetChooserDialog +import com.simplemobiletools.commons.dialogs.BottomSheetChooserDialog import com.simplemobiletools.commons.extensions.appLaunched import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.models.SimpleListItem @@ -49,14 +49,13 @@ class MainActivity : BaseSimpleActivity() { } private fun launchBottomSheetDemo() { - SimpleBottomSheetChooserDialog.createChooser( + BottomSheetChooserDialog.createChooser( fragmentManager = supportFragmentManager, title = R.string.please_select_destination, - subtitle = null, data = arrayOf( - SimpleListItem(1, R.drawable.ic_settings_cog_vector, R.string.choose_video), - SimpleListItem(2, R.drawable.ic_settings_cog_vector, R.string.choose_photo), - SimpleListItem(4, R.drawable.ic_settings_cog_vector, R.string.choose_contact) + SimpleListItem(1, R.drawable.ic_camera_vector, R.string.record_video), + SimpleListItem(2, R.drawable.ic_microphone_vector, R.string.record_audio, selected = true), + SimpleListItem(4, R.drawable.ic_add_person_vector, R.string.choose_contact) ) ) { toast("Clicked ${it.id}") From 67280f98534501d8f6fb6a3674e6a2bb04f7096e Mon Sep 17 00:00:00 2001 From: Naveen Date: Fri, 11 Nov 2022 06:06:22 +0530 Subject: [PATCH 02/11] Optimize drawable --- commons/src/main/res/drawable/ic_telegram_vector.xml | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/commons/src/main/res/drawable/ic_telegram_vector.xml b/commons/src/main/res/drawable/ic_telegram_vector.xml index 45ca0f9e8..277068ca1 100644 --- a/commons/src/main/res/drawable/ic_telegram_vector.xml +++ b/commons/src/main/res/drawable/ic_telegram_vector.xml @@ -1,5 +1,4 @@ - - - + + + From d2bbbaa309f555dfe850f663b6acccb236e7590e Mon Sep 17 00:00:00 2001 From: Naveen Date: Fri, 11 Nov 2022 17:49:00 +0530 Subject: [PATCH 03/11] Use proper navigation bar color --- commons/src/main/res/values/styles.xml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/commons/src/main/res/values/styles.xml b/commons/src/main/res/values/styles.xml index e48ad4267..30efa3eca 100644 --- a/commons/src/main/res/values/styles.xml +++ b/commons/src/main/res/values/styles.xml @@ -1,4 +1,4 @@ - +