Commit 57728fc2 by Paktalin

RecyclerView is updated when a new word is added

parent 24371d24
......@@ -10,7 +10,7 @@ import android.widget.LinearLayout
import android.widget.TextView
import com.paktalin.vocabularynotebook.ui.WordItemInfoActivity
class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private val context: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
class VocabularyAdapter(val wordItems: MutableList<WordItem>, private val context: Activity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
private lateinit var recyclerView: RecyclerView
......@@ -68,6 +68,11 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
this.notifyItemRangeChanged(position, wordItems.size)
}
fun addWordItem(newWordItem: WordItem) {
wordItems.add(0, newWordItem)
this.notifyItemInserted(0)
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val tvWord: TextView = itemView.findViewById(R.id.etWord)
val tvTranslation: TextView = itemView.findViewById(R.id.etTranslation)
......@@ -78,4 +83,5 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
companion object {
private val TAG = "VN/" + VocabularyAdapter::class.java.simpleName
}
}
\ No newline at end of file
......@@ -9,7 +9,7 @@ import com.google.firebase.firestore.FirebaseFirestore
import java.io.Serializable
class WordItem(word: String?, translation: String?, var id: String?, private val vocabularyId: String?) : Serializable {
class WordItem(word: String, translation: String, var id: String, private val vocabularyId: String) : Serializable {
var pojo: WordItemPojo? = null
class WordItemPojo(var word: String?, var translation: String?) : Serializable
......@@ -18,20 +18,17 @@ class WordItem(word: String?, translation: String?, var id: String?, private val
this.pojo = WordItemPojo(word, translation)
}
constructor(pojo: WordItemPojo, id: String, vocabularyId: String)
: this(pojo.word!!, pojo.translation!!, id, vocabularyId)
fun delete() {
if (vocabularyId != null) {
FirebaseFirestore.getInstance().collection("vocabularies").document(vocabularyId)
.collection("words").document(id!!).delete()
.addOnSuccessListener { Log.i(TAG, "Successfully deleted word with id $id") }
.addOnFailureListener { e -> Log.w(TAG, "deleteWordWithId $id:failure", e.fillInStackTrace()) }
}
FirebaseFirestore.getInstance().collection("vocabularies").document(vocabularyId)
.collection("words").document(id!!).delete()
.addOnSuccessListener { Log.i(TAG, "Successfully deleted word with id $id") }
.addOnFailureListener { e -> Log.w(TAG, "deleteWordWithId $id:failure", e.fillInStackTrace()) }
}
companion object {
private val TAG = "VN/" + WordItem::class.java.simpleName
fun createEmpty() : WordItem {
return WordItem(null, null, null, null)
}
}
}
package com.paktalin.vocabularynotebook.ui
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.widget.Toast
import com.google.firebase.firestore.FirebaseFirestore
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.Utils
import com.paktalin.vocabularynotebook.WordItem.WordItemPojo
import kotlinx.android.synthetic.main.activity_add_word.*
class AddWordActivity : AppCompatActivity() {
companion object {
private val TAG = "VN/" + AddWordActivity::class.simpleName
private const val VOCABULARIES = "vocabularies"
private const val WORDS = "words"
}
private lateinit var vocabularyId: String
private val db = FirebaseFirestore.getInstance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_word)
vocabularyId = intent.getStringExtra("vocabularyId")
btnSubmitNewWord.setOnClickListener { addWordToDb() }
btnCancel.setOnClickListener { cancel() }
}
private fun addWordToDb() {
val word = etWord.text.toString()
val translation = etTranslation.text.toString()
if (Utils.fieldsNotEmpty(word, translation, "Please, enter word and translation", this)) {
db.collection(VOCABULARIES).document(vocabularyId)
.collection(WORDS).add(WordItemPojo(word, translation)).addOnSuccessListener {
Log.i(TAG, "Successfully added a new word $word")
clearFields()
cancel()
}
.addOnFailureListener {
Log.w(TAG, "addNewWordToDb:failure", it.fillInStackTrace())
Toast.makeText(this, "Couldn't add the word", Toast.LENGTH_SHORT).show()
}
}
}
private fun cancel() {
val intentMainActivity = Intent(this, MainActivity::class.java)
startActivity(intentMainActivity)
}
private fun clearFields() {
etWord.text.clear()
etTranslation.text.clear()
}
}
......@@ -20,8 +20,6 @@ class MainActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)
setUpNavigationView()
extractVocabularyData()
startVocabularyFragment()
startNewWordFragment()
}
private fun logOut() {
......@@ -53,18 +51,9 @@ class MainActivity : AppCompatActivity() {
vocabularyId = vocabulary.id
(supportFragmentManager.findFragmentById(R.id.fragment_vocabulary) as VocabularyFragment)
.retrieveWordsData(vocabularyId)
}
}
private fun startVocabularyFragment() {
}
private fun startNewWordFragment() {
}
companion object {
private val TAG = "VN/" + MainActivity::class.simpleName
}
......
......@@ -86,14 +86,15 @@ class NewWordFragment : Fragment() {
val word = etWord.text.toString()
val translation = etTranslation.text.toString()
val vocabularyId = (activity as MainActivity).vocabularyId
val newWordItemPojo = WordItem.WordItemPojo(word, translation)
FirebaseFirestore.getInstance()
.collection("vocabularies").document(vocabularyId)
.collection("words").add(WordItem.WordItemPojo(word, translation))
.collection("words").add(newWordItemPojo)
.addOnSuccessListener {
Log.i(TAG, "Successfully added a new word")
clearFields()
//todo update recycleView
}
val wordItem = WordItem(newWordItemPojo, it.id, vocabularyId)
updateRecycleView(wordItem) }
.addOnFailureListener {
Log.w(TAG, "addNewWordToDb:failure", it.fillInStackTrace())
Toast.makeText(activity, "Couldn't add the word", Toast.LENGTH_SHORT).show()}
......@@ -104,5 +105,11 @@ class NewWordFragment : Fragment() {
etTranslation.text.clear()
}
private fun updateRecycleView(wordItem: WordItem) {
(activity!!.supportFragmentManager
.findFragmentById(R.id.fragment_vocabulary) as VocabularyFragment)
.addWordItem(wordItem)
}
companion object { private val TAG = "VN/" + NewWordFragment::class.java.simpleName }
}
\ No newline at end of file
......@@ -61,4 +61,8 @@ class VocabularyFragment : Fragment() {
val adapter = VocabularyAdapter(wordItems, activity!!)
recyclerView.adapter = adapter
}
fun addWordItem(newWordItem: WordItem) {
(recyclerView.adapter as VocabularyAdapter).addWordItem(newWordItem)
}
}
\ No newline at end of file
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