Skip to content

Commit

Permalink
Propagate insets to children views
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz committed Nov 5, 2021
1 parent 1a1aa3e commit b458c86
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 36 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
# Changelog drag-to-close

## `2.0.0` (05/11/21)

- BREAKING CHANGE: minSdkVersion increased to 21 (Lollipop)
- Target Android 12 (API 31)
- Update dependencies / Remove JCenter
- Propagate insets to children views

## `1.1.0` (04/12/19)

- Unregister dragging listener when closing card programatically #13
- Unregister dragging listener when closing card programmatically #13
- Fix onViewCosed() never called if the screen size changes while closing view #25
- Target Android 10 (API 29)
- Update dependencies
Expand Down
2 changes: 1 addition & 1 deletion configurations.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ext {
kotlinJvmTarget = "11"

androidConfig = [
minSdkVersion : 19,
minSdkVersion : 21,
targetSdkVersion : 31,
compileSdkVersion: 31
]
Expand Down
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ ext {
androidxAppCompat = '1.3.1'

appDependencies = [
androidxCore: "androidx.core:core:$androidxCore",
androidxCore: "androidx.core:core-ktx:$androidxCore",
androidxAnnotation : "androidx.annotation:annotation:$androidxAnnotation",
androidxCustomView : "androidx.customview:customview:$androidxCustomView",
androidxAppCompat : "androidx.appcompat:appcompat:$androidxAppCompat",
Expand Down
4 changes: 2 additions & 2 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

ext.versionMajor = 1 // API Changes, adding big new feature, redesign the App
ext.versionMinor = 2 // New features in a backwards-compatible manner
ext.versionMajor = 2 // API Changes, adding big new feature, redesign the App
ext.versionMinor = 0 // New features in a backwards-compatible manner
ext.versionPatch = 0 // Backwards-compatible bug fixes
ext.versionClassifier = null // Pre-releases (alpha, beta, rc, SNAPSHOT...)

Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<manifest package="com.davidmiguel.dragtoclose" />
<manifest package="com.davidmiguel.dragtoclose" />
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import com.davidmiguel.dragtoclose.DragToClose.Companion.SPEED_THRESHOLD_TO_CLOS
* Dragging controller.
*/
internal class DragHelperCallback(
private val dragToClose: DragToClose,
private val draggableContainer: View
private val dragToClose: DragToClose,
private val draggableContainer: View
) : ViewDragHelper.Callback() {

private var lastDraggingState: Int = ViewDragHelper.STATE_IDLE
Expand All @@ -42,9 +42,10 @@ internal class DragHelperCallback(
// equal to the vertical draggable range, the view has being dragged out,
// so close activity is called
if ((lastDraggingState == ViewDragHelper.STATE_DRAGGING
|| lastDraggingState == ViewDragHelper.STATE_SETTLING)
&& state == ViewDragHelper.STATE_IDLE
&& topBorderDraggableContainer >= dragToClose.getDraggableRange()) {
|| lastDraggingState == ViewDragHelper.STATE_SETTLING)
&& state == ViewDragHelper.STATE_IDLE
&& topBorderDraggableContainer >= dragToClose.getDraggableRange()
) {
dragToClose.closeActivity()
}
// If the view has just started being dragged, notify event
Expand Down Expand Up @@ -113,6 +114,6 @@ internal class DragHelperCallback(
override fun clampViewPositionVertical(child: View, top: Int, dy: Int): Int {
val topBound = dragToClose.paddingTop // Top limit
val bottomBound = dragToClose.getDraggableRange() // Bottom limit
return Math.min(Math.max(top, topBound), bottomBound)
return top.coerceAtLeast(topBound).coerceAtMost(bottomBound)
}
}
40 changes: 28 additions & 12 deletions lib/src/main/java/com/davidmiguel/dragtoclose/DragToClose.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ package com.davidmiguel.dragtoclose
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import androidx.annotation.AttrRes
import androidx.annotation.IdRes
import androidx.core.view.ViewCompat
import androidx.customview.widget.ViewDragHelper
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.WindowInsets
import android.widget.FrameLayout
import androidx.annotation.AttrRes
import androidx.annotation.IdRes
import androidx.core.view.ViewCompat
import androidx.core.view.forEach
import androidx.customview.widget.ViewDragHelper
import kotlin.math.abs

/**
Expand All @@ -37,6 +39,7 @@ class DragToClose : FrameLayout {
// Attributes
@IdRes
private var draggableContainerId: Int = -1

@IdRes
private var draggableViewId: Int = -1
private var finishActivity: Boolean = false
Expand Down Expand Up @@ -84,6 +87,13 @@ class DragToClose : FrameLayout {
}
}

override fun onApplyWindowInsets(insets: WindowInsets): WindowInsets {
this.forEach { children -> // Let children know about WindowInsets
children.dispatchApplyWindowInsets(insets)
}
return insets
}

/**
* Intercepts only touch events over the draggable view.
*/
Expand Down Expand Up @@ -192,7 +202,7 @@ class DragToClose : FrameLayout {
* dragged, even when it's triggered programatically. If false, it will only be called when
* then user is dragging the view (default).
*/
fun setAlwaysNotifyOnDragging(alwaysNotify : Boolean) {
fun setAlwaysNotifyOnDragging(alwaysNotify: Boolean) {
alwaysNotifyOnDragging = alwaysNotify
}

Expand All @@ -208,8 +218,10 @@ class DragToClose : FrameLayout {
* Slides up draggable container to its original position.
*/
fun openDraggableContainer() {
slideViewTo(draggableContainer, paddingLeft + draggableContainerLeft,
paddingTop + draggableContainerTop)
slideViewTo(
draggableContainer, paddingLeft + draggableContainerLeft,
paddingTop + draggableContainerTop
)
uiBlocked = false
}

Expand Down Expand Up @@ -247,7 +259,7 @@ class DragToClose : FrameLayout {
internal fun onViewPositionChanged() {
val verticalDragOffset = getVerticalDragOffset()
changeDragViewViewAlpha(verticalDragOffset)
if(alwaysNotifyOnDragging || !uiBlocked) {
if (alwaysNotifyOnDragging || !uiBlocked) {
listener?.onDragging(verticalDragOffset)
}
}
Expand Down Expand Up @@ -293,11 +305,11 @@ class DragToClose : FrameLayout {
*/
private fun initViews() {
draggableContainer = findViewById(draggableContainerId)
?: throw IllegalArgumentException("draggableContainer not found!")
?: throw IllegalArgumentException("draggableContainer not found!")
draggableContainerTop = draggableContainer.top
draggableContainerLeft = draggableContainer.left
draggableView = findViewById(draggableViewId)
?: throw IllegalArgumentException("draggableView not found!")
?: throw IllegalArgumentException("draggableView not found!")
if (closeOnClick) {
initOnClickListener(draggableView)
}
Expand All @@ -314,8 +326,10 @@ class DragToClose : FrameLayout {
* Initializes ViewDragHelper.
*/
private fun initViewDragHelper() {
dragHelper = ViewDragHelper.create(this, DRAG_SENSITIVITY,
DragHelperCallback(this, draggableContainer))
dragHelper = ViewDragHelper.create(
this, DRAG_SENSITIVITY,
DragHelperCallback(this, draggableContainer)
)
}

/**
Expand Down Expand Up @@ -351,9 +365,11 @@ class DragToClose : FrameLayout {

// Sensitivity detecting the start of a drag (larger values are more sensitive)
private const val DRAG_SENSITIVITY = 1.0f

// If the view is dragged with a higher speed than the threshold, the view is
// closed automatically
internal const val SPEED_THRESHOLD_TO_CLOSE = 800.0f

// If dragging finishes below this threshold the view returns to its original position,
// if the threshold is exceeded, the view is closed automatically
internal const val HEIGHT_THRESHOLD_TO_CLOSE = 0.5f
Expand Down
6 changes: 4 additions & 2 deletions sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="com.davidmiguel.sample.MainActivity">
<activity
android:name="com.davidmiguel.sample.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand All @@ -21,4 +23,4 @@
<activity android:name="com.davidmiguel.sample.Card3Activity" />
</application>

</manifest>
</manifest>
4 changes: 1 addition & 3 deletions sample/src/main/java/com/davidmiguel/sample/Card1Activity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package com.davidmiguel.sample
import android.os.Bundle
import android.util.Log
import android.widget.Button

import androidx.appcompat.app.AppCompatActivity
import com.davidmiguel.dragtoclose.DragListener
import com.davidmiguel.dragtoclose.DragToClose

import androidx.appcompat.app.AppCompatActivity

/**
* In this example the activity is closed when the card is dragged out.
* Dragging events are logged.
Expand Down
4 changes: 1 addition & 3 deletions sample/src/main/java/com/davidmiguel/sample/Card2Activity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package com.davidmiguel.sample

import android.os.Bundle
import android.view.View

import com.davidmiguel.dragtoclose.DragToClose

import androidx.appcompat.app.AppCompatActivity
import com.davidmiguel.dragtoclose.DragToClose

/**
* In this example the card can be dragged out card but the activity is not closed.
Expand Down
9 changes: 5 additions & 4 deletions sample/src/main/java/com/davidmiguel/sample/Card3Fragment.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.davidmiguel.sample

import android.os.Bundle
import androidx.fragment.app.Fragment

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment

/**
* Example of closing activity both dragging or clicking.
*/
class Card3Fragment : Fragment() {

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
super.onCreateView(inflater, container, savedInstanceState)
return inflater.inflate(R.layout.fragment_card3, container, false)
}
Expand Down

0 comments on commit b458c86

Please sign in to comment.