Commit 57728fc2 by Paktalin

RecyclerView is updated when a new word is added

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