Skip to content

Commit

Permalink
Merge pull request #126 from tumblr/editor-view-delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
bjtitus authored Jan 4, 2022
2 parents bbfad19 + aebf563 commit 7652d66
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Classes/Camera/CameraController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ open class CameraController: UIViewController, MediaClipsEditorDelegate, CameraP
}

private func createEditorViewController(_ segments: [CameraSegment], selected: Array<CameraSegment>.Index, edit: EditorViewController.Edit? = nil, drawing: IgnoreTouchesView? = nil) -> EditorViewController {
let controller = EditorViewController(settings: settings,
let controller = EditorViewController(delegate: self,
settings: settings,
segments: segments,
assetsHandler: segmentsHandler,
exporterClass: MediaExporter.self,
Expand All @@ -411,7 +412,6 @@ open class CameraController: UIViewController, MediaClipsEditorDelegate, CameraP
controller.editorView.movableViewCanvas.trashCompletion = { [weak self] in
self?.clipsController.removeDraggingClip()
}
controller.delegate = self
return controller
}

Expand Down
5 changes: 3 additions & 2 deletions Classes/Editor/EditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ final class EditorView: UIView, MovableViewCanvasDelegate, MediaPlayerViewDelega
return delegate.getBlogSwitcher()
}()

weak var delegate: EditorViewDelegate?
private weak var delegate: EditorViewDelegate?
private var mediaContentMode: UIView.ContentMode
private var aspectRatio: CGFloat?

Expand Down Expand Up @@ -273,6 +273,7 @@ final class EditorView: UIView, MovableViewCanvasDelegate, MediaPlayerViewDelega
self.movableViewCanvas = movableViewCanvas ?? MovableViewCanvas()
super.init(frame: .zero)
self.movableViewCanvas.delegate = self
setupViews()
}

func updateUI(forDraggingClip: Bool) {
Expand All @@ -287,7 +288,7 @@ final class EditorView: UIView, MovableViewCanvasDelegate, MediaPlayerViewDelega
})
}

func setupViews() {
private func setupViews() {
setupPlayer()
setupDrawingCanvas()
setupMovableViewCanvas()
Expand Down
43 changes: 28 additions & 15 deletions Classes/Editor/EditorViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public final class EditorViewController: UIViewController, MediaPlayerController
}
}

var editorView: EditorView
// Set immediately after the initializer super call to ensure it is never `nil`.
var editorView: EditorView!

