Skip to content

feat: Add no internet view on TicketsFragment #1492

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
Apr 4, 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 @@ -16,6 +16,8 @@ import androidx.recyclerview.widget.RecyclerView
import androidx.navigation.Navigation.findNavController
import androidx.navigation.fragment.navArgs
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.content_no_internet.view.retry
import kotlinx.android.synthetic.main.content_no_internet.view.noInternetCard
import kotlinx.android.synthetic.main.fragment_tickets.ticketsCoordinatorLayout
import kotlinx.android.synthetic.main.fragment_tickets.view.eventName
import kotlinx.android.synthetic.main.fragment_tickets.view.organizerName
Expand All @@ -32,6 +34,7 @@ import org.fossasia.openevent.general.event.Event
import org.fossasia.openevent.general.event.EventUtils
import org.fossasia.openevent.general.utils.Utils.getAnimFade
import org.fossasia.openevent.general.utils.Utils.getAnimSlide
import org.fossasia.openevent.general.utils.Utils.isNetworkConnected
import org.fossasia.openevent.general.utils.extensions.nonNull
import org.fossasia.openevent.general.utils.nullToEmpty
import org.koin.androidx.viewmodel.ext.android.viewModel
Expand Down Expand Up @@ -121,16 +124,12 @@ class TicketsFragment : Fragment() {
Snackbar.make(ticketsCoordinatorLayout, it, Snackbar.LENGTH_LONG).show()
})

ticketsViewModel.loadEvent(safeArgs.eventId)
ticketsViewModel.loadTickets(safeArgs.eventId)
val retainedTicketIdAndQty: List<Pair<Int, Int>>? = ticketsViewModel.ticketIdAndQty.value
if (retainedTicketIdAndQty != null) {
for (idAndQty in retainedTicketIdAndQty) {
handleTicketSelect(idAndQty.first, idAndQty.second)
}
ticketsRecyclerAdapter.setTicketAndQty(retainedTicketIdAndQty)
ticketsRecyclerAdapter.notifyDataSetChanged()
rootView.retry.setOnClickListener {
loadTickets()
}

loadTickets()

return rootView
}

Expand Down Expand Up @@ -202,4 +201,31 @@ class TicketsFragment : Fragment() {
val alert = builder.create()
alert.show()
}

private fun loadTickets() {
if (!isNetworkConnected(context) && ticketsViewModel.tickets.value.isNullOrEmpty())
showNoInternetScreen(true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you are showing no internet screen if tickets value is null or empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because if the tickets information is loaded and already retained in the ViewModel then I think we can just show that information, no need for no internet card

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anhanh11001 This is wrong. It should be shown from the ViewModel. I am merging this, but create an issue to move all network logic to VM. Use BroadcastReceiver to make it reactive

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will do it

else {
showNoInternetScreen(false)
ticketsViewModel.loadEvent(safeArgs.eventId)
ticketsViewModel.loadTickets(safeArgs.eventId)

val retainedTicketIdAndQty: List<Pair<Int, Int>>? = ticketsViewModel.ticketIdAndQty.value
if (retainedTicketIdAndQty != null) {
for (idAndQty in retainedTicketIdAndQty) {
handleTicketSelect(idAndQty.first, idAndQty.second)
}
ticketsRecyclerAdapter.setTicketAndQty(retainedTicketIdAndQty)
ticketsRecyclerAdapter.notifyDataSetChanged()
}
}
}

private fun showNoInternetScreen(show: Boolean) {
rootView.noInternetCard.isVisible = show
rootView.ticketTableHeader.isVisible = !show
rootView.ticketsRecycler.isVisible = !show
rootView.progressBarTicket.isVisible = !show
rootView.register.isVisible = !show
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/fragment_tickets.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
android:layout_marginTop="@dimen/layout_margin_large"
android:layout_marginBottom="@dimen/layout_margin_large"
android:text="@string/register" />
<include layout="@layout/content_no_internet" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</androidx.core.widget.NestedScrollView>