Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UIViewController interop #882

Merged
merged 7 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion compose/mpp/demo-uikit/src/iosAppMain/apple/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,20 @@ struct ContentView: View {
}
}

struct NestedContentView: View {
let index: Int

var body: some View {
Text("Hello from SwiftUI #\(index)")
}
}

struct ComposeView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
SwiftHelper().getViewController()
SwiftHelper().getViewController { index in
let viewController = UIHostingController(rootView: NestedContentView(index: Int(index)))
return viewController
}
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ package androidx.compose.mpp.demo
import platform.UIKit.UIViewController

class SwiftHelper {
fun getViewController(): UIViewController = getViewControllerWithCompose()
fun getViewController(makeHostingViewController: (Int) -> UIViewController): UIViewController = getViewControllerWithCompose(makeHostingViewController)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package androidx.compose.mpp.demo

import androidx.compose.ui.window.ComposeUIViewController
import platform.UIKit.UIViewController

// TODO This module is just a proxy to run the demo from mpp:demo. Figure out how to get rid of it.
// If it is removed, there is no available configuration in IDE
fun getViewControllerWithCompose() = ComposeUIViewController {
IosDemo("")
fun getViewControllerWithCompose(makeHostingViewController: (Int) -> UIViewController) = ComposeUIViewController {
IosDemo("", makeHostingViewController)
}
42 changes: 42 additions & 0 deletions compose/mpp/demo/src/uikitMain/kotlin/SwiftUIInteropExample.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.mpp.demo.Screen
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.interop.UIKitViewController
import androidx.compose.ui.unit.dp
import platform.UIKit.UIViewController

/*
* 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
*
* http://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.
*/

fun SwiftUIInteropExample(makeViewController: (Int) -> UIViewController) = Screen.Example("SwiftUI interop example") {
LazyColumn(Modifier.fillMaxSize()) {
items(20) {
UIKitViewController(factory = {
makeViewController(it)
}, Modifier.fillMaxWidth().height(160.dp))
Spacer(Modifier.height(160.dp))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
package androidx.compose.mpp.demo

import NativeModalWithNaviationExample
import SwiftUIInteropExample
import androidx.compose.runtime.*
import androidx.compose.ui.main.defaultUIKitMain
import androidx.compose.ui.window.ComposeUIViewController
import bugs.IosBugs
import bugs.StartRecompositionCheck
import platform.UIKit.UIViewController


fun main(vararg args: String) {
Expand All @@ -17,13 +19,17 @@ fun main(vararg args: String) {
}

@Composable
fun IosDemo(arg: String) {
fun IosDemo(arg: String, makeHostingController: ((Int) -> UIViewController)? = null) {
val app = remember {
App(
extraScreens = listOf(
IosBugs,
NativeModalWithNaviationExample,
)
) + listOf(makeHostingController).mapNotNull {
it?.let {
SwiftUIInteropExample(it)
}
}
)
}
when (arg) {
Expand Down
Loading