Repository navigation
Expand file tree
/
Copy pathJetLaggedDrawer.kt
More file actions
268 lines (250 loc) · 9.92 KB
/
Copy pathJetLaggedDrawer.kt
File metadata and controls
268 lines (250 loc) · 9.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
* Copyright 2023 The Android Open Source Project
*
* 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
*
* https://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.example.jetlagged
import android.os.SystemClock
import androidx.activity.compose.PredictiveBackHandler
import androidx.annotation.DrawableRes
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.calculateTargetValue
import androidx.compose.animation.rememberSplineBasedDecay
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.draggable
import androidx.compose.foundation.gestures.rememberDraggableState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Icon
import androidx.compose.material3.NavigationDrawerItem
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.windowsizeclass.WindowSizeClass
import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.input.pointer.util.VelocityTracker
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.util.lerp
import kotlin.coroutines.cancellation.CancellationException
import kotlinx.coroutines.launch
@Composable
fun HomeScreenDrawer(windowSizeClass: WindowSizeClass) {
Surface(
modifier = Modifier.fillMaxSize(),
) {
var drawerState by remember {
mutableStateOf(DrawerState.Closed)
}
var screenState by remember {
mutableStateOf(Screen.Home)
}
val translationX = remember {
Animatable(0f)
}
val drawerWidth = with(LocalDensity.current) {
DrawerWidth.toPx()
}
translationX.updateBounds(0f, drawerWidth)
val coroutineScope = rememberCoroutineScope()
suspend fun closeDrawer(velocity: Float = 0f) {
translationX.animateTo(targetValue = 0f, initialVelocity = velocity)
drawerState = DrawerState.Closed
}
suspend fun openDrawer(velocity: Float = 0f) {
translationX.animateTo(targetValue = drawerWidth, initialVelocity = velocity)
drawerState = DrawerState.Open
}
fun toggleDrawerState() {
coroutineScope.launch {
if (drawerState == DrawerState.Open) {
closeDrawer()
} else {
openDrawer()
}
}
}
val velocityTracker = remember {
VelocityTracker()
}
PredictiveBackHandler(drawerState == DrawerState.Open) { progress ->
try {
progress.collect { backEvent ->
val targetSize = (drawerWidth - (drawerWidth * backEvent.progress))
translationX.snapTo(targetSize)
velocityTracker.addPosition(
SystemClock.uptimeMillis(),
Offset(backEvent.touchX, backEvent.touchY),
)
}
closeDrawer(velocityTracker.calculateVelocity().x)
} catch (_: CancellationException) {
openDrawer(velocityTracker.calculateVelocity().x)
}
velocityTracker.resetTracking()
}
HomeScreenDrawerContents(
selectedScreen = screenState,
onScreenSelected = { screen ->
screenState = screen
},
)
val draggableState = rememberDraggableState(onDelta = { dragAmount ->
coroutineScope.launch {
translationX.snapTo(translationX.value + dragAmount)
}
})
val decay = rememberSplineBasedDecay<Float>()
ScreenContents(
windowWidthSizeClass = windowSizeClass.widthSizeClass,
selectedScreen = screenState,
onDrawerClicked = ::toggleDrawerState,
modifier = Modifier
.graphicsLayer {
this.translationX = translationX.value
val scale = lerp(1f, 0.8f, translationX.value / drawerWidth)
this.scaleX = scale
this.scaleY = scale
val roundedCorners = lerp(0f, 32.dp.toPx(), translationX.value / drawerWidth)
this.shape = RoundedCornerShape(roundedCorners)
this.clip = true
this.shadowElevation = 32f
}
// This example is showing how to use draggable with custom logic on stop to snap to the edges
// You can also use `anchoredDraggable()` to set up anchors and not need to worry about more calculations.
.draggable(
draggableState, Orientation.Horizontal,
onDragStopped = { velocity ->
val targetOffsetX = decay.calculateTargetValue(
translationX.value,
velocity,
)
coroutineScope.launch {
val actualTargetX = if (targetOffsetX > drawerWidth * 0.5) {
drawerWidth
} else {
0f
}
// checking if the difference between the target and actual is + or -
val targetDifference = (actualTargetX - targetOffsetX)
val canReachTargetWithDecay =
(
targetOffsetX > actualTargetX &&
velocity > 0f &&
targetDifference > 0f
) ||
(
targetOffsetX < actualTargetX &&
velocity < 0 &&
targetDifference < 0f
)
if (canReachTargetWithDecay) {
translationX.animateDecay(
initialVelocity = velocity,
animationSpec = decay,
)
} else {
translationX.animateTo(actualTargetX, initialVelocity = velocity)
}
drawerState = if (actualTargetX == drawerWidth) {
DrawerState.Open
} else {
DrawerState.Closed
}
}
},
),
)
}
}
@Composable
private fun ScreenContents(
windowWidthSizeClass: WindowWidthSizeClass,
selectedScreen: Screen,
onDrawerClicked: () -> Unit,
modifier: Modifier = Modifier,
) {
Box(modifier) {
when (selectedScreen) {
Screen.Home ->
JetLaggedScreen(
windowSizeClass = windowWidthSizeClass,
modifier = Modifier,
onDrawerClicked = onDrawerClicked,
)
Screen.SleepDetails ->
Surface(
modifier = Modifier.fillMaxSize(),
) {
}
Screen.Leaderboard ->
Surface(
modifier = Modifier.fillMaxSize(),
) {
}
Screen.Settings ->
Surface(
modifier = Modifier.fillMaxSize(),
) {
}
}
}
}
private enum class DrawerState {
Open,
Closed,
}
@Composable
private fun HomeScreenDrawerContents(selectedScreen: Screen, onScreenSelected: (Screen) -> Unit, modifier: Modifier = Modifier) {
Column(
modifier
.fillMaxSize()
.padding(16.dp),
verticalArrangement = Arrangement.Center,
) {
Screen.entries.forEach {
NavigationDrawerItem(
label = {
Text(it.text)
},
icon = {
Icon(painter = painterResource(id = it.icon), contentDescription = it.text)
},
selected = selectedScreen == it,
onClick = {
onScreenSelected(it)
},
)
}
}
}
private val DrawerWidth = 300.dp
private enum class Screen(val text: String, @DrawableRes val icon: Int) {
Home("Home", R.drawable.ic_home),
SleepDetails("Sleep", R.drawable.ic_bedtime),
Leaderboard("Leaderboard", R.drawable.ic_leaderboard),
Settings("Settings", R.drawable.ic_settings),
}