fix various bugs when creating tab with multiple hashtags (#1925)

This commit is contained in:
Konrad Pozniak 2020-09-02 12:28:13 +02:00 committed by Alibek Omarov
parent dceda501ad
commit 2f137d8c92
2 changed files with 26 additions and 29 deletions

View File

@ -78,7 +78,7 @@ class TabPreferenceActivity : BaseActivity(), Injectable, ItemInteractionListene
setDisplayShowHomeEnabled(true) setDisplayShowHomeEnabled(true)
} }
currentTabs = (accountManager.activeAccount?.tabPreferences ?: emptyList()).toMutableList() currentTabs = accountManager.activeAccount?.tabPreferences.orEmpty().toMutableList()
currentTabsAdapter = TabAdapter(currentTabs, false, this, currentTabs.size <= MIN_TAB_COUNT) currentTabsAdapter = TabAdapter(currentTabs, false, this, currentTabs.size <= MIN_TAB_COUNT)
currentTabsRecyclerView.adapter = currentTabsAdapter currentTabsRecyclerView.adapter = currentTabsAdapter
currentTabsRecyclerView.layoutManager = LinearLayoutManager(this) currentTabsRecyclerView.layoutManager = LinearLayoutManager(this)
@ -175,20 +175,20 @@ class TabPreferenceActivity : BaseActivity(), Injectable, ItemInteractionListene
saveTabs() saveTabs()
} }
override fun onActionChipClicked(tab: TabData) { override fun onActionChipClicked(tab: TabData, tabPosition: Int) {
showAddHashtagDialog(tab) showAddHashtagDialog(tab, tabPosition)
} }
override fun onChipClicked(tab: TabData, chipPosition: Int) { override fun onChipClicked(tab: TabData, tabPosition: Int, chipPosition: Int) {
val newArguments = tab.arguments.filterIndexed { i, _ -> i != chipPosition } val newArguments = tab.arguments.filterIndexed { i, _ -> i != chipPosition }
val newTab = tab.copy(arguments = newArguments) val newTab = tab.copy(arguments = newArguments)
val position = currentTabs.indexOf(tab) currentTabs[tabPosition] = newTab
currentTabs[position] = newTab saveTabs()
currentTabsAdapter.notifyItemChanged(position) currentTabsAdapter.notifyItemChanged(tabPosition)
} }
private fun showAddHashtagDialog(tab: TabData? = null) { private fun showAddHashtagDialog(tab: TabData? = null, tabPosition: Int = 0) {
val frameLayout = FrameLayout(this) val frameLayout = FrameLayout(this)
val padding = Utils.dpToPx(this, 8) val padding = Utils.dpToPx(this, 8)
@ -211,10 +211,9 @@ class TabPreferenceActivity : BaseActivity(), Injectable, ItemInteractionListene
currentTabsAdapter.notifyItemInserted(currentTabs.size - 1) currentTabsAdapter.notifyItemInserted(currentTabs.size - 1)
} else { } else {
val newTab = tab.copy(arguments = tab.arguments + input) val newTab = tab.copy(arguments = tab.arguments + input)
val position = currentTabs.indexOf(tab) currentTabs[tabPosition] = newTab
currentTabs[position] = newTab
currentTabsAdapter.notifyItemChanged(position) currentTabsAdapter.notifyItemChanged(tabPosition)
} }
updateAvailableTabs() updateAvailableTabs()

View File

