Use view binding in FeedGroupDialog.

This commit is contained in:
Isira Seneviratne 2020-11-03 14:15:43 +05:30
parent 83f33a7d1b
commit 5994cd8ea2

View file

@ -24,10 +24,10 @@ import com.xwray.groupie.Section
import com.xwray.groupie.kotlinandroidextensions.GroupieViewHolder import com.xwray.groupie.kotlinandroidextensions.GroupieViewHolder
import icepick.Icepick import icepick.Icepick
import icepick.State import icepick.State
import kotlinx.android.synthetic.main.dialog_feed_group_create.*
import kotlinx.android.synthetic.main.toolbar_search_layout.*
import org.schabi.newpipe.R import org.schabi.newpipe.R
import org.schabi.newpipe.database.feed.model.FeedGroupEntity import org.schabi.newpipe.database.feed.model.FeedGroupEntity
import org.schabi.newpipe.databinding.DialogFeedGroupCreateBinding
import org.schabi.newpipe.databinding.ToolbarSearchLayoutBinding
import org.schabi.newpipe.fragments.BackPressable import org.schabi.newpipe.fragments.BackPressable
import org.schabi.newpipe.local.subscription.FeedGroupIcon import org.schabi.newpipe.local.subscription.FeedGroupIcon
import org.schabi.newpipe.local.subscription.dialog.FeedGroupDialog.ScreenState.DeleteScreen import org.schabi.newpipe.local.subscription.dialog.FeedGroupDialog.ScreenState.DeleteScreen
@ -45,6 +45,12 @@ import java.io.Serializable
import kotlin.collections.contains import kotlin.collections.contains
class FeedGroupDialog : DialogFragment(), BackPressable { class FeedGroupDialog : DialogFragment(), BackPressable {
private var _feedGroupCreateBinding: DialogFeedGroupCreateBinding? = null
private val feedGroupCreateBinding get() = _feedGroupCreateBinding!!
private var _searchLayoutBinding: ToolbarSearchLayoutBinding? = null
private val searchLayoutBinding get() = _searchLayoutBinding!!
private lateinit var viewModel: FeedGroupDialogViewModel private lateinit var viewModel: FeedGroupDialogViewModel
private var groupId: Long = NO_GROUP_SELECTED private var groupId: Long = NO_GROUP_SELECTED
private var groupIcon: FeedGroupIcon? = null private var groupIcon: FeedGroupIcon? = null
@ -107,14 +113,16 @@ class FeedGroupDialog : DialogFragment(), BackPressable {
override fun onSaveInstanceState(outState: Bundle) { override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState) super.onSaveInstanceState(outState)
iconsListState = icon_selector.layoutManager?.onSaveInstanceState() iconsListState = feedGroupCreateBinding.iconSelector.layoutManager?.onSaveInstanceState()
subscriptionsListState = subscriptions_selector_list.layoutManager?.onSaveInstanceState() subscriptionsListState = feedGroupCreateBinding.subscriptionsSelectorList.layoutManager?.onSaveInstanceState()
Icepick.saveInstanceState(this, outState) Icepick.saveInstanceState(this, outState)
} }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
_feedGroupCreateBinding = DialogFeedGroupCreateBinding.bind(view)
_searchLayoutBinding = feedGroupCreateBinding.subscriptionsHeaderSearchContainer
viewModel = ViewModelProvider( viewModel = ViewModelProvider(
this, this,
@ -146,7 +154,7 @@ class FeedGroupDialog : DialogFragment(), BackPressable {
add(subscriptionEmptyFooter) add(subscriptionEmptyFooter)
spanCount = 4 spanCount = 4
} }
subscriptions_selector_list.apply { feedGroupCreateBinding.subscriptionsSelectorList.apply {
// Disable animations, too distracting. // Disable animations, too distracting.
itemAnimator = null itemAnimator = null
adapter = subscriptionGroupAdapter adapter = subscriptionGroupAdapter
@ -172,8 +180,11 @@ class FeedGroupDialog : DialogFragment(), BackPressable {
override fun onDestroyView() { override fun onDestroyView() {
super.onDestroyView() super.onDestroyView()
subscriptions_selector_list?.adapter = null feedGroupCreateBinding.subscriptionsSelectorList.adapter = null
icon_selector?.adapter = null feedGroupCreateBinding.iconSelector.adapter = null
_feedGroupCreateBinding = null
_searchLayoutBinding = null
} }
/*/////////////////////////////////////////////////////////////////////////// /*///////////////////////////////////////////////////////////////////////////
@ -193,30 +204,30 @@ class FeedGroupDialog : DialogFragment(), BackPressable {
} }
private fun setupListeners() { private fun setupListeners() {
delete_button.setOnClickListener { showScreen(DeleteScreen) } feedGroupCreateBinding.deleteButton.setOnClickListener { showScreen(DeleteScreen) }
cancel_button.setOnClickListener { feedGroupCreateBinding.cancelButton.setOnClickListener {
when (currentScreen) { when (currentScreen) {
InitialScreen -> dismiss() InitialScreen -> dismiss()
else -> showScreen(InitialScreen) else -> showScreen(InitialScreen)
} }
} }
group_name_input_container.error = null feedGroupCreateBinding.groupNameInputContainer.error = null
group_name_input.doOnTextChanged { text, _, _, _ -> feedGroupCreateBinding.groupNameInput.doOnTextChanged { text, _, _, _ ->
if (group_name_input_container.isErrorEnabled && !text.isNullOrBlank()) { if (feedGroupCreateBinding.groupNameInputContainer.isErrorEnabled && !text.isNullOrBlank()) {
group_name_input_container.error = null feedGroupCreateBinding.groupNameInputContainer.error = null
} }
} }
confirm_button.setOnClickListener { handlePositiveButton() } feedGroupCreateBinding.confirmButton.setOnClickListener { handlePositiveButton() }
select_channel_button.setOnClickListener { feedGroupCreateBinding.selectChannelButton.setOnClickListener {
subscriptions_selector_list.scrollToPosition(0) feedGroupCreateBinding.subscriptionsSelectorList.scrollToPosition(0)
showScreen(SubscriptionsPickerScreen) showScreen(SubscriptionsPickerScreen)
} }
val headerMenu = subscriptions_header_toolbar.menu val headerMenu = feedGroupCreateBinding.subscriptionsHeaderToolbar.menu
requireActivity().menuInflater.inflate(R.menu.menu_feed_group_dialog, headerMenu) requireActivity().menuInflater.inflate(R.menu.menu_feed_group_dialog, headerMenu)
headerMenu.findItem(R.id.action_search).setOnMenuItemClickListener { headerMenu.findItem(R.id.action_search).setOnMenuItemClickListener {
@ -234,8 +245,8 @@ class FeedGroupDialog : DialogFragment(), BackPressable {
} }
} }
toolbar_search_clear.setOnClickListener { searchLayoutBinding.toolbarSearchClear.setOnClickListener {
if (toolbar_search_edit_text.text.isEmpty()) { if (searchLayoutBinding.toolbarSearchEditText.text.isNullOrEmpty()) {
hideSearch() hideSearch()
return@setOnClickListener return@setOnClickListener
} }
@ -243,14 +254,14 @@ class FeedGroupDialog : DialogFragment(), BackPressable {
showKeyboardSearch() showKeyboardSearch()
} }
toolbar_search_edit_text.setOnClickListener { searchLayoutBinding.toolbarSearchEditText.setOnClickListener {
if (DeviceUtils.isTv(context)) { if (DeviceUtils.isTv(context)) {
showKeyboardSearch() showKeyboardSearch()
} }
} }
toolbar_search_edit_text.doOnTextChanged { _, _, _, _ -> searchLayoutBinding.toolbarSearchEditText.doOnTextChanged { _, _, _, _ ->
val newQuery: String = toolbar_search_edit_text.text.toString() val newQuery: String = searchLayoutBinding.toolbarSearchEditText.text.toString()
subscriptionsCurrentSearchQuery = newQuery subscriptionsCurrentSearchQuery = newQuery
viewModel.filterSubscriptionsBy(newQuery) viewModel.filterSubscriptionsBy(newQuery)
} }
@ -266,16 +277,16 @@ class FeedGroupDialog : DialogFragment(), BackPressable {
} }
private fun handlePositiveButtonInitialScreen() { private fun handlePositiveButtonInitialScreen() {
val name = group_name_input.text.toString().trim() val name = feedGroupCreateBinding.groupNameInput.text.toString().trim()
val icon = selectedIcon ?: groupIcon ?: FeedGroupIcon.ALL val icon = selectedIcon ?: groupIcon ?: FeedGroupIcon.ALL
if (name.isBlank()) { if (name.isBlank()) {
group_name_input_container.error = getString(R.string.feed_group_dialog_empty_name) feedGroupCreateBinding.groupNameInputContainer.error = getString(R.string.feed_group_dialog_empty_name)
group_name_input.text = null feedGroupCreateBinding.groupNameInput.text = null
group_name_input.requestFocus() feedGroupCreateBinding.groupNameInput.requestFocus()
return return
} else { } else {
group_name_input_container.error = null feedGroupCreateBinding.groupNameInputContainer.error = null
} }
if (selectedSubscriptions.isEmpty()) { if (selectedSubscriptions.isEmpty()) {
@ -296,10 +307,10 @@ class FeedGroupDialog : DialogFragment(), BackPressable {
groupSortOrder = feedGroupEntity?.sortOrder ?: -1 groupSortOrder = feedGroupEntity?.sortOrder ?: -1
val feedGroupIcon = if (selectedIcon == null) icon else selectedIcon!! val feedGroupIcon = if (selectedIcon == null) icon else selectedIcon!!
icon_preview.setImageResource(feedGroupIcon.getDrawableRes(requireContext())) feedGroupCreateBinding.iconPreview.setImageResource(feedGroupIcon.getDrawableRes(requireContext()))
if (group_name_input.text.isNullOrBlank()) { if (feedGroupCreateBinding.groupNameInput.text.isNullOrBlank()) {
group_name_input.setText(name) feedGroupCreateBinding.groupNameInput.setText(name)
} }
} }
@ -346,10 +357,10 @@ class FeedGroupDialog : DialogFragment(), BackPressable {
subscriptionMainSection.update(subscriptions, false) subscriptionMainSection.update(subscriptions, false)
if (subscriptionsListState != null) { if (subscriptionsListState != null) {
subscriptions_selector_list.layoutManager?.onRestoreInstanceState(subscriptionsListState) feedGroupCreateBinding.subscriptionsSelectorList.layoutManager?.onRestoreInstanceState(subscriptionsListState)
subscriptionsListState = null subscriptionsListState = null
} else { } else {
subscriptions_selector_list.scrollToPosition(0) feedGroupCreateBinding.subscriptionsSelectorList.scrollToPosition(0)
} }
} }
@ -357,17 +368,16 @@ class FeedGroupDialog : DialogFragment(), BackPressable {
val selectedCount = this.selectedSubscriptions.size val selectedCount = this.selectedSubscriptions.size
val selectedCountText = resources.getQuantityString( val selectedCountText = resources.getQuantityString(
R.plurals.feed_group_dialog_selection_count, R.plurals.feed_group_dialog_selection_count,
selectedCount, selectedCount selectedCount, selectedCount)
) feedGroupCreateBinding.selectedSubscriptionCountView.text = selectedCountText
selected_subscription_count_view.text = selectedCountText feedGroupCreateBinding.subscriptionsHeaderInfo.text = selectedCountText
subscriptions_header_info.text = selectedCountText
} }
private fun setupIconPicker() { private fun setupIconPicker() {
val groupAdapter = GroupAdapter<GroupieViewHolder>() val groupAdapter = GroupAdapter<GroupieViewHolder>()
groupAdapter.addAll(FeedGroupIcon.values().map { PickerIconItem(requireContext(), it) }) groupAdapter.addAll(FeedGroupIcon.values().map { PickerIconItem(requireContext(), it) })
icon_selector.apply { feedGroupCreateBinding.iconSelector.apply {
layoutManager = GridLayoutManager(requireContext(), 7, RecyclerView.VERTICAL, false) layoutManager = GridLayoutManager(requireContext(), 7, RecyclerView.VERTICAL, false)
adapter = groupAdapter adapter = groupAdapter
@ -381,20 +391,20 @@ class FeedGroupDialog : DialogFragment(), BackPressable {
when (item) { when (item) {
is PickerIconItem -> { is PickerIconItem -> {
selectedIcon = item.icon selectedIcon = item.icon
icon_preview.setImageResource(item.iconRes) feedGroupCreateBinding.iconPreview.setImageResource(item.iconRes)
showScreen(InitialScreen) showScreen(InitialScreen)
} }
} }
} }
icon_preview.setOnClickListener { feedGroupCreateBinding.iconPreview.setOnClickListener {
icon_selector.scrollToPosition(0) feedGroupCreateBinding.iconSelector.scrollToPosition(0)
showScreen(IconPickerScreen) showScreen(IconPickerScreen)
} }
if (groupId == NO_GROUP_SELECTED) { if (groupId == NO_GROUP_SELECTED) {
val icon = selectedIcon ?: FeedGroupIcon.ALL val icon = selectedIcon ?: FeedGroupIcon.ALL
icon_preview.setImageResource(icon.getDrawableRes(requireContext())) feedGroupCreateBinding.iconPreview.setImageResource(icon.getDrawableRes(requireContext()))
} }
} }
@ -405,22 +415,20 @@ class FeedGroupDialog : DialogFragment(), BackPressable {
private fun showScreen(screen: ScreenState) { private fun showScreen(screen: ScreenState) {
currentScreen = screen currentScreen = screen
options_root.onlyVisibleIn(InitialScreen) feedGroupCreateBinding.optionsRoot.onlyVisibleIn(InitialScreen)
icon_selector.onlyVisibleIn(IconPickerScreen) feedGroupCreateBinding.iconSelector.onlyVisibleIn(IconPickerScreen)
subscriptions_selector.onlyVisibleIn(SubscriptionsPickerScreen) feedGroupCreateBinding.subscriptionsSelector.onlyVisibleIn(SubscriptionsPickerScreen)
delete_screen_message.onlyVisibleIn(DeleteScreen) feedGroupCreateBinding.deleteScreenMessage.onlyVisibleIn(DeleteScreen)
separator.onlyVisibleIn(SubscriptionsPickerScreen, IconPickerScreen) feedGroupCreateBinding.separator.onlyVisibleIn(SubscriptionsPickerScreen, IconPickerScreen)
cancel_button.onlyVisibleIn(InitialScreen, DeleteScreen) feedGroupCreateBinding.cancelButton.onlyVisibleIn(InitialScreen, DeleteScreen)
confirm_button.setText( feedGroupCreateBinding.confirmButton.setText(when {
when { currentScreen == InitialScreen && groupId == NO_GROUP_SELECTED -> R.string.create
currentScreen == InitialScreen && groupId == NO_GROUP_SELECTED -> R.string.create else -> android.R.string.ok
else -> android.R.string.ok })
}
)
delete_button.isGone = currentScreen != InitialScreen || groupId == NO_GROUP_SELECTED feedGroupCreateBinding.deleteButton.isGone = currentScreen != InitialScreen || groupId == NO_GROUP_SELECTED
hideKeyboard() hideKeyboard()
hideSearch() hideSearch()
@ -434,26 +442,26 @@ class FeedGroupDialog : DialogFragment(), BackPressable {
// Utils // Utils
////////////////////////////////////////////////////////////////////////// */ ////////////////////////////////////////////////////////////////////////// */
private fun isSearchVisible() = subscriptions_header_search_container?.visibility == View.VISIBLE private fun isSearchVisible() = _searchLayoutBinding?.root?.visibility == View.VISIBLE
private fun resetSearch() { private fun resetSearch() {
toolbar_search_edit_text.setText("") searchLayoutBinding.toolbarSearchEditText.setText("")
subscriptionsCurrentSearchQuery = "" subscriptionsCurrentSearchQuery = ""
viewModel.clearSubscriptionsFilter() viewModel.clearSubscriptionsFilter()
} }
private fun hideSearch() { private fun hideSearch() {
resetSearch() resetSearch()
subscriptions_header_search_container.visibility = View.GONE searchLayoutBinding.root.visibility = View.GONE
subscriptions_header_info_container.visibility = View.VISIBLE feedGroupCreateBinding.subscriptionsHeaderInfoContainer.visibility = View.VISIBLE
subscriptions_header_toolbar.menu.findItem(R.id.action_search).isVisible = true feedGroupCreateBinding.subscriptionsHeaderToolbar.menu.findItem(R.id.action_search).isVisible = true
hideKeyboardSearch() hideKeyboardSearch()
} }
private fun showSearch() { private fun showSearch() {
subscriptions_header_search_container.visibility = View.VISIBLE searchLayoutBinding.root.visibility = View.VISIBLE
subscriptions_header_info_container.visibility = View.GONE feedGroupCreateBinding.subscriptionsHeaderInfoContainer.visibility = View.GONE
subscriptions_header_toolbar.menu.findItem(R.id.action_search).isVisible = false feedGroupCreateBinding.subscriptionsHeaderToolbar.menu.findItem(R.id.action_search).isVisible = false
showKeyboardSearch() showKeyboardSearch()
} }
@ -462,37 +470,35 @@ class FeedGroupDialog : DialogFragment(), BackPressable {
} }
private fun showKeyboardSearch() { private fun showKeyboardSearch() {
if (toolbar_search_edit_text.requestFocus()) { if (searchLayoutBinding.toolbarSearchEditText.requestFocus()) {
inputMethodManager.showSoftInput(toolbar_search_edit_text, InputMethodManager.SHOW_IMPLICIT) inputMethodManager.showSoftInput(searchLayoutBinding.toolbarSearchEditText,
InputMethodManager.SHOW_IMPLICIT)
} }
} }
private fun hideKeyboardSearch() { private fun hideKeyboardSearch() {
inputMethodManager.hideSoftInputFromWindow( inputMethodManager.hideSoftInputFromWindow(searchLayoutBinding.toolbarSearchEditText.windowToken,
toolbar_search_edit_text.windowToken, InputMethodManager.RESULT_UNCHANGED_SHOWN)
InputMethodManager.RESULT_UNCHANGED_SHOWN searchLayoutBinding.toolbarSearchEditText.clearFocus()
)
toolbar_search_edit_text.clearFocus()
} }
private fun showKeyboard() { private fun showKeyboard() {
if (group_name_input.requestFocus()) { if (feedGroupCreateBinding.groupNameInput.requestFocus()) {
inputMethodManager.showSoftInput(group_name_input, InputMethodManager.SHOW_IMPLICIT) inputMethodManager.showSoftInput(feedGroupCreateBinding.groupNameInput,
InputMethodManager.SHOW_IMPLICIT)
} }
} }
private fun hideKeyboard() { private fun hideKeyboard() {
inputMethodManager.hideSoftInputFromWindow( inputMethodManager.hideSoftInputFromWindow(feedGroupCreateBinding.groupNameInput.windowToken,
group_name_input.windowToken, InputMethodManager.RESULT_UNCHANGED_SHOWN)
InputMethodManager.RESULT_UNCHANGED_SHOWN feedGroupCreateBinding.groupNameInput.clearFocus()
)
group_name_input.clearFocus()
} }
private fun disableInput() { private fun disableInput() {
delete_button?.isEnabled = false _feedGroupCreateBinding?.deleteButton?.isEnabled = false
confirm_button?.isEnabled = false _feedGroupCreateBinding?.confirmButton?.isEnabled = false
cancel_button?.isEnabled = false _feedGroupCreateBinding?.cancelButton?.isEnabled = false
isCancelable = false isCancelable = false
hideKeyboard() hideKeyboard()