Skip to content

Commit

Permalink
Collecting NGO sign up info to the database from both the registratio…
Browse files Browse the repository at this point in the history
…n pages
  • Loading branch information
1HazArd1 committed Sep 15, 2022
1 parent 814b5e3 commit aba7ad3
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 35 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions app/src/main/java/com/hazard/samarpan/common/LoginFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class LoginFragment: Fragment() {
).show()
activity?.let{
val intent = Intent (it, Main2Activity::class.java)
// here send the data of the received user type from the database and according to the
//0 or 1 populate the respective fragment in the activity
it.startActivity(intent)
}
activity?.finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Main2Activity : AppCompatActivity() {
} */

}
//create one more function that will be responsible to open the ngo dashboard
private fun addFragment() {
val dashboard= UserDashboardFragment()
supportFragmentManager.beginTransaction().add(R.id.fragment_container2,dashboard).commit()
Expand Down
26 changes: 25 additions & 1 deletion app/src/main/java/com/hazard/samarpan/common/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package com.hazard.samarpan.common
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.hazard.samarpan.R
import com.hazard.samarpan.ngo.Communicator
import com.hazard.samarpan.ngo.NgoSignup2Fragment

class MainActivity : AppCompatActivity() {
class MainActivity : AppCompatActivity(),Communicator {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
/*This part of the code is used to set the default theme as the theme was set to the splash screen before setting the main activity layout
Expand All @@ -19,4 +21,26 @@ class MainActivity : AppCompatActivity() {
val userSignInLayout= LoginFragment()
supportFragmentManager.beginTransaction().add(R.id.fragment_container1,userSignInLayout).commit()
}

override fun passDataCom(
orgName: String,
orgMail: String,
orgPhone: String,
officeAdd: String,
orgPin: String,
orgPass: String
) {
val bundle=Bundle()
bundle.putString("Name",orgName)
bundle.putString("Mail",orgMail)
bundle.putString("Phone",orgPhone)
bundle.putString("Address",officeAdd)
bundle.putString("PinCode",orgPin)
bundle.putString("Password",orgPass)

val transaction=this.supportFragmentManager.beginTransaction()
val ngoSignUp2 = NgoSignup2Fragment()
ngoSignUp2.arguments = bundle
transaction.commit()
}
}
5 changes: 5 additions & 0 deletions app/src/main/java/com/hazard/samarpan/ngo/Communicator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.hazard.samarpan.ngo

interface Communicator {
fun passDataCom(orgName :String,orgMail :String ,orgPhone :String ,officeAdd :String,orgPin :String,orgPass:String)
}
115 changes: 95 additions & 20 deletions app/src/main/java/com/hazard/samarpan/ngo/NgoSignup1Fragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import com.google.android.material.textfield.TextInputLayout
import com.google.android.material.textfield.TextInputEditText
import com.hazard.samarpan.R
import java.util.regex.Pattern

class NgoSignup1Fragment : Fragment() {

private lateinit var communicator: Communicator
private lateinit var nextBtn: Button

private var orgName : TextInputLayout ?=null
private var orgMail : TextInputLayout ?=null
private var orgPhone : TextInputLayout ?=null
private var officeAdd :TextInputLayout ?=null
private var orgPinCode :TextInputLayout ?=null
private var pass :TextInputLayout ?=null
private var confirmPass :TextInputLayout ?=null
private var name : TextInputEditText ?=null
private var mail : TextInputEditText ?=null
private var phone : TextInputEditText ?=null
private var add :TextInputEditText ?=null
private var pinCode :TextInputEditText ?=null
private var password :TextInputEditText ?=null
private var confirmPassword :TextInputEditText ?=null

private val emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+"

override fun onCreateView(
inflater: LayoutInflater,
Expand All @@ -28,25 +32,96 @@ class NgoSignup1Fragment : Fragment() {
): View? {
val v = layoutInflater.inflate(R.layout.ngo_signup1_fragment, container, false)

communicator = activity as Communicator

nextBtn = v.findViewById(R.id.btn_ngosignup1_next)

name = v?.findViewById(R.id.et_ngosignup_name)
mail = v?.findViewById(R.id.et_ngosignup_mail)
phone = v?.findViewById(R.id.et_ngosignup_phone)
add = v?.findViewById(R.id.et_ngosignup_address)
pinCode = v?.findViewById(R.id.et_ngosignup_pincode)
password =v?.findViewById(R.id.et_ngosignup_password)
confirmPassword =v?.findViewById(R.id.et_ngosignup_confirmpassword)

nextBtn.setOnClickListener {

orgName = v?.findViewById(R.id.et_ngosignup_name)
orgMail = v?.findViewById(R.id.et_ngosignup_mail)
orgPhone = v?.findViewById(R.id.et_ngosignup_phone)
officeAdd = v?.findViewById(R.id.et_ngosignup_address)
orgPinCode = v?.findViewById(R.id.et_ngosignup_pincode)
pass =v?.findViewById(R.id.et_ngosignup_password)
confirmPass =v?.findViewById(R.id.et_ngosignup_confirmpassword)

val orgName = name?.text.toString().trim()
val orgMail = mail?.text.toString().trim()
val orgPhone = phone?.text.toString().trim()
val officeAdd = add?.text.toString().trim()
val orgPinCode = pinCode?.text.toString().trim()
val pass = password?.text.toString().trim()
val confirmPass=confirmPassword?.text.toString().trim()


if (orgName.isEmpty()) {
name?.error = "Please provide organisation's name"
markButtonDisable(nextBtn)
}
if (orgMail.isEmpty()) {
mail?.error = "Mail cannot be empty"
markButtonDisable(nextBtn)
}
if (orgPhone.isEmpty()) {
phone?.error = "Enter your phone number"
markButtonDisable(nextBtn)
}
if (officeAdd.isEmpty()) {
add?.error = "Address cannot be empty"
markButtonDisable(nextBtn)
}
if (orgPinCode.isEmpty()) {
name?.error = "Name cannot be empty"
markButtonDisable(nextBtn)
}
if (pass.isEmpty()){
password?.error = "Password cannot be empty"
markButtonDisable(nextBtn)
}
if (!(orgMail.matches(emailPattern.toRegex()))) {
mail?.error = "Invalid email"
markButtonDisable(nextBtn)
}
if (!(isValidPassword(pass))) {
password?.error = "Password format is invalid"
markButtonDisable(nextBtn)
}
if(pass != confirmPass){
confirmPassword?.error = "Passwords doesn't match"
markButtonDisable(nextBtn)
}else{
communicator.passDataCom(orgName,orgMail,orgPhone,officeAdd,orgPinCode,pass)
activity?.supportFragmentManager?.beginTransaction()
?.replace(
R.id.fragment_container1,
NgoSignup2Fragment()
)?.addToBackStack(null)?.commit()
}

//this opens up the sign up 2 page
activity?.supportFragmentManager?.beginTransaction()
?.replace(
R.id.fragment_container1,
NgoSignup2Fragment()
)?.addToBackStack(null)?.commit()

}

return v
}
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
"$"
)
return passwordREGEX.matcher(password).matches()
}
private fun markButtonDisable(b:Button){
b.isEnabled =false
b.setBackgroundColor(resources.getColor(R.color.theme_button_disabled))
}
}
83 changes: 79 additions & 4 deletions app/src/main/java/com/hazard/samarpan/ngo/NgoSignup2Fragment.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,103 @@
package com.hazard.samarpan.ngo

import android.content.ContentValues
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.*
import androidx.fragment.app.Fragment
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 com.hazard.samarpan.R

class NgoSignup2Fragment : Fragment(){

private var v : View?=null
private var btnNgoRegister: Button?=null
private var tvSample :TextView?=null

private var orgName:String =""
private var orgMail:String =""
private var orgPhone:String =""
private var officeAdd:String =""
private var orgPin:String =""
private var pass:String =""
private lateinit var registerAuth: FirebaseAuth


override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
v = layoutInflater.inflate(R.layout.ngo_signup2_fragment,container,false)

val types = resources.getStringArray(R.array.organisation_type)
val arrayAdapter = ArrayAdapter(requireContext(),R.layout.dropdown_item,types)
btnNgoRegister=v?.findViewById(R.id.btnNgoRegister)
tvSample=v?.findViewById(R.id.tv_Sample)

val organisationTypes = resources.getStringArray(R.array.organisation_type)
val arrayAdapter = ArrayAdapter(requireContext(),R.layout.dropdown_item,organisationTypes)
val autoCompleteTV= v?.findViewById<AutoCompleteTextView>(R.id.ngosignup_select_orgtype_items)
autoCompleteTV?.setAdapter(arrayAdapter)



registerAuth= Firebase.auth

orgName=arguments?.getString("Name").toString()
orgMail= arguments?.getString("Mail").toString()
orgPhone=arguments?.getString("Phone").toString()
officeAdd=arguments?.getString("Address").toString()
orgPin=arguments?.getString("PinCode").toString()
pass=arguments?.getString("Password").toString()

btnNgoRegister?.setOnClickListener {
val db = FirebaseFirestore.getInstance()
try{
registerAuth.createUserWithEmailAndPassword(orgMail,pass)
.addOnCompleteListener{ task->
if(task.isSuccessful){
val ngo: FirebaseUser? =registerAuth.currentUser
Toast.makeText(
activity,
"Thank You! for choosing us",
Toast.LENGTH_SHORT
).show()
val documentReference : DocumentReference =db.collection("users").document(
ngo?.uid ?: ""
)
val ngoInfo :MutableMap<String,Any> = HashMap()
ngoInfo["OrgName"]= orgName
ngoInfo["OrgMail"]= orgMail
ngoInfo["OrgPhone"]=orgPhone
ngoInfo["OfficeAdd"]=officeAdd
ngoInfo["OrgPinCode"]=orgPin
ngoInfo["isDonor"]=0
// add the value of the org type from the dropdown and upload document work
documentReference.set(ngoInfo).addOnSuccessListener {
Log.d(ContentValues.TAG, "User data for $orgName was collected successfully ")
}.addOnFailureListener{ e ->
Log.w(ContentValues.TAG, "Error adding data", e)
}
}else{
Toast.makeText(
activity,
task.exception!!.message.toString(),
Toast.LENGTH_SHORT
).show()
}
}
}
catch(e :Exception){
Toast.makeText(context, "Fields cannot be empty $e", Toast.LENGTH_SHORT).show()
}
}
return v
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.view.ViewGroup
import android.widget.Button
import android.widget.LinearLayout
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import com.google.android.material.textfield.TextInputEditText
import com.google.firebase.auth.FirebaseAuth
Expand Down Expand Up @@ -101,6 +102,7 @@ class UserRegisterFragment : Fragment() {
password?.error = "Password format is invalid"
} else {
//user registration
btnRegister?.isEnabled=true
try {

registerAuth.createUserWithEmailAndPassword(mail, pass)
Expand Down Expand Up @@ -178,4 +180,8 @@ class UserRegisterFragment : Fragment() {
)
return passwordREGEX.matcher(password).matches()
}
private fun markButtonDisable(b:Button){
b.isEnabled =false
b.setBackgroundColor(resources.getColor(R.color.theme_button_disabled))
}
}
Loading

0 comments on commit aba7ad3

Please sign in to comment.