var isMuted: Bool {
return player.isMuted
Expand Down Expand Up @@ -267,8 +268,10 @@ public final class EditorViewController: UIViewController, MediaPlayerController
public static func createEditor(for image: UIImage,
settings: CameraSettings,
stickerProvider: StickerProvider,
analyticsProvider: KanvasAnalyticsProvider) -> EditorViewController {
EditorViewController(settings: settings,
analyticsProvider: KanvasAnalyticsProvider,
delegate: EditorControllerDelegate) -> EditorViewController {
EditorViewController(delegate: delegate,
settings: settings,
segments: [.image(image, nil, nil, MediaInfo(source: .media_library))],
assetsHandler: CameraSegmentHandler(),
exporterClass: MediaExporter.self,
Expand All @@ -280,8 +283,12 @@ public final class EditorViewController: UIViewController, MediaPlayerController
tagCollection: nil)
}

public static func createEditor(for videoURL: URL, settings: CameraSettings, stickerProvider: StickerProvider) -> EditorViewController {
EditorViewController(settings: settings,
public static func createEditor(for videoURL: URL,
settings: CameraSettings,
stickerProvider: StickerProvider,
delegate: EditorControllerDelegate) -> EditorViewController {
EditorViewController(delegate: delegate,
settings: settings,
segments: [.video(videoURL, MediaInfo(source: .media_library))],
assetsHandler: CameraSegmentHandler(),
exporterClass: MediaExporter.self,
Expand All @@ -298,22 +305,26 @@ public final class EditorViewController: UIViewController, MediaPlayerController
settings: CameraSettings,
stickerProvider: StickerProvider,
analyticsProvider: KanvasAnalyticsProvider,
delegate: EditorControllerDelegate,
completion: @escaping (EditorViewController) -> Void) {
GIFDecoderFactory.main().decode(image: url) { frames in
let segments = CameraSegment.from(frames: frames, info: info)
let editor = EditorViewController(settings: settings,
let editor = EditorViewController(delegate: delegate,
settings: settings,
segments: segments,
stickerProvider: stickerProvider,
analyticsProvider: analyticsProvider)
completion(editor)
}
}

convenience init(settings: CameraSettings,
convenience init(delegate: EditorControllerDelegate,
settings: CameraSettings,
segments: [CameraSegment],
stickerProvider: StickerProvider,
analyticsProvider: KanvasAnalyticsProvider) {
self.init(settings: settings,
self.init(delegate: delegate,
settings: settings,
segments: segments,
assetsHandler: CameraSegmentHandler(),
exporterClass: MediaExporter.self,
Expand All @@ -334,7 +345,8 @@ public final class EditorViewController: UIViewController, MediaPlayerController
/// - cameraMode: The camera mode that the preview was coming from, if any
/// - stickerProvider: Class that will provide the stickers in the editor.
/// - analyticsProvider: A class conforming to KanvasAnalyticsProvider
init(settings: CameraSettings,
init(delegate: EditorControllerDelegate,
settings: CameraSettings,
segments: [CameraSegment],
assetsHandler: AssetsHandlerType,
exporterClass: MediaExporting.Type,
Expand All @@ -345,6 +357,7 @@ public final class EditorViewController: UIViewController, MediaPlayerController
quickBlogSelectorCoordinator: KanvasQuickBlogSelectorCoordinating?,
edit: Edit? = nil,
tagCollection: UIView?) {
self.delegate = delegate
self.settings = settings
self.originalSegments = segments
self.assetsHandler = assetsHandler
Expand All @@ -356,22 +369,24 @@ public final class EditorViewController: UIViewController, MediaPlayerController
self.quickBlogSelectorCoordinater = quickBlogSelectorCoordinator
self.tagCollection = tagCollection



let metalContext: MetalContext? = settings.features.metalPreview ? MetalContext.createContext() : nil
self.player = MediaPlayer(renderer: Renderer(settings: settings, metalContext: metalContext))
self.player.isMuted = edit?.isMuted == true
let muteButtonShown = settings.features.muteButton && segments.first?.isVideo == true
self.editorView = EditorViewController.editor(delegate: nil,

super.init(nibName: .none, bundle: .none)

self.editorView = EditorViewController.editor(delegate: self,
settings: settings,
showsMuteButton: muteButtonShown,
edit: edit,
quickBlogSelectorCoordinator: quickBlogSelectorCoordinator,
tagCollection: tagCollection,
metalContext: metalContext)
super.init(nibName: .none, bundle: .none)
self.editorView.delegate = self
self.editorView.muteButtonSelected = player.isMuted

editorView.delegate = self
player.playerView = editorView.playerView

self.player.delegate = self
Expand Down Expand Up @@ -407,8 +422,6 @@ public final class EditorViewController: UIViewController, MediaPlayerController
override public func viewDidLoad() {
super.viewDidLoad()

editorView.setupViews()

view.backgroundColor = .black
editorView.add(into: view)
drawingController.drawingLayer = editorView.drawingCanvas.layer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ final class EditorControllerTests: FBSnapshotTestCase {
let cameraSettings = settings ?? getCameraSettings()
let handler = assetsHandler ?? AssetsHandlerStub()
let analytics = analyticsProvider ?? KanvasAnalyticsStub()
let viewController = EditorViewController(settings: cameraSettings, segments: segments, assetsHandler: handler, exporterClass: MediaExporterStub.self, gifEncoderClass: GIFEncoderStub.self, cameraMode: cameraMode, stickerProvider: StickerProviderStub(), analyticsProvider: analytics, quickBlogSelectorCoordinator: nil, tagCollection: nil)
viewController.delegate = delegate ?? newDelegateStub()
let viewController = EditorViewController(delegate: delegate ?? newDelegateStub(), settings: cameraSettings, segments: segments, assetsHandler: handler, exporterClass: MediaExporterStub.self, gifEncoderClass: GIFEncoderStub.self, cameraMode: cameraMode, stickerProvider: StickerProviderStub(), analyticsProvider: analytics, quickBlogSelectorCoordinator: nil, tagCollection: nil)
viewController.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
return viewController
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ final class EditorViewTests: FBSnapshotTestCase {
mediaContentMode: .scaleAspectFit,
movableViewCanvas: nil)
view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
view.setupViews()
return view
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ final class MultiEditorControllerDelegateStub: MultiEditorComposerDelegate {
}

func editor(segment: CameraSegment, edit: EditorViewController.Edit?) -> EditorViewController {
return EditorViewController(settings: settings,
return EditorViewController(delegate: self,
settings: settings,
segments: [segment],
assetsHandler: assetsHandler,
exporterClass: exporterClass,
Expand Down

0 comments on commit 7652d66

Please sign in to comment.