Skip to content

Commit

Permalink
Add sizeClass event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed Oct 22, 2024
1 parent d8f0b45 commit 6181ac2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ extension ExampleView {
* - Note: ref the apple bug: https://forums.developer.apple.com/forums/thread/708721
* - Fixme: ⚠️️ We need to populate detail on sidebar change, this can be done by setting the initial state, at least if we use navlinkdata construct etc, see production code
* - Fixme: ⚠️️ Maybe setting detail index to nil in compactmode will avoid moving directly to detailview from sidebar?
* - Fixme: ⚠️️ Get rid of index, just use item. it has uuid etc
* - Fixme: ⚠️️ Move the bellow into SideBarView scope?
* - Fixme: ⚠️️ Remove index, just use item. it has uuid etc.
* - Fixme: ⚠️️ Move the event handler into SideBarView scope?
* - Parameters:
* - splitConfig: The configuration object for the split view, which determines the layout and interaction behavior of the split view components.
* - sizeClass: A binding to the current size class of the user interface, which may affect layout decisions and adaptive behaviors.
Expand All @@ -49,8 +49,9 @@ extension ExampleView {
sizeClass: sizeClass,
splitConfig: splitConfig
)
// - Fixme: ⚠️️ Make handleSideBarChange ?
.onChange(of: selectedSideBarIndex) { handleSideBarChange(splitConfig, sizeClass) }
.onChange(of: selectedSideBarIndex) {
Self.handleSideBarChange(splitConfig, sizeClass)
} // forward state change to handleSideBarChange
}
/**
* Creates the center column view (aka mainview)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ extension ExampleView {
/**
* Handles changes in the sidebar selection.
* - Description: This function updates the `selectedMainItem` based on the current sidebar index. When `selectedMainItem` is updated, the views are regenerated to reflect the new selection. This mechanism ensures that the view automatically displays the last selected item when the sidebar changes.
* - Note: Method is static because we only read and modify param variables. Nothing in the instance scoope gets read or modified
* - Fixme: ⚠️️ possibly merge switch and guard. ask copilot
* - Parameters:
* - splitConfig: The configuration for the split view, controlling the layout and behavior.
* - sizeClass: The current size class of the user interface, which may affect layout decisions.
*/
func handleSideBarChange(_ splitConfig: SplitConfig, _ sizeClass: Binding<UserInterfaceSizeClass?>) {
guard let sizeClass = sizeClass.wrappedValue else { print("⚠️️ error"); return }
static func handleSideBarChange(_ splitConfig: SplitConfig, _ sizeClass: Binding<UserInterfaceSizeClass?>) {
guard let sizeClass: UserInterfaceSizeClass = sizeClass.wrappedValue else { print("⚠️️ error"); return }
switch sizeClass {
case .regular: // Only auto select mainitem if all columns are visible etc
$selectedMainItem.wrappedValue = DataModel.dataModel.getMainModel( // Only do this, if not in compact, because it will open detail mode, and skip main if in compact mode etc
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ These are some of the resources that was founds when solving edge cases for the

- or maybe add debug container through init wrapper

- Format the description comments to be 80 character wide

## Future improvements:

- Consider adding better support for 2-coulmn working right out of the box. Or add notes on how to do it
Expand Down
12 changes: 9 additions & 3 deletions Sources/SplitViewKit/SplitViewContainer+Content.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ extension SplitViewContainer {
splitViewContainer // Bellow debug container
debugContainer // Floats above navSplitView
}
.onChange(of: sizeClass) { oldValue, newValue in
if newValue == .compact {
print("👉 Switched to compact size class")
} else if newValue == .regular {
print("👉 Switched to regular size class")
}
}
}
}
/**
Expand All @@ -47,13 +54,12 @@ extension SplitViewContainer {
* Create navigationSplitView
* - Description: Creates a `NavigationSplitView` with the provided configuration and views. It dynamically adjusts the layout based on the window width and orientation.
* - Fixme: ⚠️️ Try to find a different way to pass horizontalSizeClass 👈 rebinding!
* - Fixme: ⚠️️ Make a binding navigationSplitViewStyle: NavigationSplitViewStyle
* - Fixme: ⚠️️ try to get rid of the forced unwrap
* - Fixme: ⚠️️ Make a binding `navigationSplitViewStyle: NavigationSplitViewStyle
* - Fixme: ⚠️️ try to figure out a better way to use sizeClass with out rebinding it etc
* - Fixme: ⚠️️ we might need to wrap detail in `NavigationStack` in some cases where presenting became an issue. or not. if not. remove this fixme
* - Note: We use `.balanced` as `navigationSplitViewStyle` in this case, as `.prominent` breaks the excpected UX a bit
* - Note: We got rid of environmentObject and now do param drill instead, param-drill the sizeClass and splitConfig, environment variables is confusing if its not passed correctly, it can jump to compact in the wrong scope where it should be regular etc, and doesnt attach if views are replaced, like detailview etc
* - Parameter winWidth: window width (from geomtry-reader)
* - Parameter winWidth: window width (from geomtry-reader) needed to calculate / evalute correct columnwidths
* - Returns: Nav-split-view
*/
func navigationSplitView(winWidth: CGFloat) -> some View {
Expand Down

0 comments on commit 6181ac2

Please sign in to comment.