Michał Rymarski
12/16/2024, 8:14 PMclass GestureViewController(
private val onBackPressed: () -> Unit
) : UIViewController() {
override fun viewDidLoad() {
super.viewDidLoad()
val edgePanGesture = UIScreenEdgePanGestureRecognizer(target = this, action = NSSelectorFromString("handleEdgePan:"))
edgePanGesture.edges = UIRectEdgeRight
view.addGestureRecognizer(edgePanGesture)
}
@ObjCAction
fun handleEdgePan(gesture: UIScreenEdgePanGestureRecognizer) {
if (gesture.state == UIGestureRecognizerStateEnded) {
onBackPressed()
}
}
}
fun MainViewController(): UIViewController {
GlobalKtorClient.initClient()
initKoin()
return ComposeUIViewController {
AppTheme {
val navController = rememberNavController()
GestureViewController(onBackPressed = navController::navigateUp).apply {
setContent {
NavigationHost(
navController = navController,
loginDataStore = remember { createDataStore() },
testDataStore = remember { createDataStore() }
)
}
}
}
}
}
But it doesnt work. If it isnt possible with compose navigation, then it's ok. It's just a featureHristijan
12/16/2024, 10:59 PMAndrei Salavei
12/17/2024, 10:39 AMUIScreenEdgePanGestureRecognizer
to trigger navigate back actions.Hristijan
12/17/2024, 10:49 AMAndrei Salavei
12/17/2024, 11:40 AMGestureViewController(onBackPressed = navController::navigateUp)
You didn't add GestureViewController to hierarchy, that's why it does not work.
You either should put the ComposeUIViewController inside the GestureViewController as a child view controller, or add UIScreenEdgePanGestureRecognizer
directly to the ComposeUIViewController.view (See LocalUIViewController
)
Ah, and it should work (but with no guarantee) only with 1.7.2 when it becomes available.Hristijan
12/20/2024, 7:45 PMfun MainViewController(
onAppInitialized: (() -> Unit)? = null,
): UIViewController =
GestureViewController(
contentViewController = ComposeUIViewController(configure = {
onFocusBehavior = OnFocusBehavior.DoNothing
}) {
MyApp(onAppInitialized = onAppInitialized)
},
onBackPressed = coreComponent.navigatorDispatcher::navigateUp
)
class GestureViewController(
private val contentViewController: UIViewController,
private val onBackPressed: () -> Unit
) : UIViewController(null, null) {
@OptIn(ExperimentalForeignApi::class)
override fun viewDidLoad() {
super.viewDidLoad()
view.addSubview(contentViewController.view)
val edgePanGesture = UIScreenEdgePanGestureRecognizer(
target = this,
action = NSSelectorFromString("handleEdgePan:")
)
edgePanGesture.edges = UIRectEdgeRight
contentViewController.view.addGestureRecognizer(edgePanGesture)
}
@OptIn(BetaInteropApi::class)
@ObjCAction
fun handleEdgePan(gesture: UIScreenEdgePanGestureRecognizer) {
if (gesture.state == UIGestureRecognizerStateEnded) {
onBackPressed()
}
}
}
i guess this is not the correct code?Andrei Salavei
12/20/2024, 9:45 PMcontentViewController.view
either by adding constrains or in viewDidLayoutSubiviews
method.
2. Add contentViewController
as a child and don't forget to call didMoveToParent
3. Are you using compose 1.7.3 ?
4. Not sure if it's important, but still - try to attach UIScreenEdgePanGestureRecognizer
to the self.view
, not to the contentViewController.view
Hristijan
12/20/2024, 10:54 PMHristijan
12/20/2024, 10:55 PMstruct ComposeView: UIViewControllerRepresentable {
var onAppInitialized: (() -> Void)?
func makeUIViewController(context: Context) -> UIViewController {
MainViewControllerKt.MainViewController(
onAppInitialized: onAppInitialized
)
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}
struct ContentView: View {
var body: some View {
ZStack {
Color(.surface)
ComposeView(onAppInitialized: {
}
})
}
}
Hristijan
12/20/2024, 10:58 PMfun MainViewController(
onAppInitialized: (() -> Unit)? = null
) = ComposeUIViewController(
configure = {
onFocusBehavior = OnFocusBehavior.DoNothing
}
) {
CMPApp(
onAppInitialized = onAppInitialized
)
}
Hristijan
01/30/2025, 2:39 PMAndrei Salavei
01/30/2025, 2:42 PMHristijan
01/30/2025, 2:42 PMAndrei Salavei
01/30/2025, 2:54 PMBackHandler
/ PredictiveBackHandler
API