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

main
Konrad Pozniak 4 years ago committed by Alibek Omarov
parent ac00c62e5e
commit 65edac9195
  1. 21
      app/src/main/java/com/keylesspalace/tusky/TabPreferenceActivity.kt
  2. 34
      app/src/main/java/com/keylesspalace/tusky/adapter/TabAdapter.kt

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

@ -36,8 +36,8 @@ interface ItemInteractionListener {
fun onTabRemoved(position: Int)
fun onStartDelete(viewHolder: RecyclerView.ViewHolder)
fun onStartDrag(viewHolder: RecyclerView.ViewHolder)
fun onActionChipClicked(tab: TabData)
fun onChipClicked(tab: TabData, chipPosition: Int)
fun onActionChipClicked(tab: TabData, tabPosition: Int)
fun onChipClicked(tab: TabData, tabPosition: Int, chipPosition: Int)
}
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) {
val context = holder.itemView.context
if (!small && data[position].id == LIST) {
holder.itemView.textView.text = data[position].arguments.getOrNull(1).orEmpty()
val tab = data[position]
if (!small && tab.id == LIST) {
holder.itemView.textView.text = tab.arguments.getOrNull(1).orEmpty()
} 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)
if (small) {
holder.itemView.textView.setOnClickListener {
listener.onTabAdded(data[position])
listener.onTabAdded(tab)
}
}
holder.itemView.imageView?.setOnTouchListener { _, event ->
@ -96,7 +97,7 @@ class TabAdapter(private var data: List<TabData>,
if (!small) {
if (data[position].id == HASHTAG) {
if (tab.id == HASHTAG) {
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.
* 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?
?: Chip(context).apply {
text = arg
holder.itemView.chipGroup.addView(this, holder.itemView.chipGroup.size - 1)
}
chip.text = arg
if(data[position].arguments.size <= 1) {
if(tab.arguments.size <= 1) {
chip.chipIcon = null
chip.setOnClickListener(null)
} else {
val cancelIcon = ThemeUtils.getTintedDrawable(context, R.drawable.ic_cancel_24dp, android.R.attr.textColorPrimary)
chip.chipIcon = cancelIcon
chip.setOnClickListener {
listener.onChipClicked(data[position], i)
listener.onChipClicked(tab, holder.adapterPosition, i)
}
}
}
while(holder.itemView.chipGroup.size - 1 > data[position].arguments.size) {
holder.itemView.chipGroup.removeViewAt(data[position].arguments.size - 1)
while(holder.itemView.chipGroup.size - 1 > tab.arguments.size) {
holder.itemView.chipGroup.removeViewAt(tab.arguments.size)
}
holder.itemView.actionChip.setOnClickListener {
listener.onActionChipClicked(data[position])
listener.onActionChipClicked(tab, holder.adapterPosition)
}
} else {
@ -140,9 +140,7 @@ class TabAdapter(private var data: List<TabData>,
}
}
override fun getItemCount(): Int {
return data.size
}
override fun getItemCount() = data.size
fun setRemoveButtonVisible(enabled: Boolean) {
if (removeButtonEnabled != enabled) {

Loading…
Cancel
Save