Skip to content

Commit

Permalink
Show hide bottom bar on vertical scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
rt4914 committed Feb 23, 2023
1 parent e5d1562 commit d267045
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/com/foremlibrary/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import com.forem.webview.WebViewConstants
import com.forem.webview.WebViewFragment
import com.forem.webview.utils.WebViewStatus
Expand All @@ -34,6 +35,7 @@ class MainActivity : AppCompatActivity() {

private var currentForem = DEV_TO

private lateinit var bottomNavigationBar: ConstraintLayout
private lateinit var loadingViewContainer: FrameLayout
private lateinit var foremNameTextView: TextView
private lateinit var backImageView: ImageView
Expand All @@ -51,6 +53,7 @@ class MainActivity : AppCompatActivity() {
loadOrUpdateFragment(url)
}

bottomNavigationBar = findViewById(R.id.bottom_navigation_container)
loadingViewContainer = findViewById(R.id.loading_view_container)
foremNameTextView = findViewById(R.id.forem_name_text_view)
backImageView = findViewById(R.id.back_image_view)
Expand Down Expand Up @@ -128,6 +131,13 @@ class MainActivity : AppCompatActivity() {
loadingViewContainer.visibility = View.GONE
}
}
webViewFragment.hideBottomNavigationBar.observe(this) { hideBottomBar ->
if (hideBottomBar) {
bottomNavigationBar.visibility = View.GONE
} else {
bottomNavigationBar.visibility = View.VISIBLE
}
}
} else {
getWebViewFragment()?.updateForemInstance(url)
}
Expand Down
4 changes: 2 additions & 2 deletions foremwebview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ plugins {
id 'maven-publish'
}
ext{
version_code = 16
version_name = "1.0.16"
version_code = 17
version_name = "1.0.17"
}

android {
Expand Down
27 changes: 26 additions & 1 deletion foremwebview/src/main/java/com/forem/webview/WebViewFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class WebViewFragment : Fragment(), FileChooserListener {
/** Provides an observable which can reflect the current status of WebView. */
val currentWebViewStatus = MutableLiveData(WebViewStatus.LOADING)

// This is required so as to compare with next upcoming y coordinate and accordingly decide
// if the scroll/movement was upward or downward.
private var currentYCoordinate = 0
val hideBottomNavigationBar = MutableLiveData(false)

private val imagePickerLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK && result.data != null && result.data?.data != null) {
Expand All @@ -97,6 +102,7 @@ class WebViewFragment : Fragment(), FileChooserListener {
val view = inflater.inflate(R.layout.web_view_fragment, container, false)

currentWebViewStatus.value = WebViewStatus.LOADING
hideBottomNavigationBar.value = false

noInternetConnectionContainer = view.findViewById(R.id.no_internet_connection_container)
webView = view.findViewById(R.id.web_view)
Expand Down Expand Up @@ -187,6 +193,25 @@ class WebViewFragment : Fragment(), FileChooserListener {
webView!!.webChromeClient = ForemWebChromeClient(fileChooserListener = this)

webView!!.loadUrl(baseUrl)
setWebViewScrollListener(webView!!)
}

private fun setWebViewScrollListener(webView: WebView) {
webView.viewTreeObserver.addOnScrollChangedListener {
when {
currentYCoordinate > webView.scrollY -> {
hideBottomNavigationBar.value = false
}
currentYCoordinate <= webView.scrollY -> {
hideBottomNavigationBar.value = true
}
else -> {
// Save side check, will never arise ideally
hideBottomNavigationBar.value = false
}
}
currentYCoordinate = webView.scrollY
}
}

@SuppressLint("SetJavaScriptEnabled")
Expand Down Expand Up @@ -316,7 +341,7 @@ class WebViewFragment : Fragment(), FileChooserListener {
} else {
destroyOauthWebView()
}
} else if (oauthWebView == null && webView!=null && webView!!.canGoBack()) {
} else if (oauthWebView == null && webView != null && webView!!.canGoBack()) {
// Case where oauthWebView is fully inactive.
webView?.goBack()
} else {
Expand Down

0 comments on commit d267045

Please sign in to comment.