-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/db button #7
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
9509929
added DBButton and DBLink
ahersch dbfd972
fixed preview titles
ahersch af58d89
added DBCard
ahersch 0b910f0
added behavior to DBCard
ahersch f7b9c83
fixed linting
ahersch d864502
added DBAccordion
ahersch bc48b9a
added DBSwitch
ahersch 970ca07
linting
ahersch b40937d
refactoring
ahersch f2ce383
refactoring
ahersch 0764c64
refactoring
ahersch 79adb2c
review comments
ahersch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,222 @@ | ||
| // | ||
| // Copyright 2024 by DB Systel GmbH | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
|
|
||
| import SwiftUI | ||
| import DBUXFoundation | ||
|
|
||
| enum DBAccordionVariant: CaseIterable { | ||
| case divider | ||
| case card | ||
|
|
||
| internal func previewName(def: DBAccordionVariant = .divider) -> String { | ||
| var name = "\(def == self ? "(Def) " : "")" | ||
|
|
||
| switch self { | ||
| case .divider: | ||
| name.append("Divider") | ||
| case .card: | ||
| name.append("Card") | ||
| } | ||
|
|
||
| return name | ||
| } | ||
| } | ||
|
|
||
| enum DBAccordionBehavior: CaseIterable { | ||
| case multiple | ||
| case single | ||
|
|
||
| internal func previewName(def: DBAccordionBehavior = .multiple) -> String { | ||
| var name = "\(def == self ? "(Def) " : "")" | ||
|
|
||
| switch self { | ||
| case .multiple: | ||
| name.append("Multiple") | ||
| case .single: | ||
| name.append("Single") | ||
| } | ||
|
|
||
| return name | ||
| } | ||
| } | ||
|
|
||
| struct DBAccordionItem: Equatable { | ||
| var title: String | ||
| var content: () -> any View | ||
| let uuid = UUID() | ||
|
|
||
| static func ==(lhs: DBAccordionItem, rhs: DBAccordionItem) -> Bool { | ||
| return lhs.uuid == rhs.uuid | ||
| } | ||
| } | ||
|
|
||
| struct DBAccordion: View { | ||
| @Environment(\.theme) var theme | ||
|
|
||
| var items: [DBAccordionItem] = [] | ||
| var variant: DBAccordionVariant = .divider | ||
| var behavior: DBAccordionBehavior = .multiple | ||
| var disabled: Bool = false | ||
|
|
||
| @State var expandedStates: [UUID: Bool] = [:] | ||
|
|
||
| var body: some View { | ||
| VStack(spacing: theme.dimensions.spacing.fixedSm) { | ||
| ForEach(items, id: \.uuid) { item in | ||
| DBAccordionSection(item: item, disabled: disabled, expanded: expandedState(for: item.uuid)) | ||
| .background( | ||
| variant == .card | ||
| ? | ||
| RoundedRectangle(cornerRadius: theme.dimensions.border.radiusSm) | ||
| .stroke(theme.activeColor.onBgBasicEmphasis60Default, lineWidth: theme.dimensions.border.height3xs) | ||
| .padding(0.5) | ||
| : nil | ||
| ) | ||
| .cornerRadius(theme.dimensions.border.radiusSm) | ||
|
ahersch marked this conversation as resolved.
|
||
|
|
||
| if variant == .divider && item != items.last { | ||
| theme.activeColor.onBgBasicEmphasis60Default | ||
| .frame(height: 1) | ||
| .padding(.vertical, theme.dimensions.spacing.fixedSm) | ||
| } | ||
| } | ||
|
|
||
| } | ||
| .opacity(disabled ? 0.4 : 1) | ||
| .onChange(of: expandedStates) { oldValue, newValue in | ||
| if behavior == .single { | ||
| let newExpandedItems = newValue.filter { $0.value }.map { $0.key } | ||
| let oldExpandedItems = oldValue.filter { $0.value }.map { $0.key } | ||
| if newExpandedItems.count > oldExpandedItems.count { | ||
| withAnimation(.snappy) { | ||
| for uuid in newExpandedItems { | ||
| expandedStates[uuid] = oldValue[uuid] ?? false ? false : true | ||
|
ahersch marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
| } | ||
|
ahersch marked this conversation as resolved.
|
||
| } | ||
| } | ||
|
|
||
| func expandedState(for key: UUID) -> Binding<Bool> { | ||
| return .init( | ||
| get: { | ||
| self.expandedStates[key, default: false] | ||
| }, | ||
| set: { | ||
| self.expandedStates[key] = $0 | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| struct DBAccordionSection: View { | ||
| @Environment(\.theme) var theme | ||
|
|
||
| var item: DBAccordionItem | ||
| var disabled: Bool = false | ||
| @Binding var expanded: Bool | ||
| @GestureState private var pressed: Bool = false | ||
|
|
||
| var body: some View { | ||
| let pressGesture = DragGesture(minimumDistance: 0) | ||
| .updating($pressed) { _, state, _ in | ||
| if !disabled { | ||
| state = true | ||
|
ahersch marked this conversation as resolved.
|
||
| } | ||
| } | ||
| .onEnded { _ in | ||
| if !disabled { | ||
| withAnimation(.snappy) { | ||
|
ahersch marked this conversation as resolved.
|
||
| expanded.toggle() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| VStack(spacing: 0) { | ||
| ZStack { | ||
|
ahersch marked this conversation as resolved.
|
||
| HStack { | ||
| Text(item.title) | ||
| .dsTextStyle(theme.fonts.bodyMd) | ||
| Spacer() | ||
| Image(expanded ? .chevronUp : .chevronDown) | ||
| } | ||
| .padding(theme.dimensions.spacing.fixedMd) | ||
| } | ||
| .contentShape(Rectangle()) | ||
| .foregroundColor(theme.activeColor.onBgBasicEmphasis100Default) | ||
| .background(pressed ? theme.activeColor.basic.background.transparent.pressed : Color.clear) | ||
| .cornerRadius(theme.dimensions.border.radiusSm) | ||
| .gesture(pressGesture) | ||
|
|
||
| if expanded { | ||
| AnyView(item.content()) | ||
| .padding(.horizontal, theme.dimensions.spacing.fixedMd) | ||
| .padding(.top, theme.dimensions.spacing.fixedMd) | ||
| .padding(.bottom, theme.dimensions.spacing.fixedLg) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #Preview(traits: .sizeThatFitsLayout) { | ||
| var itemsVariants = [ | ||
| DBAccordionItem(title: "Accordion Item", content: { Text("Content") }), | ||
| DBAccordionItem(title: "Accordion Item", content: { Text("Content") }), | ||
| DBAccordionItem(title: "Accordion Item", content: { Text("Content") }) | ||
| ] | ||
| var itemsProperties = [ | ||
| DBAccordionItem(title: "Headline", content: { Text("Content") }), | ||
| DBAccordionItem(title: "Headline", content: { Text("Content") }), | ||
| DBAccordionItem(title: "Headline", content: { Text("Content") }) | ||
| ] | ||
| PreviewTemplate( | ||
| title: "DBAccordion", | ||
| previewVariants: DBAccordionVariant.allCases.map({ accordionVariant in | ||
| [ | ||
| AnyView(DBAccordion(items: itemsVariants, variant: accordionVariant)) | ||
| ] | ||
| }), | ||
| previewProperties: [ | ||
| PreviewPropertiesSection( | ||
| name: "Variant", | ||
| content: DBAccordionVariant.allCases.map({ accordionVariant in | ||
| PreviewPropertiesElement( | ||
| description: accordionVariant.previewName(), | ||
| content: { DBAccordion(items: itemsProperties, variant: accordionVariant) } | ||
| ) | ||
| }) | ||
| ), | ||
| PreviewPropertiesSection( | ||
| name: "Disabled", | ||
| content: [false, true].map({ accordionDisabled in | ||
| PreviewPropertiesElement( | ||
| description: "\(accordionDisabled ? "(Def) " : "")\(accordionDisabled.description.capitalized)", | ||
| content: { DBAccordion(items: itemsProperties, disabled: accordionDisabled) } | ||
| ) | ||
| }) | ||
| ), | ||
| PreviewPropertiesSection( | ||
| name: "Behavior", | ||
| content: DBAccordionBehavior.allCases.map({ accordionBehavior in | ||
| PreviewPropertiesElement( | ||
| description: accordionBehavior.previewName(), | ||
| content: { DBAccordion(items: itemsProperties, behavior: accordionBehavior) } | ||
| ) | ||
| }) | ||
| ), | ||
| ], | ||
| ) | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.