From db7eea723cdd52c59d733119f0414dea2e35e337 Mon Sep 17 00:00:00 2001 From: Hiroshi Kimura Date: Tue, 15 Oct 2024 23:56:49 +0900 Subject: [PATCH] Get Configuration to have modifier (#10) --- .../Development.xcodeproj/project.pbxproj | 6 ++-- Development/Development/AppDelegate.swift | 2 +- Development/Development/Util.swift | 1 + Package.swift | 5 +-- Sources/SwiftUIHosting/BaseModifier.swift | 35 +++++++++++++++++++ .../SwiftUIHosting/SwiftUIHostingView.swift | 15 +++++--- 6 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 Sources/SwiftUIHosting/BaseModifier.swift diff --git a/Development/Development.xcodeproj/project.pbxproj b/Development/Development.xcodeproj/project.pbxproj index 973afd5..e86061b 100644 --- a/Development/Development.xcodeproj/project.pbxproj +++ b/Development/Development.xcodeproj/project.pbxproj @@ -239,6 +239,7 @@ SDKROOT = iphoneos; SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 6.0; }; name = Debug; }; @@ -292,6 +293,7 @@ SDKROOT = iphoneos; SWIFT_COMPILATION_MODE = wholemodule; SWIFT_OPTIMIZATION_LEVEL = "-O"; + SWIFT_VERSION = 6.0; VALIDATE_PRODUCT = YES; }; name = Release; @@ -321,7 +323,7 @@ PRODUCT_BUNDLE_IDENTIFIER = app.muukii.Development; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; - SWIFT_VERSION = 5.0; + SWIFT_VERSION = 6.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; @@ -351,7 +353,7 @@ PRODUCT_BUNDLE_IDENTIFIER = app.muukii.Development; PRODUCT_NAME = "$(TARGET_NAME)"; SWIFT_EMIT_LOC_STRINGS = YES; - SWIFT_VERSION = 5.0; + SWIFT_VERSION = 6.0; TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; diff --git a/Development/Development/AppDelegate.swift b/Development/Development/AppDelegate.swift index df5f594..859df38 100644 --- a/Development/Development/AppDelegate.swift +++ b/Development/Development/AppDelegate.swift @@ -1,7 +1,7 @@ import UIKit import SwiftUI -@UIApplicationMain +@main final class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? diff --git a/Development/Development/Util.swift b/Development/Development/Util.swift index e22737d..accabf0 100644 --- a/Development/Development/Util.swift +++ b/Development/Development/Util.swift @@ -2,6 +2,7 @@ import SwiftUI import UIKit import SwiftUIHosting +@MainActor func measureSize(content: some View, targetSize: CGSize) { do { diff --git a/Package.swift b/Package.swift index 9e7ac8d..fd77252 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version: 5.7 +// swift-tools-version: 6.0 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription @@ -28,5 +28,6 @@ let package = Package( name: "SwiftUIHostingTests", dependencies: ["SwiftUIHosting"] ), - ] + ], + swiftLanguageModes: [.v6] ) diff --git a/Sources/SwiftUIHosting/BaseModifier.swift b/Sources/SwiftUIHosting/BaseModifier.swift new file mode 100644 index 0000000..af5373f --- /dev/null +++ b/Sources/SwiftUIHosting/BaseModifier.swift @@ -0,0 +1,35 @@ +import SwiftUI + +public struct BaseModifier: ViewModifier { + + @MainActor + public static var shared: Self = .init(locale: nil) + + public let locale: Locale? + + public init(locale: Locale?) { + self.locale = locale + } + + public func body(content: Content) -> some View { + content + .map { + if let locale { + $0.environment(\.locale, locale) + } else { + $0 + } + } + } + +} + +extension View { + + consuming func map( + @ViewBuilder transform: (consuming Self) -> Content + ) -> some View { + transform(self) + } + +} diff --git a/Sources/SwiftUIHosting/SwiftUIHostingView.swift b/Sources/SwiftUIHosting/SwiftUIHostingView.swift index 5f05740..64026e3 100644 --- a/Sources/SwiftUIHosting/SwiftUIHostingView.swift +++ b/Sources/SwiftUIHosting/SwiftUIHostingView.swift @@ -15,6 +15,7 @@ open class SwiftUIHostingView: UIView { case compressedSizeThatFits } + @MainActor public struct Configuration { /** @@ -36,15 +37,19 @@ open class SwiftUIHostingView: UIView { public var disableSafeArea: Bool public var sizeMeasureMode: SizeMeasureMode + + public var baseModifier: BaseModifier public init( registersAsChildViewController: Bool = true, disableSafeArea: Bool = true, - sizeMeasureMode: SizeMeasureMode = .systemSizeThatFits + sizeMeasureMode: SizeMeasureMode = .systemSizeThatFits, + baseModifier: BaseModifier = .shared ) { self.registersAsChildViewController = registersAsChildViewController self.disableSafeArea = disableSafeArea self.sizeMeasureMode = sizeMeasureMode + self.baseModifier = baseModifier } } @@ -62,26 +67,26 @@ open class SwiftUIHostingView: UIView { ) { self.configuration = configuration + + let usingContent = content().modifier(configuration.baseModifier) #if DEBUG self.hostingController = HostingController( accessibilityIdentifier: _typeName(Content.self), disableSafeArea: configuration.disableSafeArea, - rootView: AnyView(content()) + rootView: AnyView(usingContent) ) #else self.hostingController = HostingController( disableSafeArea: configuration.disableSafeArea, - rootView: AnyView(content()) + rootView: AnyView(usingContent) ) #endif - - super.init(frame: .null) #if DEBUG