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

Add POC for swiftUI version of emoji-search #628

Open
wants to merge 13 commits into
base: trunk
Choose a base branch
from

Conversation

skorulis-ap
Copy link
Collaborator

This creates a version of emoji-search which is built on top of SwiftUI. It's currently as bare bones as possible to get something to render. The following have not been touched:

  1. Handling layoutModifiers
  2. Responding to WidgetRow and WidgetColumn parameters (waiting on enums to come through)

The code was based off the UIKit app with the rendering and binding parts replaced. Because SwiftUI isn't representable in Kotlin the implementations of common layout structures need to be done in Swift. This creates the biggest hurdle which is how to share this code between projects.

How this works

Each Binding is implemented as a class, which has a number of @Published properties. When one of these changes SwiftUI will compute which parts of the view tree need to be rendered. Each binding also has a view parameter which defines how to render the binding. Classes get an automatic id property which allows swiftUI to understand the difference between an object being modified or added/removed.

import SwiftUI

final class ColumnBinding: BaseWidget, WidgetColumn, SwiftUIViewBinding {
func height(height: Int32) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof yeah Kotlin value classes don't interop well with Swift unfortunately. On the JVM (Android) these let us define 0 overhead enums.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might want to start considering using primitives and language features that do have better inter-opt, since the framework is meant to target both platforms. are there alternatives that could be considered for some of these?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could convert these properties to enums, though that has a small performance impact. Initially the plan was these interfaces would only be implemented in Kotlin.


}

func horizontalAlignment(horizontalAlignment: Int32) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@skorulis-ap The way we've been implementing these params to far is to pass them to a layout engine written in common Kotlin code. Basically it takes size constraints + views and returns positions for each of the children.

Is it possible to position children manually in a VStack or HStack? Else, we'll probably have to do something custom for Swift UI.

Copy link
Collaborator Author

@skorulis-ap skorulis-ap Dec 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Manual positioning is possible, but kind of fights the system. That I think is less of a worry than getting the sizes of each child. SwiftUI calculates the sizes during the layout pass so you would need to have some sort of side rendering happening to get these values.
iOS 16 adds a Layout object but that's not an option for a few years.

My plan was to use the SwiftUI layout system and respect the parameters that are passed in. wrapping likely being the hardest one to implement.

@JakeWharton
Copy link
Collaborator

Haven't forgot about this! Going to try to get it (and a version for the Counter sample) landed first thing next year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants