Commit 350ce549 by Paktalin

Refactoring

parent 638c1d44
package com.paktalin.vocabularynotebook
import android.content.Context
import android.os.Bundle
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.activities.MainActivity
import com.paktalin.vocabularynotebook.ui.fragments.ProgressFragment
import kotlinx.android.synthetic.main.content_main.*
val progressFragment: Fragment = ProgressFragment()
fun addFragment(fragmentManager: FragmentManager, fragment: Fragment, containerId: Int, arguments: Bundle?) {
fragment.arguments = arguments
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, null)
}
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 disableScrolling(mainActivity: MainActivity) {
mainActivity.scrollView!!.setScrollingEnabled(false)
}
fun enableScrolling(mainActivity: MainActivity) {
mainActivity.scrollView!!.setScrollingEnabled(true)
}
fun shortToast(context: Context, text: String) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
}
\ No newline at end of file
package com.paktalin.vocabularynotebook
import android.util.Log
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.paktalin.vocabularynotebook.appsetup.ConfiguredFirestore
import com.paktalin.vocabularynotebook.ui.activities.LogInActivity
import com.paktalin.vocabularynotebook.firestoreitems.UserPojo
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
import java.util.*
private const val TAG = "VN/UserManager"
private fun deleteUser(user: FirebaseUser) {
user.delete()
.addOnSuccessListener { Log.i(TAG, "UserPojo was successfully deleted") }
.addOnFailureListener { Log.i(TAG, "deleteUser:failure", it.cause)}
}
fun addNewUserToDb(newUser: FirebaseUser, logInActivity: LogInActivity) {
//todo add condition to writing to the db in Firebase Console (request.auth.uid)
val db = ConfiguredFirestore.instance
val user = UserPojo(newUser.email)
db.collection(VOCABULARIES).add(Vocabulary.Pojo(null))
.addOnSuccessListener { firstVocabularyRef ->
Log.d(TAG, "VocabularyPojo successfully created: " + firstVocabularyRef.path)
user.vocabularies = Collections.singletonList(firstVocabularyRef)
db.collection("users").document(newUser.uid).set(user)
.addOnCompleteListener { task ->
if (task.isSuccessful) {
Log.i(TAG, "Successfully added user to the collection")
logInActivity.startUserActivity()
} else Log.w(TAG, "addUser:failure", task.exception)
}
}
.addOnFailureListener {
Log.w(TAG, "Couldn't add user to the database", it.cause)
deleteUser(newUser)
}
}
fun login(onComplete: () -> Unit, onSuccess: () -> Unit, onFailure: () -> Unit,
mAuth: FirebaseAuth?, email: String, password: String) {
mAuth!!.signInWithEmailAndPassword(email, password)
.addOnCompleteListener { onComplete() }
.addOnSuccessListener {
Log.d(TAG, "Successfully signed in")
onSuccess()
}
.addOnFailureListener {
Log.w(TAG, "signInWithEmail:failure", it)
onFailure()
}
}
fun signUp(mAuth: FirebaseAuth?, activity: LogInActivity, email: String, password: String) {
mAuth!!.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener { removeProgressBar(activity.supportFragmentManager) }
.addOnSuccessListener {
Log.d(TAG, "Successfully signed up a new user")
addNewUserToDb(mAuth.currentUser!!, activity)
activity.login()
}
.addOnFailureListener {
Log.d(TAG, "createUserWithEmail:failure", it.fillInStackTrace())
shortToast(activity, it.message!!)
}
}
......@@ -12,6 +12,7 @@ import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary
import com.paktalin.vocabularynotebook.firestoreitems.WordItem
import com.paktalin.vocabularynotebook.ui.fragments.EditWordFragment
import com.paktalin.vocabularynotebook.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.addFragment
import kotlinx.android.synthetic.main.word_item.view.*
class VocabularyAdapter(private val displayedVocabulary: Vocabulary, private val mainActivity: MainActivity) : RecyclerView.Adapter<VocabularyAdapter.ViewHolder>() {
......
......@@ -8,6 +8,8 @@ import android.util.Log
import com.google.firebase.auth.FirebaseAuth
import com.paktalin.vocabularynotebook.*
import com.paktalin.vocabularynotebook.utils.fieldsNotEmpty
import com.paktalin.vocabularynotebook.utils.shortToast
import kotlinx.android.synthetic.main.activity_log_in.*
class LogInActivity : AppCompatActivity() {
......@@ -31,13 +33,15 @@ class LogInActivity : AppCompatActivity() {
}
fun login() {
processUser { login(
processUser {
com.paktalin.vocabularynotebook.utils.login(
{ removeProgressBar() },
{ startUserActivity() },
{ showToastFailure() },
mAuth, email!!, password!!) } }
mAuth, email!!, password!!)
} }
private fun signUp() { processUser { signUp(mAuth, this, email!!, password!!) } }
private fun signUp() { processUser { com.paktalin.vocabularynotebook.utils.signUp(mAuth, this, email!!, password!!) } }
private fun processUser(authAction: () -> Unit) {
email = etEmail!!.text.toString()
......@@ -55,9 +59,13 @@ class LogInActivity : AppCompatActivity() {
startActivity(userActivityIntent)
}
private fun addProgressBar() { addProgressBar(supportFragmentManager, R.id.container_login) }
private fun addProgressBar() {
com.paktalin.vocabularynotebook.utils.addProgressBar(supportFragmentManager, R.id.container_login)
}
private fun removeProgressBar() { removeProgressBar(supportFragmentManager) }
private fun removeProgressBar() {
com.paktalin.vocabularynotebook.utils.removeProgressBar(supportFragmentManager)
}
private fun showToastFailure() {
shortToast(this@LogInActivity, getString(R.string.toast_auth_failed))
......@@ -67,7 +75,7 @@ class LogInActivity : AppCompatActivity() {
private fun createRandomUser() {
etEmail.setText("random@gmail.com")
etPassword.setText("123456")
processUser { signUp(mAuth, this@LogInActivity, email!!, password!!) }
processUser { com.paktalin.vocabularynotebook.utils.signUp(mAuth, this@LogInActivity, email!!, password!!) }
}
companion object { private val TAG = "VN/" + LogInActivity::class.simpleName }
......
......@@ -20,6 +20,8 @@ import android.support.v7.widget.SearchView
import com.paktalin.vocabularynotebook.*
import com.paktalin.vocabularynotebook.firestoreitems.Vocabulary.Companion.VOCABULARIES
import com.paktalin.vocabularynotebook.ui.fragments.VocabularyFragment
import com.paktalin.vocabularynotebook.utils.addFragment
import com.paktalin.vocabularynotebook.utils.shortToast
class MainActivity : AppCompatActivity() {
......@@ -101,11 +103,11 @@ class MainActivity : AppCompatActivity() {
}
fun addProgressBar() {
addProgressBar(supportFragmentManager, R.id.container_main)
com.paktalin.vocabularynotebook.utils.addProgressBar(supportFragmentManager, R.id.container_main)
}
fun removeProgressBar() {
removeProgressBar(supportFragmentManager)
com.paktalin.vocabularynotebook.utils.removeProgressBar(supportFragmentManager)
}
fun showToastNoWords() {
......
......@@ -7,7 +7,7 @@ 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 com.paktalin.vocabularynotebook.utils.shortToast
import kotlinx.android.synthetic.main.fragment_editable_word.*
class AddWordFragment : WordFragment() {
......
......@@ -13,6 +13,10 @@ 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.ui.activities.MainActivity
import com.paktalin.vocabularynotebook.utils.disableScrolling
import com.paktalin.vocabularynotebook.utils.enableScrolling
import com.paktalin.vocabularynotebook.utils.removeFragment
import com.paktalin.vocabularynotebook.utils.shortToast
import kotlinx.android.synthetic.main.fragment_editable_word.*
import kotlinx.android.synthetic.main.content_main.*
import kotlinx.android.synthetic.main.word_item.view.*
......
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