Commit 6737ef4b by Paktalin

Refactoring

parent 350ce549
package com.paktalin.vocabularynotebook.utils
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.utils
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!!)
}
}
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