Commit 98806c93 by Paktalin

Moved fragment logic to ActivityUtil

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