-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from allaboutapps/use-button-configuration
use button configuration
- Loading branch information
Showing
7 changed files
with
123 additions
and
20 deletions.
There are no files selected for viewing
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
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
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
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,78 @@ | ||
import Foundation | ||
import SwiftUI | ||
|
||
public struct GDPRButtonConfig { | ||
|
||
let labelColor: Color | ||
let font: Font | ||
let backgroundConfig: BackgroundConfig? | ||
|
||
public init(labelColor: Color, font: Font, backgroundConfig: BackgroundConfig?) { | ||
self.labelColor = labelColor | ||
self.font = font | ||
self.backgroundConfig = backgroundConfig | ||
} | ||
|
||
public struct BackgroundConfig { | ||
|
||
let color: Color | ||
let disabledColor: Color | ||
let padding: EdgeInsets | ||
let cornerModus: CornerRadius | ||
|
||
public init(color: Color, disabledColor: Color, padding: EdgeInsets, cornerModus: CornerRadius = .radius(10)) { | ||
self.color = color | ||
self.disabledColor = disabledColor | ||
self.padding = padding | ||
self.cornerModus = cornerModus | ||
} | ||
|
||
public enum CornerRadius: ViewModifier { | ||
case radius(CGFloat) | ||
case round | ||
|
||
public func body(content: Content) -> some View { | ||
switch self { | ||
case .radius(let float): | ||
content.clipShape(RoundedRectangle(cornerRadius: float)) | ||
case .round: | ||
content.clipShape(Capsule()) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public struct GDPRButtonStyle: ButtonStyle { | ||
|
||
let config: GDPRButtonConfig | ||
|
||
@Environment(\.isEnabled) | ||
var isEnabled: Bool | ||
|
||
func backgroundColor(for configuration: Configuration, backgroundConfig: GDPRButtonConfig.BackgroundConfig) -> Color { | ||
if configuration.isPressed { | ||
backgroundConfig.color.opacity(0.7) | ||
} else if isEnabled { | ||
backgroundConfig.color | ||
} else { | ||
backgroundConfig.disabledColor | ||
} | ||
} | ||
|
||
public func makeBody(configuration: Configuration) -> some View { | ||
if let backgroundConfig = config.backgroundConfig { | ||
configuration.label | ||
.font(config.font) | ||
.foregroundStyle(config.labelColor) | ||
.padding(backgroundConfig.padding) | ||
.background(backgroundColor(for: configuration, backgroundConfig: backgroundConfig)) | ||
.tint(backgroundConfig.color) | ||
.modifier(backgroundConfig.cornerModus) | ||
} else { | ||
configuration.label | ||
.font(config.font) | ||
.foregroundStyle(config.labelColor) | ||
} | ||
} | ||
} |
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
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
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