https://kotlinlang.org logo
#compose-ios
Title
# compose-ios
a

Angga Ardinata

10/09/2023, 11:25 PM
Hi everyone .. Im Using Voyager as my Navigation .. However i cant swipe to go back on ios target .. is there a way or sample repo to implement back gesture on iosApp ? thank you ..
k

Konstantin Tskhovrebov

10/10/2023, 9:47 AM
cc @SrSouza
m

mangubatrj

10/11/2023, 5:43 AM
same here
a

Angga Ardinata

10/12/2023, 5:47 AM
im able to imitate back gesture manually with something like this
Copy code
abstract class BaseScreen : Screen, BaseScreenContent, ScreenLifecycleOwner {

    @Composable
    override fun Content() {
        val navigator = LocalNavigator.currentOrThrow
        var offsetState by remember { mutableFloatStateOf(0f) }
        Box(
            modifier = Modifier.background(Color.White)
        ) {
            ScreenContent()
            if (Platform.os == PlatformOS.iOS){
                Box(
                    modifier = Modifier
                        .fillMaxHeight()
                        .width(20.dp)
                        .pointerInput(Unit){
                            detectHorizontalDragGestures(
                                onDragEnd = {
                                    if (offsetState > 100) {
                                        if (navigator.items.size > 1) navigator.pop()
                                    }
                                    offsetState = 0f
                                }
                            ) { _, dragAmount ->
                                if (offsetState < 0) return@detectHorizontalDragGestures
                                offsetState += dragAmount
                            }
                        }
                )
            }
        }
    }
}

private interface BaseScreenContent {
    @Composable
    fun ScreenContent()
}
❤️ 1
m

mangubatrj

10/12/2023, 5:57 AM
@Angga Ardinata wow nice! I will try this. Did you only apply this to a BaseScreen and does that work for all? or you apply to each screen?
a

Angga Ardinata

10/12/2023, 5:59 AM
How do you create a new screen ? If just class HomeScreen : Screen {} Change the implementation of Screen with BaseScreen
To all of your screen
m

mangubatrj

10/12/2023, 6:02 AM
Right now I dont have a BaseScreen yet
but okay I will add that then
a

Angga Ardinata

10/12/2023, 6:03 AM
I will not work like ios back gesture where you can peek previous screen .. bit for me atleast user can goback with swipe 😁
Voyager still developing this feature
m

mangubatrj

10/12/2023, 6:04 AM
ahh yes that is all I need as well actually 😄
thank you very much @Angga Ardinata will try this on my end
a

Angga Ardinata

10/12/2023, 6:06 AM
https://github.com/adrielcafe/voyager/issues/144 .. here the issue for back gesture
1
m

mangubatrj

10/14/2023, 12:41 AM
hello @Angga Ardinata just want let you know I managed to implement the back gesture thanks to your help. Hopefully voyager adds the peek previous screen but your solution was a great alternative as well! 🙇
a

Angga Ardinata

10/14/2023, 11:43 AM
@mangubatrj Wow glad that im actually helping someone with my limited knowledge
🙌 1
s

SrSouza

10/14/2023, 2:58 PM
I have not being able to contribute to Voyager lately, but PR are welcome always. Suggestion folks, instead of using
BaseScreen
you folks could just add this component to the main Navigation Content. Example:
Copy code
Navigator(...) { navigator ->
   Box(..) {
     CurrentScreen()
     if (Platform.os == PlatformOS.iOS){
        ...
     }
   }
}
About previous screen peek, I need to validate about recomposition of it, but, Voyager Stack is built with SnapshotStateList, so, in practice, something like this:
Copy code
val previousScreen = remember(navigator) {
  derivedStateOf {
    navigator.toList().getOrNull(navigator.size - 2)
  }
}
But if this does not work properly we could easily add
peekPreviousItem
on it.
👍 1
I did share a implementation from PreCompose that I manage to make it work on Voyager, check it out: https://github.com/adrielcafe/voyager/issues/144#issuecomment-1763186873
🔥 1
❤️ 1
m

mangubatrj

10/16/2023, 12:36 AM
Thank you @SrSouza will definitely check this out! ❤️
7 Views