Commit 548c2ee9 by Paktalin

VocabularyFragment is started from MainActivity after retrieving Firestore data

parent 6a28bf07
......@@ -2,6 +2,7 @@ package com.paktalin.vocabularynotebook.firestoreitems
class Vocabulary {
var pojo:Pojo
private lateinit var words:MutableList<WordItem>
class Pojo(var title:String?) {
init {
......@@ -12,4 +13,6 @@ class Vocabulary {
init {
pojo = Pojo(null)
}
}
......@@ -34,9 +34,7 @@ class AddWordFragment : WordFragment() {
}
override fun updateRecycleView(wordItem: WordItem) {
val vocabularyFragment = mainActivity
.supportFragmentManager.findFragmentById(R.id.fragment_vocabulary) as VocabularyFragment
vocabularyFragment.addWordItem(wordItem)
mainActivity.vocabularyFragment.addWordItem(wordItem)
}
companion object { private val TAG = "VN/" + AddWordFragment::class.java.simpleName }
......
......@@ -66,9 +66,7 @@ class EditWordFragment : WordFragment() {
Toast.makeText(mainActivity, "Couldn't update the word", Toast.LENGTH_SHORT).show()} }
override fun updateRecycleView(wordItem: WordItem) {
val vocabularyFragment = mainActivity
.supportFragmentManager.findFragmentById(R.id.fragment_vocabulary) as VocabularyFragment
vocabularyFragment.updateWordItem(wordItem)
mainActivity.vocabularyFragment.updateWordItem(wordItem)
}
companion object { private val TAG = "VN/" + EditWordFragment::class.java.simpleName }
......
......@@ -23,14 +23,13 @@ import kotlinx.android.synthetic.main.fragment_vocabulary.*
class MainActivity : AppCompatActivity() {
lateinit var vocabularyId: String
private lateinit var vocabularyFragment: VocabularyFragment
lateinit var vocabularyFragment: VocabularyFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
hideKeyboard()
setUpNavigationView()
vocabularyFragment = supportFragmentManager.findFragmentById(R.id.fragment_vocabulary) as VocabularyFragment
extractVocabularyData()
}
......@@ -74,7 +73,13 @@ class MainActivity : AppCompatActivity() {
val vocabularies: List<DocumentReference> = task.get("vocabularies") as List<DocumentReference>
val vocabulary = db.collection("vocabularies").document(vocabularies[0].id)
vocabularyId = vocabulary.id
vocabularyFragment.retrieveWordsData(vocabularyId)
// start VocabularyFragment
vocabularyFragment = VocabularyFragment()
val arguments = Bundle()
arguments.putString("vocabularyId", vocabularyId)
vocabularyFragment.arguments = arguments
supportFragmentManager.beginTransaction().add(R.id.fragment_container, vocabularyFragment).commitNowAllowingStateLoss()
} else { showToastNoWords() }
}
}
......
......@@ -3,6 +3,7 @@ package com.paktalin.vocabularynotebook.ui
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
......@@ -27,9 +28,11 @@ class VocabularyFragment : Fragment() {
return inflater.inflate(R.layout.fragment_vocabulary, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setEmptyAdapter()
retrieveWordsData(arguments!!["vocabularyId"] as String)
}
override fun onDestroy() {
......@@ -44,14 +47,17 @@ class VocabularyFragment : Fragment() {
recyclerView.layoutManager = mLayoutManager
}
fun retrieveWordsData(vocabularyId: String) {
private fun retrieveWordsData(vocabularyId: String) {
db.collection(VOCABULARIES).document(vocabularyId).collection(WORDS)
.orderBy("time", Query.Direction.DESCENDING)
.get()
.addOnSuccessListener {
if (it.documents.size != 0)
setVocabularyAdapter(it.documents, vocabularyId)
else (activity as MainActivity).showToastNoWords()}
else {
Log.i(TAG, "There are no documents in collection \"words\"")
(activity as MainActivity).showToastNoWords()
}}
}
private fun setVocabularyAdapter(documents: MutableList<DocumentSnapshot>, vocabularyId: String) {
......
......@@ -17,6 +17,7 @@
android:layout_margin="8dp">
<LinearLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
......@@ -30,11 +31,6 @@
android:layout_height="wrap_content"
android:layout_marginTop="8dp" />
<fragment
android:id="@+id/fragment_vocabulary"
android:name="com.paktalin.vocabularynotebook.ui.VocabularyFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
......
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