@ -36,8 +36,8 @@ interface ItemInteractionListener {
fun onTabRemoved(position: Int) fun onTabRemoved(position: Int)
fun onStartDelete(viewHolder: RecyclerView.ViewHolder) fun onStartDelete(viewHolder: RecyclerView.ViewHolder)
fun onStartDrag(viewHolder: RecyclerView.ViewHolder) fun onStartDrag(viewHolder: RecyclerView.ViewHolder)
fun onActionChipClicked(tab: TabData) fun onActionChipClicked(tab: TabData, tabPosition: Int)
fun onChipClicked(tab: TabData, chipPosition: Int) fun onChipClicked(tab: TabData, tabPosition: Int, chipPosition: Int)
} }
class TabAdapter(private var data: List<TabData>, class TabAdapter(private var data: List<TabData>,
@ -62,16 +62,17 @@ class TabAdapter(private var data: List<TabData>,
override fun onBindViewHolder(holder: ViewHolder, position: Int) { override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val context = holder.itemView.context val context = holder.itemView.context
if (!small && data[position].id == LIST) { val tab = data[position]
holder.itemView.textView.text = data[position].arguments.getOrNull(1).orEmpty() if (!small && tab.id == LIST) {
holder.itemView.textView.text = tab.arguments.getOrNull(1).orEmpty()
} else { } else {
holder.itemView.textView.setText(data[position].text) holder.itemView.textView.setText(tab.text)
} }
val iconDrawable = ThemeUtils.getTintedDrawable(context, data[position].icon, android.R.attr.textColorSecondary) val iconDrawable = ThemeUtils.getTintedDrawable(context, tab.icon, android.R.attr.textColorSecondary)
holder.itemView.textView.setCompoundDrawablesRelativeWithIntrinsicBounds(iconDrawable, null, null, null) holder.itemView.textView.setCompoundDrawablesRelativeWithIntrinsicBounds(iconDrawable, null, null, null)
if (small) { if (small) {
holder.itemView.textView.setOnClickListener { holder.itemView.textView.setOnClickListener {
listener.onTabAdded(data[position]) listener.onTabAdded(tab)
} }
} }
holder.itemView.imageView?.setOnTouchListener { _, event -> holder.itemView.imageView?.setOnTouchListener { _, event ->
@ -96,7 +97,7 @@ class TabAdapter(private var data: List<TabData>,
if (!small) { if (!small) {
if (data[position].id == HASHTAG) { if (tab.id == HASHTAG) {
holder.itemView.chipGroup.show() holder.itemView.chipGroup.show()
/* /*
@ -104,34 +105,33 @@ class TabAdapter(private var data: List<TabData>,
* The other dynamic chips are inserted in front of the actionChip. * The other dynamic chips are inserted in front of the actionChip.
* This code tries to reuse already added chips to reduce the number of Views created. * This code tries to reuse already added chips to reduce the number of Views created.
*/ */
data[position].arguments.forEachIndexed { i, arg -> tab.arguments.forEachIndexed { i, arg ->
val chip = holder.itemView.chipGroup.getChildAt(i).takeUnless { it.id == R.id.actionChip } as Chip? val chip = holder.itemView.chipGroup.getChildAt(i).takeUnless { it.id == R.id.actionChip } as Chip?
?: Chip(context).apply { ?: Chip(context).apply {
text = arg
holder.itemView.chipGroup.addView(this, holder.itemView.chipGroup.size - 1) holder.itemView.chipGroup.addView(this, holder.itemView.chipGroup.size - 1)
} }
chip.text = arg chip.text = arg
if(data[position].arguments.size <= 1) { if(tab.arguments.size <= 1) {
chip.chipIcon = null chip.chipIcon = null
chip.setOnClickListener(null) chip.setOnClickListener(null)
} else { } else {
val cancelIcon = ThemeUtils.getTintedDrawable(context, R.drawable.ic_cancel_24dp, android.R.attr.textColorPrimary) val cancelIcon = ThemeUtils.getTintedDrawable(context, R.drawable.ic_cancel_24dp, android.R.attr.textColorPrimary)
chip.chipIcon = cancelIcon chip.chipIcon = cancelIcon
chip.setOnClickListener { chip.setOnClickListener {
listener.onChipClicked(data[position], i) listener.onChipClicked(tab, holder.adapterPosition, i)
} }
} }
} }
while(holder.itemView.chipGroup.size - 1 > data[position].arguments.size) { while(holder.itemView.chipGroup.size - 1 > tab.arguments.size) {
holder.itemView.chipGroup.removeViewAt(data[position].arguments.size - 1) holder.itemView.chipGroup.removeViewAt(tab.arguments.size)
} }
holder.itemView.actionChip.setOnClickListener { holder.itemView.actionChip.setOnClickListener {
listener.onActionChipClicked(data[position]) listener.onActionChipClicked(tab, holder.adapterPosition)
} }
} else { } else {
@ -140,9 +140,7 @@ class TabAdapter(private var data: List<TabData>,
} }
} }
override fun getItemCount(): Int { override fun getItemCount() = data.size
return data.size
}
fun setRemoveButtonVisible(enabled: Boolean) { fun setRemoveButtonVisible(enabled: Boolean) {
if (removeButtonEnabled != enabled) { if (removeButtonEnabled != enabled) {