Commit f33592da by Paktalin

Words can be edited

parent 685ba537
......@@ -62,6 +62,12 @@ class VocabularyAdapter(private val wordItems: MutableList<WordItem>, private va
this.notifyItemInserted(0)
}
fun updateWordItem(updatedWordItem: WordItem) {
val updatedItemId = wordItems.indexOf(updatedWordItem)
wordItems[updatedItemId] = updatedWordItem
this.notifyDataSetChanged()
}
@SuppressLint("ResourceType")
private fun editWordItem(container:View, wordItem: WordItem) {
//set container id
......
......@@ -6,15 +6,15 @@ import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import java.io.Serializable
class WordItem(word: String, translation: String, var id: String, private val vocabularyId: String) : Serializable {
var pojo: WordItemPojo? = null
var pojo: Pojo? = null
class WordItemPojo(var word: String?, var translation: String?) : Serializable
class Pojo(var word: String?, var translation: String?) : Serializable
init {
this.pojo = WordItemPojo(word, translation)
this.pojo = Pojo(word, translation)
}
constructor(pojo: WordItemPojo, id: String, vocabularyId: String)
constructor(pojo: Pojo, id: String, vocabularyId: String)
: this(pojo.word!!, pojo.translation!!, id, vocabularyId)
fun delete() {
......
package com.paktalin.vocabularynotebook.appsetup
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.firestore.FirebaseFirestoreSettings
object ConfiguredFirestore {
val instance: FirebaseFirestore
get() {
val firestore = FirebaseFirestore.getInstance()
val settings = FirebaseFirestoreSettings.Builder()
.setTimestampsInSnapshotsEnabled(true)
.build()
firestore.firestoreSettings = settings
return firestore
}
}
package com.paktalin.vocabularynotebook.ui
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.WordItem
class AddWordFragment : WordFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_new_word, container, false)
}
override fun saveToFirestore(wordPojo: WordItem.Pojo, vocabularyId: String) {
ConfiguredFirestore.instance
.collection(VOCABULARIES).document(vocabularyId)
.collection(WORDS).add(wordPojo)
.addOnSuccessListener {
Log.i(TAG, "Successfully added a new word")
clearFields()
val wordItem = WordItem(wordPojo, 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()}
}
override fun updateRecycleView(wordItem: WordItem) {
val vocabularyFragment = activity!!
.supportFragmentManager.findFragmentById(R.id.fragment_vocabulary) as VocabularyFragment
vocabularyFragment.addWordItem(wordItem)
}
companion object { private val TAG = "VN/" + AddWordFragment::class.java.simpleName }
}
\ No newline at end of file
......@@ -2,51 +2,74 @@ package com.paktalin.vocabularynotebook.ui
import android.content.Context
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.WordItem
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import kotlinx.android.synthetic.main.fragment_new_word.*
class EditWordFragment : Fragment() {
class EditWordFragment : WordFragment() {
private lateinit var wordItem: WordItem
private lateinit var etWord: EditText
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
hidePreviousViews(container)
wordItem = arguments!!["wordItem"] as WordItem
return inflater.inflate(R.layout.editable_word_item, container, false)
return inflater.inflate(R.layout.fragment_new_word, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
etWord = view!!.findViewById(R.id.word)
setWordItemData()
setFocusOnWord()
}
private fun setWordItemData() {
etWord.setText(wordItem.pojo!!.word)
view!!.findViewById<EditText>(R.id.translation).setText(wordItem.pojo!!.translation)
word.setText(wordItem.pojo!!.word)
translation.setText(wordItem.pojo!!.translation)
}
private fun setFocusOnWord() {
activity!!.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
etWord.requestFocus()
activity!!.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
word.requestFocus()
val imm = activity!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
imm!!.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}
private fun hidePreviousViews(container: ViewGroup?) {
if (container != null) {
container.findViewById<ImageView>(R.id.line).visibility = View.GONE
container.findViewById<TextView>(R.id.word).visibility = View.GONE
container.findViewById<TextView>(R.id.translation).visibility = View.GONE
}
}
override fun saveToFirestore(wordPojo: WordItem.Pojo, vocabularyId: String) {
ConfiguredFirestore.instance
.collection(VOCABULARIES).document(vocabularyId)
.collection(WORDS).document(wordItem.id).set(wordPojo)
.addOnSuccessListener {
Log.i(TAG, "Successfully updated the word")
hideSubmitButton()
wordItem.pojo = wordPojo
updateRecycleView(wordItem) }
.addOnFailureListener {
Log.w(TAG, "updateExistingWord:failure", it.fillInStackTrace())
Toast.makeText(activity, "Couldn't update the word", Toast.LENGTH_SHORT).show()} }
override fun updateRecycleView(wordItem: WordItem) {
val vocabularyFragment = activity!!
.supportFragmentManager.findFragmentById(R.id.fragment_vocabulary) as VocabularyFragment
vocabularyFragment.updateWordItem(wordItem)
}
companion object { private val TAG = "VN/" + EditWordFragment::class.java.simpleName }
}
\ No newline at end of file
......@@ -63,4 +63,8 @@ class VocabularyFragment : Fragment() {
fun addWordItem(newWordItem: WordItem) {
(recyclerView.adapter as VocabularyAdapter).addWordItem(newWordItem)
}
fun updateWordItem(updatedWordItem: WordItem) {
(recyclerView.adapter as VocabularyAdapter).updateWordItem(updatedWordItem)
}
}
\ No newline at end of file
......@@ -4,30 +4,22 @@ import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageButton
import android.widget.Toast
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.WordItem
import kotlinx.android.synthetic.main.editable_word_item.*
import kotlinx.android.synthetic.main.fragment_new_word.*
class NewWordFragment : Fragment() {
abstract class WordFragment : Fragment() {
protected val VOCABULARIES = "vocabularies"
protected val WORDS = "words"
private var wordEmpty: Boolean = true
set(value) { field = value; updateButtons() }
set(value) { field = value; updateButtons() }
private var translationEmpty: Boolean = true
set(value) { field = value; updateButtons() }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_new_word, container, false)
}
set(value) { field = value; updateButtons() }
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
......@@ -37,72 +29,56 @@ class NewWordFragment : Fragment() {
translation.addTextChangedListener(textWatcher {
translationEmpty = translation.text.isEmpty() })
btnClear.setOnClickListener {
word.text.clear()
translation.text.clear()
}
activity!!.findViewById<ImageButton>(R.id.btnAddWord).setOnClickListener { addWord() }
btnClear.setOnClickListener { clearFields() }
activity!!.findViewById<ImageButton>(R.id.btnAddWord).setOnClickListener { submitWord() }
}
private fun textWatcher(setEmpty: () -> Unit): TextWatcher {
return object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun afterTextChanged(editable: Editable) { setEmpty() }
}
}
private fun updateButtons() {
if (!wordEmpty || !translationEmpty)
showClearButton()
if (!wordEmpty && !translationEmpty)
showAddWordButton()
showSubmitButton()
if (wordEmpty || translationEmpty)
hideAddWordButton()
hideSubmitButton()
if (wordEmpty && translationEmpty)
hideClearButton()
}
private fun showAddWordButton() {
activity!!.findViewById<FrameLayout>(R.id.btnAddWordLayout).visibility = View.VISIBLE }
private fun textWatcher(setEmpty: () -> Unit): TextWatcher {
return object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun afterTextChanged(editable: Editable) { setEmpty() }
}
}
private fun showSubmitButton() {
activity!!.findViewById<FrameLayout>(R.id.btnSubmitLayout).visibility = View.VISIBLE }
private fun hideAddWordButton() {
activity!!.findViewById<FrameLayout>(R.id.btnAddWordLayout).visibility = View.GONE }
protected fun hideSubmitButton() {
activity!!.findViewById<FrameLayout>(R.id.btnSubmitLayout).visibility = View.GONE }
private fun hideClearButton() { btnClear.visibility = View.INVISIBLE }
private fun showClearButton() { btnClear.visibility = View.VISIBLE }
private fun addWord() {
private fun submitWord() {
(activity as MainActivity).hideKeyboardNotFromActivity(activity as MainActivity)
val word = word.text.toString()
val translation = translation.text.toString()
val vocabularyId = (activity as MainActivity).vocabularyId
val newWordItemPojo = WordItem.WordItemPojo(word, translation)
ConfiguredFirestore.instance
.collection("vocabularies").document(vocabularyId)
.collection("words").add(newWordItemPojo)
.addOnSuccessListener {
Log.i(TAG, "Successfully added a new word")
clearFields()
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()}
val wordPojo = WordItem.Pojo(word, translation)
saveToFirestore(wordPojo, vocabularyId)
}
private fun clearFields() {
protected fun clearFields() {
word.text.clear()
translation.text.clear()
}
private fun updateRecycleView(newWordItem: WordItem) {
(activity!!.supportFragmentManager
.findFragmentById(R.id.fragment_vocabulary) as VocabularyFragment)
.addWordItem(newWordItem)
}
companion object { private val TAG = "VN/" + NewWordFragment::class.java.simpleName }
protected abstract fun saveToFirestore(wordPojo:WordItem.Pojo, vocabularyId:String)
protected abstract fun updateRecycleView(wordItem: WordItem)
}
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:paddingLeft="16dp"
android:paddingStart="16dp"
android:paddingRight="16dp"
android:paddingEnd="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:background="@android:color/transparent"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:id="@+id/word"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:hint="@string/hint_new_word"
android:inputType="text"
android:textSize="22sp"
app:fontFamily="@font/neucha"
android:textColor="#000F55"
tools:ignore="LabelFor" />
<EditText
android:id="@+id/translation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:hint="@string/hint_translation"
android:inputType="text"
android:textSize="22sp"
app:fontFamily="@font/neucha"
android:textColor="#000F55"
tools:ignore="LabelFor" />
</LinearLayout>
\ No newline at end of file
......@@ -20,7 +20,44 @@
android:layout_marginStart="8dp"
tools:ignore="ContentDescription" />
<include layout="@layout/editable_word_item" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="16dp"
android:paddingStart="16dp"
android:paddingRight="16dp"
android:paddingEnd="16dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:background="@android:color/transparent">
<EditText
android:id="@+id/word"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:hint="@string/hint_new_word"
android:inputType="text"
android:textSize="22sp"
app:fontFamily="@font/neucha"
android:textColor="#000F55"
tools:ignore="LabelFor" />
<EditText
android:id="@+id/translation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/transparent"
android:hint="@string/hint_translation"
android:inputType="text"
android:textSize="22sp"
app:fontFamily="@font/neucha"
android:textColor="#000F55"
tools:ignore="LabelFor" />
</LinearLayout>
<ImageButton
android:id="@+id/btnClear"
......
......@@ -25,7 +25,7 @@
<fragment
android:id="@+id/fragment_new_word"
android:name="com.paktalin.vocabularynotebook.ui.NewWordFragment"
android:name="com.paktalin.vocabularynotebook.ui.AddWordFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp" />
......@@ -40,7 +40,7 @@
</ScrollView>
<FrameLayout
android:id="@+id/btnAddWordLayout"
android:id="@+id/btnSubmitLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
......
......@@ -8,6 +8,7 @@
android:layout_height="wrap_content">
<ImageView
android:id="@+id/line"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/line"
......
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