2019-04-28 22:43:54 +02:00
|
|
|
|
package org.schabi.newpipe.local.subscription.dialog
|
|
|
|
|
|
|
|
|
|
import android.app.Dialog
|
|
|
|
|
import android.os.Bundle
|
|
|
|
|
import android.os.Parcelable
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.view.ViewGroup
|
|
|
|
|
import android.view.inputmethod.InputMethodManager
|
|
|
|
|
import android.widget.Toast
|
2020-09-13 13:50:29 +02:00
|
|
|
|
import androidx.core.content.getSystemService
|
2020-10-17 12:08:45 +02:00
|
|
|
|
import androidx.core.os.bundleOf
|
2020-10-17 12:24:35 +02:00
|
|
|
|
import androidx.core.view.isGone
|
|
|
|
|
import androidx.core.view.isVisible
|
2020-10-17 14:01:50 +02:00
|
|
|
|
import androidx.core.widget.doOnTextChanged
|
2019-04-28 22:43:54 +02:00
|
|
|
|
import androidx.fragment.app.DialogFragment
|
|
|
|
|
import androidx.lifecycle.Observer
|
2020-04-05 21:25:44 +02:00
|
|
|
|
import androidx.lifecycle.ViewModelProvider
|
2019-04-28 22:43:54 +02:00
|
|
|
|
import androidx.recyclerview.widget.GridLayoutManager
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
2021-09-23 01:08:03 +02:00
|
|
|
|
import com.xwray.groupie.GroupieAdapter
|
2020-04-05 21:25:44 +02:00
|
|
|
|
import com.xwray.groupie.OnItemClickListener
|
2019-04-28 22:43:54 +02:00
|
|
|
|
import com.xwray.groupie.Section
|
|
|
|
|
import icepick.Icepick
|
|
|
|
|
import icepick.State
|
|
|
|
|
import org.schabi.newpipe.R
|
|
|
|
|
import org.schabi.newpipe.database.feed.model.FeedGroupEntity
|
2020-11-03 09:45:43 +01:00
|
|
|
|
import org.schabi.newpipe.databinding.DialogFeedGroupCreateBinding
|
|
|
|
|
import org.schabi.newpipe.databinding.ToolbarSearchLayoutBinding
|
2020-04-05 21:25:44 +02:00
|
|
|
|
import org.schabi.newpipe.fragments.BackPressable
|
2019-04-28 22:43:54 +02:00
|
|
|
|
import org.schabi.newpipe.local.subscription.FeedGroupIcon
|
2020-05-01 20:13:01 +02:00
|
|
|
|
import org.schabi.newpipe.local.subscription.dialog.FeedGroupDialog.ScreenState.DeleteScreen
|
|
|
|
|
import org.schabi.newpipe.local.subscription.dialog.FeedGroupDialog.ScreenState.IconPickerScreen
|
|
|
|
|
import org.schabi.newpipe.local.subscription.dialog.FeedGroupDialog.ScreenState.InitialScreen
|
|
|
|
|
import org.schabi.newpipe.local.subscription.dialog.FeedGroupDialog.ScreenState.SubscriptionsPickerScreen
|
2020-03-06 00:57:00 +01:00
|
|
|
|
import org.schabi.newpipe.local.subscription.dialog.FeedGroupDialogViewModel.DialogEvent.ProcessingEvent
|
|
|
|
|
import org.schabi.newpipe.local.subscription.dialog.FeedGroupDialogViewModel.DialogEvent.SuccessEvent
|
2019-04-28 22:43:54 +02:00
|
|
|
|
import org.schabi.newpipe.local.subscription.item.EmptyPlaceholderItem
|
|
|
|
|
import org.schabi.newpipe.local.subscription.item.PickerIconItem
|
|
|
|
|
import org.schabi.newpipe.local.subscription.item.PickerSubscriptionItem
|
2020-07-21 00:43:49 +02:00
|
|
|
|
import org.schabi.newpipe.util.DeviceUtils
|
2019-04-28 22:43:54 +02:00
|
|
|
|
import org.schabi.newpipe.util.ThemeHelper
|
2020-10-31 21:55:45 +01:00
|
|
|
|
import java.io.Serializable
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
class FeedGroupDialog : DialogFragment(), BackPressable {
|
2020-11-03 09:45:43 +01:00
|
|
|
|
private var _feedGroupCreateBinding: DialogFeedGroupCreateBinding? = null
|
|
|
|
|
private val feedGroupCreateBinding get() = _feedGroupCreateBinding!!
|
|
|
|
|
|
|
|
|
|
private var _searchLayoutBinding: ToolbarSearchLayoutBinding? = null
|
|
|
|
|
private val searchLayoutBinding get() = _searchLayoutBinding!!
|
|
|
|
|
|
2019-04-28 22:43:54 +02:00
|
|
|
|
private lateinit var viewModel: FeedGroupDialogViewModel
|
|
|
|
|
private var groupId: Long = NO_GROUP_SELECTED
|
|
|
|
|
private var groupIcon: FeedGroupIcon? = null
|
2020-02-26 03:01:23 +01:00
|
|
|
|
private var groupSortOrder: Long = -1
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
|
|
sealed class ScreenState : Serializable {
|
|
|
|
|
object InitialScreen : ScreenState()
|
2020-03-06 00:57:00 +01:00
|
|
|
|
object IconPickerScreen : ScreenState()
|
|
|
|
|
object SubscriptionsPickerScreen : ScreenState()
|
2020-03-06 00:27:00 +01:00
|
|
|
|
object DeleteScreen : ScreenState()
|
2019-04-28 22:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
@State @JvmField var selectedIcon: FeedGroupIcon? = null
|
|
|
|
|
@State @JvmField var selectedSubscriptions: HashSet<Long> = HashSet()
|
|
|
|
|
@State @JvmField var wasSubscriptionSelectionChanged: Boolean = false
|
|
|
|
|
@State @JvmField var currentScreen: ScreenState = InitialScreen
|
|
|
|
|
|
|
|
|
|
@State @JvmField var subscriptionsListState: Parcelable? = null
|
|
|
|
|
@State @JvmField var iconsListState: Parcelable? = null
|
|
|
|
|
@State @JvmField var wasSearchSubscriptionsVisible = false
|
|
|
|
|
@State @JvmField var subscriptionsCurrentSearchQuery = ""
|
2020-04-09 18:07:02 +02:00
|
|
|
|
@State @JvmField var subscriptionsShowOnlyUngrouped = false
|
2020-04-05 21:25:44 +02:00
|
|
|
|
|
|
|
|
|
private val subscriptionMainSection = Section()
|
|
|
|
|
private val subscriptionEmptyFooter = Section()
|
2021-09-23 01:08:03 +02:00
|
|
|
|
private lateinit var subscriptionGroupAdapter: GroupieAdapter
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
|
Icepick.restoreInstanceState(this, savedInstanceState)
|
|
|
|
|
|
|
|
|
|
setStyle(STYLE_NO_TITLE, ThemeHelper.getMinWidthDialogTheme(requireContext()))
|
|
|
|
|
groupId = arguments?.getLong(KEY_GROUP_ID, NO_GROUP_SELECTED) ?: NO_GROUP_SELECTED
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
override fun onCreateView(
|
|
|
|
|
inflater: LayoutInflater,
|
|
|
|
|
container: ViewGroup?,
|
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
|
): View? {
|
2019-04-28 22:43:54 +02:00
|
|
|
|
return inflater.inflate(R.layout.dialog_feed_group_create, container)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
|
|
|
|
|
return object : Dialog(requireActivity(), theme) {
|
|
|
|
|
override fun onBackPressed() {
|
2020-04-05 21:25:44 +02:00
|
|
|
|
if (!this@FeedGroupDialog.onBackPressed()) {
|
2019-04-28 22:43:54 +02:00
|
|
|
|
super.onBackPressed()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
override fun onPause() {
|
|
|
|
|
super.onPause()
|
|
|
|
|
|
|
|
|
|
wasSearchSubscriptionsVisible = isSearchVisible()
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 22:43:54 +02:00
|
|
|
|
override fun onSaveInstanceState(outState: Bundle) {
|
|
|
|
|
super.onSaveInstanceState(outState)
|
|
|
|
|
|
2020-11-03 09:45:43 +01:00
|
|
|
|
iconsListState = feedGroupCreateBinding.iconSelector.layoutManager?.onSaveInstanceState()
|
|
|
|
|
subscriptionsListState = feedGroupCreateBinding.subscriptionsSelectorList.layoutManager?.onSaveInstanceState()
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
|
|
Icepick.saveInstanceState(this, outState)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
2020-11-03 09:45:43 +01:00
|
|
|
|
_feedGroupCreateBinding = DialogFeedGroupCreateBinding.bind(view)
|
|
|
|
|
_searchLayoutBinding = feedGroupCreateBinding.subscriptionsHeaderSearchContainer
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-10-31 21:55:45 +01:00
|
|
|
|
viewModel = ViewModelProvider(
|
|
|
|
|
this,
|
|
|
|
|
FeedGroupDialogViewModel.Factory(
|
|
|
|
|
requireContext(),
|
|
|
|
|
groupId, subscriptionsCurrentSearchQuery, subscriptionsShowOnlyUngrouped
|
|
|
|
|
)
|
2020-04-05 21:25:44 +02:00
|
|
|
|
).get(FeedGroupDialogViewModel::class.java)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
|
|
viewModel.groupLiveData.observe(viewLifecycleOwner, Observer(::handleGroup))
|
2021-08-30 16:18:40 +02:00
|
|
|
|
viewModel.subscriptionsLiveData.observe(viewLifecycleOwner) {
|
|
|
|
|
setupSubscriptionPicker(it.first, it.second)
|
|
|
|
|
}
|
|
|
|
|
viewModel.dialogEventLiveData.observe(viewLifecycleOwner) {
|
|
|
|
|
when (it) {
|
|
|
|
|
ProcessingEvent -> disableInput()
|
|
|
|
|
SuccessEvent -> dismiss()
|
2020-10-31 21:55:45 +01:00
|
|
|
|
}
|
2021-08-30 16:18:40 +02:00
|
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2021-09-23 01:08:03 +02:00
|
|
|
|
subscriptionGroupAdapter = GroupieAdapter().apply {
|
2020-04-05 21:25:44 +02:00
|
|
|
|
add(subscriptionMainSection)
|
|
|
|
|
add(subscriptionEmptyFooter)
|
|
|
|
|
spanCount = 4
|
|
|
|
|
}
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.subscriptionsSelectorList.apply {
|
2020-04-05 21:25:44 +02:00
|
|
|
|
// Disable animations, too distracting.
|
|
|
|
|
itemAnimator = null
|
|
|
|
|
adapter = subscriptionGroupAdapter
|
2020-10-31 21:55:45 +01:00
|
|
|
|
layoutManager = GridLayoutManager(
|
|
|
|
|
requireContext(), subscriptionGroupAdapter.spanCount,
|
|
|
|
|
RecyclerView.VERTICAL, false
|
|
|
|
|
).apply {
|
2020-04-05 21:25:44 +02:00
|
|
|
|
spanSizeLookup = subscriptionGroupAdapter.spanSizeLookup
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 22:43:54 +02:00
|
|
|
|
setupIconPicker()
|
2020-03-06 00:57:00 +01:00
|
|
|
|
setupListeners()
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-03-06 00:57:00 +01:00
|
|
|
|
showScreen(currentScreen)
|
2020-04-05 21:25:44 +02:00
|
|
|
|
|
|
|
|
|
if (currentScreen == SubscriptionsPickerScreen && wasSearchSubscriptionsVisible) {
|
|
|
|
|
showSearch()
|
|
|
|
|
} else if (currentScreen == InitialScreen && groupId == NO_GROUP_SELECTED) {
|
|
|
|
|
showKeyboard()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onDestroyView() {
|
|
|
|
|
super.onDestroyView()
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.subscriptionsSelectorList.adapter = null
|
|
|
|
|
feedGroupCreateBinding.iconSelector.adapter = null
|
|
|
|
|
|
|
|
|
|
_feedGroupCreateBinding = null
|
|
|
|
|
_searchLayoutBinding = null
|
2020-03-06 00:57:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
/*///////////////////////////////////////////////////////////////////////////
|
2020-03-06 00:57:00 +01:00
|
|
|
|
// Setup
|
2020-04-05 21:25:44 +02:00
|
|
|
|
////////////////////////////////////////////////////////////////////////// */
|
|
|
|
|
|
|
|
|
|
override fun onBackPressed(): Boolean {
|
|
|
|
|
if (currentScreen is SubscriptionsPickerScreen && isSearchVisible()) {
|
|
|
|
|
hideSearch()
|
|
|
|
|
return true
|
|
|
|
|
} else if (currentScreen !is InitialScreen) {
|
|
|
|
|
showScreen(InitialScreen)
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
}
|
2020-03-06 00:57:00 +01:00
|
|
|
|
|
|
|
|
|
private fun setupListeners() {
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.deleteButton.setOnClickListener { showScreen(DeleteScreen) }
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.cancelButton.setOnClickListener {
|
2020-03-06 00:57:00 +01:00
|
|
|
|
when (currentScreen) {
|
|
|
|
|
InitialScreen -> dismiss()
|
|
|
|
|
else -> showScreen(InitialScreen)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.groupNameInputContainer.error = null
|
|
|
|
|
feedGroupCreateBinding.groupNameInput.doOnTextChanged { text, _, _, _ ->
|
|
|
|
|
if (feedGroupCreateBinding.groupNameInputContainer.isErrorEnabled && !text.isNullOrBlank()) {
|
|
|
|
|
feedGroupCreateBinding.groupNameInputContainer.error = null
|
2019-04-28 22:43:54 +02:00
|
|
|
|
}
|
2020-10-17 14:01:50 +02:00
|
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.confirmButton.setOnClickListener { handlePositiveButton() }
|
2020-04-05 21:25:44 +02:00
|
|
|
|
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.selectChannelButton.setOnClickListener {
|
|
|
|
|
feedGroupCreateBinding.subscriptionsSelectorList.scrollToPosition(0)
|
2020-04-05 21:25:44 +02:00
|
|
|
|
showScreen(SubscriptionsPickerScreen)
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 09:45:43 +01:00
|
|
|
|
val headerMenu = feedGroupCreateBinding.subscriptionsHeaderToolbar.menu
|
2020-04-05 21:25:44 +02:00
|
|
|
|
requireActivity().menuInflater.inflate(R.menu.menu_feed_group_dialog, headerMenu)
|
|
|
|
|
|
|
|
|
|
headerMenu.findItem(R.id.action_search).setOnMenuItemClickListener {
|
|
|
|
|
showSearch()
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-09 18:07:02 +02:00
|
|
|
|
headerMenu.findItem(R.id.feed_group_toggle_show_only_ungrouped_subscriptions).apply {
|
|
|
|
|
isChecked = subscriptionsShowOnlyUngrouped
|
|
|
|
|
setOnMenuItemClickListener {
|
|
|
|
|
subscriptionsShowOnlyUngrouped = !subscriptionsShowOnlyUngrouped
|
|
|
|
|
it.isChecked = subscriptionsShowOnlyUngrouped
|
|
|
|
|
viewModel.toggleShowOnlyUngrouped(subscriptionsShowOnlyUngrouped)
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 09:45:43 +01:00
|
|
|
|
searchLayoutBinding.toolbarSearchClear.setOnClickListener {
|
|
|
|
|
if (searchLayoutBinding.toolbarSearchEditText.text.isNullOrEmpty()) {
|
2020-04-05 21:25:44 +02:00
|
|
|
|
hideSearch()
|
|
|
|
|
return@setOnClickListener
|
|
|
|
|
}
|
|
|
|
|
resetSearch()
|
|
|
|
|
showKeyboardSearch()
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 09:45:43 +01:00
|
|
|
|
searchLayoutBinding.toolbarSearchEditText.setOnClickListener {
|
2020-07-21 00:43:49 +02:00
|
|
|
|
if (DeviceUtils.isTv(context)) {
|
2020-04-05 21:25:44 +02:00
|
|
|
|
showKeyboardSearch()
|
2020-03-06 00:57:00 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-05 21:25:44 +02:00
|
|
|
|
|
2020-11-03 09:45:43 +01:00
|
|
|
|
searchLayoutBinding.toolbarSearchEditText.doOnTextChanged { _, _, _, _ ->
|
|
|
|
|
val newQuery: String = searchLayoutBinding.toolbarSearchEditText.text.toString()
|
2020-10-17 14:01:50 +02:00
|
|
|
|
subscriptionsCurrentSearchQuery = newQuery
|
|
|
|
|
viewModel.filterSubscriptionsBy(newQuery)
|
|
|
|
|
}
|
2020-04-05 21:25:44 +02:00
|
|
|
|
|
2020-08-27 22:56:58 +02:00
|
|
|
|
subscriptionGroupAdapter.setOnItemClickListener(subscriptionPickerItemListener)
|
2020-04-05 21:25:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun handlePositiveButton() = when {
|
|
|
|
|
currentScreen is InitialScreen -> handlePositiveButtonInitialScreen()
|
|
|
|
|
currentScreen is DeleteScreen -> viewModel.deleteGroup()
|
|
|
|
|
currentScreen is SubscriptionsPickerScreen && isSearchVisible() -> hideSearch()
|
|
|
|
|
else -> showScreen(InitialScreen)
|
2020-03-06 00:57:00 +01:00
|
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-03-06 00:57:00 +01:00
|
|
|
|
private fun handlePositiveButtonInitialScreen() {
|
2020-11-03 09:45:43 +01:00
|
|
|
|
val name = feedGroupCreateBinding.groupNameInput.text.toString().trim()
|
2020-03-06 00:57:00 +01:00
|
|
|
|
val icon = selectedIcon ?: groupIcon ?: FeedGroupIcon.ALL
|
|
|
|
|
|
|
|
|
|
if (name.isBlank()) {
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.groupNameInputContainer.error = getString(R.string.feed_group_dialog_empty_name)
|
|
|
|
|
feedGroupCreateBinding.groupNameInput.text = null
|
|
|
|
|
feedGroupCreateBinding.groupNameInput.requestFocus()
|
2020-03-06 00:57:00 +01:00
|
|
|
|
return
|
|
|
|
|
} else {
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.groupNameInputContainer.error = null
|
2020-03-06 00:57:00 +01:00
|
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-03-06 00:57:00 +01:00
|
|
|
|
if (selectedSubscriptions.isEmpty()) {
|
|
|
|
|
Toast.makeText(requireContext(), getString(R.string.feed_group_dialog_empty_selection), Toast.LENGTH_SHORT).show()
|
|
|
|
|
return
|
2019-04-28 22:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 00:57:00 +01:00
|
|
|
|
when (groupId) {
|
|
|
|
|
NO_GROUP_SELECTED -> viewModel.createGroup(name, icon, selectedSubscriptions)
|
|
|
|
|
else -> viewModel.updateGroup(name, icon, selectedSubscriptions, groupSortOrder)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun handleGroup(feedGroupEntity: FeedGroupEntity? = null) {
|
|
|
|
|
val icon = feedGroupEntity?.icon ?: FeedGroupIcon.ALL
|
|
|
|
|
val name = feedGroupEntity?.name ?: ""
|
|
|
|
|
groupIcon = feedGroupEntity?.icon
|
2020-02-26 03:01:23 +01:00
|
|
|
|
groupSortOrder = feedGroupEntity?.sortOrder ?: -1
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
val feedGroupIcon = if (selectedIcon == null) icon else selectedIcon!!
|
2021-03-27 15:45:49 +01:00
|
|
|
|
feedGroupCreateBinding.iconPreview.setImageResource(feedGroupIcon.getDrawableRes())
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-11-03 09:45:43 +01:00
|
|
|
|
if (feedGroupCreateBinding.groupNameInput.text.isNullOrBlank()) {
|
|
|
|
|
feedGroupCreateBinding.groupNameInput.setText(name)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
private val subscriptionPickerItemListener = OnItemClickListener { item, view ->
|
|
|
|
|
if (item is PickerSubscriptionItem) {
|
|
|
|
|
val subscriptionId = item.subscriptionEntity.uid
|
|
|
|
|
wasSubscriptionSelectionChanged = true
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
val isSelected = if (this.selectedSubscriptions.contains(subscriptionId)) {
|
|
|
|
|
this.selectedSubscriptions.remove(subscriptionId)
|
|
|
|
|
false
|
|
|
|
|
} else {
|
|
|
|
|
this.selectedSubscriptions.add(subscriptionId)
|
|
|
|
|
true
|
|
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
item.updateSelected(view, isSelected)
|
|
|
|
|
updateSubscriptionSelectedCount()
|
2019-04-28 22:43:54 +02:00
|
|
|
|
}
|
2020-04-05 21:25:44 +02:00
|
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
private fun setupSubscriptionPicker(
|
|
|
|
|
subscriptions: List<PickerSubscriptionItem>,
|
|
|
|
|
selectedSubscriptions: Set<Long>
|
|
|
|
|
) {
|
|
|
|
|
if (!wasSubscriptionSelectionChanged) {
|
|
|
|
|
this.selectedSubscriptions.addAll(selectedSubscriptions)
|
|
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
updateSubscriptionSelectedCount()
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
if (subscriptions.isEmpty()) {
|
|
|
|
|
subscriptionEmptyFooter.clear()
|
|
|
|
|
subscriptionEmptyFooter.add(EmptyPlaceholderItem())
|
|
|
|
|
} else {
|
|
|
|
|
subscriptionEmptyFooter.clear()
|
2019-04-28 22:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
subscriptions.forEach {
|
|
|
|
|
it.isSelected = this@FeedGroupDialog.selectedSubscriptions
|
|
|
|
|
.contains(it.subscriptionEntity.uid)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
subscriptionMainSection.update(subscriptions, false)
|
|
|
|
|
|
|
|
|
|
if (subscriptionsListState != null) {
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.subscriptionsSelectorList.layoutManager?.onRestoreInstanceState(subscriptionsListState)
|
2020-04-05 21:25:44 +02:00
|
|
|
|
subscriptionsListState = null
|
|
|
|
|
} else {
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.subscriptionsSelectorList.scrollToPosition(0)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
private fun updateSubscriptionSelectedCount() {
|
|
|
|
|
val selectedCount = this.selectedSubscriptions.size
|
|
|
|
|
val selectedCountText = resources.getQuantityString(
|
|
|
|
|
R.plurals.feed_group_dialog_selection_count,
|
2021-01-15 02:15:07 +01:00
|
|
|
|
selectedCount, selectedCount
|
|
|
|
|
)
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.selectedSubscriptionCountView.text = selectedCountText
|
|
|
|
|
feedGroupCreateBinding.subscriptionsHeaderInfo.text = selectedCountText
|
2020-04-05 21:25:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 22:43:54 +02:00
|
|
|
|
private fun setupIconPicker() {
|
2021-09-23 01:08:03 +02:00
|
|
|
|
val groupAdapter = GroupieAdapter()
|
2021-04-27 23:28:36 +02:00
|
|
|
|
groupAdapter.addAll(FeedGroupIcon.values().map { PickerIconItem(it) })
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.iconSelector.apply {
|
2019-04-28 22:43:54 +02:00
|
|
|
|
layoutManager = GridLayoutManager(requireContext(), 7, RecyclerView.VERTICAL, false)
|
|
|
|
|
adapter = groupAdapter
|
|
|
|
|
|
|
|
|
|
if (iconsListState != null) {
|
|
|
|
|
layoutManager?.onRestoreInstanceState(iconsListState)
|
|
|
|
|
iconsListState = null
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groupAdapter.setOnItemClickListener { item, _ ->
|
|
|
|
|
when (item) {
|
|
|
|
|
is PickerIconItem -> {
|
|
|
|
|
selectedIcon = item.icon
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.iconPreview.setImageResource(item.iconRes)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-03-06 00:57:00 +01:00
|
|
|
|
showScreen(InitialScreen)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.iconPreview.setOnClickListener {
|
|
|
|
|
feedGroupCreateBinding.iconSelector.scrollToPosition(0)
|
2020-03-06 00:57:00 +01:00
|
|
|
|
showScreen(IconPickerScreen)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (groupId == NO_GROUP_SELECTED) {
|
|
|
|
|
val icon = selectedIcon ?: FeedGroupIcon.ALL
|
2021-03-27 15:45:49 +01:00
|
|
|
|
feedGroupCreateBinding.iconPreview.setImageResource(icon.getDrawableRes())
|
2019-04-28 22:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
/*///////////////////////////////////////////////////////////////////////////
|
2019-04-28 22:43:54 +02:00
|
|
|
|
// Screen Selector
|
2020-04-05 21:25:44 +02:00
|
|
|
|
////////////////////////////////////////////////////////////////////////// */
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-03-06 00:57:00 +01:00
|
|
|
|
private fun showScreen(screen: ScreenState) {
|
|
|
|
|
currentScreen = screen
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.optionsRoot.onlyVisibleIn(InitialScreen)
|
|
|
|
|
feedGroupCreateBinding.iconSelector.onlyVisibleIn(IconPickerScreen)
|
|
|
|
|
feedGroupCreateBinding.subscriptionsSelector.onlyVisibleIn(SubscriptionsPickerScreen)
|
|
|
|
|
feedGroupCreateBinding.deleteScreenMessage.onlyVisibleIn(DeleteScreen)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.separator.onlyVisibleIn(SubscriptionsPickerScreen, IconPickerScreen)
|
|
|
|
|
feedGroupCreateBinding.cancelButton.onlyVisibleIn(InitialScreen, DeleteScreen)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2021-01-15 02:15:07 +01:00
|
|
|
|
feedGroupCreateBinding.confirmButton.setText(
|
|
|
|
|
when {
|
|
|
|
|
currentScreen == InitialScreen && groupId == NO_GROUP_SELECTED -> R.string.create
|
2021-08-30 16:18:40 +02:00
|
|
|
|
else -> R.string.ok
|
2021-01-15 02:15:07 +01:00
|
|
|
|
}
|
|
|
|
|
)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.deleteButton.isGone = currentScreen != InitialScreen || groupId == NO_GROUP_SELECTED
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
hideKeyboard()
|
|
|
|
|
hideSearch()
|
2019-04-28 22:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 00:57:00 +01:00
|
|
|
|
private fun View.onlyVisibleIn(vararg screens: ScreenState) {
|
2020-10-17 12:24:35 +02:00
|
|
|
|
isVisible = currentScreen in screens
|
2020-03-06 00:27:00 +01:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-05 21:25:44 +02:00
|
|
|
|
/*///////////////////////////////////////////////////////////////////////////
|
2019-04-28 22:43:54 +02:00
|
|
|
|
// Utils
|
2020-04-05 21:25:44 +02:00
|
|
|
|
////////////////////////////////////////////////////////////////////////// */
|
|
|
|
|
|
2020-11-03 09:45:43 +01:00
|
|
|
|
private fun isSearchVisible() = _searchLayoutBinding?.root?.visibility == View.VISIBLE
|
2020-04-05 21:25:44 +02:00
|
|
|
|
|
|
|
|
|
private fun resetSearch() {
|
2020-11-03 09:45:43 +01:00
|
|
|
|
searchLayoutBinding.toolbarSearchEditText.setText("")
|
2020-04-05 21:25:44 +02:00
|
|
|
|
subscriptionsCurrentSearchQuery = ""
|
|
|
|
|
viewModel.clearSubscriptionsFilter()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun hideSearch() {
|
|
|
|
|
resetSearch()
|
2020-11-03 09:45:43 +01:00
|
|
|
|
searchLayoutBinding.root.visibility = View.GONE
|
|
|
|
|
feedGroupCreateBinding.subscriptionsHeaderInfoContainer.visibility = View.VISIBLE
|
|
|
|
|
feedGroupCreateBinding.subscriptionsHeaderToolbar.menu.findItem(R.id.action_search).isVisible = true
|
2020-04-05 21:25:44 +02:00
|
|
|
|
hideKeyboardSearch()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun showSearch() {
|
2020-11-03 09:45:43 +01:00
|
|
|
|
searchLayoutBinding.root.visibility = View.VISIBLE
|
|
|
|
|
feedGroupCreateBinding.subscriptionsHeaderInfoContainer.visibility = View.GONE
|
|
|
|
|
feedGroupCreateBinding.subscriptionsHeaderToolbar.menu.findItem(R.id.action_search).isVisible = false
|
2020-04-05 21:25:44 +02:00
|
|
|
|
showKeyboardSearch()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private val inputMethodManager by lazy {
|
2020-09-13 13:50:29 +02:00
|
|
|
|
requireActivity().getSystemService<InputMethodManager>()!!
|
2020-04-05 21:25:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun showKeyboardSearch() {
|
2020-11-03 09:45:43 +01:00
|
|
|
|
if (searchLayoutBinding.toolbarSearchEditText.requestFocus()) {
|
2021-01-15 02:15:07 +01:00
|
|
|
|
inputMethodManager.showSoftInput(
|
|
|
|
|
searchLayoutBinding.toolbarSearchEditText,
|
|
|
|
|
InputMethodManager.SHOW_IMPLICIT
|
|
|
|
|
)
|
2020-04-05 21:25:44 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun hideKeyboardSearch() {
|
2021-01-15 02:15:07 +01:00
|
|
|
|
inputMethodManager.hideSoftInputFromWindow(
|
|
|
|
|
searchLayoutBinding.toolbarSearchEditText.windowToken,
|
|
|
|
|
InputMethodManager.RESULT_UNCHANGED_SHOWN
|
|
|
|
|
)
|
2020-11-03 09:45:43 +01:00
|
|
|
|
searchLayoutBinding.toolbarSearchEditText.clearFocus()
|
2020-04-05 21:25:44 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun showKeyboard() {
|
2020-11-03 09:45:43 +01:00
|
|
|
|
if (feedGroupCreateBinding.groupNameInput.requestFocus()) {
|
2021-01-15 02:15:07 +01:00
|
|
|
|
inputMethodManager.showSoftInput(
|
|
|
|
|
feedGroupCreateBinding.groupNameInput,
|
|
|
|
|
InputMethodManager.SHOW_IMPLICIT
|
|
|
|
|
)
|
2020-04-05 21:25:44 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-28 22:43:54 +02:00
|
|
|
|
|
|
|
|
|
private fun hideKeyboard() {
|
2021-01-15 02:15:07 +01:00
|
|
|
|
inputMethodManager.hideSoftInputFromWindow(
|
|
|
|
|
feedGroupCreateBinding.groupNameInput.windowToken,
|
|
|
|
|
InputMethodManager.RESULT_UNCHANGED_SHOWN
|
|
|
|
|
)
|
2020-11-03 09:45:43 +01:00
|
|
|
|
feedGroupCreateBinding.groupNameInput.clearFocus()
|
2019-04-28 22:43:54 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-05 23:37:00 +01:00
|
|
|
|
private fun disableInput() {
|
2020-11-03 09:45:43 +01:00
|
|
|
|
_feedGroupCreateBinding?.deleteButton?.isEnabled = false
|
|
|
|
|
_feedGroupCreateBinding?.confirmButton?.isEnabled = false
|
|
|
|
|
_feedGroupCreateBinding?.cancelButton?.isEnabled = false
|
2020-03-05 23:37:00 +01:00
|
|
|
|
isCancelable = false
|
|
|
|
|
|
|
|
|
|
hideKeyboard()
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 22:43:54 +02:00
|
|
|
|
companion object {
|
|
|
|
|
private const val KEY_GROUP_ID = "KEY_GROUP_ID"
|
|
|
|
|
private const val NO_GROUP_SELECTED = -1L
|
|
|
|
|
|
|
|
|
|
fun newInstance(groupId: Long = NO_GROUP_SELECTED): FeedGroupDialog {
|
|
|
|
|
val dialog = FeedGroupDialog()
|
2020-10-17 12:08:45 +02:00
|
|
|
|
dialog.arguments = bundleOf(KEY_GROUP_ID to groupId)
|
2019-04-28 22:43:54 +02:00
|
|
|
|
return dialog
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-05-01 20:13:01 +02:00
|
|
|
|
}
|