~repos /only-bible-app

#kotlin#android#ios

git clone https://pyrossh.dev/repos/only-bible-app.git

The only bible app you will ever need. No ads. No in-app purchases. No distractions.


992672f7 Peter John

1 year ago
fix swipe
app/src/main/java/dev/pyrossh/onlyBible/ChapterScreen.kt CHANGED
@@ -11,7 +11,7 @@ import androidx.compose.animation.AnimatedVisibility
11
11
  import androidx.compose.animation.slideInVertically
12
12
  import androidx.compose.animation.slideOutVertically
13
13
  import androidx.compose.foundation.clickable
14
- import androidx.compose.foundation.gestures.detectHorizontalDragGestures
14
+ import androidx.compose.foundation.gestures.detectDragGestures
15
15
  import androidx.compose.foundation.interaction.MutableInteractionSource
16
16
  import androidx.compose.foundation.isSystemInDarkTheme
17
17
  import androidx.compose.foundation.layout.Arrangement
@@ -43,8 +43,10 @@ import androidx.compose.material3.TextButton
43
43
  import androidx.compose.material3.TopAppBar
44
44
  import androidx.compose.runtime.Composable
45
45
  import androidx.compose.runtime.DisposableEffect
46
+ import androidx.compose.runtime.MutableIntState
46
47
  import androidx.compose.runtime.getValue
47
48
  import androidx.compose.runtime.mutableFloatStateOf
49
+ import androidx.compose.runtime.mutableIntStateOf
48
50
  import androidx.compose.runtime.mutableStateOf
49
51
  import androidx.compose.runtime.remember
50
52
  import androidx.compose.runtime.rememberCoroutineScope
@@ -53,6 +55,7 @@ import androidx.compose.runtime.setValue
53
55
  import androidx.compose.ui.Alignment
54
56
  import androidx.compose.ui.Modifier
55
57
  import androidx.compose.ui.graphics.Color
58
+ import androidx.compose.ui.input.pointer.PointerInputScope
56
59
  import androidx.compose.ui.input.pointer.pointerInput
57
60
  import androidx.compose.ui.platform.LocalContext
58
61
  import androidx.compose.ui.text.SpanStyle
@@ -72,6 +75,7 @@ import kotlinx.coroutines.Job
72
75
  import kotlinx.coroutines.launch
73
76
  import kotlinx.parcelize.Parcelize
74
77
  import kotlinx.serialization.Serializable
78
+ import kotlin.math.abs
75
79
 
76
80
  @Serializable
77
81
  @Parcelize
@@ -98,6 +102,38 @@ enum class Dir : Parcelable {
98
102
  }
99
103
  }
100
104
 
105
+ suspend fun PointerInputScope.detectSwipe(
106
+ swipeState: MutableIntState = mutableIntStateOf(-1),
107
+ onSwipeLeft: () -> Unit = {},
108
+ onSwipeRight: () -> Unit = {},
109
+ onSwipeUp: () -> Unit = {},
110
+ onSwipeDown: () -> Unit = {},
111
+ ) = detectDragGestures(
112
+ onDrag = { change, dragAmount ->
113
+ change.consume()
114
+ val (x, y) = dragAmount
115
+ if (abs(x) > abs(y)) {
116
+ when {
117
+ x > 0 -> swipeState.intValue = 0
118
+ x < 0 -> swipeState.intValue = 1
119
+ }
120
+ } else {
121
+ when {
122
+ y > 0 -> swipeState.intValue = 2
123
+ y < 0 -> swipeState.intValue = 3
124
+ }
125
+ }
126
+ },
127
+ onDragEnd = {
128
+ when (swipeState.intValue) {
129
+ 0 -> onSwipeRight()
130
+ 1 -> onSwipeLeft()
131
+ 2 -> onSwipeDown()
132
+ 3 -> onSwipeUp()
133
+ }
134
+ }
135
+ )
136
+
101
137
  @OptIn(ExperimentalMaterial3Api::class)
102
138
  @Composable
103
139
  fun ChapterScreen(
@@ -292,9 +328,8 @@ fun ChapterScreen(
292
328
  .padding(innerPadding)
293
329
  .padding(horizontal = 16.dp)
294
330
  .pointerInput(Unit) {
295
- detectHorizontalDragGestures(onDragEnd = {
331
+ detectSwipe(
296
- // println("END " + dragAmount);
297
- if (dragAmount < 0) {
332
+ onSwipeLeft = {
298
333
  val pair = Verse.getForwardPair(bookIndex, chapterIndex)
299
334
  navController.navigate(
300
335
  ChapterScreenProps(
@@ -302,7 +337,8 @@ fun ChapterScreen(
302
337
  chapterIndex = pair.second,
303
338
  )
304
339
  )
340
+ },
305
- } else if (dragAmount > 0) {
341
+ onSwipeRight = {
306
342
  val pair = Verse.getBackwardPair(bookIndex, chapterIndex)
307
343
  if (navController.previousBackStackEntry != null) {
308
344
  val previousBook =
@@ -334,11 +370,8 @@ fun ChapterScreen(
334
370
  )
335
371
  )
336
372
  }
337
- }
338
- }, onHorizontalDrag = { change, da ->
339
- dragAmount = da
340
- change.consume()
341
- })
373
+ },
374
+ )
342
375
  }) {
343
376
  items(chapterVerses) { v ->
344
377
  if (v.heading.isNotEmpty()) {
readme.md CHANGED
@@ -1,5 +1,4 @@
1
1
  ## TODO
2
2
 
3
- * Fix swipe calculations (look at horizontal pager)
4
3
  * Improve Paging in dark mode
5
4
  * Improve splash screen in dark mode