Commit fccb0efd by Paktalin

Added progressBar to AddWordFragment and EditWordFragment

parent bb6016cf
......@@ -13,25 +13,28 @@ import com.paktalin.vocabularynotebook.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
.collection(vocabularies).document(vocabularyId)
.collection(words).add(wordPojo)
.addOnSuccessListener {
Log.i(TAG, "Successfully added a new word")
clearFields()
mainActivity.hideProgressBar()
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()}
Toast.makeText(mainActivity, "Couldn't add the word", Toast.LENGTH_SHORT).show()}
}
override fun updateRecycleView(wordItem: WordItem) {
val vocabularyFragment = activity!!
val vocabularyFragment = mainActivity
.supportFragmentManager.findFragmentById(R.id.fragment_vocabulary) as VocabularyFragment
vocabularyFragment.addWordItem(wordItem)
}
......
......@@ -6,7 +6,6 @@ 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.ImageView
import android.widget.TextView
......@@ -20,6 +19,7 @@ class EditWordFragment : WordFragment() {
private lateinit var wordItem: WordItem
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
mainActivity = activity as MainActivity
hidePreviousViews(container)
wordItem = arguments!!["wordItem"] as WordItem
return inflater.inflate(R.layout.fragment_new_word, container, false)
......@@ -37,9 +37,8 @@ class EditWordFragment : WordFragment() {
}
private fun setFocusOnWord() {
activity!!.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
word.requestFocus()
val imm = activity!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
val imm = mainActivity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
imm!!.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}
......@@ -52,20 +51,22 @@ class EditWordFragment : WordFragment() {
}
override fun saveToFirestore(wordPojo: WordItem.Pojo, vocabularyId: String) {
mainActivity.showProgressBar()
ConfiguredFirestore.instance
.collection(vocabularies).document(vocabularyId)
.collection(words).document(wordItem.id).set(wordPojo)
.addOnSuccessListener {
Log.i(TAG, "Successfully updated the word")
hideSubmitButton()
mainActivity.hideProgressBar()
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()} }
Toast.makeText(mainActivity, "Couldn't update the word", Toast.LENGTH_SHORT).show()} }
override fun updateRecycleView(wordItem: WordItem) {
val vocabularyFragment = activity!!
val vocabularyFragment = mainActivity
.supportFragmentManager.findFragmentById(R.id.fragment_vocabulary) as VocabularyFragment
vocabularyFragment.updateWordItem(wordItem)
}
......
......@@ -51,10 +51,10 @@ class MainActivity : AppCompatActivity() {
val db = ConfiguredFirestore.instance
val userDocument = db.collection("users").document(userId)
progress.visibility = View.VISIBLE
showProgressBar()
userDocument.get()
.addOnSuccessListener { task ->
progress.visibility = View.GONE
hideProgressBar()
if (task.get("vocabularies") != null) {
val vocabularies: List<DocumentReference> = task.get("vocabularies") as List<DocumentReference>
//todo represent specific vocabulary instead of the first one
......@@ -78,6 +78,14 @@ class MainActivity : AppCompatActivity() {
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
fun showProgressBar() {
progress.visibility = View.VISIBLE
}
fun hideProgressBar() {
progress.visibility = View.GONE
}
override fun onPause() {
super.onPause()
hideKeyboard()
......
......@@ -14,6 +14,7 @@ import kotlinx.android.synthetic.main.fragment_new_word.*
abstract class WordFragment : Fragment() {
protected val vocabularies = "vocabularies"
protected val words = "words"
protected lateinit var mainActivity: MainActivity
private var wordEmpty: Boolean = true
set(value) { field = value; updateButtons() }
......
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