Hiding Multiple Attendee Recycler for Single Tickets in Open Event Android
Multiple attendee recycler allows the user to easily write in attendee details for all tickets he/she is buying. The UI for attendee details fragment can be divided into two segments, one contains the details of the person creating the whole order and followed by multiple details section for the attendee of every ticket. This is a good way to take details when multiple ticket or tickets with greater than 1 quantity is involved. However, if there is only one attendee detail to be taken instead of asking the user for order creator’s details and then first attendee details it is better to add some views in the creator segment and not ask for the same details again from the user. This blog post will help you in understanding how it’s done in Open Event Android.
Keeping a track of orders with single attendee
We keep a boolean to track whether or not the sum of quantities of tickets selected by a user is one since one attendee is to be generated per ticket quantity. And incase it is boolean variable singleTicket is set to True which is false by default initially. Following is how it can be done
singleTicket = ticketIdAndQty?.map { it.second }?.sum() == 1 |
The above statements basically iterate over the list of pairs of tickets and their quantity and then sums over the second element of all pairs that is the quantities, if the summation leads to 1 single ticket is set to True else false.
Adding elements to recycler adapter
The next step is to update the logic as to when blank attendees are added to recycler adapter. The idea is to check the boolean singleTicket and if it is false dont insert any attendee.
attendeeFragmentViewModel.tickets.observe(this, Observer { it?.let { if (!singleTicket) |
If a single ticket is false regular procedure to add blank attendee is carried out. The quantity of ticket is found and the same quantity of attendees are added to recycler adapter’s attendee’s list similary ticket information is also added to the recycler adapter. The id for blank attendees is taken from the attendeeViewModel’s getId function which in turn simply returns id using the authHolder’s getId function.
Modify the register method to check for the single attendee
So far what we have discussed helps us display the right no of details section on the UI. We will also have to modify the register method to take into account the above changes. Following is the implementation of what happens when the user selects register.
rootView.register.setOnClickListener { if (selectedPaymentOption == “Stripe”) sendToken() val attendees = ArrayList<Attendee>() if (singleTicket) { val pos = ticketIdAndQty?.map { it.second }?.indexOf(1) val ticket = pos?.let { it1 -> ticketIdAndQty?.get(it1)?.first?.toLong() } ?: –1 val attendee = Attendee(id = attendeeFragmentViewModel.getId(), firstname = firstName.text.toString(), lastname = lastName.text.toString(), city = getAttendeeField(“city”), address = getAttendeeField(“address”), state = getAttendeeField(“state”), email = email.text.toString(), ticket = TicketId(ticket), event = eventId) attendees.add(attendee) } else { attendees.addAll(attendeeRecyclerAdapter.attendeeList) } val country = if (country.text.isEmpty()) country.text.toString() else null attendeeFragmentViewModel.createAttendees(attendees, country, selectedPaymentOption) } |
For create attendee object and then add it to the attendees ArrayList first we need to find the ticket it is to be created for as the attendee object accepts Ticket Id as one of the parameters. The logic to get the ticket is fairly straightforward if singleTicket is true first we find out the ticket for which quantity is 1 and then we get the ticket object for the same. Finally, we take the id field of the ticket and add it to our attendee constructor. Other fields for attendee such as first name, last name, email, and country are taken from the EditText View’s rendered on the screen. Else if the single ticket is false which means there are multiple attendee details section on the fragment we simply add the recycler adapter’s attendee list into attendees ArrayList which is later provided to the order function for creating an order of it.
References
- Android Hive Working With Recycler Views: https://www.androidhive.info/2016/01/android-working-with-recycler-view/
- Google Developers Recycler Views: https://developer.android.com/guide/topics/ui/layout/recyclerview
You must be logged in to post a comment.