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

Enable swipe-to-refresh for browser tabs #864

Merged
merged 10 commits into from Jul 9, 2020
2 changes: 2 additions & 0 deletions app/build.gradle
Expand Up @@ -99,6 +99,7 @@ android {
ext {
androidX = "1.2.0-beta01"
materialDesign = "1.2.0-alpha05"
swipeRefreshLayout = "1.0.0"
architectureComponents = "1.1.1"
architectureComponentsExtensions = "1.1.1"
androidKtxCore = "1.2.0"
Expand Down Expand Up @@ -146,6 +147,7 @@ dependencies {
implementation "androidx.appcompat:appcompat:$androidX"
implementation "com.google.android.material:material:$materialDesign"
implementation "androidx.constraintlayout:constraintlayout:$constraintLayout"
implementation "androidx.swiperefreshlayout:swiperefreshlayout:$swipeRefreshLayout"
implementation "androidx.webkit:webkit:$webkit"
implementation "com.squareup.okhttp3:okhttp:$okHttp"
implementation "com.squareup.retrofit2:retrofit:$retrofit"
Expand Down
20 changes: 20 additions & 0 deletions app/src/main/java/com/duckduckgo/app/browser/BrowserTabFragment.kt
Expand Up @@ -287,6 +287,7 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope, DaxDialogLi
configureObservers()
configurePrivacyGrade()
configureWebView()
configureSwipeRefresh()
viewModel.registerWebViewListener(webViewClient, webChromeClient)
configureOmnibarTextInput()
configureFindInPage()
Expand Down Expand Up @@ -842,6 +843,21 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope, DaxDialogLi
}
}

private fun configureSwipeRefresh() {
swipeRefreshContainer.setColorSchemeColors(ContextCompat.getColor(requireContext(), R.color.cornflowerBlue))

swipeRefreshContainer.setOnRefreshListener {
onRefreshRequested()
}

swipeRefreshContainer.setCanChildScrollUpCallback {
webView?.canScrollVertically(-1) ?: false
}

// avoids progressView from showing under toolbar
swipeRefreshContainer.progressViewStartOffset = swipeRefreshContainer.progressViewStartOffset - 15
}

/**
* Explicitly disable database to try protect against Magellan WebSQL/SQLite vulnerability
*/
Expand Down Expand Up @@ -1451,6 +1467,10 @@ class BrowserTabFragment : Fragment(), FindListener, CoroutineScope, DaxDialogLi
createTrackersAnimation()
}
}

if (!viewState.isLoading) {
swipeRefreshContainer.isRefreshing = false
}

Choose a reason for hiding this comment

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

Suggested change
if (!viewState.isLoading) {
swipeRefreshContainer.isRefreshing = false
}
swipeRefreshContainer.isRefreshing = viewState.isLoading

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since this callback is used for other loading operations (e.g. loading a link), we would show the refresh loading indicator in these cases too with the suggested change.

}
}

Expand Down
Expand Up @@ -83,7 +83,10 @@ class DuckDuckGoWebView : WebView, NestedScrollingChild {

returnValue = super.onTouchEvent(event)

if (dispatchNestedScroll(0, scrollOffset[1], 0, deltaY, scrollOffset)) {
if (scrollY == 0) {
// Give parents control (required for SwipeRefreshLayout to work)
stopNestedScroll()
} else if (dispatchNestedScroll(0, scrollOffset[1], 0, deltaY, scrollOffset)) {
event.offsetLocation(0f, scrollOffset[1].toFloat())
nestedOffsetY += scrollOffset[1]
lastY -= scrollOffset[1]
Expand Down
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2020 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.app.browser.ui

import android.content.Context
import android.util.AttributeSet
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout

class ScrollAwareRefreshLayout(context: Context, attrs: AttributeSet?) : SwipeRefreshLayout(context, attrs) {

private var canChildScrollUpCallback: (() -> Boolean)? = null

fun setProgressViewStartOffset(start: Int) {
mOriginalOffsetTop = start
}

override fun canChildScrollUp(): Boolean {
return canChildScrollUpCallback?.invoke() ?: super.canChildScrollUp()
}

fun setCanChildScrollUpCallback(callback: () -> Boolean) {
canChildScrollUpCallback = callback
}
}
27 changes: 17 additions & 10 deletions app/src/main/res/layout/fragment_browser_tab.xml
Expand Up @@ -71,16 +71,23 @@
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.duckduckgo.app.browser.BrowserActivity">

<FrameLayout
android:id="@+id/webViewContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:background="#4F00" />
<com.duckduckgo.app.browser.ui.ScrollAwareRefreshLayout
android:id="@+id/swipeRefreshContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
android:id="@+id/webViewContainer"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:background="#4F00" />

Choose a reason for hiding this comment

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

I suggest to use a color resource.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, will adapt this assuming the blocker (see comment below) can be resolved.


</com.duckduckgo.app.browser.ui.ScrollAwareRefreshLayout>

<View
android:id="@+id/focusDummy"
Expand Down