Skip to content

Commit

Permalink
#217: Handle different sizes of TimelineView.Context type
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Naumov committed Jan 8, 2023
1 parent 2d14b14 commit d758f9b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
29 changes: 25 additions & 4 deletions Sources/ViewInspector/SwiftUI/TimelineView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public extension InspectableView where View == ViewType.TimelineView {

func contentView(_ context: ViewType.TimelineView.Context = .init()
) throws -> InspectableView<ViewType.ClassifiedView> {
let contextCopy = context
return try ViewType.TimelineView.view(for: contextCopy, parent: self)
return try ViewType.TimelineView.view(for: context, parent: self)
}
}

Expand All @@ -74,12 +73,23 @@ public extension ViewType.TimelineView {
}
public let date: Date
public let cadence: Cadence
private let filler: (Int64, Int64) = (0, 0)

public init(date: Date = Date(), cadence: Cadence = .live) {
self.date = date
self.cadence = cadence
}

fileprivate var context32: Context32 { Context32(context: self) }
}
fileprivate struct Context32 {
private let date: Date
private let cadence: Context.Cadence
private let filler: (Int64, Int64) = (0, 0)

init(context: Context) {
self.date = context.date
self.cadence = context.cadence
}
}
}

Expand All @@ -91,7 +101,18 @@ extension TimelineView: ElementViewProvider {
label: "content", value: self, type: Builder.self)
let param = try Inspector.cast(
value: element, type: ViewType.TimelineView.Context.self)
let context = try Inspector.unsafeMemoryRebind(value: param, type: Context.self)
let context = try Self.adapt(context: param)
return builder(context)
}

static func adapt(context: ViewType.TimelineView.Context) throws -> Context {
switch MemoryLayout<Context>.size {
case 9:
return try Inspector.unsafeMemoryRebind(value: context, type: Context.self)
case 32:
return try Inspector.unsafeMemoryRebind(value: context.context32, type: Context.self)
default:
fatalError(MemoryLayout<Context>.actualSize())
}
}
}
10 changes: 3 additions & 7 deletions Tests/ViewInspectorTests/SwiftUI/TimelineViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ final class TimelineViewTests: XCTestCase {
guard #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, *)
else { throw XCTSkip() }
typealias SUT = ViewType.TimelineView.Context
typealias REF = TimelineView<EveryMinuteTimelineSchedule, EmptyView>.Context
typealias SUTMemLayout = MemoryLayout<SUT>
typealias REFMemLayout = MemoryLayout<REF>
XCTAssertEqual(SUTMemLayout.size, REFMemLayout.size)
XCTAssertEqual(SUTMemLayout.alignment, REFMemLayout.alignment)
XCTAssertEqual(SUTMemLayout.stride, REFMemLayout.stride)
typealias SpecificTimelineView = TimelineView<EveryMinuteTimelineSchedule, EmptyView>
let date = Date()
let value = SUT(date: date, cadence: .minutes)
let rebound = try Inspector.unsafeMemoryRebind(value: value, type: REF.self)
let adapted = try SpecificTimelineView.adapt(context: value)
let rebound = try Inspector.unsafeMemoryRebind(value: adapted, type: SpecificTimelineView.Context.self)
XCTAssertEqual(rebound.date, date)
XCTAssertEqual(rebound.cadence, .minutes)
}
Expand Down

0 comments on commit d758f9b

Please sign in to comment.