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