Skip to content

Commit 1b83d11

Browse files
fix
1 parent 363b6e7 commit 1b83d11

2 files changed

Lines changed: 25 additions & 63 deletions

File tree

app/src/main/java/io/nekohasekai/sagernet/ui/OtherMenuBottomSheet.kt

Lines changed: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,32 @@
11
package io.nekohasekai.sagernet.ui
22

3-
import android.animation.Animator
4-
import android.animation.ValueAnimator
53
import android.content.Context
64
import android.os.Bundle
75
import android.view.LayoutInflater
86
import android.view.View
97
import android.view.ViewGroup
10-
import android.view.animation.DecelerateInterpolator
118
import android.widget.CheckedTextView
129
import android.widget.ImageView
10+
import androidx.transition.AutoTransition
11+
import androidx.transition.TransitionManager
1312
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
1413
import io.nekohasekai.sagernet.GroupOrder
1514
import io.nekohasekai.sagernet.R
1615

1716

18-
private const val ANIMATION_DURATION = 300L
17+
private const val TRANSITION_DURATION = 300L
1918

20-
private fun View.expand() {
21-
measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
22-
val targetHeight = measuredHeight
19+
private fun View.toggleWithTransition(parentView: ViewGroup, isExpanding: Boolean) {
20+
21+
TransitionManager.beginDelayedTransition(parentView, AutoTransition().setDuration(TRANSITION_DURATION))
2322

24-
layoutParams.height = 1
25-
visibility = View.VISIBLE
26-
27-
val valueAnimator = ValueAnimator.ofInt(1, targetHeight)
28-
valueAnimator.addUpdateListener { animator ->
29-
layoutParams.height = animator.animatedValue as Int
30-
requestLayout()
31-
}
32-
33-
valueAnimator.interpolator = DecelerateInterpolator()
34-
valueAnimator.duration = ANIMATION_DURATION
35-
valueAnimator.start()
36-
}
37-
38-
private fun View.collapse() {
39-
val initialHeight = measuredHeight
40-
41-
val valueAnimator = ValueAnimator.ofInt(initialHeight, 0)
42-
valueAnimator.addUpdateListener { animator ->
43-
layoutParams.height = animator.animatedValue as Int
44-
requestLayout()
45-
}
46-
47-
valueAnimator.interpolator = DecelerateInterpolator()
48-
valueAnimator.duration = ANIMATION_DURATION
49-
valueAnimator.start()
50-
51-
valueAnimator.addListener(object : Animator.AnimatorListener {
52-
override fun onAnimationStart(animation: Animator) {}
53-
override fun onAnimationEnd(animation: Animator) {
54-
visibility = View.GONE
55-
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT // Reset height
56-
}
57-
override fun onAnimationCancel(animation: Animator) {}
58-
override fun onAnimationRepeat(animation: Animator) {}
59-
})
23+
this.visibility = if (isExpanding) View.VISIBLE else View.GONE
6024
}
6125

6226
private fun View.animateRotation(endDegree: Float) {
6327
this.animate()
6428
.rotation(endDegree)
65-
.setDuration(ANIMATION_DURATION)
29+
.setDuration(TRANSITION_DURATION)
6630
.start()
6731
}
6832

@@ -114,13 +78,17 @@ class OtherMenuBottomSheet : BottomSheetDialogFragment() {
11478
GroupOrder.BY_DELAY -> checkDelay?.isChecked = true
11579
}
11680

81+
val menuContainerParent = view.findViewById<ViewGroup>(R.id.menu_container_parent) ?: view as ViewGroup
82+
11783
setupExpandable(
84+
parent = menuContainerParent,
11885
toggleHeader = view.findViewById(R.id.action_group_setting_1),
11986
expandableContent = view.findViewById(R.id.expandable_setting_content_1),
12087
arrowIcon = view.findViewById(R.id.arrow_icon_1)
12188
)
12289

12390
setupExpandable(
91+
parent = menuContainerParent,
12492
toggleHeader = view.findViewById(R.id.action_group_setting_2),
12593
expandableContent = view.findViewById(R.id.expandable_setting_content_2),
12694
arrowIcon = view.findViewById(R.id.arrow_icon_2)
@@ -149,28 +117,21 @@ class OtherMenuBottomSheet : BottomSheetDialogFragment() {
149117
}
150118
}
151119

152-
private fun setupExpandable(toggleHeader: View?, expandableContent: View?, arrowIcon: ImageView?) {
120+
private fun setupExpandable(parent: ViewGroup, toggleHeader: View?, expandableContent: View?, arrowIcon: ImageView?) {
153121
if (toggleHeader != null && expandableContent != null) {
154122

155-
if (expandableContent.visibility == View.VISIBLE) {
156-
arrowIcon?.rotation = 90f
157-
expandableContent.layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
158-
} else {
159-
arrowIcon?.rotation = 0f
123+
if (arrowIcon != null) {
124+
arrowIcon.rotation = if (expandableContent.visibility == View.VISIBLE) 90f else 0f
160125
}
161-
126+
162127
toggleHeader.setOnClickListener {
163-
if (expandableContent.visibility == View.GONE) {
164-
165-
expandableContent.visibility = View.VISIBLE
166-
167-
expandableContent.post {
168-
expandableContent.expand()
169-
arrowIcon?.animateRotation(90f)
170-
}
171-
128+
val isExpanding = expandableContent.visibility == View.GONE
129+
130+
if (isExpanding) {
131+
expandableContent.toggleWithTransition(parent, true)
132+
arrowIcon?.animateRotation(90f)
172133
} else {
173-
expandableContent.collapse()
134+
expandableContent.toggleWithTransition(parent, false)
174135
arrowIcon?.animateRotation(0f)
175136
}
176137
}

app/src/main/res/layout/uwu_bottom_sheet_other_menu.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
app:cardBackgroundColor="#00000000"
2020
app:cardCornerRadius="28.0dip"
2121
app:cardElevation="0.0dip">
22-
<ScrollView
22+
<androidx.core.widget.NestedScrollView
2323
android:layout_width="fill_parent"
2424
android:layout_height="fill_parent"
2525
android:layout_marginTop="-16.0dip"
2626
android:layout_marginStart="-16.0dip"
2727
android:layout_marginEnd="-16.0dip">
2828
<LinearLayout
29+
android:id="@+id/menu_container_parent"
2930
android:orientation="vertical"
3031
android:layout_width="fill_parent"
3132
android:layout_height="wrap_content">
@@ -1085,6 +1086,6 @@
10851086
</LinearLayout>
10861087
</com.google.android.material.card.MaterialCardView>
10871088
</LinearLayout>
1088-
</ScrollView>
1089+
</androidx.core.widget.NestedScrollView>
10891090
</com.google.android.material.card.MaterialCardView>
10901091
</LinearLayout>

0 commit comments

Comments
 (0)