Commit fccb0efd by Paktalin

Added progressBar to AddWordFragment and EditWordFragment

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