Commit 20b97ab4 by Paktalin

Dirty selection implementation

parent 79b07129
......@@ -44,7 +44,7 @@ dependencies {
implementation 'com.google.firebase:firebase-auth:16.2.1'
implementation 'com.firebase:firebase-client-android:2.3.1'
implementation 'com.google.firebase:firebase-firestore:18.2.0'
implementation 'com.crashlytics.sdk.android:crashlytics:2.9.9'
// implementation 'com.crashlytics.sdk.android:crashlytics:2.9.9'
implementation 'androidx.recyclerview:recyclerview-selection:1.0.0'
testImplementation 'junit:junit:4.12'
......
package com.paktalin.vocabularynotebook.ui
import android.view.MotionEvent
import androidx.recyclerview.selection.ItemDetailsLookup
import androidx.recyclerview.widget.RecyclerView
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()
}
return null
}
}
\ No newline at end of file
package com.paktalin.vocabularynotebook.ui
import android.graphics.Color
import androidx.recyclerview.widget.RecyclerView
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.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.Log
import kotlinx.android.synthetic.main.word_item.view.*
......@@ -20,10 +20,17 @@ class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnCli
private val tvTranslation: TextView = itemView.translation
val layout: LinearLayout = itemView.layout
fun bind(wordPojo: WordPojo, position: Int, showPopupMenu: (View, Int) -> Unit) {
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
}
fun getItemDetails(): ItemDetailsLookup.ItemDetails<Long> =
object : ItemDetailsLookup.ItemDetails<Long>() {
override fun getPosition(): Int = adapterPosition
override fun getSelectionKey(): Long? = itemId
}
companion object {
......
......@@ -3,14 +3,17 @@ package com.paktalin.vocabularynotebook.ui
import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.widget.PopupMenu
import androidx.recyclerview.selection.SelectionTracker
import androidx.recyclerview.widget.RecyclerView
import android.view.*
import com.paktalin.vocabularynotebook.OnQueryTextListener
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.firestoreitems.WordPojo
import com.paktalin.vocabularynotebook.ui.fragments.EditWordFragment
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
......@@ -22,6 +25,13 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
private lateinit var recyclerView: RecyclerView
private lateinit var viewHolder: ViewHolder
var tracker: SelectionTracker<Long>? = null
init {
setHasStableIds(true)
}
override fun getItemId(position: Int): Long = position.toLong()
var sort: Sort = Sort.BY_TIME
set(value) {
......@@ -46,7 +56,9 @@ class VocabularyAdapter(private val vocabulary: VocabSet, private val mainActivi
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val wordItem = vocabulary.displayedAt(position)
viewHolder.bind(wordItem, position) { view, p -> showPopupMenu(view, p)}
tracker?.let {
viewHolder.bind(wordItem, position, it.isSelected(position.toLong())) { view, p -> showPopupMenu(view, p) }
}
this.viewHolder = viewHolder
//todo set click listener to menu
}
......
......@@ -2,14 +2,19 @@ package com.paktalin.vocabularynotebook.ui.activities
import android.app.Activity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import android.view.Menu
import android.view.MenuItem
import android.view.View
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.MyItemDetailsLookup
import com.paktalin.vocabularynotebook.ui.VocabularyAdapter
import com.paktalin.vocabularynotebook.ui.views.LockableLayoutManager
import com.paktalin.vocabularynotebook.utils.*
......@@ -17,12 +22,12 @@ import com.paktalin.vocabularynotebook.vocabulary.Sort
import com.paktalin.vocabularynotebook.vocabulary.VocabSet
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
import java.lang.Exception
class MainActivity : AppCompatActivity() {
lateinit var searchView: SearchView
lateinit var vocabularyAdapter: VocabularyAdapter
private var tracker: SelectionTracker<Long>? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -84,13 +89,23 @@ class MainActivity : AppCompatActivity() {
private fun setUpVocabularyAdapter() {
addProgressBar(supportFragmentManager, R.id.container_main)
FirestoreManager().extractVocabularyId({
// recyclerView.adapter = VocabularyAdapter(BasicVocabulary(), this@MainActivity)
recyclerView.layoutManager = LockableLayoutManager(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()
}
......
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/holo_blue_light" android:state_activated="true" />
<item android:drawable="@android:color/white" />
</selector>
\ No newline at end of file
......@@ -33,6 +33,7 @@
android:paddingEnd="16dp"
android:paddingRight="16dp"
android:paddingBottom="8dp"
android:background="@drawable/selector"
app:layout_constraintTop_toTopOf="parent">
<TextView
......
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