Commit 9e553faa by Paktalin

VocabularyId is retrieved inside MainActivity

parent 8eee40c0
......@@ -5,31 +5,64 @@ import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
lateinit var vocabularyId: String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
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 ->
menuItem.isChecked = true
if(menuItem.itemId == R.id.logOut) { logOut() }
drawerLayout!!.closeDrawers()
true
}
}
private fun logOut() {
Log.i(TAG, "User logged out")
FirebaseAuth.getInstance()!!.signOut()
val intentLogInActivity = Intent(this@MainActivity, LogInActivity::class.java)
startActivity(intentLogInActivity)
private fun extractVocabularyData() {
val userId = FirebaseAuth.getInstance().currentUser!!.uid
val db = FirebaseFirestore.getInstance()
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 {
......
......@@ -9,6 +9,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R
import kotlinx.android.synthetic.main.fragment_new_word.*
......@@ -80,6 +81,12 @@ class NewWordFragment : Fragment() {
private fun addWord() {
//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 }
......
package com.paktalin.vocabularynotebook.ui
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
......@@ -8,7 +7,6 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R
......@@ -21,14 +19,9 @@ class VocabularyFragment : Fragment() {
private val TAG = "VN/" + VocabularyFragment::class.simpleName
private const val VOCABULARIES = "vocabularies"
private const val WORDS = "words"
private const val USERS = "users"
}
private lateinit var userDocument: DocumentReference
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? {
return inflater.inflate(R.layout.fragment_vocabulary, container, false)
......@@ -37,7 +30,6 @@ class VocabularyFragment : Fragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setEmptyAdapter()
retrieveData()
}
override fun onDestroy() {
......@@ -46,47 +38,24 @@ class VocabularyFragment : Fragment() {
}
private fun setEmptyAdapter() {
val emptyList:MutableList<WordItem> = mutableListOf()
val emptyList: MutableList<WordItem> = mutableListOf()
recyclerView.adapter = VocabularyAdapter(emptyList, activity!!)
val mLayoutManager = LinearLayoutManager(activity)
recyclerView.layoutManager = mLayoutManager
}
private fun retrieveData() {
val userId = FirebaseAuth.getInstance().currentUser!!.uid
userDocument = db.collection(USERS).document(userId)
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)
fun retrieveWordsData(vocabularyId: String) {
db.collection(VOCABULARIES).document(vocabularyId).collection(WORDS).get()
.addOnSuccessListener { setVocabularyAdapter(it.documents, vocabularyId) }
}
private fun setVocabularyAdapter(documents: MutableList<DocumentSnapshot>) {
private fun setVocabularyAdapter(documents: MutableList<DocumentSnapshot>, vocabularyId: String) {
val wordItems: MutableList<WordItem> = mutableListOf()
for (ref in documents) {
val word = ref.get("word").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!!)
......
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