Skip to content

feat: Add checking required fields #1859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ import kotlinx.android.synthetic.main.fragment_attendee.view.city
import kotlinx.android.synthetic.main.fragment_attendee.view.company
import kotlinx.android.synthetic.main.fragment_attendee.view.taxId
import kotlinx.android.synthetic.main.fragment_attendee.view.address
import kotlinx.android.synthetic.main.fragment_attendee.view.firstNameLayout
import kotlinx.android.synthetic.main.fragment_attendee.view.lastNameLayout
import kotlinx.android.synthetic.main.fragment_attendee.view.emailLayout
import kotlinx.android.synthetic.main.fragment_attendee.view.companyLayout
import kotlinx.android.synthetic.main.fragment_attendee.view.addressLayout
import kotlinx.android.synthetic.main.fragment_attendee.view.cvcLayout
import kotlinx.android.synthetic.main.fragment_attendee.view.cityLayout
import kotlinx.android.synthetic.main.fragment_attendee.view.postalCodeLayout
import kotlinx.android.synthetic.main.fragment_attendee.view.cardNumberLayout
import org.fossasia.openevent.general.BuildConfig
import org.fossasia.openevent.general.R
import org.fossasia.openevent.general.attendees.forms.CustomForm
Expand All @@ -87,6 +96,9 @@ import org.fossasia.openevent.general.utils.extensions.nonNull
import org.fossasia.openevent.general.utils.nullToEmpty
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.fossasia.openevent.general.utils.Utils.setToolbar
import org.fossasia.openevent.general.utils.setRequired
import org.fossasia.openevent.general.utils.checkEmpty
import org.fossasia.openevent.general.utils.checkValidEmail
import org.jetbrains.anko.design.longSnackbar
import org.jetbrains.anko.design.snackbar
import java.util.Calendar
Expand Down Expand Up @@ -247,6 +259,9 @@ class AttendeeFragment : Fragment() {
}

