Skip to content

Commit

Permalink
Added the possibility to specify the calendar and time zone when rend…
Browse files Browse the repository at this point in the history
…ering
  • Loading branch information
MatsMoll committed May 4, 2019
1 parent 93e12e9 commit 006d4c0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ extension TemplateForEach: CompiledTemplate {

// View `BrewableFormula` documentation
public func brew<T>(_ formula: HTMLRenderer.Formula<T>) throws {
localFormula.calendar = formula.calendar
localFormula.timeZone = formula.timeZone
formula.add(mappable: self)
try view.build().brew(localFormula)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ extension TemplateIF: CompiledTemplate {
public func brew<T>(_ formula: HTMLRenderer.Formula<T>) throws {
formula.add(mappable: self)
for condition in conditions {
condition.localFormula.calendar = formula.calendar
condition.localFormula.timeZone = formula.timeZone
try condition.brew(formula)
}
}
Expand Down
6 changes: 2 additions & 4 deletions Sources/HTMLKit/DateVariable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ struct DateVariable<T: ContextualTemplate>: CompiledTemplate {

// View `CompiledTemplate`
func brew<T>(_ formula: HTMLRenderer.Formula<T>) throws where T : ContextualTemplate {
formatter.calendar = formula.calendar
formatter.timeZone = formula.timeZone
formula.add(mappable: self)
}
}
Expand All @@ -76,7 +78,6 @@ extension ContextualTemplate {
let formatter = DateFormatter()
formatter.dateStyle = dateStyle
formatter.timeStyle = timeStyle
formatter.timeZone = TimeZone(identifier: "UTC")
return DateVariable<Self>(formatter: formatter, dateReferance: .solid(datePath))
}

Expand All @@ -99,7 +100,6 @@ extension ContextualTemplate {
public func date(_ datePath: KeyPath<Context, Date>, format: String) -> CompiledTemplate {
let formatter = DateFormatter()
formatter.dateFormat = format
formatter.timeZone = TimeZone(identifier: "UTC")
return DateVariable<Self>(formatter: formatter, dateReferance: .solid(datePath))
}

Expand All @@ -114,7 +114,6 @@ extension ContextualTemplate {
let formatter = DateFormatter()
formatter.dateStyle = dateStyle
formatter.timeStyle = timeStyle
formatter.timeZone = TimeZone(identifier: "UTC")
return DateVariable<Self>(formatter: formatter, dateReferance: .optional(datePath))
}

Expand All @@ -127,7 +126,6 @@ extension ContextualTemplate {
public func date(_ datePath: KeyPath<Context, Date?>, format: String) -> CompiledTemplate {
let formatter = DateFormatter()
formatter.dateFormat = format
formatter.timeZone = TimeZone(identifier: "UTC")
return DateVariable<Self>(formatter: formatter, dateReferance: .optional(datePath))
}

Expand Down
20 changes: 17 additions & 3 deletions Sources/HTMLKit/HTMLRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public struct HTMLRenderer {
/// The localization to use when rendering
var lingo: Lingo?

/// The calendar to use when rendering dates
var calendar: Calendar = Calendar(identifier: .gregorian)

/// The time zone to use when rendering dates
var timeZone: TimeZone = TimeZone(secondsFromGMT: 0) ?? TimeZone.current

public init() {
formulaCache = [:]
}
Expand Down Expand Up @@ -105,7 +111,7 @@ public struct HTMLRenderer {
/// - Parameter type: The view type to brew
/// - Throws: If the brewing process fails for some reason
public mutating func add<T: ContextualTemplate>(template view: T) throws {
let formula = Formula(view: T.self)
let formula = Formula(view: T.self, calendar: calendar, timeZone: timeZone)
try view.build().brew(formula)
formulaCache[String(reflecting: T.self)] = formula
}
Expand All @@ -117,7 +123,7 @@ public struct HTMLRenderer {
/// - Parameter type: The view type to brew
/// - Throws: If the brewing process fails for some reason
public mutating func add<T: LocalizedTemplate>(template view: T) throws {
let formula = Formula(view: T.self)
let formula = Formula(view: T.self, calendar: calendar, timeZone: timeZone)
formula.localePath = T.localePath
guard formula.localePath != nil else {
throw Localize<T, NoContext>.Errors.missingLocalePath
Expand Down Expand Up @@ -197,14 +203,22 @@ public struct HTMLRenderer {
/// The path to the selected locale to use in localization
var localePath: KeyPath<T.Context, String>?

/// The calendar to use when rendering dates
var calendar: Calendar

/// The time zone to use when rendering dates
var timeZone: TimeZone

/// Init's a view
///
/// - Parameters:
/// - view: The view type
/// - contextPaths: The contextPaths. *Is empty by default*
init(view: T.Type, contextPaths: [String : AnyKeyPath] = [:]) {
init(view: T.Type, calendar: Calendar, timeZone: TimeZone, contextPaths: [String : AnyKeyPath] = [:]) {
self.contextPaths = contextPaths
ingredient = []
self.calendar = calendar
self.timeZone = timeZone
}

/// Registers a key-path for later referancing
Expand Down
2 changes: 1 addition & 1 deletion Sources/HTMLKit/TemplateForEach.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public class TemplateForEach<Root: ContextualTemplate, Value: ContextualTemplate
init(view: Value, referance: ContextReferance<Root, [Value.Context]>) {
self.view = view
self.referance = referance
self.localFormula = HTMLRenderer.Formula(view: Value.self)
self.localFormula = HTMLRenderer.Formula(view: Value.self, calendar: .current, timeZone: .current)
}
}
2 changes: 1 addition & 1 deletion Sources/HTMLKit/TemplateIF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TemplateIF<Root> where Root: ContextualTemplate {
/// - Parameter condition: The condition to evaluate
init(condition: Conditionable) {
self.condition = condition
localFormula = HTMLRenderer.Formula(view: Root.self)
localFormula = HTMLRenderer.Formula(view: Root.self, calendar: .current, timeZone: .current)
}
}

Expand Down
7 changes: 7 additions & 0 deletions Tests/HTMLKitTests/HTMLKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ final class HTMLKitTests: XCTestCase {
let testDate = Date()
let shortDateFormatter = DateFormatter()
let customDateFormatter = DateFormatter()
shortDateFormatter.calendar = .current
shortDateFormatter.timeZone = .current
shortDateFormatter.dateStyle = .short
shortDateFormatter.timeStyle = .short
customDateFormatter.calendar = .current
customDateFormatter.timeZone = .current
customDateFormatter.dateFormat = "MM/dd/yyyy"

renderer.calendar = .current
renderer.timeZone = .current

try renderer.add(template: StaticEmbedView())
try renderer.add(template: StaticEmbedView())
try renderer.add(template: SomeView())
Expand Down

0 comments on commit 006d4c0

Please sign in to comment.