Skip to content

Commit

Permalink
Get Configuration to have modifier (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Oct 15, 2024
1 parent 28bd930 commit db7eea7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
6 changes: 4 additions & 2 deletions Development/Development.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@
SDKROOT = iphoneos;
SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 6.0;
};
name = Debug;
};
Expand Down Expand Up @@ -292,6 +293,7 @@
SDKROOT = iphoneos;
SWIFT_COMPILATION_MODE = wholemodule;
SWIFT_OPTIMIZATION_LEVEL = "-O";
SWIFT_VERSION = 6.0;
VALIDATE_PRODUCT = YES;
};
name = Release;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion Development/Development/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import UIKit
import SwiftUI

@UIApplicationMain
@main
final class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
Expand Down
1 change: 1 addition & 0 deletions Development/Development/Util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import SwiftUI
import UIKit
import SwiftUIHosting

@MainActor
func measureSize(content: some View, targetSize: CGSize) {

do {
Expand Down
5 changes: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -28,5 +28,6 @@ let package = Package(
name: "SwiftUIHostingTests",
dependencies: ["SwiftUIHosting"]
),
]
],
swiftLanguageModes: [.v6]
)
35 changes: 35 additions & 0 deletions Sources/SwiftUIHosting/BaseModifier.swift
Original file line number Diff line number Diff line change
@@ -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<Content: View>(
@ViewBuilder transform: (consuming Self) -> Content
) -> some View {
transform(self)
}

}
15 changes: 10 additions & 5 deletions Sources/SwiftUIHosting/SwiftUIHostingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ open class SwiftUIHostingView: UIView {
case compressedSizeThatFits
}

@MainActor
public struct Configuration {

/**
Expand All @@ -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
}
}

Expand All @@ -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
Expand Down

0 comments on commit db7eea7

Please sign in to comment.