Commit 781ae10e by Paktalin

Removed inEditMode property + refactoring

parent 307bfc47
...@@ -8,16 +8,17 @@ import com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.vocabula ...@@ -8,16 +8,17 @@ import com.paktalin.vocabularynotebook.utils.FirestoreManager.Companion.vocabula
class Vocabulary() { class Vocabulary() {
private var words: MutableList<WordItem> = mutableListOf() private var words: MutableList<WordItem> = mutableListOf()
/* constructor(words: MutableList<WordItem>) : this() { constructor(vocabulary: Vocabulary) : this() {
this.words = words copyWordsFrom(vocabulary)
}*/ }
constructor(documents: MutableList<DocumentSnapshot>) : this() { constructor(documents: MutableList<DocumentSnapshot>) : this() {
for (ref in documents) { documents.forEach { ref ->
val word = ref["word"].toString() words.add(WordItem(
val translation = ref["translation"].toString() ref["word"].toString(),
val time = ref["time"] as Timestamp ref["translation"].toString(),
words.add(WordItem(word, translation, time.toDate(), ref.id, vocabularyId!!)) (ref["time"] as Timestamp).toDate(),
ref.id, vocabularyId!!))
} }
} }
...@@ -46,24 +47,12 @@ class Vocabulary() { ...@@ -46,24 +47,12 @@ class Vocabulary() {
words.add(0, newWord) words.add(0, newWord)
} }
fun addWords(newWords:MutableList<WordItem>) { fun copyWordsFrom(vocabulary: Vocabulary) {
words.addAll(newWords) words.addAll(vocabulary.get())
} }
fun addWordsAsDocuments(documents: MutableList<DocumentSnapshot>) { fun addWordsFittingQuery(vocabulary: Vocabulary, query:String) {
for (ref in documents) { vocabulary.get().filter { wordItem -> wordItem.contains(query) }.toCollection(words)
val word = ref["word"].toString()
val translation = ref["translation"].toString()
val time = ref["time"] as Timestamp
words.add(WordItem(word, translation, time.toDate(), ref.id, vocabularyId!!))
}
}
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) {
......
...@@ -14,9 +14,9 @@ import com.paktalin.vocabularynotebook.ui.activities.MainActivity ...@@ -14,9 +14,9 @@ import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.addFragment import com.paktalin.vocabularynotebook.utils.addFragment
import kotlinx.android.synthetic.main.word_item.view.* import kotlinx.android.synthetic.main.word_item.view.*
class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val mainActivity: MainActivity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() { class VocabularyAdapter(private val fullVocabulary: Vocabulary, private val mainActivity: MainActivity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private var fullVocabulary: MutableList<WordItem> = mutableListOf() // stores all the words loaded from the db private var displayedVocabulary = Vocabulary(fullVocabulary)
private lateinit var recyclerView: RecyclerView private lateinit var recyclerView: RecyclerView
var sortOrder: Int = 0 var sortOrder: Int = 0
...@@ -24,10 +24,6 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val ...@@ -24,10 +24,6 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
field = value; sort() field = value; sort()
} }
init {
fullVocabulary.addAll(displayedVocabulary.get())
}
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView) super.onAttachedToRecyclerView(recyclerView)
this.recyclerView = recyclerView this.recyclerView = recyclerView
...@@ -58,10 +54,11 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val ...@@ -58,10 +54,11 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
inflater.inflate(R.menu.word_item_menu, popup.menu) inflater.inflate(R.menu.word_item_menu, popup.menu)
popup.setOnMenuItemClickListener { popup.setOnMenuItemClickListener {
if (it.itemId == R.id.option_delete) { deleteWord(position) } if (it.itemId == R.id.option_delete) { deleteWord(position) }
if (it.itemId == R.id.option_edit) { editWord(v, displayedVocabulary.getAt(position)) } if (it.itemId == R.id.option_edit) { startEditFragment(v, displayedVocabulary.getAt(position)) }
true true
} }
if (!mainActivity.inEditMode) popup.show() if (mainActivity.supportFragmentManager.findFragmentByTag("edit_fragment") == null)
popup.show()
} }
private fun deleteWord(position: Int) { private fun deleteWord(position: Int) {
...@@ -96,8 +93,7 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val ...@@ -96,8 +93,7 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
} }
@SuppressLint("ResourceType") @SuppressLint("ResourceType")
private fun editWord(container: View, wordItem: WordItem) { private fun startEditFragment(container: View, wordItem: WordItem) {
if (!mainActivity.inEditMode) {
//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()
...@@ -106,8 +102,7 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val ...@@ -106,8 +102,7 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
// start EditWordFragment // start EditWordFragment
val arguments = Bundle() val arguments = Bundle()
arguments.putSerializable("wordItem", wordItem) arguments.putSerializable("wordItem", wordItem)
addFragment(mainActivity.supportFragmentManager, EditWordFragment(), container.id, arguments) addFragment(mainActivity.supportFragmentManager, EditWordFragment(), container.id, arguments, "edit_fragment")
}
} }
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
...@@ -119,7 +114,7 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val ...@@ -119,7 +114,7 @@ class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val
fun filter(query: String) { fun filter(query: String) {
displayedVocabulary.clear() displayedVocabulary.clear()
if (query.isEmpty()) if (query.isEmpty())
displayedVocabulary.addWords(fullVocabulary) displayedVocabulary.copyWordsFrom(fullVocabulary)
else else
displayedVocabulary.addWordsFittingQuery(fullVocabulary, query.toLowerCase()) displayedVocabulary.addWordsFittingQuery(fullVocabulary, query.toLowerCase())
notifyDataSetChanged() notifyDataSetChanged()
......
...@@ -24,7 +24,6 @@ class MainActivity : AppCompatActivity() { ...@@ -24,7 +24,6 @@ class MainActivity : AppCompatActivity() {
lateinit var vocabularyFragment: VocabularyFragment lateinit var vocabularyFragment: VocabularyFragment
lateinit var searchView: SearchView lateinit var searchView: SearchView
var inEditMode = false
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
......
...@@ -28,7 +28,6 @@ class EditWordFragment : WordFragment() { ...@@ -28,7 +28,6 @@ class EditWordFragment : WordFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mainActivity = activity as MainActivity mainActivity = activity as MainActivity
mainActivity.inEditMode = true
this.container = container this.container = container
hidePreviousViews() hidePreviousViews()
wordItem = arguments!!["wordItem"] as WordItem wordItem = arguments!!["wordItem"] as WordItem
...@@ -87,8 +86,8 @@ class EditWordFragment : WordFragment() { ...@@ -87,8 +86,8 @@ class EditWordFragment : WordFragment() {
} }
private fun onFailure() { private fun onFailure() {
shortToast(mainActivity, getString(R.string.toast_update_word_failed)) /*shortToast(mainActivity, getString(R.string.toast_update_word_failed))
stop() stop()*/
} }
private fun stop() { private fun stop() {
...@@ -97,7 +96,6 @@ class EditWordFragment : WordFragment() { ...@@ -97,7 +96,6 @@ class EditWordFragment : WordFragment() {
removeFragment(mainActivity.supportFragmentManager, this) removeFragment(mainActivity.supportFragmentManager, this)
mainActivity.hideKeyboardNotFromActivity(mainActivity) mainActivity.hideKeyboardNotFromActivity(mainActivity)
gone(mainActivity.btnSubmitLayout) gone(mainActivity.btnSubmitLayout)
mainActivity.inEditMode = false
} }
companion object { companion object {
......
...@@ -13,12 +13,12 @@ import org.apache.commons.lang3.StringUtils ...@@ -13,12 +13,12 @@ import org.apache.commons.lang3.StringUtils
val progressFragment: Fragment = ProgressFragment() val progressFragment: Fragment = ProgressFragment()
fun addFragment(fragmentManager: FragmentManager, fragment: Fragment, containerId: Int, arguments: Bundle?) { fun addFragment(fragmentManager: FragmentManager, fragment: Fragment, containerId: Int, arguments: Bundle?, tag: String? = null) {
fragment.arguments = arguments fragment.arguments = arguments
// remove progressFragment if it exists // remove progressFragment if it exists
if(fragmentManager.findFragmentById(fragment.id) != null) if(fragmentManager.findFragmentById(fragment.id) != null)
removeFragment(fragmentManager, fragment) removeFragment(fragmentManager, fragment)
fragmentManager.beginTransaction().add(containerId, fragment).commitAllowingStateLoss() fragmentManager.beginTransaction().add(containerId, fragment, tag).commitAllowingStateLoss()
} }
fun removeFragment(fragmentManager: FragmentManager, fragment: Fragment) { fun removeFragment(fragmentManager: FragmentManager, fragment: Fragment) {
......
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