Commit 54f4a681 by Paktalin

Introduced ProgressFragment and ActivityUtil

parent 0bd690e9
package com.paktalin.vocabularynotebook package com.paktalin.vocabularynotebook
import android.content.Context import android.content.Context
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.text.TextUtils import android.text.TextUtils
import android.widget.Toast import android.widget.Toast
import com.paktalin.vocabularynotebook.ui.ProgressFragment
class Utils { val progressFragment: Fragment = ProgressFragment()
companion object {
fun fieldsNotEmpty(text1: String, text2: String, toastMessage: String, context: Context): Boolean { fun addFragment(fragmentManager: FragmentManager, fragment: Fragment, containerId: Int) {
fragmentManager.beginTransaction().add(containerId, fragment).commitAllowingStateLoss()
}
fun removeFragment(fragmentManager: FragmentManager, fragment: Fragment) {
fragmentManager.beginTransaction().remove(fragment).commitAllowingStateLoss()
}
fun addProgressBar(fragmentManager: FragmentManager, containerId: Int) {
addFragment(fragmentManager, progressFragment, containerId)
}
fun removeProgressBar(fragmentManager: FragmentManager) {
removeFragment(fragmentManager, progressFragment)
}
fun fieldsNotEmpty(text1: String, text2: String, toastMessage: String, context: Context): Boolean {
if (TextUtils.isEmpty(text1) || TextUtils.isEmpty(text2)) { if (TextUtils.isEmpty(text1) || TextUtils.isEmpty(text2)) {
Utils.shortToast(context, toastMessage) shortToast(context, toastMessage)
return false return false
} }
return true return true
} }
fun shortToast(context: Context, text: String) { fun shortToast(context: Context, text: String) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show() Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
}
}
} }
\ No newline at end of file
...@@ -3,11 +3,11 @@ package com.paktalin.vocabularynotebook.ui ...@@ -3,11 +3,11 @@ package com.paktalin.vocabularynotebook.ui
import android.util.Log import android.util.Log
import android.view.View import android.view.View
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.Utils
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.WORDS import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.WORDS
import com.paktalin.vocabularynotebook.firestoreitems.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.shortToast
import kotlinx.android.synthetic.main.fragment_new_word.* import kotlinx.android.synthetic.main.fragment_new_word.*
class AddWordFragment : WordFragment() { class AddWordFragment : WordFragment() {
...@@ -20,12 +20,12 @@ class AddWordFragment : WordFragment() { ...@@ -20,12 +20,12 @@ class AddWordFragment : WordFragment() {
.addOnSuccessListener { .addOnSuccessListener {
Log.i(TAG, "Successfully added a new word") Log.i(TAG, "Successfully added a new word")
clearFields() clearFields()
mainActivity.hideProgressBar() mainActivity.removeProgressBar()
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())
Utils.shortToast(mainActivity, getString(R.string.toast_new_word_fail)) shortToast(mainActivity, getString(R.string.toast_new_word_fail))
} }
} }
......
...@@ -7,13 +7,12 @@ import android.view.LayoutInflater ...@@ -7,13 +7,12 @@ import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.Utils
import com.paktalin.vocabularynotebook.firestoreitems.WordItem import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.WORDS import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.WORDS
import com.paktalin.vocabularynotebook.shortToast
import kotlinx.android.synthetic.main.fragment_new_word.* import kotlinx.android.synthetic.main.fragment_new_word.*
import kotlinx.android.synthetic.main.notebook_sheet.* import kotlinx.android.synthetic.main.notebook_sheet.*
import kotlinx.android.synthetic.main.word_item.view.* import kotlinx.android.synthetic.main.word_item.view.*
...@@ -61,14 +60,14 @@ class EditWordFragment : WordFragment() { ...@@ -61,14 +60,14 @@ class EditWordFragment : WordFragment() {
.addOnSuccessListener { .addOnSuccessListener {
Log.i(TAG, "Successfully updated the word") Log.i(TAG, "Successfully updated the word")
hideSubmitButton() hideSubmitButton()
mainActivity.hideProgressBar() mainActivity.removeProgressBar()
wordItem.pojo = wordPojo wordItem.pojo = wordPojo
updateRecycleView(wordItem) updateRecycleView(wordItem)
stop() stop()
} }
.addOnFailureListener { .addOnFailureListener {
Log.w(TAG, "updateExistingWord:failure", it.fillInStackTrace()) Log.w(TAG, "updateExistingWord:failure", it.fillInStackTrace())
Utils.shortToast(mainActivity, getString(R.string.toast_update_word_failed)) shortToast(mainActivity, getString(R.string.toast_update_word_failed))
stop() stop()
} }
} }
......
...@@ -11,7 +11,8 @@ import com.google.firebase.auth.FirebaseAuth ...@@ -11,7 +11,8 @@ import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.activity_log_in.* import kotlinx.android.synthetic.main.activity_log_in.*
import com.paktalin.vocabularynotebook.R import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.UserManager import com.paktalin.vocabularynotebook.UserManager
import com.paktalin.vocabularynotebook.Utils import com.paktalin.vocabularynotebook.fieldsNotEmpty
import com.paktalin.vocabularynotebook.shortToast
class LogInActivity : AppCompatActivity() { class LogInActivity : AppCompatActivity() {
private var mAuth: FirebaseAuth? = null private var mAuth: FirebaseAuth? = null
...@@ -35,7 +36,7 @@ class LogInActivity : AppCompatActivity() { ...@@ -35,7 +36,7 @@ class LogInActivity : AppCompatActivity() {
val email = etEmail!!.text.toString() val email = etEmail!!.text.toString()
val password = etPassword!!.text.toString() val password = etPassword!!.text.toString()
if (Utils.fieldsNotEmpty(email, password, "Please, enter email and password", this)) { if (fieldsNotEmpty(email, password, "Please, enter email and password", this)) {
showProgressBar() showProgressBar()
mAuth!!.signInWithEmailAndPassword(email, password) mAuth!!.signInWithEmailAndPassword(email, password)
.addOnCompleteListener { hideProgressBar() } .addOnCompleteListener { hideProgressBar() }
...@@ -45,7 +46,7 @@ class LogInActivity : AppCompatActivity() { ...@@ -45,7 +46,7 @@ class LogInActivity : AppCompatActivity() {
} }
.addOnFailureListener { .addOnFailureListener {
Log.w(TAG, "signInWithEmail:failure", it) Log.w(TAG, "signInWithEmail:failure", it)
Utils.shortToast(this@LogInActivity, getString(R.string.toast_auth_failed)) shortToast(this@LogInActivity, getString(R.string.toast_auth_failed))
} }
} }
} }
...@@ -54,7 +55,7 @@ class LogInActivity : AppCompatActivity() { ...@@ -54,7 +55,7 @@ class LogInActivity : AppCompatActivity() {
val email = etEmail!!.text.toString() val email = etEmail!!.text.toString()
val password = etPassword!!.text.toString() val password = etPassword!!.text.toString()
if (Utils.fieldsNotEmpty(email, password, "Please, enter email and password", this)) { if (fieldsNotEmpty(email, password, "Please, enter email and password", this)) {
//todo check if the password is good //todo check if the password is good
// todo verify email // todo verify email
showProgressBar() showProgressBar()
...@@ -66,7 +67,7 @@ class LogInActivity : AppCompatActivity() { ...@@ -66,7 +67,7 @@ class LogInActivity : AppCompatActivity() {
} }
.addOnFailureListener { .addOnFailureListener {
Log.d(TAG, "createUserWithEmail:failure", it.fillInStackTrace()) Log.d(TAG, "createUserWithEmail:failure", it.fillInStackTrace())
Utils.shortToast(this@LogInActivity, it.message!!) shortToast(this@LogInActivity, it.message!!)
} }
} }
} }
......
...@@ -7,7 +7,6 @@ import android.util.Log ...@@ -7,7 +7,6 @@ import android.util.Log
import com.google.firebase.auth.FirebaseAuth import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.DocumentReference import com.google.firebase.firestore.DocumentReference
import com.paktalin.vocabularynotebook.R
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import android.view.WindowManager import android.view.WindowManager
import android.app.Activity import android.app.Activity
...@@ -16,11 +15,10 @@ import android.view.Menu ...@@ -16,11 +15,10 @@ import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import com.paktalin.vocabularynotebook.VocabularyAdapter
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import kotlinx.android.synthetic.main.fragment_vocabulary.* import kotlinx.android.synthetic.main.fragment_vocabulary.*
import android.support.v7.widget.SearchView import android.support.v7.widget.SearchView
import com.paktalin.vocabularynotebook.Utils import com.paktalin.vocabularynotebook.*
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
...@@ -71,10 +69,10 @@ class MainActivity : AppCompatActivity() { ...@@ -71,10 +69,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)
showProgressBar() addProgressBar()
userDocument.get() userDocument.get()
.addOnSuccessListener { task -> .addOnSuccessListener { task ->
hideProgressBar() removeProgressBar()
//todo move Firestore logic and collections names to a separate class //todo move Firestore logic and collections names to a separate class
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>
...@@ -105,12 +103,16 @@ class MainActivity : AppCompatActivity() { ...@@ -105,12 +103,16 @@ class MainActivity : AppCompatActivity() {
imm.hideSoftInputFromWindow(view.windowToken, 0) imm.hideSoftInputFromWindow(view.windowToken, 0)
} }
fun showProgressBar() { progress.visibility = View.VISIBLE } fun addProgressBar() {
addProgressBar(supportFragmentManager, R.id.frameLayout)
}
fun hideProgressBar() { progress.visibility = View.GONE } fun removeProgressBar() {
removeProgressBar(supportFragmentManager)
}
fun showToastNoWords() { fun showToastNoWords() {
Utils.shortToast(this, getString(R.string.toast_no_words)) shortToast(this, getString(R.string.toast_no_words))
} }
fun removeFragment(fragment: Fragment) { fun removeFragment(fragment: Fragment) {
......
package com.paktalin.vocabularynotebook.ui
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.paktalin.vocabularynotebook.R
class ProgressFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_progress, container, false)
}
}
...@@ -63,7 +63,7 @@ abstract class WordFragment : Fragment() { ...@@ -63,7 +63,7 @@ abstract class WordFragment : Fragment() {
val word = word.text.toString() val word = word.text.toString()
val translation = translation.text.toString() val translation = translation.text.toString()
val vocabularyId = mainActivity.vocabularyId val vocabularyId = mainActivity.vocabularyId
mainActivity.showProgressBar() mainActivity.addProgressBar()
saveToFirestore(word, translation, vocabularyId) saveToFirestore(word, translation, vocabularyId)
return return
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
android:background="#66330E"> android:background="#66330E">
<FrameLayout <FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent">
......
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
\ 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