Commit bde85cd1 by Paktalin

Words are searchable

parent d51c6159
package com.paktalin.vocabularynotebook
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.SearchView
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
class OnQueryTextListener(var recyclerView: RecyclerView) : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
(recyclerView.adapter as VocabularyAdapter).filter(query)
return true
}
override fun onQueryTextChange(query: String): Boolean {
(recyclerView.adapter as VocabularyAdapter).filter(query)
return true
}
}
\ No newline at end of file
......@@ -15,13 +15,19 @@ import com.paktalin.vocabularynotebook.ui.EditWordFragment
import com.paktalin.vocabularynotebook.ui.MainActivity
import kotlinx.android.synthetic.main.word_item.view.*
class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
class VocabularyAdapter(internal val vocabulary: Vocabulary, private val activity: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private lateinit var recyclerView: RecyclerView
private var sortOrder:Int = 0
set(value) { field = value; sort() }
private var wordsCopy:MutableList<WordItem> = mutableListOf()
init {
wordsCopy.addAll(vocabulary.words)
}
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
this.recyclerView = recyclerView
......@@ -98,11 +104,40 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity
(activity as MainActivity).supportFragmentManager.beginTransaction().add(container.id, editWordFragment).commit()
}
fun replaceAll(words: List<WordItem>) {
//vocabulary.words.beginBatchedUpdates()
for (i in vocabulary.words.size - 1 downTo 0) {
val model = vocabulary.words[i]
if (!words.contains(model)) {
vocabulary.words.remove(model)
}
}
vocabulary.words.addAll(words)
//vocabulary.words.endBatchedUpdates()
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tvWord: TextView = itemView.word
val tvTranslation: TextView = itemView.translation
val layout: LinearLayout = itemView.layout
}
fun filter(query: String) {
var query = query
vocabulary.words.clear()
if (query.isEmpty()) {
vocabulary.words.addAll(wordsCopy)
} else {
query = query.toLowerCase()
for (wordCopy in wordsCopy) {
if (wordCopy.pojo.word.toLowerCase().contains(query) ||
wordCopy.pojo.translation.toLowerCase().contains(query)) {
vocabulary.words.add(wordCopy)
}
}
}
notifyDataSetChanged()
}
companion object { private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName }
}
\ No newline at end of file
......@@ -10,7 +10,7 @@ class Vocabulary(words: MutableList<WordItem>) {
}
var pojo:Pojo
private var words: MutableList<WordItem>
var words: MutableList<WordItem>
class Pojo(var title:String?) {
init {
......
......@@ -11,6 +11,7 @@ import com.paktalin.vocabularynotebook.R
import kotlinx.android.synthetic.main.activity_main.*
import android.view.WindowManager
import android.app.Activity
import android.app.SearchManager
import android.support.v4.app.Fragment
import android.view.Menu
import android.view.MenuItem
......@@ -20,18 +21,16 @@ import android.widget.Toast
import com.paktalin.vocabularynotebook.VocabularyAdapter
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import kotlinx.android.synthetic.main.fragment_vocabulary.*
import android.support.v4.view.MenuItemCompat.getActionView
import android.content.Context.SEARCH_SERVICE
import android.support.v4.content.ContextCompat.getSystemService
import android.app.SearchManager
import android.content.Context
import android.widget.SearchView
import android.support.v7.widget.SearchView
import com.paktalin.vocabularynotebook.OnQueryTextListener
class MainActivity : AppCompatActivity() {
lateinit var vocabularyId: String
lateinit var vocabularyFragment: VocabularyFragment
lateinit var searchView: SearchView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
......@@ -43,10 +42,7 @@ class MainActivity : AppCompatActivity() {
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.options_menu, menu)
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
val searchView = menu!!.findItem(R.id.search).actionView as SearchView
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName))
searchView.queryHint = resources.getString(R.string.search_hint)
searchView = menu!!.findItem(R.id.search).actionView as SearchView
return true
}
......@@ -94,6 +90,7 @@ class MainActivity : AppCompatActivity() {
vocabularyFragment.arguments = arguments
supportFragmentManager.beginTransaction().add(R.id.fragment_container, vocabularyFragment)
.commitNowAllowingStateLoss()
searchView.setOnQueryTextListener(OnQueryTextListener(recyclerView))
} else {
Log.w(TAG, "There's no collection \"vocabularies\"")
showToastNoWords() }
......
......@@ -6,12 +6,12 @@
android:title="@string/option_search"
android:icon="@drawable/ic_search_icon"
app:showAsAction="ifRoom"
app:actionViewClass="android.widget.SearchView" />
app:actionViewClass="android.support.v7.widget.SearchView" />
<item
android:id="@+id/sort"
android:title="@string/option_sort"
android:icon="@drawable/ic_sort_icon"
app:showAsAction="ifRoom" />
app:showAsAction="ifRoom|collapseActionView" />
</menu>
\ No newline at end of file
......@@ -15,5 +15,4 @@
<string name="menu_option_edit">Edit</string>
<string name="option_sort">Sort</string>
<string name="option_search">Search</string>
<string name="search_hint">Enter word</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"/>
\ No newline at end of file
android:label="@string/app_name" />
\ 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