Commit 6dbccfca by Paktalin

Enables/disables popup menu according to the tag selection

parent 1473c257
......@@ -9,6 +9,7 @@ import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.selection.Selection
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.ui.recycler_view.VocabularyAdapter
import com.paktalin.vocabularynotebook.ui.recycler_view.selection_tracker.MySelectionTracker
......@@ -120,17 +121,18 @@ class MainActivity : AppCompatActivity() {
private fun setUpSelectionTracker() {
vocabularyAdapter.selectionTracker = MySelectionTracker().apply {
init(recyclerView)
addObserver { selection ->
run {
if (!selection.isEmpty) {
// TODO disable popup menu
Log.d(TAG, "selection ${selection.toList()}")
visible(tagLayout)
} else {
gone(tagLayout)
}
}
}
addObserver { selection -> whenSelectionChanges(selection) }
}
}
private fun whenSelectionChanges(selection: Selection<Long>) {
if (!selection.isEmpty) {
vocabularyAdapter.showPopupMenu = false
Log.d(TAG, "selection ${selection.toList()}")
visible(tagLayout)
} else {
vocabularyAdapter.showPopupMenu = true
gone(tagLayout)
}
}
......
......@@ -24,6 +24,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
private lateinit var recyclerView: RecyclerView
private lateinit var viewHolder: ViewHolder
lateinit var selectionTracker: MySelectionTracker
var showPopupMenu = true
init {
setHasStableIds(true)
......@@ -62,20 +63,22 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
}
private fun showPopupMenu(v: View, position: Int) {
val popup = PopupMenu(mainActivity, v)
popup.menuInflater.inflate(R.menu.word_item_menu, popup.menu)
popup.setOnMenuItemClickListener {
if (it.itemId == R.id.option_delete) {
deleteWord(vocabulary.displayedAt(position), position)
if (showPopupMenu) {
val popup = PopupMenu(mainActivity, v)
popup.menuInflater.inflate(R.menu.word_item_menu, popup.menu)
popup.setOnMenuItemClickListener {
if (it.itemId == R.id.option_delete) {
deleteWord(vocabulary.displayedAt(position), position)
}
if (it.itemId == R.id.option_edit) {
startEditFragment(v, vocabulary.displayedAt(position))
}
true
}
if (it.itemId == R.id.option_edit) {
startEditFragment(v, vocabulary.displayedAt(position))
}
true
// if not edit mode, then start EditFragment
if (mainActivity.supportFragmentManager.findFragmentByTag("edit_fragment") == null)
popup.show()
}
// if not edit mode, then start EditFragment
if (mainActivity.supportFragmentManager.findFragmentByTag("edit_fragment") == null)
popup.show()
}
fun refresh() {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment