Skip to content

Commit

Permalink
Merge pull request #7 from allaboutapps/use-button-configuration
Browse files Browse the repository at this point in the history
use button configuration
  • Loading branch information
draskovits authored Feb 19, 2024
2 parents b264e08 + 88d765b commit 75e03eb
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// swift-tools-version:5.5
// swift-tools-version:5.9

import PackageDescription

let package = Package(
name: "GDPR",
defaultLocalization: "en",
platforms: [
.iOS(.v14)
.iOS(.v15)
],
products: [
.library(name: "GDPR", targets: ["GDPR"])
Expand Down
32 changes: 31 additions & 1 deletion Sources/GDPR/GDPRAppearance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import Foundation
import SwiftUI

public enum GDPRAppearance {

// MARK: Color

public static var primaryColor: Color = .primary
public static var navigationBarTintColor: Color = .accentColor
public static var bodyColor: Color = .primary
public static var disabledColor: Color = Color(white: 0.8)
public static var disabledColor: Color = .init(white: 0.8)

// MARK: Font

public static var headlineFont = Font.title3
public static var bodyFont = Font.body
Expand All @@ -14,6 +19,31 @@ public enum GDPRAppearance {
public static var subheaderFont = Font.headline
public static var controlsFont = Font.subheadline

// MARK: Buttons

public static var primaryButtonConfig = GDPRButtonConfig(
labelColor: .white,
font: bodyFont,
backgroundConfig: .init(
color: primaryColor,
disabledColor: disabledColor,
padding: EdgeInsets(top: 8, leading: 100, bottom: 8, trailing: 100),
cornerModus: .radius(10)
)
)

public static var linkButtonConfig = GDPRButtonConfig(
labelColor: primaryColor,
font: linkFont,
backgroundConfig: nil
)

public static var acceptAllButtonConfig = GDPRButtonConfig(
labelColor: primaryColor,
font: linkFont,
backgroundConfig: nil
)

enum Padding {
public static var single: CGFloat = 8
public static var double: CGFloat = 16
Expand Down
5 changes: 1 addition & 4 deletions Sources/GDPR/Views/ConfirmationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@ public struct ConfirmationView: View {
self.onConfirm?()
}, label: {
Text("confirm", bundle: Bundle.module)
.padding(EdgeInsets(top: 8, leading: 100, bottom: 8, trailing: 100))
.background(isEnabled ? GDPRAppearance.primaryColor : GDPRAppearance.disabledColor)
.clipShape(RoundedRectangle(cornerRadius: 10))
.accentColor(.white)
})
.disabled(!isEnabled)
.padding(.bottom, 10.0)
.buttonStyle(GDPRButtonStyle(config: GDPRAppearance.primaryButtonConfig))
}
}
}
Expand Down
78 changes: 78 additions & 0 deletions Sources/GDPR/Views/GDPRButtonStyle.swift
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)
}
}
}
12 changes: 6 additions & 6 deletions Sources/GDPR/Views/PolicyItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ public struct PolicyItem: View {
self.showWebView.toggle()

}, label: {
Text("privacyTitle", bundle: Bundle.module)
}).sheet(isPresented: self.$showWebView) {
Text("privacyTitle",bundle: Bundle.module)
})
.buttonStyle(GDPRButtonStyle(config: GDPRAppearance.linkButtonConfig))
.sheet(isPresented: self.$showWebView) {
NavigationView {
ServiceWebView(url: url)
.navigationBarTitle(Text("privacyTitle", bundle: Bundle.module), displayMode: .inline)
Expand All @@ -31,11 +33,9 @@ public struct PolicyItem: View {
}
.foregroundColor(GDPRAppearance.navigationBarTintColor)
}
.foregroundColor(GDPRAppearance.primaryColor)
.font(GDPRAppearance.linkFont)


Divider()
}.buttonStyle(PlainButtonStyle())
}
}
}

Expand Down
7 changes: 3 additions & 4 deletions Sources/GDPR/Views/TermsItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ struct TermsItem: View {

Button(action: {
self.showWebView.toggle()

}, label: {
Text("termsTitle", bundle: Bundle.module)
}).sheet(isPresented: self.$showWebView) {
})
.buttonStyle(GDPRButtonStyle(config: GDPRAppearance.linkButtonConfig))
.sheet(isPresented: self.$showWebView) {
NavigationView {
ServiceWebView(url: termsURL)
.navigationBarTitle(Text("termsTitle", bundle: Bundle.module), displayMode: .inline)
Expand All @@ -35,8 +36,6 @@ struct TermsItem: View {
}
.foregroundColor(GDPRAppearance.navigationBarTintColor)
}
.foregroundColor(GDPRAppearance.primaryColor)
.font(GDPRAppearance.linkFont)
}

if showSwitch {
Expand Down
5 changes: 2 additions & 3 deletions Sources/GDPR/Views/TrackingItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ struct TrackingItem: View {
self.acceptAll = true
}, label: {
Text("allowAll", bundle: Bundle.module)
.font(GDPRAppearance.controlsFont)
.foregroundColor(GDPRAppearance.primaryColor)
}).buttonStyle(PlainButtonStyle())
})
.buttonStyle(GDPRButtonStyle(config: GDPRAppearance.acceptAllButtonConfig))
}
Divider()
}.buttonStyle(PlainButtonStyle())
Expand Down

0 comments on commit 75e03eb

Please sign in to comment.