diff --git a/app/build.gradle b/app/build.gradle index 2678fe5..64377d7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,7 +10,7 @@ android { defaultConfig { applicationId "com.hazard.samarpan" - minSdk 16 + minSdk 23 targetSdk 31 versionCode 1 versionName "1.0" @@ -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' diff --git a/app/src/main/java/com/hazard/samarpan/LoginFragment.kt b/app/src/main/java/com/hazard/samarpan/LoginFragment.kt index 6abd534..ee675ac 100644 --- a/app/src/main/java/com/hazard/samarpan/LoginFragment.kt +++ b/app/src/main/java/com/hazard/samarpan/LoginFragment.kt @@ -1,6 +1,5 @@ package com.hazard.samarpan -import NgoSignup1Fragment import android.os.Bundle import android.text.TextUtils import android.view.LayoutInflater @@ -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{ @@ -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", diff --git a/app/src/main/java/com/hazard/samarpan/NgoSignup1Fragment.kt b/app/src/main/java/com/hazard/samarpan/NgoSignup1Fragment.kt index 9ed33a1..ef39a02 100644 --- a/app/src/main/java/com/hazard/samarpan/NgoSignup1Fragment.kt +++ b/app/src/main/java/com/hazard/samarpan/NgoSignup1Fragment.kt @@ -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, diff --git a/app/src/main/java/com/hazard/samarpan/UserRegisterFragment.kt b/app/src/main/java/com/hazard/samarpan/UserRegisterFragment.kt index b04f28e..1f87d52 100644 --- a/app/src/main/java/com/hazard/samarpan/UserRegisterFragment.kt +++ b/app/src/main/java/com/hazard/samarpan/UserRegisterFragment.kt @@ -1,7 +1,9 @@ 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 @@ -9,80 +11,128 @@ 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 { 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 = 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 { @@ -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() } } \ No newline at end of file diff --git a/app/src/main/res/layout/user_registration_fragment.xml b/app/src/main/res/layout/user_registration_fragment.xml index f3ce50c..3101534 100644 --- a/app/src/main/res/layout/user_registration_fragment.xml +++ b/app/src/main/res/layout/user_registration_fragment.xml @@ -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"/>