private fun setupUser() {
rootView.firstNameLayout.setRequired()
rootView.lastNameLayout.setRequired()
rootView.emailLayout.setRequired()
attendeeViewModel.user
.nonNull()
.observe(viewLifecycleOwner, Observer { user ->
Expand Down Expand Up @@ -352,6 +367,10 @@ class AttendeeFragment : Fragment() {
}

private fun setupBillingInfo() {
rootView.companyLayout.setRequired()
rootView.addressLayout.setRequired()
rootView.cityLayout.setRequired()
rootView.postalCodeLayout.setRequired()
rootView.billingInfoContainer.isVisible = rootView.billingEnabledCheckbox.isChecked
attendeeViewModel.billingEnabled = rootView.billingEnabledCheckbox.isChecked
rootView.billingEnabledCheckbox.setOnCheckedChangeListener { _, isChecked ->
Expand Down Expand Up @@ -433,6 +452,8 @@ class AttendeeFragment : Fragment() {
}

private fun setupCardNumber() {
rootView.cardNumberLayout.setRequired()
rootView.cvcLayout.setRequired()
rootView.cardNumber.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) { /*Do Nothing*/ }
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { /*Do Nothing*/ }
Expand Down Expand Up @@ -510,7 +531,6 @@ class AttendeeFragment : Fragment() {
private fun setupYearOptions() {
val year = ArrayList<String>()
val currentYear = Calendar.getInstance().get(Calendar.YEAR)
year.add(getString(R.string.year_string))
val a = currentYear + 20
for (i in currentYear..a) {
year.add(i.toString())
Expand All @@ -535,7 +555,6 @@ class AttendeeFragment : Fragment() {

private fun setupCardType() {
val cardType = ArrayList<String>()
cardType.add(getString(R.string.select_card))
cardType.add(getString(R.string.american_express_pay_message))
cardType.add(getString(R.string.mastercard_pay_message))
cardType.add(getString(R.string.visa_pay_message))
Expand Down Expand Up @@ -613,6 +632,35 @@ class AttendeeFragment : Fragment() {
else -> true
}

private fun checkRequiredFields(): Boolean {
val checkBasicInfo = rootView.firstName.checkEmpty() && rootView.lastName.checkEmpty() &&
rootView.email.checkEmpty() && rootView.email.checkEmpty() && rootView.email.checkValidEmail()

var checkBillingInfo = true
if (rootView.billingEnabledCheckbox.isChecked) {
checkBillingInfo = rootView.company.checkEmpty() && rootView.company.checkEmpty() &&
rootView.address.checkEmpty() && rootView.city.checkEmpty() && rootView.postalCode.checkEmpty()
}

var checkStripeInfo = true
if (totalAmount != 0F && rootView.paymentSelector.selectedItem.toString() == getString(R.string.stripe)) {
checkStripeInfo = rootView.cardNumber.checkEmpty() && rootView.cvc.checkEmpty()
}

return checkBasicInfo && checkBillingInfo && checkAttendeesInfo() && checkStripeInfo
}

private fun checkAttendeesInfo(): Boolean {
var valid = true
for (pos in 0..attendeeRecyclerAdapter.itemCount) {
val viewHolderItem = rootView.attendeeRecycler.findViewHolderForAdapterPosition(pos)
if (viewHolderItem is AttendeeViewHolder) {
if (!viewHolderItem.checkValidFields()) valid = false
}
}
return valid
}

private fun setupRegisterOrder() {
rootView.register.setOnClickListener {
if (!isNetworkConnected(context)) {
Expand All @@ -624,6 +672,8 @@ class AttendeeFragment : Fragment() {
return@setOnClickListener
}

if (!checkRequiredFields()) return@setOnClickListener

if (attendeeViewModel.totalAmount.value != 0F && !checkPaymentOptions()) return@setOnClickListener

val builder = AlertDialog.Builder(requireContext())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import org.fossasia.openevent.general.attendees.forms.CustomForm
import org.fossasia.openevent.general.event.EventId
import org.fossasia.openevent.general.ticket.Ticket
import org.fossasia.openevent.general.ticket.TicketId
import org.fossasia.openevent.general.utils.checkEmpty
import org.fossasia.openevent.general.utils.checkValidEmail
import org.fossasia.openevent.general.utils.setRequired

class AttendeeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private var identifierList = ArrayList<String>()
Expand All @@ -19,6 +22,9 @@ class AttendeeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

fun bind(attendee: Attendee, ticket: Ticket, customForm: List<CustomForm>, position: Int, eventId: Long) {

itemView.itemEmailLayout.setRequired()
itemView.itemFirstNameLayout.setRequired()
itemView.itemLastNameLayout.setRequired()
itemView.attendeeItemCountry.setText(attendee.country)
itemView.attendeeItemLastName.setText(attendee.lastname)
itemView.attendeeItemEmail.setText(attendee.email)
Expand Down Expand Up @@ -57,6 +63,10 @@ class AttendeeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
}
}

fun checkValidFields(): Boolean =
itemView.attendeeItemFirstName.checkEmpty() && itemView.attendeeItemLastName.checkEmpty() &&
itemView.attendeeItemEmail.checkEmpty() && itemView.attendeeItemEmail.checkValidEmail()

private fun fillInformationSection(forms: List<CustomForm>, textWatcher: TextWatcher) {
val layout = itemView.attendeeInformation
for (form in forms) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package org.fossasia.openevent.general.utils

import android.text.Html
import android.util.Patterns
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import org.fossasia.openevent.general.R

fun String?.nullToEmpty(): String {
return this ?: ""
Expand All @@ -14,3 +18,24 @@ fun String?.stripHtml(): String? {
Html.fromHtml(this)?.toString() ?: this
}
}

fun TextInputEditText.checkEmpty(): Boolean {
if (text.isNullOrBlank()) {
error = resources.getString(R.string.empty_field_error_message)
return false
}
return true
}

fun TextInputEditText.checkValidEmail(): Boolean {
if (text.isNullOrBlank()) return false
if (!Patterns.EMAIL_ADDRESS.matcher(text.toString()).matches()) {
error = resources.getString(R.string.invalid_email_message)
return false
}
return true
}

fun TextInputLayout.setRequired() {
hint = "$hint *"
}
56 changes: 33 additions & 23 deletions app/src/main/res/layout/fragment_attendee.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,47 +196,50 @@
android:textSize="@dimen/heading_text_size" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/firstNameLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding_medium"
android:hint="@string/first_name"
android:layout_marginTop="@dimen/padding_medium">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/firstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/first_name"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/lastNameLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding_medium"
android:hint="@string/last_name"
android:layout_marginTop="@dimen/padding_medium">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/lastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/last_name"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/emailLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding_medium"
android:layout_marginTop="@dimen/padding_medium">
android:layout_marginTop="@dimen/padding_medium"
android:hint="@string/email">

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/email"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>

Expand Down Expand Up @@ -292,17 +295,18 @@
android:visibility="gone"
tools:visibility="visible">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/companyLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding_medium"
android:layout_marginTop="@dimen/padding_medium">
android:layout_marginTop="@dimen/padding_medium"
android:hint="@string/company" >

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/company"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/company" />
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
Expand All @@ -318,43 +322,46 @@
android:hint="@string/tax_id" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/addressLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding_medium"
android:layout_marginTop="@dimen/padding_medium">
android:layout_marginTop="@dimen/padding_medium"
android:hint="@string/address" >

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/address" />
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/cityLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding_medium"
android:layout_marginTop="@dimen/padding_medium">
android:layout_marginTop="@dimen/padding_medium"
android:hint="@string/city" >

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/city" />
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/postalCodeLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding_medium"
android:layout_marginTop="@dimen/padding_medium">
android:layout_marginTop="@dimen/padding_medium"
android:hint="@string/postal_code" >

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/postalCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/postal_code" />
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:id="@+id/countryPickerContainer"
Expand Down Expand Up @@ -421,7 +428,8 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
android:visibility="gone"
tools:visibility="visible">

<TextView
android:layout_width="wrap_content"
Expand All @@ -432,18 +440,19 @@
android:textSize="@dimen/heading_text_size" />

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/cardNumberLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding_medium"
android:layout_marginTop="@dimen/padding_medium">
android:layout_marginTop="@dimen/padding_medium"
android:hint="@string/card_number" >

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/cardNumber"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/card_number" />
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>

<TextView
Expand Down Expand Up @@ -538,18 +547,19 @@
</LinearLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/cvcLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/padding_medium"
android:layout_marginTop="@dimen/padding_medium">
android:layout_marginTop="@dimen/padding_medium"
android:hint="@string/cvc" >

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/cvc"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/cvc" />
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

Expand Down
Loading