Commit 54f4a681 by Paktalin

Introduced ProgressFragment and ActivityUtil

parent 0bd690e9
package com.paktalin.vocabularynotebook
import android.content.Context
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.text.TextUtils
import android.widget.Toast
import com.paktalin.vocabularynotebook.ui.ProgressFragment
class Utils {
companion object {
val progressFragment: Fragment = ProgressFragment()
fun fieldsNotEmpty(text1: String, text2: String, toastMessage: String, context: Context): Boolean {
if (TextUtils.isEmpty(text1) || TextUtils.isEmpty(text2)) {
Utils.shortToast(context, toastMessage)
return false
}
return true
}
fun addFragment(fragmentManager: FragmentManager, fragment: Fragment, containerId: Int) {
fragmentManager.beginTransaction().add(containerId, fragment).commitAllowingStateLoss()
}
fun shortToast(context: Context, text: String) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
}
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)) {
shortToast(context, toastMessage)
return false
}
return true
}
fun shortToast(context: Context, text: String) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
}
\ No newline at end of file
......@@ -3,11 +3,11 @@ package com.paktalin.vocabularynotebook.ui
import android.util.Log
import android.view.View
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.Utils
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.WORDS
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.shortToast
import kotlinx.android.synthetic.main.fragment_new_word.*
class AddWordFragment : WordFragment() {
......@@ -20,12 +20,12 @@ class AddWordFragment : WordFragment() {
.addOnSuccessListener {
Log.i(TAG, "Successfully added a new word")
clearFields()
mainActivity.hideProgressBar()
mainActivity.removeProgressBar()
val wordItem = WordItem(wordPojo, it.id, vocabularyId)
updateRecycleView(wordItem) }
.addOnFailureListener {
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
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.Utils
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
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.notebook_sheet.*
import kotlinx.android.synthetic.main.word_item.view.*
......@@ -61,14 +60,14 @@ class EditWordFragment : WordFragment() {
.addOnSuccessListener {
Log.i(TAG, "Successfully updated the word")
hideSubmitButton()
mainActivity.hideProgressBar()
mainActivity.removeProgressBar()
wordItem.pojo = wordPojo
updateRecycleView(wordItem)
stop()
}
.addOnFailureListener {
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()
}
}
......
......@@ -11,7 +11,8 @@ import com.google.firebase.auth.FirebaseAuth
import kotlinx.android.synthetic.main.activity_log_in.*
import com.paktalin.vocabularynotebook.R
import com.paktalin.vocabularynotebook.UserManager
import com.paktalin.vocabularynotebook.Utils
import com.paktalin.vocabularynotebook.fieldsNotEmpty
import com.paktalin.vocabularynotebook.shortToast
class LogInActivity : AppCompatActivity() {
private var mAuth: FirebaseAuth? = null
......@@ -35,7 +36,7 @@ class LogInActivity : AppCompatActivity() {
val email = etEmail!!.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()
mAuth!!.signInWithEmailAndPassword(email, password)
.addOnCompleteListener { hideProgressBar() }
......@@ -45,7 +46,7 @@ class LogInActivity : AppCompatActivity() {
}
.addOnFailureListener {
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() {
val email = etEmail!!.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 verify email
showProgressBar()
......@@ -66,7 +67,7 @@ class LogInActivity : AppCompatActivity() {
}
.addOnFailureListener {
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
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.DocumentReference
import com.paktalin.vocabularynotebook.R
import kotlinx.android.synthetic.main.activity_main.*
import android.view.WindowManager
import android.app.Activity
......@@ -16,11 +15,10 @@ import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.inputmethod.InputMethodManager
import com.paktalin.vocabularynotebook.VocabularyAdapter
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import kotlinx.android.synthetic.main.fragment_vocabulary.*
import android.support.v7.widget.SearchView
import com.paktalin.vocabularynotebook.Utils
import com.paktalin.vocabularynotebook.*
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
......@@ -71,10 +69,10 @@ class MainActivity : AppCompatActivity() {
val db = ConfiguredFirestore.instance
val userDocument = db.collection("users").document(userId)
showProgressBar()
addProgressBar()
userDocument.get()
.addOnSuccessListener { task ->
hideProgressBar()
removeProgressBar()
//todo move Firestore logic and collections names to a separate class
if (task.get(VOCABULARIES) != null) {
val vocabularies: List<DocumentReference> = task.get(VOCABULARIES) as List<DocumentReference>
......@@ -105,12 +103,16 @@ class MainActivity : AppCompatActivity() {
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() {
Utils.shortToast(this, getString(R.string.toast_no_words))
shortToast(this, getString(R.string.toast_no_words))
}
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() {
val word = word.text.toString()
val translation = translation.text.toString()
val vocabularyId = mainActivity.vocabularyId
mainActivity.showProgressBar()
mainActivity.addProgressBar()
saveToFirestore(word, translation, vocabularyId)
return
}
......
......@@ -9,6 +9,7 @@
android:background="#66330E">
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="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