ejeinc / MetalScope

Metal-backed 360° panorama view for iOS
MIT License
324 stars 72 forks source link

Extract default implementations for `Loadable` protocols #19

Closed junpluse closed 7 years ago

junpluse commented 7 years ago

Extract the load methods (originally implemented as a protocol extension for [Image|Video]Loadable) as new [Image|Video]SceneLoader structs.

// old
extension ImageLoadable where Self: SceneLoadable {
    public func load(_ image: UIImage, format: MediaFormat) {
        // create scene...

        self.scene = scene
    }
}

// new
extension ImageLoadable where Self: SceneLoadable {
    public func load(_ image: UIImage, format: MediaFormat) {
        ImageSceneLoader(target: self).load(image, format: format)
    }
}

public struct ImageSceneLoader<Target: SceneLoadable>: ImageLoadable {
    public let target: Target

    public init(target: Target) {
        self.target = target
    }

    public func load(_ image: UIImage, format: MediaFormat) {
        // create scene...

        target.scene = scene
    }
}

So now you can reuse the default implementation when you override them.

class MyViewController: SceneLoadable, ImageLoadable {
    var scene: SCNScene?

    func load(_ image: UIImage, format: MediaFormat) {
        ImageSceneLoader(target: self).load(image, format: format)

        // your custom logic...
    }
}