aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps
diff options
context:
space:
mode:
authorAllan Wang <me@allanwang.ca>2019-07-01 20:33:36 -0700
committerGitHub <noreply@github.com>2019-07-01 20:33:36 -0700
commit646a3a1b2849dd8a8cb7cc9d6da4f303f5bd939e (patch)
tree625ebddb2a21c5306751045aa34b6ba416125996 /app/src/main/kotlin/com/pitchedapps
parent35305f7a83daabf535d3fb22938eb5dd68fd2c19 (diff)
parent70b42ba365d00f7ec19abe1050c51aee9931045b (diff)
downloadfrost-646a3a1b2849dd8a8cb7cc9d6da4f303f5bd939e.tar.gz
frost-646a3a1b2849dd8a8cb7cc9d6da4f303f5bd939e.tar.bz2
frost-646a3a1b2849dd8a8cb7cc9d6da4f303f5bd939e.zip
Merge pull request #1457 from AllanWang/enhancement/refresh
Disable refresh if not at the top, resolves #1450
Diffstat (limited to 'app/src/main/kotlin/com/pitchedapps')
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt1
-rw-r--r--app/src/main/kotlin/com/pitchedapps/frost/views/SwipeRefreshLayout.kt87
2 files changed, 87 insertions, 1 deletions
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt b/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt
index ed207896..0fb24f7e 100644
--- a/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt
+++ b/app/src/main/kotlin/com/pitchedapps/frost/activities/LoginActivity.kt
@@ -41,7 +41,6 @@ import com.pitchedapps.frost.facebook.FbItem
import com.pitchedapps.frost.facebook.profilePictureUrl
import com.pitchedapps.frost.glide.FrostGlide
import com.pitchedapps.frost.glide.GlideApp
-import com.pitchedapps.frost.glide.transform
import com.pitchedapps.frost.utils.L
import com.pitchedapps.frost.utils.Showcase
import com.pitchedapps.frost.utils.frostEvent
diff --git a/app/src/main/kotlin/com/pitchedapps/frost/views/SwipeRefreshLayout.kt b/app/src/main/kotlin/com/pitchedapps/frost/views/SwipeRefreshLayout.kt
new file mode 100644
index 00000000..2154ce2d
--- /dev/null
+++ b/app/src/main/kotlin/com/pitchedapps/frost/views/SwipeRefreshLayout.kt
@@ -0,0 +1,87 @@
+package com.pitchedapps.frost.views
+
+import android.content.Context
+import android.util.AttributeSet
+import android.view.MotionEvent
+import android.view.View
+import android.view.ViewConfiguration
+import android.webkit.WebView
+import android.widget.ListView
+import androidx.core.widget.ListViewCompat
+import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
+import androidx.swiperefreshlayout.widget.SwipeRefreshLayout.OnChildScrollUpCallback
+import com.pitchedapps.frost.utils.L
+
+/**
+ * Variant that forbids refreshing if child layout is not at the top
+ * Inspired by https://github.com/slapperwan/gh4a/blob/master/app/src/main/java/com/gh4a/widget/SwipeRefreshLayout.java
+ *
+ */
+class SwipeRefreshLayout @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
+ SwipeRefreshLayout(context, attrs) {
+
+ private var preventRefresh: Boolean = false
+ private var downY: Float = -1f
+ private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop
+
+ private var nestedCanChildScrollUp: OnChildScrollUpCallback? = null
+
+ /**
+ * Copy of [canChildScrollUp], with additional support if necessary
+ */
+ private val canChildScrollUp = OnChildScrollUpCallback { parent, child ->
+ nestedCanChildScrollUp?.canChildScrollUp(parent, child) ?: when (child) {
+ is WebView -> child.canScrollVertically(-1).apply {
+ L.d { "Webview can scroll up $this" }
+ }
+ is ListView -> ListViewCompat.canScrollList(child, -1)
+ // Supports webviews as well
+ else -> child?.canScrollVertically(-1) ?: false
+ }
+ }
+
+ init {
+ setOnChildScrollUpCallback(canChildScrollUp)
+ }
+
+ override fun setOnChildScrollUpCallback(callback: OnChildScrollUpCallback?) {
+ this.nestedCanChildScrollUp = callback
+ }
+
+ override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
+ if (ev.action != MotionEvent.ACTION_DOWN && preventRefresh) {
+ return false
+ }
+ when (ev.action) {
+ MotionEvent.ACTION_DOWN -> {
+ downY = ev.y
+ preventRefresh = canChildScrollUp()
+ }
+ MotionEvent.ACTION_MOVE -> {
+ if (downY - ev.y > touchSlop) {
+ preventRefresh = true
+ return false
+ }
+ }
+ }
+ return super.onInterceptTouchEvent(ev)
+ }
+
+ override fun onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int) {
+ if (preventRefresh) {
+ /*
+ * Ignoring offsetInWindow since
+ * 1. It doesn't seem to matter in the typical use case
+ * 2. It isn't being transferred to the underlying array used by the super class
+ */
+ dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, null)
+ } else {
+ super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed)
+ }
+ }
+
+ /**
+ * Alias for adding on refresh listener
+ */
+ interface OnRefreshListener : SwipeRefreshLayout.OnRefreshListener
+} \ No newline at end of file