From 67225dd4c0dc65081c2c670fadabc1c8eb5fb16d Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Wed, 2 Sep 2020 12:28:13 +0200 Subject: [PATCH] fix various bugs when creating tab with multiple hashtags (#1925) --- .../tusky/TabPreferenceActivity.kt | 21 ++++++------ .../keylesspalace/tusky/adapter/TabAdapter.kt | 34 +++++++++---------- 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/TabPreferenceActivity.kt b/app/src/main/java/com/keylesspalace/tusky/TabPreferenceActivity.kt index ad622ea79..66c053617 100644 --- a/app/src/main/java/com/keylesspalace/tusky/TabPreferenceActivity.kt +++ b/app/src/main/java/com/keylesspalace/tusky/TabPreferenceActivity.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() diff --git a/app/src/main/java/com/keylesspalace/tusky/adapter/TabAdapter.kt b/app/src/main/java/com/keylesspalace/tusky/adapter/TabAdapter.kt index 007ba5465..cd877a15e 100644 --- a/app/src/main/java/com/keylesspalace/tusky/adapter/TabAdapter.kt +++ b/app/src/main/java/com/keylesspalace/tusky/adapter/TabAdapter.kt @@ -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, @@ -62,16 +62,17 @@ class TabAdapter(private var data: List, 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, 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, * 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, } } - override fun getItemCount(): Int { - return data.size - } + override fun getItemCount() = data.size fun setRemoveButtonVisible(enabled: Boolean) { if (removeButtonEnabled != enabled) {