Implementing Custom Forms viewing under Attendee Details in Open Event Android App

This blog will illustrate about how order custom forms are viewed for events for which they are required in Open Event Android. These forms help the event organizer in gathering more information from the user or attendee. For example, in an event X, the organizer might want to know the state from which the user is from. Let’s head onto the code.

1. Create the CustomForm model

The custom form model is to be created as per the API.

@Type(“custom-form”)
@JsonNaming(PropertyNamingStrategy.KebabCaseStrategy::class)
data class CustomForm(
@Id(IntegerIdHandler::class)
val id: Long,
val form: String,
val fieldIdentifier: String,
val type: String,
val isRequired: Boolean? = false,
val isIncluded: Boolean? = false,
val isFixed: Boolean? = false,
val ticketsNumber: Int? = null,
@Relationship(“event”)
var event: EventId? = null
)

We can observe that there are some special fields in the model. For example, field “field identifier”, identifies the extra field of an attendee to to be taken in while making an attendee POST request. The field “isRequired” specifies whether the field is required to be taken.

2. Add the function to get custom forms from API

    @GET(“events/{id}/custom-forms”)
fun getCustomFormsForAttendees(@Path(“id”) id: Long): Single<List<CustomForm>>

3. Modify the code of Attendee Fragment

   attendeeFragmentViewModel.getCustomFormsForAttendees(eventId.id)

           attendeeFragmentViewModel.forms.observe(this, Observer {
               it?.let {
                   fillInformationSection(it)
                   if (!it.isEmpty()) {
                       rootView.moreAttendeeInformation.visibility = View.VISIBLE
                   }
               }
               rootView.register.isEnabled = true
           })

           rootView.register.setOnClickListener {
               if (selectedPaymentOption == “Stripe”)
                   sendToken()

               val attendees = ArrayList<Attendee>()
               ticketIdAndQty?.forEach {
                   for (i in 0..it.second) {
                       val attendee = Attendee(id = attendeeFragmentViewModel.getId(),
                               …
                               city = getAttendeeField(“city”),
                               address = getAttendeeField(“address”),
                               state = getAttendeeField(“state”),
                               …
                       attendees.add(attendee)
                   }
               }

2. Create two more methods

We create two more methods which help in filling the area, whether it should be a text view or image view. As we are only taking type of text, we are creating EditText for all fields!

   private fun fillInformationSection(forms: List<CustomForm>) {
val layout = rootView.attendeeInformation

for (form in forms) {
if (form.type == “text”) {
val inputLayout = TextInputLayout(context)
val editTextSection = EditText(context)
editTextSection.hint = form.fieldIdentifier.capitalize()
inputLayout.addView(editTextSection)
inputLayout.setPadding( 0, 0, 0, 20)
layout.addView(inputLayout)
identifierList.add(form.fieldIdentifier)
editTextList.add(editTextSection)
}
}
}

fun getAttendeeField(identifier: String): String {
val index = identifierList.indexOf(identifier)
return if (index == -1) “” else index.let { editTextList[it] }.text.toString()
}

Thus, we have successfully implemented Custom Forms viewing in the app.

Resources

Tags: GSoC18, FOSSASIA, Open Event, Android, Custom Forms