Commit a046a823 by Paktalin

Fixed bug with wrong clickListener

parent d14cc276
......@@ -90,11 +90,11 @@ class VocabularyAdapter(private val vocabulary: Vocabulary, private val activity
} else container.id = 18071999
// start EditWordFragment
val wordInfoFragment = EditWordFragment()
val editWordFragment = EditWordFragment()
val arguments = Bundle()
arguments.putSerializable("wordItem", wordItem)
wordInfoFragment.arguments = arguments
(activity as MainActivity).supportFragmentManager.beginTransaction().add(container.id, wordInfoFragment).commit()
editWordFragment.arguments = arguments
(activity as MainActivity).supportFragmentManager.beginTransaction().add(container.id, editWordFragment).commit()
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
......
......@@ -13,10 +13,6 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p
init {
if (time == null) time = Date(System.currentTimeMillis())
}
fun print(): String {
return "word: $word; translation: $translation; time: $time"
}
}
constructor(pojo: Pojo, id: String, vocabularyId: String)
......@@ -24,7 +20,7 @@ class WordItem(word: String, translation: String, time: Date?, var id: String, p
fun delete() {
ConfiguredFirestore.instance.collection("vocabularies").document(vocabularyId)
.collection("WORDS").document(id).delete()
.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()) }
}
......
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.firestoreitems.WordItem
class AddWordFragment : WordFragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mainActivity = activity as MainActivity
return inflater.inflate(R.layout.fragment_new_word, container, false)
}
override fun saveToFirestore(wordPojo: WordItem.Pojo, vocabularyId: String) {
mainActivity.showProgressBar()
ConfiguredFirestore.instance
......
......@@ -7,6 +7,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
......@@ -22,7 +23,7 @@ class EditWordFragment : WordFragment() {
mainActivity = activity as MainActivity
hidePreviousViews(container)
wordItem = arguments!!["wordItem"] as WordItem
return inflater.inflate(R.layout.fragment_new_word, container, false)
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
......@@ -60,7 +61,12 @@ class EditWordFragment : WordFragment() {
hideSubmitButton()
mainActivity.hideProgressBar()
wordItem.pojo = wordPojo
updateRecycleView(wordItem) }
updateRecycleView(wordItem)
// set onClickListener from AddWordFragment
mainActivity.findViewById<ImageButton>(R.id.btnAddWord).setOnClickListener {
(mainActivity.supportFragmentManager.findFragmentById(R.id.fragment_new_word) as AddWordFragment).submitWord()}
mainActivity.removeFragment(this@EditWordFragment) }
.addOnFailureListener {
Log.w(TAG, "updateExistingWord:failure", it.fillInStackTrace())
Toast.makeText(mainActivity, "Couldn't update the word", Toast.LENGTH_SHORT).show()} }
......
......@@ -11,6 +11,7 @@ import com.paktalin.vocabularynotebook.R
import kotlinx.android.synthetic.main.activity_main.*
import android.view.WindowManager
import android.app.Activity
import android.support.v4.app.Fragment
import android.view.Menu
import android.view.MenuItem
import android.view.View
......@@ -80,7 +81,8 @@ class MainActivity : AppCompatActivity() {
val arguments = Bundle()
arguments.putString("vocabularyId", vocabularyId)
vocabularyFragment.arguments = arguments
supportFragmentManager.beginTransaction().add(R.id.fragment_container, vocabularyFragment).commitNowAllowingStateLoss()
supportFragmentManager.beginTransaction().add(R.id.fragment_container, vocabularyFragment)
.commitNowAllowingStateLoss()
} else {
Log.w(TAG, "There's no collection \"vocabularies\"")
showToastNoWords() }
......@@ -94,9 +96,7 @@ class MainActivity : AppCompatActivity() {
fun hideKeyboardNotFromActivity(activity: Activity) {
val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
var view = activity.currentFocus
if (view == null) {
view = View(activity)
}
if (view == null) { view = View(activity) }
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
......@@ -106,7 +106,11 @@ class MainActivity : AppCompatActivity() {
fun showToastNoWords() {
Toast.makeText(this@MainActivity,
"You don't have any WORDS yet. Add your fist one!", Toast.LENGTH_SHORT).show()
"You don't have any words yet. Add your fist one!", Toast.LENGTH_SHORT).show()
}
fun removeFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction().remove(fragment).commitAllowingStateLoss()
}
override fun onPause() {
......
......@@ -30,7 +30,6 @@ class VocabularyFragment : Fragment() {
return inflater.inflate(R.layout.fragment_vocabulary, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
setEmptyAdapter()
......@@ -57,7 +56,7 @@ class VocabularyFragment : Fragment() {
if (it.documents.size != 0)
setVocabularyAdapter(it.documents, vocabularyId)
else {
Log.i(TAG, "There are no documents in collection \"WORDS\"")
Log.i(TAG, "There are no documents in collection \"words\"")
(activity as MainActivity).showToastNoWords()
}}
}
......
......@@ -4,7 +4,9 @@ import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageButton
import com.paktalin.vocabularynotebook.R
......@@ -18,6 +20,7 @@ abstract class WordFragment : Fragment() {
const val WORDS = "words"
}
protected lateinit var mainActivity: MainActivity
open var TAG = WordFragment::class.simpleName
private var wordEmpty: Boolean = true
set(value) { field = value; updateButtons() }
......@@ -25,8 +28,14 @@ abstract class WordFragment : Fragment() {
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)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
mainActivity = activity as MainActivity
mainActivity.findViewById<ImageButton>(R.id.btnAddWord).setOnClickListener { submitWord() }
word.addTextChangedListener(textWatcher {
wordEmpty = word.text.isEmpty() })
......@@ -34,10 +43,8 @@ abstract class WordFragment : Fragment() {
translationEmpty = translation.text.isEmpty() })
btnClear.setOnClickListener { clearFields() }
activity!!.findViewById<ImageButton>(R.id.btnAddWord).setOnClickListener { submitWord() }
}
private fun updateButtons() {
if (!wordEmpty || !translationEmpty)
showClearButton()
......@@ -58,24 +65,25 @@ abstract class WordFragment : Fragment() {
}
private fun showSubmitButton() {
activity!!.findViewById<FrameLayout>(R.id.btnSubmitLayout).visibility = View.VISIBLE }
mainActivity.findViewById<FrameLayout>(R.id.btnSubmitLayout).visibility = View.VISIBLE }
protected fun hideSubmitButton() {
activity!!.findViewById<FrameLayout>(R.id.btnSubmitLayout).visibility = View.GONE }
mainActivity.findViewById<FrameLayout>(R.id.btnSubmitLayout).visibility = View.GONE }
private fun hideClearButton() { btnClear.visibility = View.INVISIBLE }
private fun showClearButton() { btnClear.visibility = View.VISIBLE }
private fun submitWord() {
(activity as MainActivity).hideKeyboardNotFromActivity(activity as MainActivity)
fun submitWord() {
mainActivity.hideKeyboardNotFromActivity(mainActivity)
val word = word.text.toString()
val translation = translation.text.toString()
val vocabularyId = (activity as MainActivity).vocabularyId
val vocabularyId = mainActivity.vocabularyId
val wordPojo = WordItem.Pojo(word, translation, null)
saveToFirestore(wordPojo, vocabularyId)
return
}
protected fun clearFields() {
......
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