Commit 28dea025 by Paktalin

Refactoring in Vocabulary class

parent bde85cd1
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 {
class OnQueryTextListener(var adapter: VocabularyAdapter) : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
(recyclerView.adapter as VocabularyAdapter).filter(query)
adapter.filter(query)
return true
}
override fun onQueryTextChange(query: String): Boolean {
(recyclerView.adapter as VocabularyAdapter).filter(query)
adapter.filter(query)
return true
}
}
\ No newline at end of file
package com.paktalin.vocabularynotebook
import android.annotation.SuppressLint
import android.app.Activity
import android.os.Build
import android.os.Bundle
import android.support.v7.widget.PopupMenu
......@@ -15,22 +14,25 @@ import com.paktalin.vocabularynotebook.ui.EditWordFragment
import com.paktalin.vocabularynotebook.ui.MainActivity
import kotlinx.android.synthetic.main.word_item.view.*
class VocabularyAdapter(internal val vocabulary: Vocabulary, private val activity: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
class VocabularyAdapter(private val vocabulary: Vocabulary, private val mainActivity: MainActivity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private lateinit var recyclerView: RecyclerView
private var sortOrder:Int = 0
set(value) { field = value; sort() }
private var sortOrder: Int = 0
set(value) {
field = value; sort()
}
private var wordsCopy:MutableList<WordItem> = mutableListOf()
private var wordsCopy: MutableList<WordItem> = mutableListOf() // stores all the words loaded from the db
init {
wordsCopy.addAll(vocabulary.words)
wordsCopy.addAll(vocabulary.get())
}
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
this.recyclerView = recyclerView
mainActivity.searchView.setOnQueryTextListener(OnQueryTextListener(this@VocabularyAdapter))
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
......@@ -47,10 +49,12 @@ class VocabularyAdapter(internal val vocabulary: Vocabulary, private val activit
//todo set click listener to menu
}
override fun getItemCount(): Int { return vocabulary.size() }
override fun getItemCount(): Int {
return vocabulary.size()
}
private fun showPopupMenu(v: View, position: Int) {
val popup = PopupMenu(activity, v)
val popup = PopupMenu(mainActivity, v)
val inflater = popup.menuInflater
inflater.inflate(R.menu.word_item_menu, popup.menu)
popup.setOnMenuItemClickListener {
......@@ -90,7 +94,7 @@ class VocabularyAdapter(internal val vocabulary: Vocabulary, private val activit
}
@SuppressLint("ResourceType")
private fun editWord(container:View, wordItem: WordItem) {
private fun editWord(container: View, wordItem: WordItem) {
//set container id
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
container.id = View.generateViewId()
......@@ -101,19 +105,7 @@ class VocabularyAdapter(internal val vocabulary: Vocabulary, private val activit
val arguments = Bundle()
arguments.putSerializable("wordItem", wordItem)
editWordFragment.arguments = arguments
(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()
mainActivity.supportFragmentManager.beginTransaction().add(container.id, editWordFragment).commit()
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
......@@ -123,21 +115,15 @@ class VocabularyAdapter(internal val vocabulary: Vocabulary, private val activit
}
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)
}
}
}
vocabulary.clear()
if (query.isEmpty())
vocabulary.addWords(wordsCopy)
else
vocabulary.addWordsFittingQuery(wordsCopy, query.toLowerCase())
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>) {
}
var pojo:Pojo
var words: MutableList<WordItem>
private var words: MutableList<WordItem>
class Pojo(var title:String?) {
init {
......@@ -40,6 +40,17 @@ class Vocabulary(words: MutableList<WordItem>) {
words.add(0, newWord)
}
fun addWords(newWords:MutableList<WordItem>) {
words.addAll(newWords)
}
fun addWordsFittingQuery(newWords:MutableList<WordItem>, query:String) {
for (newWord in newWords) {
if (newWord.contains(query))
this.addWord(newWord)
}
}
fun updateWord(updatedWord:WordItem) {
val updatedItemIndex = words.indexOf(updatedWord)
words[updatedItemIndex] = updatedWord
......@@ -49,8 +60,12 @@ class Vocabulary(words: MutableList<WordItem>) {
return words[position]
}
fun get():MutableList<WordItem> { return words }
fun size():Int { return words.size }
fun clear() { words.clear() }
private fun sortByTime() {
words.sortWith(Comparator { item1, item2 ->
-item1.pojo.time!!.compareTo(item2.pojo.time) })
......
......@@ -25,5 +25,10 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p
.addOnFailureListener { e -> Log.w(TAG, "deleteWordWithId $id:failure", e.fillInStackTrace()) }
}
fun contains(string:String):Boolean {
return pojo.word.toLowerCase().contains(string) ||
pojo.translation.toLowerCase().contains(string)
}
companion object { private val TAG = "VN/" + WordItem::class.java.simpleName }
}
......@@ -11,7 +11,6 @@ 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
......@@ -21,9 +20,7 @@ import android.widget.Toast
import com.paktalin.vocabularynotebook.VocabularyAdapter
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import kotlinx.android.synthetic.main.fragment_vocabulary.*
import android.content.Context
import android.support.v7.widget.SearchView
import com.paktalin.vocabularynotebook.OnQueryTextListener
class MainActivity : AppCompatActivity() {
......@@ -90,7 +87,6 @@ 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() }
......
......@@ -25,6 +25,7 @@ class VocabularyFragment : Fragment() {
}
private val db = ConfiguredFirestore.instance
private lateinit var mainActivity: MainActivity
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_vocabulary, container, false)
......@@ -32,14 +33,15 @@ class VocabularyFragment : Fragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
mainActivity = activity as MainActivity
setEmptyAdapter()
retrieveWordsData(arguments!!["vocabularyId"] as String)
}
private fun setEmptyAdapter() {
val emptyList: MutableList<WordItem> = mutableListOf()
recyclerView.adapter = VocabularyAdapter(Vocabulary(emptyList), activity!!)
val mLayoutManager = LinearLayoutManager(activity)
recyclerView.adapter = VocabularyAdapter(Vocabulary(emptyList), mainActivity)
val mLayoutManager = LinearLayoutManager(mainActivity)
recyclerView.layoutManager = mLayoutManager
}
......@@ -52,7 +54,7 @@ class VocabularyFragment : Fragment() {
setVocabularyAdapter(it.documents, vocabularyId)
else {
Log.i(TAG, "There are no documents in collection \"words\"")
(activity as MainActivity).showToastNoWords()
mainActivity.showToastNoWords()
}}
}
......@@ -67,7 +69,7 @@ class VocabularyFragment : Fragment() {
}
val vocabulary = Vocabulary(wordItems)
val adapter = VocabularyAdapter(vocabulary, activity!!)
val adapter = VocabularyAdapter(vocabulary, mainActivity)
recyclerView.adapter = adapter
}
......
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