Skip to content
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

Finding coordinator layout to handle snackbar gracefully #196

Merged
merged 1 commit into from
Oct 19, 2019
Merged
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 @@ -2,11 +2,14 @@ package com.jraska.github.client.networkstatus.internal

import android.app.Activity
import android.view.View
import android.view.ViewGroup
import androidx.coordinatorlayout.widget.CoordinatorLayout
import com.google.android.material.snackbar.Snackbar
import com.jraska.github.client.core.android.DefaultActivityCallbacks
import com.jraska.github.client.networkstatus.R
import com.jraska.github.client.rx.AppSchedulers
import io.reactivex.disposables.CompositeDisposable
import timber.log.Timber
import javax.inject.Inject

internal class NetworkStatusDisplayer @Inject constructor(
Expand Down Expand Up @@ -43,15 +46,38 @@ internal class NetworkStatusDisplayer @Inject constructor(
}

private fun showOfflineSnackbar(activity: Activity) {
Timber.i("Showing offline snackbar in %s", activity::class.java.name)

dismissAnySnackbar()

val view = activity.findViewById<View>(android.R.id.content)
val contentView = activity.findViewById<View>(android.R.id.content)
val viewForSnackbar = findCoordinatorLayout(contentView) ?: contentView

val snackbar = Snackbar.make(view, R.string.status_is_offline, Snackbar.LENGTH_INDEFINITE)
val snackbar = Snackbar.make(viewForSnackbar, R.string.status_is_offline, Snackbar.LENGTH_INDEFINITE)
snackbar.show()
offlineSnackbar = snackbar
}

private fun findCoordinatorLayout(view: View): CoordinatorLayout? {
if (view is CoordinatorLayout) {
return view
}

if (view is ViewGroup) {
val childCount = view.childCount
for (childViewIndex in 0 until childCount) {
val childView = view.getChildAt(childViewIndex)

val coordinatorLayout = findCoordinatorLayout(childView)
if (coordinatorLayout != null) {
return coordinatorLayout
}
}
}

return null
}

internal class Callbacks(
private val displayer: NetworkStatusDisplayer
) : DefaultActivityCallbacks() {
Expand Down