Fix the support on watchOS. Using the Platform Image class instead of hard-coded `SDAnimatedImage` class

This commit is contained in:
DreamPiggy 2019-10-01 15:01:22 +08:00
parent 08979446b2
commit 970938922a
3 changed files with 23 additions and 14 deletions

View File

@ -13,7 +13,7 @@ import SDWebImage
// Data Binding Object // Data Binding Object
final class AnimatedImageModel : ObservableObject { final class AnimatedImageModel : ObservableObject {
@Published var image: SDAnimatedImage? @Published var image: PlatformImage?
@Published var url: URL? @Published var url: URL?
} }
@ -146,7 +146,7 @@ public struct AnimatedImage : ViewRepresentable {
#endif #endif
} }
public func image(_ image: SDAnimatedImage?) -> Self { public func image(_ image: PlatformImage?) -> Self {
imageModel.image = image imageModel.image = image
return self return self
} }
@ -202,7 +202,7 @@ public struct AnimatedImage : ViewRepresentable {
} }
extension AnimatedImage { extension AnimatedImage {
public init(url: URL?, placeholder: SDAnimatedImage? = nil, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) { public init(url: URL?, placeholder: PlatformImage? = nil, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) {
self.webOptions = options self.webOptions = options
self.webContext = context self.webContext = context
self.imageModel.url = url self.imageModel.url = url

View File

@ -9,6 +9,8 @@
import Foundation import Foundation
import SDWebImage import SDWebImage
#if !os(watchOS)
// View Wrapper // View Wrapper
public class AnimatedImageViewWrapper : PlatformView { public class AnimatedImageViewWrapper : PlatformView {
var wrapped = SDAnimatedImageView() var wrapped = SDAnimatedImageView()
@ -59,3 +61,5 @@ extension PlatformView {
} }
} }
#endif

View File

@ -10,15 +10,9 @@ import Foundation
import SwiftUI import SwiftUI
#if os(macOS) #if os(macOS)
typealias PlatformImage = NSImage public typealias PlatformImage = NSImage
#else #else
typealias PlatformImage = UIImage public typealias PlatformImage = UIImage
#endif
#if os(macOS)
public typealias PlatformView = NSView
#else
public typealias PlatformView = UIView
#endif #endif
extension Image { extension Image {
@ -31,14 +25,25 @@ extension Image {
} }
} }
#if !os(watchOS) #if os(macOS)
public typealias PlatformView = NSView
#endif
#if os(iOS) || os(tvOS)
public typealias PlatformView = UIView
#endif
#if os(watchOS)
public typealias PlatformView = WKInterfaceObject
#endif
#if os(macOS) #if os(macOS)
typealias ViewRepresentable = NSViewRepresentable typealias ViewRepresentable = NSViewRepresentable
typealias ViewRepresentableContext = NSViewRepresentableContext typealias ViewRepresentableContext = NSViewRepresentableContext
#else #endif
#if os(iOS) || os(tvOS)
typealias ViewRepresentable = UIViewRepresentable typealias ViewRepresentable = UIViewRepresentable
typealias ViewRepresentableContext = UIViewRepresentableContext typealias ViewRepresentableContext = UIViewRepresentableContext
#endif #endif
#if os(watchOS)
typealias ViewRepresentable = WKInterfaceObjectRepresentable
typealias ViewRepresentableContext = WKInterfaceObjectRepresentableContext
#endif #endif