Commit c882a289 by Paktalin

Cleaned up selection implementation

parent 0e1ce4af
......@@ -9,12 +9,8 @@ import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.recyclerview.selection.SelectionPredicates
import androidx.recyclerview.selection.SelectionTracker
import androidx.recyclerview.selection.StableIdKeyProvider
import androidx.recyclerview.selection.StorageStrategy
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.ui.recycler_view.MyItemDetailsLookup
import com.paktalin.vocabularynotebook.ui.recycler_view.VocabularyAdapter
import com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
import com.paktalin.vocabularynotebook.utils.*
......@@ -54,8 +50,6 @@ class MainActivity : AppCompatActivity() {
vocabularyAdapter.sort = Sort.BY_WORD
if (item.itemId == R.id.sortByTranslation)
vocabularyAdapter.sort = Sort.BY_TRANSLATION
if (item.itemId == R.id.addTag)
vocabularyAdapter.addTag()
return super.onOptionsItemSelected(item)
}
......@@ -90,24 +84,14 @@ class MainActivity : AppCompatActivity() {
addProgressBar(supportFragmentManager, R.id.container_main)
FirestoreManager().extractVocabularyId({
recyclerView.layoutManager = LockableLayoutManager(this@MainActivity)
recyclerView.adapter = VocabularyAdapter(VocabSet(mutableListOf()), this@MainActivity)
FirestoreManager().extractVocabulary { querySnapshot ->
run {
if (querySnapshot != null && !querySnapshot.isEmpty) {
vocabularyAdapter = VocabularyAdapter(VocabSet.createFromSnapshot(querySnapshot), this@MainActivity)
recyclerView.adapter = vocabularyAdapter
tracker = SelectionTracker.Builder<Long>(
"mySelection",
recyclerView,
StableIdKeyProvider(recyclerView),
MyItemDetailsLookup(recyclerView),
StorageStrategy.createLongStorage()
).withSelectionPredicate(
SelectionPredicates.createSelectAnything()
).build()
(recyclerView.adapter as VocabularyAdapter).tracker = tracker
}
else showToastNoWords()
} else showToastNoWords()
}
}
},
......@@ -137,7 +121,8 @@ class MainActivity : AppCompatActivity() {
hideKeyboard()
try {
FirestoreManager().saveWords(vocabularyAdapter.getModifiedWords())
} catch (ignored: Exception) {}
} catch (ignored: Exception) {
}
}
companion object {
......
......@@ -4,14 +4,11 @@ import android.view.MotionEvent
import androidx.recyclerview.selection.ItemDetailsLookup
import androidx.recyclerview.widget.RecyclerView
class MyItemDetailsLookup(private val recyclerView: RecyclerView) :
ItemDetailsLookup<Long>() {
class MyItemDetailsLookup(private val recyclerView: RecyclerView) : ItemDetailsLookup<Long>() {
override fun getItemDetails(event: MotionEvent): ItemDetails<Long>? {
val view = recyclerView.findChildViewUnder(event.x, event.y)
if (view != null) {
return (recyclerView.getChildViewHolder(view) as ViewHolder)
.getItemDetails()
}
if (view != null)
return (recyclerView.getChildViewHolder(view) as ViewHolder).getItemDetails()
return null
}
}
\ No newline at end of file
package com.paktalin.vocabularynotebook.ui.recycler_view
import androidx.recyclerview.selection.*
import androidx.recyclerview.widget.RecyclerView
fun initializeTracker(recyclerView: RecyclerView, onSelection: (Selection<Long>) -> Unit): SelectionTracker<Long>? {
val tracker = SelectionTracker.Builder<Long>(
"mySelection",
recyclerView,
StableIdKeyProvider(recyclerView),
MyItemDetailsLookup(recyclerView),
StorageStrategy.createLongStorage()
).withSelectionPredicate(
SelectionPredicates.createSelectAnything()
).build()
tracker.addObserver(object : SelectionTracker.SelectionObserver<Long>() {
override fun onSelectionChanged() {
super.onSelectionChanged()
onSelection(tracker?.selection!!)
}
})
return tracker
}
private val TAG = "VN/MyTracker"
\ No newline at end of file
package com.paktalin.vocabularynotebook.ui.recycler_view
import android.graphics.Color
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.selection.ItemDetailsLookup
import androidx.recyclerview.widget.RecyclerView
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.utils.Log
import kotlinx.android.synthetic.main.word_item.view.*
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
override fun onClick(p0: View?) {
Log.d(TAG, "click")
p0?.setBackgroundColor(Color.GREEN)
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val tvWord: TextView = itemView.word
private val tvTranslation: TextView = itemView.translation
val layout: LinearLayout = itemView.layout
fun bind(wordPojo: WordPojo, position: Int, isActivated: Boolean, showPopupMenu: (View, Int) -> Unit) {
tvWord.text = wordPojo.word
tvTranslation.text = wordPojo.translation
// itemView.setOnClickListener { showPopupMenu(itemView, position) }
itemView.setOnClickListener { showPopupMenu(itemView, position) }
itemView.isActivated = isActivated
}
......
......@@ -7,6 +7,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.PopupMenu
import androidx.recyclerview.selection.Selection
import androidx.recyclerview.selection.SelectionTracker
import androidx.recyclerview.widget.RecyclerView
import com.paktalin.vocabularynotebook.R
......@@ -15,16 +16,19 @@ import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.ui.fragments.EditWordFragment
import com.paktalin.vocabularynotebook.utils.Log
import com.paktalin.vocabularynotebook.utils.addFragment
import com.paktalin.vocabularynotebook.utils.shortToast
import com.paktalin.vocabularynotebook.utils.gone
import com.paktalin.vocabularynotebook.utils.visible
import com.paktalin.vocabularynotebook.vocabulary.ModifiedVocabulary
import com.paktalin.vocabularynotebook.vocabulary.Sort
import com.paktalin.vocabularynotebook.vocabulary.VocabSet
import kotlinx.android.synthetic.main.content_main.*
class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivity: MainActivity) : RecyclerView.Adapter<ViewHolder>() {
private lateinit var recyclerView: RecyclerView
private lateinit var viewHolder: ViewHolder
var tracker: SelectionTracker<Long>? = null
private var tracker: SelectionTracker<Long>? = null
private var menu = true
init {
setHasStableIds(true)
......@@ -41,6 +45,7 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
this.recyclerView = recyclerView
this.tracker = initializeTracker(recyclerView) { s -> wordsSelected(s) }
mainActivity.searchView.setOnQueryTextListener(OnQueryTextListener(this@VocabularyAdapter))
}
......@@ -62,18 +67,25 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
//todo set click listener to menu
}
private fun showPopupMenu(v: View, position: Int) {
if (menu) {
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)) }
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 not edit mode, then start EditFragment
if (mainActivity.supportFragmentManager.findFragmentByTag("edit_fragment") == null)
popup.show()
} else {
Log.d(TAG, "not menu!")
}
}
fun refresh() {
......@@ -133,11 +145,15 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
return vocabulary.getModified()
}
fun addTag() {
shortToast(mainActivity, mainActivity.getString(R.string.toast_select_words_to_tag))
// TODO make words selectable
// TODO add complete button
// TODO collect selected words in a list
private fun wordsSelected(selection: Selection<Long>) {
if (selection.isEmpty)
gone(mainActivity.btnSubmitLayout)
else {
visible(mainActivity.btnSubmitLayout)
mainActivity.btnSubmit.setOnClickListener {
// TODO add tag to the words
}
}
}
companion object {
......
......@@ -23,9 +23,4 @@
android:id="@+id/sortByTranslation"
android:title="@string/sort_by_translation"
app:showAsAction="never" />
<item
android:id="@+id/addTag"
android:title="@string/add_tag"
app:showAsAction="never" />
</menu>
\ No newline at end of file
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