Commit 98806c93 by Paktalin

Moved fragment logic to ActivityUtil

parent 52b96bce
......@@ -25,7 +25,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
package com.paktalin.vocabularynotebook
import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.text.TextUtils
......@@ -9,7 +10,8 @@ import com.paktalin.vocabularynotebook.ui.ProgressFragment
val progressFragment: Fragment = ProgressFragment()
fun addFragment(fragmentManager: FragmentManager, fragment: Fragment, containerId: Int) {
fun addFragment(fragmentManager: FragmentManager, fragment: Fragment, containerId: Int, arguments: Bundle?) {
fragment.arguments = arguments
fragmentManager.beginTransaction().add(containerId, fragment).commitAllowingStateLoss()
}
......@@ -18,7 +20,7 @@ fun removeFragment(fragmentManager: FragmentManager, fragment: Fragment) {
}
fun addProgressBar(fragmentManager: FragmentManager, containerId: Int) {
addFragment(fragmentManager, progressFragment, containerId)
addFragment(fragmentManager, progressFragment, containerId, null)
}
fun removeProgressBar(fragmentManager: FragmentManager) {
......
......@@ -14,7 +14,7 @@ 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 mainActivity: MainActivity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val mainActivity: MainActivity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private lateinit var recyclerView: RecyclerView
......@@ -23,10 +23,10 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val mainActi
field = value; sort()
}
private var wordsCopy: MutableList<WordItem> = mutableListOf() // stores all the words loaded from the db
private var vocabularyCopy: MutableList<WordItem> = mutableListOf() // stores all the words loaded from the db
init {
wordsCopy.addAll(vocabulary.get())
vocabularyCopy.addAll(displayedVocabulary.get())
}
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
......@@ -42,7 +42,7 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val mainActi
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val wordItem = vocabulary.getAt(position)
val wordItem = displayedVocabulary.getAt(position)
holder.tvWord.text = wordItem.pojo.word
holder.tvTranslation.text = wordItem.pojo.translation
holder.itemView.setOnClickListener { showPopupMenu(holder.itemView, position) }
......@@ -50,7 +50,7 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val mainActi
}
override fun getItemCount(): Int {
return vocabulary.size()
return displayedVocabulary.size()
}
private fun showPopupMenu(v: View, position: Int) {
......@@ -59,27 +59,27 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val mainActi
inflater.inflate(R.menu.word_item_menu, popup.menu)
popup.setOnMenuItemClickListener {
if (it.itemId == R.id.option_delete) { deleteWord(position) }
if (it.itemId == R.id.option_edit) { editWord(v, vocabulary.getAt(position)) }
if (it.itemId == R.id.option_edit) { editWord(v, displayedVocabulary.getAt(position)) }
true
}
popup.show()
}
private fun deleteWord(position: Int) {
vocabulary.deleteWord(position)
displayedVocabulary.deleteWord(position)
// update recyclerView
recyclerView.removeViewAt(position)
this.notifyItemRemoved(position)
this.notifyItemRangeChanged(position, vocabulary.size())
this.notifyItemRangeChanged(position, displayedVocabulary.size())
}
fun addWord(newWord: WordItem) {
vocabulary.addWord(newWord)
displayedVocabulary.addWord(newWord)
this.sort()
}
fun updateWord(updatedWord: WordItem) {
vocabulary.updateWord(updatedWord)
displayedVocabulary.updateWord(updatedWord)
this.sort()
}
......@@ -89,7 +89,7 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val mainActi
}
private fun sort() {
vocabulary.sort(sortOrder)
displayedVocabulary.sort(sortOrder)
this.notifyDataSetChanged()
}
......@@ -101,11 +101,9 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val mainActi
} else container.id = 18071999
// start EditWordFragment
val editWordFragment = EditWordFragment()
val arguments = Bundle()
arguments.putSerializable("wordItem", wordItem)
editWordFragment.arguments = arguments
mainActivity.supportFragmentManager.beginTransaction().add(container.id, editWordFragment).commit()
addFragment(mainActivity.supportFragmentManager, EditWordFragment(), container.id, arguments)
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
......@@ -115,11 +113,11 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val mainActi
}
fun filter(query: String) {
vocabulary.clear()
displayedVocabulary.clear()
if (query.isEmpty())
vocabulary.addWords(wordsCopy)
displayedVocabulary.addWords(vocabularyCopy)
else
vocabulary.addWordsFittingQuery(wordsCopy, query.toLowerCase())
displayedVocabulary.addWordsFittingQuery(vocabularyCopy, query.toLowerCase())
notifyDataSetChanged()
}
......
......@@ -33,7 +33,6 @@ class AddWordFragment : WordFragment() {
mainActivity.vocabularyFragment.addWord(wordItem)
}
override fun updateButtons() {
super.updateButtons()
if (!wordEmpty || !translationEmpty) showClearButton()
......
......@@ -12,6 +12,7 @@ import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.WORDS
import com.paktalin.vocabularynotebook.removeFragment
import com.paktalin.vocabularynotebook.shortToast
import kotlinx.android.synthetic.main.fragment_new_word.*
import kotlinx.android.synthetic.main.notebook_sheet.*
......@@ -74,8 +75,8 @@ class EditWordFragment : WordFragment() {
private fun stop() {
// set onClickListener from AddWordFragment
mainActivity.btnSubmit.setOnClickListener { (mainActivity.fragmentNewWord as AddWordFragment).submitWord() }
mainActivity.removeFragment(this)
mainActivity.btnSubmit.setOnClickListener { (mainActivity.fragmentAddWord as AddWordFragment).submitWord() }
removeFragment(mainActivity.supportFragmentManager, this)
}
override fun updateRecycleView(wordItem: WordItem) {
......
......@@ -10,7 +10,6 @@ import com.google.firebase.firestore.DocumentReference
import kotlinx.android.synthetic.main.activity_main.*
import android.view.WindowManager
import android.app.Activity
import android.support.v4.app.Fragment
import android.view.Menu
import android.view.MenuItem
import android.view.View
......@@ -21,7 +20,6 @@ import android.support.v7.widget.SearchView
import com.paktalin.vocabularynotebook.*
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
class MainActivity : AppCompatActivity() {
lateinit var vocabularyId: String
......@@ -83,9 +81,7 @@ class MainActivity : AppCompatActivity() {
vocabularyFragment = VocabularyFragment()
val arguments = Bundle()
arguments.putString("vocabularyId", vocabularyId)
vocabularyFragment.arguments = arguments
supportFragmentManager.beginTransaction().add(R.id.fragment_container, vocabularyFragment)
.commitNowAllowingStateLoss()
addFragment(supportFragmentManager, vocabularyFragment, R.id.container_vocabulary, arguments)
} else {
Log.w(TAG, "There's no collection \"vocabularies\"")
showToastNoWords() }
......@@ -115,10 +111,6 @@ class MainActivity : AppCompatActivity() {
shortToast(this, getString(R.string.toast_no_words))
}
fun removeFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction().remove(fragment).commitAllowingStateLoss()
}
override fun onPause() {
super.onPause()
hideKeyboard()
......
......@@ -25,7 +25,7 @@
app:layout_constraintTop_toTopOf="parent">
<fragment
android:id="@+id/fragmentNewWord"
android:id="@+id/fragmentAddWord"
android:name="com.paktalin.vocabularynotebook.ui.AddWordFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
......@@ -46,7 +46,7 @@
app:layout_constraintVertical_bias="0.0">
<LinearLayout
android:id="@+id/fragment_container"
android:id="@+id/container_vocabulary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/sheet_bottom"
......
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