Ticket Details in the Open Event Android App
After entering all the attendee details and buying a ticket for an event the user expects to see the ticket so that he can use it later. This is why ticket details are shown in a separate fragment in the Open Event Android App. Let’s see how the tickets fragment was made in the Open Event Android App.
Two things that we require from the previous fragment are the event id and the order identifier so that we show the information related to the event as well as the order.
if (bundle != null) { id = bundle.getLong(EVENT_ID, -1) orderId = bundle.getString(ORDERS) }
We are requesting data from the following two endpoints. In the first GET request we are passing the order identifier in the URL and we get the list of attendees from the server. In the second endpoint we simply pass the event identifier and get the event details from the server.
@GET("/v1/orders/{orderIdentifier}/attendees") fun attendeesUnderOrder(@Path("orderIdentifier") orderIdentifier: String): Single<List<Attendee>> @GET("/v1/events/{eventIdentifier}") fun getEventFromApi(@Path("eventIdentifier") eventIdentifier: Long): Single<Event>
Here we are observing the attendees live data and adding the list of attendees returned from the server to the recyclerview so that we can show the user all the details of the attendees like the first name, last name etc. We then notify the adapter that the list of attendees have been added. In the end we log the number of attendees so that it is easier to debug in case there are any bugs.
orderDetailsViewModel.attendees.observe(this, Observer { it?.let { ordersRecyclerAdapter.addAll(it) ordersRecyclerAdapter.notifyDataSetChanged() } Timber.d("Fetched attendees of size %s", ordersRecyclerAdapter.itemCount) })
As mentioned earlier we need the event id and order identifier to show event and attendee related information to the user so here we are using the event id and appending it to the url. We are sending a GET request in a background thread and storing the list of events returned from the server in a mutable live data. In case of any errors we log it and show the error message to the user. Similarly we will use the order identifier to get the list of orders from the server and show it to the user.
compositeDisposable.add(eventService.getEventFromApi(id) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe({ event.value = it }, { Timber.e(it, "Error fetching event %d", id) message.value = "Error fetching event" }))
After fetching the list of attendees and event details, the only thing that we need to do is extract the important information and show it to the user so we pass the order and event objects to the ViewHolder. This can be done simply by using the attendee and event objects and accessing the fields required.
itemView.name.text = "${attendee.firstname} ${attendee.lastname}" itemView.eventName.text = event?.name itemView.date.text = "$formattedDate\n$formattedTime $timezone"
Resources
- ReactiveX official documentation: http://reactivex.io/
- Retrofit Android: http://square.github.io/retrofit/
- Google Android Developers Recycler View: https://developer.android.com/guide/topics/ui/layout/recyclerview
You must be logged in to post a comment.