feat: Resolving concurrency warnings

This commit is contained in:
2024-06-11 10:34:19 -04:00
parent e07df0e426
commit bf2e65fb8f
15 changed files with 156 additions and 130 deletions

View File

@@ -2,7 +2,7 @@ import ComposableArchitecture
import SwiftUI
@Reducer
public struct InfoViewFeature {
public struct InfoViewFeature: Sendable {
public init() { }

View File

@@ -10,21 +10,23 @@ public enum NextButtonType { }
public enum ResetButtonType { }
/// A type erased button style, used to style buttons in a namespace.
public struct AnyButtonStyle<ButtonType>: ButtonStyle {
private let _makeBody: (Configuration) -> AnyView
public init<S: ButtonStyle>(_ style: S) {
self._makeBody = { configuration in
AnyView(style.makeBody(configuration: configuration))
}
}
public func makeBody(configuration: Configuration) -> some View {
self._makeBody(configuration)
}
}
//@MainActor
//public struct AnyButtonStyle<ButtonType>: ButtonStyle, Sendable {
// private let _makeBody: @Sendable (Configuration) -> AnyView
//
// public init<S: ButtonStyle>(_ style: S) where S: Sendable, S.Body: Sendable {
// self._makeBody = { configuration in
// AnyView(style.makeBody(configuration: configuration))
// }
// }
//
// public func makeBody(configuration: Configuration) -> some View {
// self._makeBody(configuration)
// }
//}
/// A type erased primitive button style, used to style buttons in a namespace.
@MainActor
public struct AnyPrimitiveButtonStyle<ButtonType>: PrimitiveButtonStyle {
private let _makeBody: (Configuration) -> AnyView
@@ -40,7 +42,8 @@ public struct AnyPrimitiveButtonStyle<ButtonType>: PrimitiveButtonStyle {
}
/// The default info button style.
public struct DefaultInfoButtonStyle<Style: LabelStyle>: ButtonStyle {
@MainActor
public struct DefaultInfoButtonStyle<Style: LabelStyle>: PrimitiveButtonStyle {
let color: Color
let font: Font
let labelStyle: Style
@@ -56,11 +59,17 @@ public struct DefaultInfoButtonStyle<Style: LabelStyle>: ButtonStyle {
}
public func makeBody(configuration: Configuration) -> some View {
configuration.label
.font(font)
.foregroundStyle(color.opacity(configuration.isPressed ? 0.5 : 1))
.labelStyle(labelStyle)
.scaleEffect(configuration.isPressed ? 0.8 : 1)
Button(role: configuration.role, action: configuration.trigger) {
configuration.label
.font(font)
.foregroundStyle(color)
.labelStyle(labelStyle)
}
// configuration.label
// .font(font)
// .foregroundStyle(color.opacity(configuration.isPressed ? 0.5 : 1))
// .labelStyle(labelStyle)
// .scaleEffect(configuration.isPressed ? 0.8 : 1)
}
}
@@ -104,32 +113,40 @@ extension AnyPrimitiveButtonStyle<NextButtonType> {
public static var toolbar: Self { .init(ToolbarNextButtonStyle()) }
}
extension AnyButtonStyle where ButtonType == InfoButtonType {
public static var `default`: Self {
.init(DefaultInfoButtonStyle<IconOnlyLabelStyle>(labelStyle: .iconOnly))
}
public static func `default`<S: LabelStyle>(color: Color, font: Font, labelStyle: S) -> Self {
.init(DefaultInfoButtonStyle<S>(color: color, font: font, labelStyle: labelStyle))
}
}
//extension AnyButtonStyle where ButtonType == InfoButtonType {
// public static var `default`: Self {
// .init(DefaultInfoButtonStyle<IconOnlyLabelStyle>(labelStyle: .iconOnly))
// }
//
// public static func `default`<S: LabelStyle>(color: Color, font: Font, labelStyle: S) -> Self {
// .init(DefaultInfoButtonStyle<S>(color: color, font: font, labelStyle: labelStyle))
// }
//}
private struct InfoButtonStyleKey: EnvironmentKey {
static var defaultValue = AnyButtonStyle<InfoButtonType>.default
private struct InfoButtonStyleKey: @preconcurrency EnvironmentKey {
@MainActor
static var defaultValue: AnyPrimitiveButtonStyle<InfoButtonType> {
AnyPrimitiveButtonStyle<InfoButtonType>(DefaultInfoButtonStyle(labelStyle: .iconOnly))
}
}
private struct NextButtonStyleKey: EnvironmentKey {
static var defaultValue = AnyPrimitiveButtonStyle<NextButtonType>(
DefaultNextButtonStyle<BorderedProminentButtonStyle, NextLabelStyle>()
)
@MainActor
static var defaultValue: AnyPrimitiveButtonStyle<NextButtonType> {
AnyPrimitiveButtonStyle<NextButtonType>(
DefaultNextButtonStyle<BorderedProminentButtonStyle, NextLabelStyle>()
)
}
}
private struct ResetButtonStyleKey: EnvironmentKey {
static var defaultValue = AnyPrimitiveButtonStyle<ResetButtonType>(.borderedProminent)
static let defaultValue = AnyPrimitiveButtonStyle<ResetButtonType>(.borderedProminent)
}
extension EnvironmentValues {
var infoButtonStyle: AnyButtonStyle<InfoButtonType> {
// @Entry var infoButtonStyle: AnyButtonStyle<InfoButtonType> = AnyButtonStyle.default
var infoButtonStyle: AnyPrimitiveButtonStyle<InfoButtonType> {
get { self[InfoButtonStyleKey.self] }
set { self[InfoButtonStyleKey.self] = newValue }
}
@@ -148,13 +165,13 @@ extension EnvironmentValues {
extension View {
/// Sets the button style for the ``InfoButton`` type.
public func infoButtonStyle(_ style: AnyButtonStyle<InfoButtonType>) -> some View {
public func infoButtonStyle(_ style: AnyPrimitiveButtonStyle<InfoButtonType>) -> some View {
environment(\.infoButtonStyle, style)
}
/// Sets the button style for the ``InfoButton`` type.
public func infoButtonStyle<S: ButtonStyle>(_ style: S) -> some View {
infoButtonStyle(AnyButtonStyle(style))
public func infoButtonStyle<S: PrimitiveButtonStyle>(_ style: S) -> some View {
infoButtonStyle(AnyPrimitiveButtonStyle(style))
}
/// Sets the button style for the ``NextButton`` type.

View File

@@ -1,5 +1,6 @@
import SwiftUI
@MainActor
public protocol TextLabelStyle {
associatedtype Body: View
@@ -28,6 +29,7 @@ public struct TextLabelConfiguration {
public let label: TextLabelConfiguration.Label
}
@MainActor
public struct AnyTextLabelStyle: TextLabelStyle {
private var _makeBody: (Configuration) -> AnyView
@@ -57,6 +59,7 @@ public struct AnyTextLabelStyle: TextLabelStyle {
}
}
@MainActor
public struct AutomaticTextLabelStyle: TextLabelStyle {
public func makeBody(configuration: Configuration) -> some View {
configuration.label