Skip to content

Commit

Permalink
#397 | Disable or enable swipe on connectivity state
Browse files Browse the repository at this point in the history
  • Loading branch information
PatilShreyas committed Feb 16, 2022
1 parent ae65baf commit 02bd00e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ fun NotesContent(
SwipeRefresh(
modifier = Modifier.fillMaxSize(),
state = rememberSwipeRefreshState(isLoading),
onRefresh = onRefresh
onRefresh = onRefresh,
swipeEnabled = isConnectivityAvailable == true
) {
Column {
if (isConnectivityAvailable != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,14 @@ class NotesFragment : BaseFragment<NotesFragmentBinding, NotesState, NotesViewMo
ContextCompat.getColor(requireContext(), R.color.secondaryColor),
ContextCompat.getColor(requireContext(), R.color.onSecondary)
)
setOnRefreshListener { syncNotes() }
setOnRefreshListener { viewModel.syncNotes() }
}
}
}

override fun render(state: NotesState) {
binding.swipeRefreshNotes.isRefreshing = state.isLoading
binding.swipeRefreshNotes.isEnabled = state.isConnectivityAvailable == true

val errorMessage = state.error
if (errorMessage != null) {
Expand All @@ -99,12 +100,6 @@ class NotesFragment : BaseFragment<NotesFragmentBinding, NotesState, NotesViewMo
}
}

private fun syncNotes() {
if (isConnected() == true) {
viewModel.syncNotes()
}
}

private fun onNotesLoaded(data: List<Note>) {
binding.emptyStateLayout.run {
if (data.isEmpty()) show() else hide()
Expand All @@ -120,7 +115,6 @@ class NotesFragment : BaseFragment<NotesFragmentBinding, NotesState, NotesViewMo

private fun onConnectivityUnavailable() {
with(binding) {
swipeRefreshNotes.isEnabled = false
textNetworkStatus.apply {
setDrawableLeft(
ContextCompat.getDrawable(
Expand All @@ -141,12 +135,9 @@ class NotesFragment : BaseFragment<NotesFragmentBinding, NotesState, NotesViewMo

private fun onConnectivityAvailable() {
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
if (shouldSyncNotes()) {
syncNotes()
}
if (shouldSyncNotes()) { viewModel.syncNotes() }
}
with(binding) {
swipeRefreshNotes.isEnabled = true
textNetworkStatus.apply {
setDrawableLeft(
ContextCompat.getDrawable(
Expand Down

0 comments on commit 02bd00e

Please sign in to comment.