Show number of tickets in Orders Under User Fragment

Show number of tickets in Orders Under User Fragment

This blog will illustrate about how we, at Open Event Android, have implemented the way to show the number of tickets of every type of ticket bought by the user in Orders Under User fragment itself!

1. Modify ordersUnderUser function in Order API

The ordersUnderUser function in Order API doesn’t include attendees. So, if we we need to show the number of attendees which is basically the number of tickets, we have to include all the attendee ids.

@GET(“/v1/users/{userId}/orders?filter=[{\”name\”:\”status\”,\”op\”:\”eq\”,\”val\”:\”completed\”}]&include=event,attendees&fields[attendees]=id”)

   fun ordersUnderUser(@Path(“userId”) userId: Long): Single<List<Order>>

1. Modify the orderUnderUser function in viewmodel

Now, we have to modify the OrderViewModel where we will make changes in the orderUnderUser function. We create a LiveData of attendees number which will contain a list of attendee ids received from the order.

val attendeesNumber = MutableLiveData<ArrayList<Int>>()

Then, in the orderUnderUser function, we add a line of code of setting the value of the LiveData.

This basically takes the orders as a list and as List<AttendeeId> is a variable inside the object of Order, we map out the same ArrayList full of attendee ids!

           }.subscribe({

               order = it

               attendeesNumber.value = it.map { it.attendees?.size } as ArrayList<Int>

               val query = buildQuery(it)

               if (idList.size != 0)

                   eventsUnderUser(query)

               else

                   progress.value = false

           }

2. Modify OrdersUnderUserFragment code

We add a observer in the Fragment file. The observer will fire up whenever there is a value received for attendeesNumber. It will fire up the function setAttendeeNumber in ordersRecyclerAdapter.

ordersUnderUserVM.attendeesNumber.observe(this, Observer {

   it?.let {

       ordersRecyclerAdapter.setAttendeeNumber(it)

   }

})

3. Modify OrdersRecyclerAdapter code

We add a variable called attendeesNumber in the file. Another function called setAttendeeNumber is also made to set the Attendee number. We then proceed to bind the number to the view holder.

var attendeesNumber =  ArrayList<Int>()

fun setAttendeeNumber(number: ArrayList<Int>) {

   attendeesNumber = number

}

override fun onBindViewHolder(holder: OrdersViewHolder, position: Int) {

   holder.bind(eventAndOrderIdentifier[position].first, clickListener, eventAndOrderIdentifier[position].second, attendeesNumber[position])

}

4. Modify the ViewHolder of an Order

We create a conditional which thus sets up the message of “See N Tickets” or “See 1 Ticket” based on the ticket number.

if (attendeesNumber == 1) {

   itemView.ticketsNumber.text = “See ${attendeesNumber} Ticket”

} else {

   itemView.ticketsNumber.text = “See ${attendeesNumber} Tickets”

}

Thus, this was how, tickets number viewing was implemented in the app.

References

Tags: GSoC18, FOSSASIA, Open Event, Android, Number of Tickets

Close Menu