44//
55// Created by Giridhar on 21/06/17.
66// Copyright © 2017 Giridhar. All rights reserved.
7- //
7+ // Modified By: [
8+ // Matt Thompson 9/2018
9+ //]
810
911import Foundation
12+ import AVKit
1013
1114@objc class ScreenRecordCoordinator : NSObject
1215{
1316 let viewOverlay = WindowUtil ( )
1417 let screenRecorder = ScreenRecorder ( )
1518 var recordCompleted : ( ( Error ? ) -> Void ) ?
19+ let previewDelegateView = PreviewDelegateView ( )
1620
1721 override init ( )
1822 {
@@ -41,6 +45,19 @@ import Foundation
4145 self . recordCompleted ? ( error)
4246 }
4347 }
48+
49+ func previewRecording ( withFileName fileURL: String ) {
50+ if UIVideoEditorController . canEditVideo ( atPath: fileURL) {
51+ previewDelegateView. setCoordinator ( coordinator: self )
52+ let rootView = UIApplication . getTopMostViewController ( )
53+ let editController = UIVideoEditorController ( )
54+ editController. videoPath = fileURL
55+ editController. delegate = previewDelegateView
56+ rootView? . present ( editController, animated: true , completion: nil )
57+ } else {
58+ // handle error with onPreviewError config or something that is one of the init config options
59+ }
60+ }
4461
4562 func listAllReplays( ) -> Array < String >
4663 {
@@ -49,3 +66,68 @@ import Foundation
4966
5067
5168}
69+
70+ class PreviewDelegateView : UIViewController , UINavigationControllerDelegate , UIVideoEditorControllerDelegate {
71+
72+ var coordinator : ScreenRecordCoordinator !
73+ var isSaved : Bool = false
74+
75+ func setCoordinator( coordinator: ScreenRecordCoordinator ) -> Void {
76+ self . coordinator = coordinator
77+ }
78+
79+ func videoEditorController( _ editor: UIVideoEditorController , didSaveEditedVideoToPath editedVideoPath: String ) {
80+ print ( " save called " )
81+ if ( !self . isSaved) {
82+ self . isSaved = true
83+ print ( " trimmed video saved! " )
84+ editor. dismiss ( animated: true , completion: {
85+ ReplayFileUtil . replaceItem ( at: URL ( fileURLWithPath: editor. videoPath) , with: URL ( fileURLWithPath: editedVideoPath) )
86+ self . isSaved = false
87+ } )
88+ }
89+ }
90+ }
91+
92+ extension UIApplication {
93+ class func getTopMostViewController( base: UIViewController ? = UIApplication . shared. keyWindow? . rootViewController) -> UIViewController ? {
94+ if let nav = base as? UINavigationController {
95+ return getTopMostViewController ( base: nav. visibleViewController)
96+ }
97+ if let tab = base as? UITabBarController {
98+ if let selected = tab. selectedViewController {
99+ return getTopMostViewController ( base: selected)
100+ }
101+ }
102+ if let presented = base? . presentedViewController {
103+ return getTopMostViewController ( base: presented)
104+ }
105+ return base
106+ }
107+ }
108+
109+
110+ //extension FileManager {
111+ // func replaceWithCopyOfFile(at:URL, with:URL) {
112+ // do {
113+ // let url = try self.url(for: .itemReplacementDirectory, in: .userDomainMask, appropriateFor: with.deletingPathExtension(), create: true)
114+ // try self.copyItem(at: with, to: url.appendingPathComponent(with.lastPathComponent))
115+ //
116+ // let alert = NSAlert()
117+ // alert.messageText = "Replace \"\(at.lastPathComponent)\" in \"\(at.pathComponents[at.pathComponents.count - 2])\" with new file?"
118+ // alert.addButton(withTitle: "OK")
119+ // alert.addButton(withTitle: "Cancel")
120+ //
121+ // if alert.runModal() == NSAlertFirstButtonReturn {
122+ // _ = try FileManager.default.replaceItemAt(at, withItemAt: url.appendingPathComponent(with.lastPathComponent))
123+ // }
124+ //
125+ // // removes whole temporary directory as a clean up
126+ // try self.removeItem(at: url)
127+ // }
128+ // catch {
129+ // // error
130+ // print("unknown error")
131+ // }
132+ // }
133+ //}
0 commit comments