Skip to content

Commit

Permalink
Xcode 14 compatibility (#39)
Browse files Browse the repository at this point in the history
* Xcode 14 compatibility

* Fix tests

* Apply suggestions from code review

Co-authored-by: Daniil Subbotin <[email protected]>

Co-authored-by: Anna Kocheshkova <[email protected]>
Co-authored-by: Daniil Subbotin <[email protected]>
  • Loading branch information
3 people authored Jul 23, 2022
1 parent 6b89272 commit 51967db
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions Sources/ApexyLoader/ContentLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,32 @@ open class ContentLoader<Content>: ObservableLoader {
}
}

// Can not use `@available` with lazy properties in Xcode 14. This is a workaround.
// https://stackoverflow.com/a/55534141/7453375
private var storedStateSubject: Any?
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, *)
private lazy var stateSubject = CurrentValueSubject<LoadingState<Content>, Never>(.initial)
private var stateSubject: CurrentValueSubject<LoadingState<Content>, Never> {
if let subject = storedStateSubject as? CurrentValueSubject<LoadingState<Content>, Never> {
return subject
}
let subject = CurrentValueSubject<LoadingState<Content>, Never>(.initial)
storedStateSubject = subject
return subject
}

/// Content loading status. The default value is `.initial`.
///
/// - Remark: To change state use `update(_:)`.
private var storedStatePublisher: Any?
@available(iOS 13.0, macOS 10.15, watchOS 6.0, tvOS 13.0, *)
public lazy var statePublisher: AnyPublisher<LoadingState<Content>, Never> = stateSubject.eraseToAnyPublisher()
public var statePublisher: AnyPublisher<LoadingState<Content>, Never> {
if let publisher = storedStatePublisher as? AnyPublisher<LoadingState<Content>, Never> {
return publisher
}
let publisher = stateSubject.eraseToAnyPublisher()
storedStatePublisher = publisher
return publisher
}

public init() {}

Expand Down

0 comments on commit 51967db

Please sign in to comment.