Commit 28dea025 by Paktalin

Refactoring in Vocabulary class

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