Commit 9e553faa by Paktalin

VocabularyId is retrieved inside MainActivity

parent 8eee40c0
...@@ -5,31 +5,64 @@ import android.os.Bundle ...@@ -5,31 +5,64 @@ import android.os.Bundle
import android.support.v7.app.AppCompatActivity import android.support.v7.app.AppCompatActivity
import android.util.Log import android.util.Log
import com.google.firebase.auth.FirebaseAuth import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
lateinit var vocabularyId: String
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)
setUpNavigationView()
extractVocabularyData()
startVocabularyFragment()
startNewWordFragment()
}
private fun logOut() {
Log.i(TAG, "User logged out")
FirebaseAuth.getInstance()!!.signOut()
val intentLogInActivity = Intent(this@MainActivity, LogInActivity::class.java)
startActivity(intentLogInActivity)
}
private fun setUpNavigationView() {
navigationView.setNavigationItemSelectedListener { menuItem -> navigationView.setNavigationItemSelectedListener { menuItem ->
menuItem.isChecked = true menuItem.isChecked = true
if(menuItem.itemId == R.id.logOut) { logOut() } if(menuItem.itemId == R.id.logOut) { logOut() }
drawerLayout!!.closeDrawers() drawerLayout!!.closeDrawers()
true true
} }
} }
private fun logOut() { private fun extractVocabularyData() {
Log.i(TAG, "User logged out") val userId = FirebaseAuth.getInstance().currentUser!!.uid
FirebaseAuth.getInstance()!!.signOut() val db = FirebaseFirestore.getInstance()
val intentLogInActivity = Intent(this@MainActivity, LogInActivity::class.java)
startActivity(intentLogInActivity) val userDocument = db.collection("users").document(userId)
userDocument.get().addOnSuccessListener { task ->
val vocabularies: List<DocumentReference> = task.get("vocabularies") as List<DocumentReference>
//todo represent specific vocabulary instead of the first one
val vocabulary = db.collection("vocabularies").document(vocabularies[0].id)
vocabularyId = vocabulary.id
(supportFragmentManager.findFragmentById(R.id.fragment_vocabulary) as VocabularyFragment)
.retrieveWordsData(vocabularyId)
}
}
private fun startVocabularyFragment() {
}
private fun startNewWordFragment() {
} }
companion object { companion object {
......
...@@ -9,6 +9,7 @@ import android.view.LayoutInflater ...@@ -9,6 +9,7 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.ImageButton import android.widget.ImageButton
import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import kotlinx.android.synthetic.main.fragment_new_word.* import kotlinx.android.synthetic.main.fragment_new_word.*
...@@ -80,6 +81,12 @@ class NewWordFragment : Fragment() { ...@@ -80,6 +81,12 @@ class NewWordFragment : Fragment() {
private fun addWord() { private fun addWord() {
//todo get word data from edit texts and save it //todo get word data from edit texts and save it
val word = etWord.text.toString()
val translation = etTranslation.text.toString()
Log.d(TAG, "vocabularyId: ${(activity as MainActivity).vocabularyId})")
FirebaseFirestore.getInstance().collection("vocabularies").document()
} }
companion object { private val TAG = "VN/" + NewWordFragment::class.java.simpleName } companion object { private val TAG = "VN/" + NewWordFragment::class.java.simpleName }
......
package com.paktalin.vocabularynotebook.ui package com.paktalin.vocabularynotebook.ui
import android.content.Intent
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
...@@ -8,7 +7,6 @@ import android.view.LayoutInflater ...@@ -8,7 +7,6 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import com.google.firebase.auth.FirebaseAuth import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.DocumentSnapshot import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FirebaseFirestore import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
...@@ -21,14 +19,9 @@ class VocabularyFragment : Fragment() { ...@@ -21,14 +19,9 @@ class VocabularyFragment : Fragment() {
private val TAG = "VN/" + VocabularyFragment::class.simpleName private val TAG = "VN/" + VocabularyFragment::class.simpleName
private const val VOCABULARIES = "vocabularies" private const val VOCABULARIES = "vocabularies"
private const val WORDS = "words" private const val WORDS = "words"
private const val USERS = "users"
} }
private lateinit var userDocument: DocumentReference
private val db = FirebaseFirestore.getInstance() private val db = FirebaseFirestore.getInstance()
private lateinit var vocabulary: DocumentReference
//todo move data process to onCreate method and update the views later
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_vocabulary, container, false) return inflater.inflate(R.layout.fragment_vocabulary, container, false)
...@@ -37,7 +30,6 @@ class VocabularyFragment : Fragment() { ...@@ -37,7 +30,6 @@ class VocabularyFragment : Fragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState) super.onActivityCreated(savedInstanceState)
setEmptyAdapter() setEmptyAdapter()
retrieveData()
} }
override fun onDestroy() { override fun onDestroy() {
...@@ -46,47 +38,24 @@ class VocabularyFragment : Fragment() { ...@@ -46,47 +38,24 @@ class VocabularyFragment : Fragment() {
} }
private fun setEmptyAdapter() { private fun setEmptyAdapter() {
val emptyList:MutableList<WordItem> = mutableListOf() val emptyList: MutableList<WordItem> = mutableListOf()
recyclerView.adapter = VocabularyAdapter(emptyList, activity!!) recyclerView.adapter = VocabularyAdapter(emptyList, activity!!)
val mLayoutManager = LinearLayoutManager(activity) val mLayoutManager = LinearLayoutManager(activity)
recyclerView.layoutManager = mLayoutManager recyclerView.layoutManager = mLayoutManager
} }
private fun retrieveData() { fun retrieveWordsData(vocabularyId: String) {
val userId = FirebaseAuth.getInstance().currentUser!!.uid db.collection(VOCABULARIES).document(vocabularyId).collection(WORDS).get()
userDocument = db.collection(USERS).document(userId) .addOnSuccessListener { setVocabularyAdapter(it.documents, vocabularyId) }
userDocument.get().addOnSuccessListener { task ->
setVocabularyId(task)
retrieveVocabularyData()
}
}
private fun setVocabularyId(task: DocumentSnapshot) {
//todo if only one vocabulary exists, open it
val vocabularies: List<DocumentReference> = task.get("vocabularies") as List<DocumentReference>
vocabulary = db.collection(VOCABULARIES).document(vocabularies[0].id)
}
private fun retrieveVocabularyData() {
//todo if only one vocabulary exists, open it
vocabulary.collection(WORDS).get()
.addOnSuccessListener { setVocabularyAdapter(it.documents) }
}
private fun addWord() {
val addWordIntent = Intent(activity, AddWordActivity::class.java)
addWordIntent.putExtra("vocabularyId", vocabulary.id)
startActivity(addWordIntent)
} }
private fun setVocabularyAdapter(documents: MutableList<DocumentSnapshot>) { private fun setVocabularyAdapter(documents: MutableList<DocumentSnapshot>, vocabularyId: String) {
val wordItems: MutableList<WordItem> = mutableListOf() val wordItems: MutableList<WordItem> = mutableListOf()
for (ref in documents) { for (ref in documents) {
val word = ref.get("word").toString() val word = ref.get("word").toString()
val translation = ref.get("translation").toString() val translation = ref.get("translation").toString()
wordItems.add(WordItem(word, translation, ref.id, vocabulary.id)) wordItems.add(WordItem(word, translation, ref.id, vocabularyId))
} }
val adapter = VocabularyAdapter(wordItems, activity!!) val adapter = VocabularyAdapter(wordItems, activity!!)
......
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