Skip to content

Commit

Permalink
perf(AdaptivePanelViewModifier): Remove if in builder block
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Jun 12, 2023
1 parent 14ad3b1 commit 9d2b9f5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
27 changes: 22 additions & 5 deletions Mail/Utils/AdaptivePanelViewModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,34 @@ struct AdaptivePanelViewModifier<Item: Identifiable, PanelContent: View>: ViewMo
@Binding var item: Item?
@ViewBuilder var panelContent: (Item) -> PanelContent

enum AdaptiveSizeType {
case compact
case regular
}

func body(content: Content) -> some View {
if isCompactWindow {
content.floatingPanel(item: $item) { item in
content
.floatingPanel(item: getBindingForSize(.compact)) { item in
panelContent(item)
}
} else {
content.popover(item: $item) { item in
.popover(item: getBindingForSize(.regular)) { item in
panelContent(item)
.padding()
.frame(idealWidth: 400)
}
}
}

func getBindingForSize(_ size: AdaptiveSizeType) -> Binding<Item?> {
// We can't use if statement in this modifier because this creates performance issues in the List:
// => SwiftUI needs to create each element of the list before rendering if an if statement is present)
Binding(get: {
if isCompactWindow && size == .compact || !isCompactWindow && size == .regular {
return item
} else {
return nil
}
}, set: { newItem in
item = newItem
})
}
}
2 changes: 1 addition & 1 deletion Mail/Views/Thread List/ThreadListSwipeAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ struct ThreadListSwipeActions: ViewModifier {
edgeActions([swipeFullTrailing, swipeTrailing])
}
}
//.actionsPanel(actionsTarget: $actionsTarget)
.actionsPanel(actionsTarget: $actionsTarget)
.sheet(item: $moveAction) { moveAction in
MoveEmailView(moveAction: moveAction)
.sheetViewStyle()
Expand Down

0 comments on commit 9d2b9f5

Please sign in to comment.