Skip to content

Commit

Permalink
Refactor the code a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
mattesmohr committed Oct 28, 2024
1 parent 62e02e3 commit 4bc848a
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 80 deletions.
39 changes: 27 additions & 12 deletions Sources/HTMLKit/Framework/Environment/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Foundation
/// A class that represents the environment
///
/// The environment provides storage for various settings used by the renderer
public class Environment {
public final class Environment {

/// The storage of the environment
private var storage: [AnyKeyPath: Any]
Expand All @@ -15,29 +15,44 @@ public class Environment {
}

/// The current time zone of the environment
public var timeZone: TimeZone?
public var timeZone: TimeZone? {

get {
retrieve(for: \EnvironmentKeys.timeZone) as? TimeZone
}
}

/// The current calendar of the environment
public var calendar: Calendar?
public var calendar: Calendar? {

get {
retrieve(for: \EnvironmentKeys.calendar) as? Calendar
}
}

/// The current locale of the environment
public var locale: Locale?
public var locale: Locale? {

get {
retrieve(for: \EnvironmentKeys.locale) as? Locale
}
}

/// The current color scheme of the environment
public var colorScheme: String?
public var colorScheme: String? {

get {
retrieve(for: \EnvironmentKeys.colorScheme) as? String
}
}

/// Retrieves a value from environment for a given key path
///
/// - Parameter path: The key path used to look up the value
///
/// - Returns: The value
public func retrieve(for path: AnyKeyPath) -> Any? {

if let value = self.storage[path] {
return value
}

return nil
return storage[path]
}

/// Inserts or updates a value in the environment for the given key path
Expand All @@ -46,6 +61,6 @@ public class Environment {
/// - value: The value to be stored or updated
/// - path: The key path that identifies where the value is stored
public func upsert<T>(_ value: T, for path: AnyKeyPath) {
self.storage[path] = value
storage[path] = value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
public struct EnvironmentModifier: Content {

/// The environment key
public var key: AnyKeyPath
internal var key: AnyKeyPath

/// The environment value
public var value: Any?
internal var value: Any?

/// The sub-content
public var content: [Content]
internal var content: [Content]

/// Initializes an environment modifier
///
Expand Down
25 changes: 0 additions & 25 deletions Sources/HTMLKit/Framework/Rendering/Renderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -369,31 +369,6 @@ public final class Renderer {

if let value = modifier.value {
self.environment.upsert(value, for: modifier.key)

} else {

if let value = self.environment.retrieve(for: modifier.key) {

if let key = modifier.key as? PartialKeyPath<EnvironmentKeys> {

switch key {
case \.locale:
self.environment.locale = value as? Locale

case \.calendar:
self.environment.calendar = value as? Calendar

case \.timeZone:
self.environment.timeZone = value as? TimeZone

case \.colorScheme:
self.environment.colorScheme = value as? String

default:
throw Errors.unindendedEnvironmentKey
}
}
}
}

return try render(contents: modifier.content)
Expand Down
85 changes: 46 additions & 39 deletions Tests/HTMLKitTests/EnvironmentTests.swift
Original file line number Diff line number Diff line change
@@ -1,64 +1,71 @@
/*
Abstract:
The file tests the annotations.
*/

import HTMLKit
import XCTest

final class EnvironmentTests: XCTestCase {

struct Object: Encodable {

var title: String = "Welcome to WWDC"
var name: String = "Mattes!"
var image: String = "wwdc.jpeg"
}
var renderer = Renderer()

struct ParentView: View {
/// Tests the environment access through the environment object
///
/// The renderer is expected to evaluate the placeholder and renderer the resulting value.
func testEnvironmentAccess() throws {

var content: [Content]
struct FamilyObject: ViewModel {

let name: String = "Doe"
let father: FatherObject = FatherObject()
}

init(@ContentBuilder<Content> content: () -> [Content]) {
self.content = content()
struct FatherObject: ViewModel {

let avatar: String = "john_doe.jpeg"
let name: String = "John"
}

var body: Content {
Division {
content
struct ParentView: View {

let content: [Content]

init(@ContentBuilder<Content> content: () -> [Content]) {
self.content = content()
}

var body: Content {
Division {
content
}
.environment(object: FamilyObject())
}
.environment(object: Object())
}
}

struct ChildView: View {

@EnvironmentObject(Object.self)
var object

var body: Content {
ParentView {
Section{
Image()
.source(object.image)
Heading2 {
object.title + " " + object.name
struct ChildView: View {

@EnvironmentObject(FamilyObject.self)
var object

var body: Content {
ParentView {
Section{
Image()
.source(object.father.avatar)
Heading1 {
object.name
}
Paragraph {
object.father.name + " " + object.name
}
}
}
}
}
}

var renderer = Renderer()

func testEnvironment() throws {

XCTAssertEqual(try renderer.render(view: ChildView()),
"""
<div>\
<section>\
<img src="wwdc.jpeg">\
<h2>Welcome to WWDC Mattes!</h2>\
<img src="john_doe.jpeg">\
<h1>Doe</h1>\
<p>John Doe</p>\
</section>\
</div>
"""
Expand Down
6 changes: 5 additions & 1 deletion Tests/HTMLKitVaporTests/ProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ final class ProviderTests: XCTestCase {
content
}
}
.environment(object: TestObject())
}
}

Expand Down Expand Up @@ -192,12 +191,17 @@ final class ProviderTests: XCTestCase {
}
}

/// Tests the access to environment through provider
///
/// The provider is expected to recieve the environment object and resolve it based on the request.
func testEnvironmentIntegration() throws {

let app = Application(.testing)

defer { app.shutdown() }

app.htmlkit.environment.upsert(TestObject(), for: \TestObject.self)

app.get("test") { request async throws -> Vapor.View in
return try await request.htmlkit.render(TestPage.SipplingView())
}
Expand Down

0 comments on commit 4bc848a

Please sign in to comment.