-
-
Notifications
You must be signed in to change notification settings - Fork 689
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Joseph Mattiello <[email protected]>
- Loading branch information
Showing
6 changed files
with
282 additions
and
212 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
PVUI/Sources/PVSwiftUI/Settings/Components/CollapsibleSection.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// | ||
// CollapsibleSection.swift | ||
// PVUI | ||
// | ||
// Created by Joseph Mattiello on 11/9/24. | ||
// | ||
|
||
import SwiftUI | ||
import PVSettings | ||
import Defaults | ||
|
||
internal struct CollapsibleSection<Content: View>: View { | ||
let title: String | ||
let content: Content | ||
@Default(.collapsedSections) var collapsedSections | ||
@State private var isExpanded: Bool | ||
|
||
init(title: String, @ViewBuilder content: () -> Content) { | ||
self.title = title | ||
self.content = content() | ||
self._isExpanded = State(initialValue: !Defaults[.collapsedSections].contains(title)) | ||
print("Init CollapsibleSection '\(title)' - collapsed sections: \(Defaults[.collapsedSections])") | ||
} | ||
|
||
var body: some View { | ||
Section { | ||
if isExpanded { | ||
content | ||
} | ||
} header: { | ||
Button(action: { | ||
withAnimation { | ||
isExpanded.toggle() | ||
print("Setting isExpanded for '\(title)' to \(isExpanded)") | ||
print("Before - collapsed sections: \(collapsedSections)") | ||
if isExpanded { | ||
collapsedSections.remove(title) | ||
} else { | ||
collapsedSections.insert(title) | ||
} | ||
print("After - collapsed sections: \(collapsedSections)") | ||
} | ||
}) { | ||
HStack { | ||
Text(title) | ||
Spacer() | ||
Image(systemName: isExpanded ? "chevron.up" : "chevron.down") | ||
.foregroundColor(.accentColor) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.