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

Fix bug replacing of root widget on UIKit #1706

Merged
merged 2 commits into from
Nov 30, 2023

Conversation

Alex009
Copy link
Contributor

@Alex009 Alex009 commented Nov 29, 2023

Hello!

We find bug on iOS with UIKit and redwood. Case:
create uikit with root UIStackView - just like in CounterApp sample:

let container = UIStackView()
container.axis = .horizontal
container.alignment = .fill
container.distribution = .fillEqually
container.translatesAutoresizingMaskIntoConstraints = false

view.addSubview(container)
container.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
container.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
container.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
container.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true

self.delegate = CounterViewControllerDelegate(root: container)

in delegate use RedwoodUIView(root):

val composition = RedwoodComposition(
  scope = scope,
  view = RedwoodUIView(root),
  provider = SchemaWidgetFactories(
    Schema = IosWidgetFactory,
    RedwoodLayout = UIViewRedwoodLayoutWidgetFactory(),
  ),
)

but in Composable of our screen we should change root view by some condition (for example we change loading state to successful):

@Composable
fun Counter(value: Int = 0) {
  var count by rememberSaveable { mutableStateOf(value) }

  if (count == 3) {
    Text("this is end")
    return
  }
  
  Column(
    width = Constraint.Fill,
    height = Constraint.Fill,
    horizontalAlignment = CrossAxisAlignment.Center,
    verticalAlignment = MainAxisAlignment.Center,
  ) {
    Button("-1", onClick = { count-- })
    Text("Count: $count")
    Button("+1", onClick = { count++ })
  }
}

but istead of change root uiview we see that no changes.
reason - UIStackView add new child in arrangedSubviews (at 0 index) but remove child from subviews (that have different order). so when new UILabel added this label will be removed, instead of remove ColumnUIView

@JakeWharton
Copy link
Collaborator

Thanks! I'd like to get a test case in for this somewhere. Probably another subclass of the abstract children test.

@JakeWharton JakeWharton force-pushed the fix-change-of-root-uikit-view branch from 8621cf8 to 061b90f Compare November 30, 2023 03:18
@JakeWharton JakeWharton enabled auto-merge (squash) November 30, 2023 03:28
@JakeWharton JakeWharton merged commit 0bd1bb3 into cashapp:trunk Nov 30, 2023
8 checks passed
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.

2 participants