Skip to content

Commit

Permalink
Merge pull request #19 from Diversion2k22/HazArdDonorFirestore
Browse files Browse the repository at this point in the history
Implemented FIRESTORE for donor's information collection
  • Loading branch information
1HazArd1 authored Feb 28, 2022
2 parents 218cfc6 + fb04631 commit bbdd992
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 61 deletions.
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ android {

defaultConfig {
applicationId "com.hazard.samarpan"
minSdk 16
minSdk 23
targetSdk 31
versionCode 1
versionName "1.0"
Expand Down Expand Up @@ -41,6 +41,7 @@ dependencies {
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
implementation 'com.google.android.material:material:1.4.0'
implementation 'com.google.firebase:firebase-auth-ktx:21.0.1'
implementation 'com.google.firebase:firebase-firestore-ktx:24.0.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/hazard/samarpan/LoginFragment.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.hazard.samarpan

import NgoSignup1Fragment
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
Expand Down Expand Up @@ -53,10 +52,10 @@ class LoginFragment: Fragment() {
btnLogin?.setOnClickListener {
val mail=email?.text.toString().trim()
val pass=password?.text.toString().trim()
if(TextUtils.isEmpty(mail)){
if(mail.isEmpty()){
email?.error = "Email cannot be empty"
}
if(TextUtils.isEmpty(pass)){
if(pass.isEmpty()){
password?.error = "Password cannot be empty"
}
else{
Expand All @@ -66,6 +65,8 @@ class LoginFragment: Fragment() {

if (task.isSuccessful) {
// logic for navigating to the dashboard
/* here based on the logic of the type of user the respective activity would be loaded
i.e is the user ngo or donor */
Toast.makeText(
activity,
"Logged in Successfully",
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/hazard/samarpan/NgoSignup1Fragment.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.hazard.samarpan

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import com.hazard.samarpan.NgoSignup2Fragment
import com.hazard.samarpan.R

class NgoSignup1Fragment : Fragment() {

lateinit var nextBtn: Button
private lateinit var nextBtn: Button

override fun onCreateView(
inflater: LayoutInflater,
Expand Down
159 changes: 106 additions & 53 deletions app/src/main/java/com/hazard/samarpan/UserRegisterFragment.kt
Original file line number Diff line number Diff line change
@@ -1,88 +1,138 @@
package com.hazard.samarpan

import android.content.ContentValues.TAG
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.google.android.gms.tasks.OnCompleteListener
import com.google.android.material.textfield.TextInputEditText
import com.google.firebase.auth.AuthResult
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.ktx.auth
import com.google.firebase.firestore.DocumentReference
import com.google.firebase.firestore.FirebaseFirestore
import com.google.firebase.ktx.Firebase
import java.util.regex.Pattern

class UserRegisterFragment: Fragment(){
private var itemView:View?=null
class UserRegisterFragment : Fragment() {
private var itemView: View? = null

private var name: TextInputEditText?=null
private var email: TextInputEditText?=null
private var password: TextInputEditText?=null
private var phoneNo: TextInputEditText?=null
private var btnRegister : Button?=null
private var btnSignIn: LinearLayout?=null
private var name: TextInputEditText? = null
private var email: TextInputEditText? = null
private var password: TextInputEditText? = null
private var phoneNo: TextInputEditText? = null
private var btnRegister: Button? = null
private var btnSignIn: LinearLayout? = null
private var etAddress: TextInputEditText? = null
private var etPinCode: TextInputEditText? = null


private lateinit var registerAuth: FirebaseAuth

private val emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+"
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {

itemView=layoutInflater.inflate(R.layout.user_registration_fragment,container,false)
itemView = layoutInflater.inflate(R.layout.user_registration_fragment, container, false)

btnRegister=itemView?.findViewById(R.id.btnUserRegister)
email=itemView?.findViewById(R.id.registerEmail)
password=itemView?.findViewById(R.id.registerPassword)
name=itemView?.findViewById(R.id.registerName)
phoneNo=itemView?.findViewById(R.id.registerPhone)
btnSignIn=itemView?.findViewById(R.id.btnRegisterSignIn)
btnRegister = itemView?.findViewById(R.id.btnUserRegister)
btnSignIn = itemView?.findViewById(R.id.btnRegisterSignIn)

registerAuth= Firebase.auth
email = itemView?.findViewById(R.id.registerEmail)
password = itemView?.findViewById(R.id.registerPassword)
name = itemView?.findViewById(R.id.registerName)
phoneNo = itemView?.findViewById(R.id.registerPhone)
etAddress = itemView?.findViewById(R.id.registerAddress)
etPinCode = itemView?.findViewById(R.id.registerPinCode)

registerAuth = Firebase.auth

btnSignIn?.setOnClickListener {
val userLogin=LoginFragment()
activity?.supportFragmentManager?.beginTransaction()?.replace(R.id.fragment_container1,userLogin)?.commit()

btnSignIn?.setOnClickListener {
val userLogin = LoginFragment()
activity?.supportFragmentManager?.beginTransaction()
?.replace(R.id.fragment_container1, userLogin)?.commit()
}
btnRegister?.setOnClickListener{
val mail=email?.text.toString().trim()
val pass=password?.text.toString().trim()
if(TextUtils.isEmpty(mail)){

btnRegister?.setOnClickListener {

val mail = email?.text.toString().trim()
val pass = password?.text.toString().trim()
val donorName = name?.text.toString().trim()
val donorPhoneNo = phoneNo?.text.toString().trim()
val donorAddress = etAddress?.text.toString()
val donorPinCode = etPinCode?.text.toString()

val db = FirebaseFirestore.getInstance()

if (mail.isEmpty()) {
email?.error = "Email cannot be empty"
}
if(TextUtils.isEmpty(pass)){
if (pass.isEmpty()) {
password?.error = "Password cannot be empty"
}
if (!(mail.matches(emailPattern.toRegex()))) {
email?.error="Invalid email"
if (donorName.isEmpty()) {
name?.error = "Enter the name"
}
if (donorPhoneNo.isEmpty()) {
phoneNo?.error = "Enter the proper phone number"
}
if (donorAddress.isEmpty()) {
etAddress?.error = "Enter the complete address"
}
if(!(isValidPassword(pass))){
password?.error="Password format is invalid"
if (donorPinCode.isEmpty()) {
etPinCode?.error = "Enter your area pin code"
}
else {
if (!(mail.matches(emailPattern.toRegex()))) {
email?.error = "Invalid email"
}
if (!(isValidPassword(pass))) {
password?.error = "Password format is invalid"
} else {
//user registration
try {
registerAuth.createUserWithEmailAndPassword(mail, pass).addOnCompleteListener(
OnCompleteListener<AuthResult> { task ->

registerAuth.createUserWithEmailAndPassword(mail, pass)
.addOnCompleteListener { task ->

//if registration is done successfully
if (task.isSuccessful) {
val user: FirebaseUser? =registerAuth.currentUser
Toast.makeText(
activity,
"You were registered successfully.",
Toast.LENGTH_SHORT
).show()

//collecting the user data from the app with the help of the below method
val documentReference :DocumentReference=db.collection("donors").document(
user?.uid ?: ""
)
val donorInfo: MutableMap<String, Any> = HashMap()
donorInfo["Full Name"] = donorName
donorInfo["Email"] = mail
donorInfo["PhoneNumber"] = donorPhoneNo
donorInfo["Address"] = donorAddress
donorInfo["Pin Code"] = donorPinCode
//specify if the user is donor
donorInfo["isDonor"] = 1 // in case of NGO give this value 0

documentReference.set(donorInfo).addOnSuccessListener {
Log.d(TAG, "User data for $donorName was collected successfully ")
}.addOnFailureListener{ e ->
Log.w(TAG, "Error adding document", e)
}
/* add the intent to go to the main dashboard of the application and also add finish() so that
if the user presses back after registration they don't go back to the registration page
it basically clears all the previous activities
*/
if the user presses back after registration they don't go back to the registration page
it basically clears all the previous activities
*/
}
// if the registration failed somehow
else {
Expand All @@ -94,34 +144,37 @@ class UserRegisterFragment: Fragment(){
}

}
)
}
catch(e:IllegalArgumentException){
Toast.makeText(context,"Fields cannot be empty",Toast.LENGTH_SHORT)

} catch (e: Exception) {
Toast.makeText(context, "Fields cannot be empty $e", Toast.LENGTH_SHORT).show()
}
}
}
return itemView
}


override fun onStart() {
super.onStart()
// check if the user is signed in then don't open login or register page directly send to the required activity
val currentUser=registerAuth.currentUser
if(currentUser !=null){
val currentUser = registerAuth.currentUser
if (currentUser != null) {
// write logic to send to the main dashboard of the application
}
}

private fun isValidPassword(password: String): Boolean {
val passwordREGEX = Pattern.compile("^" +
"(?=.*[0-9])" + //at least 1 digit
"(?=.*[a-z])" + //at least 1 lower case letter
"(?=.*[A-Z])" + //at least 1 upper case letter
"(?=.*[a-zA-Z])" + //any letter
"(?=.*[@#$%^&+=])" + //at least 1 special character
"(?=\\S+$)" + //no white spaces
".{8,}" + //at least 8 characters
"$")
val passwordREGEX = Pattern.compile(
"^" +
"(?=.*[0-9])" + //at least 1 digit
"(?=.*[a-z])" + //at least 1 lower case letter
"(?=.*[A-Z])" + //at least 1 upper case letter
"(?=.*[a-zA-Z])" + //any letter
"(?=.*[@#$%^&+=])" + //at least 1 special character
"(?=\\S+$)" + //no white spaces
".{8,}" + //at least 8 characters
"$"
)
return passwordREGEX.matcher(password).matches()
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/user_registration_fragment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
android:id="@+id/registerPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:inputType="phone"
android:textCursorDrawable="@null"/>

</com.google.android.material.textfield.TextInputLayout>
Expand Down

0 comments on commit bbdd992

Please sign in to comment.