Advertisement
TiyasAria

RoomControllerApple

Nov 17th, 2023
963
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.67 KB | None | 0 0
  1. /*
  2. See the LICENSE.txt file for this sample’s licensing information.
  3.  
  4. Abstract:
  5. The sample app's main view controller that manages the scanning process.
  6. */
  7.  
  8. import UIKit
  9. import RoomPlan
  10.  
  11. class RoomCaptureViewController: UIViewController, RoomCaptureViewDelegate, RoomCaptureSessionDelegate {
  12.    
  13.     @IBOutlet var exportButton: UIButton?
  14.    
  15.     @IBOutlet var doneButton: UIBarButtonItem?
  16.     @IBOutlet var cancelButton: UIBarButtonItem?
  17.     @IBOutlet var activityIndicator: UIActivityIndicatorView?
  18.    
  19.     private var isScanning: Bool = false
  20.    
  21.     private var roomCaptureView: RoomCaptureView!
  22.     private var roomCaptureSessionConfig: RoomCaptureSession.Configuration = RoomCaptureSession.Configuration()
  23.    
  24.     private var finalResults: CapturedRoom?
  25.    
  26.     override func viewDidLoad() {
  27.         super.viewDidLoad()
  28.        
  29.         // Set up after loading the view.
  30.         setupRoomCaptureView()
  31.         activityIndicator?.stopAnimating()
  32.     }
  33.    
  34.     private func setupRoomCaptureView() {
  35.         roomCaptureView = RoomCaptureView(frame: view.bounds)
  36.         roomCaptureView.captureSession.delegate = self
  37.         roomCaptureView.delegate = self
  38.        
  39.         view.insertSubview(roomCaptureView, at: 0)
  40.     }
  41.    
  42.     override func viewDidAppear(_ animated: Bool) {
  43.         super.viewDidAppear(animated)
  44.         startSession()
  45.     }
  46.    
  47.     override func viewWillDisappear(_ flag: Bool) {
  48.         super.viewWillDisappear(flag)
  49.         stopSession()
  50.     }
  51.    
  52.     private func startSession() {
  53.         isScanning = true
  54.         roomCaptureView?.captureSession.run(configuration: roomCaptureSessionConfig)
  55.        
  56.         setActiveNavBar()
  57.     }
  58.    
  59.     private func stopSession() {
  60.         isScanning = false
  61.         roomCaptureView?.captureSession.stop()
  62.        
  63.         setCompleteNavBar()
  64.     }
  65.    
  66.     // Decide to post-process and show the final results.
  67.     func captureView(shouldPresent roomDataForProcessing: CapturedRoomData, error: Error?) -> Bool {
  68.         return true
  69.     }
  70.    
  71.     // Access the final post-processed results.
  72.     func captureView(didPresent processedResult: CapturedRoom, error: Error?) {
  73.         finalResults = processedResult
  74.         self.exportButton?.isEnabled = true
  75.         self.activityIndicator?.stopAnimating()
  76.     }
  77.    
  78.     @IBAction func doneScanning(_ sender: UIBarButtonItem) {
  79.         if isScanning { stopSession() } else { cancelScanning(sender) }
  80.         self.exportButton?.isEnabled = false
  81.         self.activityIndicator?.startAnimating()
  82.     }
  83.  
  84.     @IBAction func cancelScanning(_ sender: UIBarButtonItem) {
  85.         navigationController?.dismiss(animated: true)
  86.     }
  87.    
  88.     // Export the USDZ output by specifying the `.parametric` export option.
  89.     // Alternatively, `.mesh` exports a nonparametric file and `.all`
  90.     // exports both in a single USDZ.
  91.     @IBAction func exportResults(_ sender: UIButton) {
  92.         let destinationFolderURL = FileManager.default.temporaryDirectory.appending(path: "Export")
  93.         let destinationURL = destinationFolderURL.appending(path: "RoomScan.usdz")
  94.         let capturedRoomURL = destinationFolderURL.appending(path: "Room.json")
  95.         do {
  96.             try FileManager.default.createDirectory(at: destinationFolderURL, withIntermediateDirectories: true)
  97.             let jsonEncoder = JSONEncoder()
  98.             let jsonData = try jsonEncoder.encode(finalResults)
  99.             try jsonData.write(to: capturedRoomURL)
  100.             try finalResults?.export(to: destinationURL, exportOptions: .parametric)
  101.            
  102.             let activityVC = UIActivityViewController(activityItems: [destinationFolderURL], applicationActivities: nil)
  103.             activityVC.modalPresentationStyle = .popover
  104.            
  105.             present(activityVC, animated: true, completion: nil)
  106.             if let popOver = activityVC.popoverPresentationController {
  107.                 popOver.sourceView = self.exportButton
  108.             }
  109.         } catch {
  110.             print("Error = \(error)")
  111.         }
  112.     }
  113.    
  114.     private func setActiveNavBar() {
  115.         UIView.animate(withDuration: 1.0, animations: {
  116.             self.cancelButton?.tintColor = .white
  117.             self.doneButton?.tintColor = .white
  118.             self.exportButton?.alpha = 0.0
  119.         }, completion: { complete in
  120.             self.exportButton?.isHidden = true
  121.         })
  122.     }
  123.    
  124.     private func setCompleteNavBar() {
  125.         self.exportButton?.isHidden = false
  126.         UIView.animate(withDuration: 1.0) {
  127.             self.cancelButton?.tintColor = .systemBlue
  128.             self.doneButton?.tintColor = .systemBlue
  129.             self.exportButton?.alpha = 1.0
  130.         }
  131.     }
  132. }
  133.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement