Commit 548c2ee9 by Paktalin

VocabularyFragment is started from MainActivity after retrieving Firestore data

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