From b09045603984c632e58b2a9f0b81ccefef2c1964 Mon Sep 17 00:00:00 2001 From: Valentin Date: Mon, 6 May 2024 13:50:36 +0500 Subject: [PATCH] minor changes --- ...gurationMaker+MPGeneralConfiguration.swift | 18 +++--- ...ConfigurationMaker+MPUIConfiguration.swift | 14 ++--- Sources/General/MPConfigurationMaker.swift | 36 +++++------ Sources/General/MPPresenter.swift | 4 +- Sources/General/UILayer/MPCameraCell.swift | 6 +- .../General/UILayer/MPViewController.swift | 31 ---------- .../UILayer/MediaViewer/MPAnimator.swift | 60 +++++++++++-------- .../MediaViewerViewController.swift | 4 +- 8 files changed, 77 insertions(+), 96 deletions(-) diff --git a/Sources/Extensions/MPConfigurationMaker+MPGeneralConfiguration.swift b/Sources/Extensions/MPConfigurationMaker+MPGeneralConfiguration.swift index 37e075a..7e961d7 100644 --- a/Sources/Extensions/MPConfigurationMaker+MPGeneralConfiguration.swift +++ b/Sources/Extensions/MPConfigurationMaker+MPGeneralConfiguration.swift @@ -26,55 +26,55 @@ import UIKit extension MPConfigurationMaker { @discardableResult - public func setAllowImage(_ value: Bool) -> MPConfigurationMakerExtendable { + public func setAllowImage(_ value: Bool) -> Self { generalConfig.allowImage = value return self } @discardableResult - public func setAllowGif(_ value: Bool) -> MPConfigurationMakerExtendable { + public func setAllowGif(_ value: Bool) -> Self { generalConfig.allowGif = value return self } @discardableResult - public func setAllowVideo(_ value: Bool) -> MPConfigurationMakerExtendable { + public func setAllowVideo(_ value: Bool) -> Self { generalConfig.allowVideo = value return self } @discardableResult - public func setAllowLivePhoto(_ value: Bool) -> MPConfigurationMakerExtendable { + public func setAllowLivePhoto(_ value: Bool) -> Self { generalConfig.allowLivePhoto = value return self } @discardableResult - public func setMaxMediaSelectCount(_ value: Int) -> MPConfigurationMakerExtendable { + public func setMaxMediaSelectCount(_ value: Int) -> Self { generalConfig.maxMediaSelectCount = value return self } @discardableResult - public func setBundleLangsDeploy(_ value: Bundle) -> MPConfigurationMakerExtendable { + public func setBundleLangsDeploy(_ value: Bundle) -> Self { generalConfig.bundleLangsDeploy = value return self } @discardableResult - public func setKeysLangsDeploy(_ value: [String: String]) -> MPConfigurationMakerExtendable { + public func setKeysLangsDeploy(_ value: [String: String]) -> Self { generalConfig.keysLangsDeploy = value return self } @discardableResult - public func setStringCatalogType(_ value: MPGeneralConfiguration.StringCatalogType) -> MPConfigurationMakerExtendable { + public func setStringCatalogType(_ value: MPGeneralConfiguration.StringCatalogType) -> Self { generalConfig.stringCatalogType = value return self } @discardableResult - public func setGeneralConfiguration(_ value: MPGeneralConfiguration) -> MPConfigurationMakerExtendable { + public func setGeneralConfiguration(_ value: MPGeneralConfiguration) -> Self { generalConfig = value return self } diff --git a/Sources/Extensions/MPConfigurationMaker+MPUIConfiguration.swift b/Sources/Extensions/MPConfigurationMaker+MPUIConfiguration.swift index 3559b2e..760703f 100644 --- a/Sources/Extensions/MPConfigurationMaker+MPUIConfiguration.swift +++ b/Sources/Extensions/MPConfigurationMaker+MPUIConfiguration.swift @@ -26,43 +26,43 @@ import UIKit extension MPConfigurationMaker { @discardableResult - public func setShowCounterOnSelectionButton(_ value: Bool) -> MPConfigurationMakerExtendable { + public func setShowCounterOnSelectionButton(_ value: Bool) -> Self { uiConfig.showCounterOnSelectionButton = value return self } @discardableResult - public func setSelectionButtonCornersStyle(_ value: MPCheckboxStyle) -> MPConfigurationMakerExtendable { + public func setSelectionButtonCornersStyle(_ value: MPCheckboxStyle) -> Self { uiConfig.selectionButtonCornersStyle = value return self } @discardableResult - public func setSelectionButtonColorStyle(_ value: MPCheckboxColor) -> MPConfigurationMakerExtendable { + public func setSelectionButtonColorStyle(_ value: MPCheckboxColor) -> Self { uiConfig.selectionButtonColorStyle = value return self } @discardableResult - public func setNavigationAppearance(_ value: MPNavigationAppearance) -> MPConfigurationMakerExtendable { + public func setNavigationAppearance(_ value: MPNavigationAppearance) -> Self { uiConfig.navigationAppearance = value return self } @discardableResult - public func setPrimaryBackgroundColor(_ value: UIColor) -> MPConfigurationMakerExtendable { + public func setPrimaryBackgroundColor(_ value: UIColor) -> Self { uiConfig.primaryBackgroundColor = value return self } @discardableResult - public func setShowCameraCell(_ value: Bool) -> MPConfigurationMakerExtendable { + public func setShowCameraCell(_ value: Bool) -> Self { uiConfig.showCameraCell = value return self } @discardableResult - public func setUIConfiguration(_ value: MPUIConfiguration) -> MPConfigurationMakerExtendable { + public func setUIConfiguration(_ value: MPUIConfiguration) -> Self { uiConfig = value return self } diff --git a/Sources/General/MPConfigurationMaker.swift b/Sources/General/MPConfigurationMaker.swift index ab15f9c..40c7a99 100644 --- a/Sources/General/MPConfigurationMaker.swift +++ b/Sources/General/MPConfigurationMaker.swift @@ -25,35 +25,35 @@ import UIKit public protocol MPUIConfigMakerExtendable: NSObjectProtocol { - func setShowCounterOnSelectionButton(_ value: Bool) -> MPConfigurationMakerExtendable - func setSelectionButtonCornersStyle(_ value: MPCheckboxStyle) -> MPConfigurationMakerExtendable - func setSelectionButtonColorStyle(_ value: MPCheckboxColor) -> MPConfigurationMakerExtendable - func setNavigationAppearance(_ value: MPNavigationAppearance) -> MPConfigurationMakerExtendable - func setPrimaryBackgroundColor(_ value: UIColor) -> MPConfigurationMakerExtendable - func setShowCameraCell(_ value: Bool) -> MPConfigurationMakerExtendable - func setUIConfiguration(_ value: MPUIConfiguration) -> MPConfigurationMakerExtendable + func setShowCounterOnSelectionButton(_ value: Bool) -> Self + func setSelectionButtonCornersStyle(_ value: MPCheckboxStyle) -> Self + func setSelectionButtonColorStyle(_ value: MPCheckboxColor) -> Self + func setNavigationAppearance(_ value: MPNavigationAppearance) -> Self + func setPrimaryBackgroundColor(_ value: UIColor) -> Self + func setShowCameraCell(_ value: Bool) -> Self + func setUIConfiguration(_ value: MPUIConfiguration) -> Self } public protocol MPGeneralConfigMakerExtendable: NSObjectProtocol { - func setAllowImage(_ value: Bool) -> MPConfigurationMakerExtendable - func setAllowGif(_ value: Bool) -> MPConfigurationMakerExtendable - func setAllowVideo(_ value: Bool) -> MPConfigurationMakerExtendable - func setAllowLivePhoto(_ value: Bool) -> MPConfigurationMakerExtendable - func setMaxMediaSelectCount(_ value: Int) -> MPConfigurationMakerExtendable - func setBundleLangsDeploy(_ value: Bundle) -> MPConfigurationMakerExtendable - func setKeysLangsDeploy(_ value: [String: String]) -> MPConfigurationMakerExtendable - func setStringCatalogType(_ value: MPGeneralConfiguration.StringCatalogType) -> MPConfigurationMakerExtendable - func setGeneralConfiguration(_ value: MPGeneralConfiguration) -> MPConfigurationMakerExtendable + func setAllowImage(_ value: Bool) -> Self + func setAllowGif(_ value: Bool) -> Self + func setAllowVideo(_ value: Bool) -> Self + func setAllowLivePhoto(_ value: Bool) -> Self + func setMaxMediaSelectCount(_ value: Int) -> Self + func setBundleLangsDeploy(_ value: Bundle) -> Self + func setKeysLangsDeploy(_ value: [String: String]) -> Self + func setStringCatalogType(_ value: MPGeneralConfiguration.StringCatalogType) -> Self + func setGeneralConfiguration(_ value: MPGeneralConfiguration) -> Self } public protocol MPConfigurationMakerExtendable: MPUIConfigMakerExtendable, MPGeneralConfigMakerExtendable { var uiConfig: MPUIConfiguration { get } var generalConfig: MPGeneralConfiguration { get } - static var `default`: MPConfigurationMakerExtendable { get } + static var `default`: Self { get } } public final class MPConfigurationMaker: NSObject, MPConfigurationMakerExtendable { - public static var `default`: MPConfigurationMakerExtendable { + public static var `default`: MPConfigurationMaker { self.init() } diff --git a/Sources/General/MPPresenter.swift b/Sources/General/MPPresenter.swift index 8232ade..386fa48 100644 --- a/Sources/General/MPPresenter.swift +++ b/Sources/General/MPPresenter.swift @@ -30,7 +30,7 @@ public final class MPPresenter: NSObject { private var preSelectedResults: [MPPhotoModel] private var selectedResult: (([MPResultModel]) -> ())? = nil private var preSelectedResult: (([MPPhotoModel]) -> ())? = nil - private var config: MPConfigurationMakerExtendable = MPConfigurationMaker() + private var config = MPConfigurationMaker() //private var uiConfig: MPUIConfiguration = .default() //private var generalConfig: MPGeneralConfiguration = .default() @@ -52,7 +52,7 @@ public final class MPPresenter: NSObject { } public func showMediaPicker( - configuration: ((MPConfigurationMakerExtendable) -> (MPConfigurationMakerExtendable))? = nil, + configuration: ((MPConfigurationMaker) -> (MPConfigurationMaker))? = nil, selectedResult: @escaping ([MPResultModel]) -> (), customPresentationStyle: ((UINavigationController) -> ())? = nil ) { diff --git a/Sources/General/UILayer/MPCameraCell.swift b/Sources/General/UILayer/MPCameraCell.swift index f9c9f82..c864d2a 100644 --- a/Sources/General/UILayer/MPCameraCell.swift +++ b/Sources/General/UILayer/MPCameraCell.swift @@ -49,10 +49,10 @@ final class MPCameraCell: CollectionViewCell { private var photoOutput: AVCapturePhotoOutput? - private var previewLayer: AVCaptureVideoPreviewLayer? - private var videoOutput: AVCaptureVideoDataOutput? + private var previewLayer: AVCaptureVideoPreviewLayer? + var processSampleBuffer: ((CMSampleBuffer, CVImageBuffer, AVCaptureConnection) -> Void)? var isEnable = true { @@ -148,7 +148,7 @@ final class MPCameraCell: CollectionViewCell { previewLayer?.videoGravity = .resizeAspectFill contentView.layer.insertSublayer(previewLayer!, at: 0) - DispatchQueue.mp.background(qos: .userInitiated, background: { [weak self] in + DispatchQueue.mp.background(qos: .background, background: { [weak self] in self?.session?.startRunning() }, completion: { [weak self] in self?.backgroundThumgnail.mp.setIsHidden(true, duration: 0.3) diff --git a/Sources/General/UILayer/MPViewController.swift b/Sources/General/UILayer/MPViewController.swift index 758fc5d..4783519 100644 --- a/Sources/General/UILayer/MPViewController.swift +++ b/Sources/General/UILayer/MPViewController.swift @@ -94,8 +94,6 @@ final class MPViewController: UIViewController { private var wasCreateSnapshot: Bool = false - private var didAttemptToDismissWasCall = false - private var allowDragAndDrop = false private var lastContentOffset: CGPoint = .zero private var lastViewHeight: CGFloat = .zero @@ -1088,18 +1086,6 @@ extension MPViewController: UICollectionViewDropDelegate { previewParameters.backgroundColor = .clear return previewParameters } - - func collectionView(_ collectionView: UICollectionView, dropSessionDidEnd session: any UIDropSession) { - debugPrint("collectionView dropSessionDidEnd") - } - - func collectionView(_ collectionView: UICollectionView, dropSessionDidExit session: any UIDropSession) { - debugPrint("collectionView dropSessionDidExit") - } - - func collectionView(_ collectionView: UICollectionView, dropSessionDidEnter session: any UIDropSession) { - debugPrint("collectionView dropSessionDidEnter") - } } // MARK: Photo library change observer @@ -1208,28 +1194,11 @@ extension MPViewController: MediaPreviewControllerDelegate { // MARK: - UISheetPresentationControllerDelegate extension MPViewController: UISheetPresentationControllerDelegate { - func sheetPresentationControllerDidChangeSelectedDetentIdentifier(_ sheetPresentationController: UISheetPresentationController) { - debugPrint("sheetPresentationController view frame \(view.frame)") - debugPrint("sheetPresentationController view bounds \(view.bounds)") - } - func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool { selectedModels.isEmpty - //if selectedModels.isEmpty { - // return true - //} else { - // // scheduledTimer is required to handle cases of attempting to close the sheet with tap outside - // didAttemptToDismissWasCall = false - // Timer.scheduledTimer(withTimeInterval: 0.3, repeats: false, block: { [weak self] (_) in - // guard let strongSelf = self, !strongSelf.didAttemptToDismissWasCall else { return } - // strongSelf.dismissAlert(needDismiss: false) - // }) - // return false - //} } func presentationControllerDidAttemptToDismiss(_ presentationController: UIPresentationController) { - //didAttemptToDismissWasCall = true dismissAlert(needDismiss: false) } } diff --git a/Sources/General/UILayer/MediaViewer/MPAnimator.swift b/Sources/General/UILayer/MediaViewer/MPAnimator.swift index 0907b53..d749224 100644 --- a/Sources/General/UILayer/MediaViewer/MPAnimator.swift +++ b/Sources/General/UILayer/MediaViewer/MPAnimator.swift @@ -31,6 +31,18 @@ protocol MPViewerBaseAnimator: UIViewControllerAnimatedTransitioning { var dismissingDuration: TimeInterval { get set } } +protocol MPViewControllerAnimatable: AnyObject { + var imageView: MPImageView { get } + var referencedView: UIView? { get set } + + func presentingAnimation() + func presentationAnimationDidFinish() + + func dismissingAnimation() + func dismissalAnimationDidFinish() + +} + final class MPAnimator: NSObject, MPViewerBaseAnimator { /// Preseting transition duration @@ -59,7 +71,7 @@ final class MPAnimator: NSObject, MPViewerBaseAnimator { let animator: UIViewPropertyAnimator if isPresenting { - guard let photoViewerController = toViewController.mp.as(MediaViewerViewController.self) else { + guard let animatableVC = toViewController as? MPViewControllerAnimatable else { fatalError("view controller does not conform DTPhotoViewer") } @@ -68,16 +80,16 @@ final class MPAnimator: NSObject, MPViewerBaseAnimator { containerView.addSubview(toView) - if let referencedView = photoViewerController.referencedView { - photoViewerController.imageView.layer.cornerRadius = referencedView.layer.cornerRadius - photoViewerController.imageView.layer.masksToBounds = referencedView.layer.masksToBounds - photoViewerController.imageView.backgroundColor = referencedView.backgroundColor + if let referencedView = animatableVC.referencedView { + animatableVC.imageView.layer.cornerRadius = referencedView.layer.cornerRadius + animatableVC.imageView.layer.masksToBounds = referencedView.layer.masksToBounds + animatableVC.imageView.backgroundColor = referencedView.backgroundColor } let animation = { - photoViewerController.presentingAnimation() - photoViewerController.imageView.mp.setRadius(0) - photoViewerController.imageView.backgroundColor = .clear + animatableVC.presentingAnimation() + animatableVC.imageView.mp.setRadius(0) + animatableVC.imageView.backgroundColor = .clear } animator = UIViewPropertyAnimator(duration: duration, dampingRatio: dampingRatio, animations: animation) @@ -87,7 +99,7 @@ final class MPAnimator: NSObject, MPViewerBaseAnimator { transitionContext.completeTransition(!isCancelled) if !isCancelled { - photoViewerController.presentationAnimationDidFinish() + animatableVC.presentationAnimationDidFinish() } // View controller appearance status @@ -96,7 +108,7 @@ final class MPAnimator: NSObject, MPViewerBaseAnimator { } // Layer animation - if let referencedView = photoViewerController.referencedView { + if let referencedView = animatableVC.referencedView { let animationGroup = CAAnimationGroup() animationGroup.timingFunction = CAMediaTimingFunction(name: .easeIn) animationGroup.duration = presentingDuration @@ -106,30 +118,30 @@ final class MPAnimator: NSObject, MPViewerBaseAnimator { let borderColorAnimation = CABasicAnimation(keyPath: "borderColor") borderColorAnimation.fromValue = referencedView.layer.borderColor borderColorAnimation.toValue = UIColor.clear.cgColor - photoViewerController.imageView.layer.borderColor = UIColor.clear.cgColor + animatableVC.imageView.layer.borderColor = UIColor.clear.cgColor // Border width let borderWidthAnimation = CABasicAnimation(keyPath: "borderWidth") borderWidthAnimation.fromValue = referencedView.layer.borderWidth borderWidthAnimation.toValue = 0 - photoViewerController.imageView.layer.borderWidth = referencedView.layer.borderWidth + animatableVC.imageView.layer.borderWidth = referencedView.layer.borderWidth animationGroup.animations = [borderColorAnimation, borderWidthAnimation] - photoViewerController.imageView.layer.add(animationGroup, forKey: nil) + animatableVC.imageView.layer.add(animationGroup, forKey: nil) } } else { - guard let photoViewerController = fromViewController.mp.as(MediaViewerViewController.self) else { + guard let animatableVC = fromViewController as? MPViewControllerAnimatable else { fatalError("view controller does not conform DTPhotoViewer") } - photoViewerController.imageView.backgroundColor = .clear + animatableVC.imageView.backgroundColor = .clear let animation = { - photoViewerController.dismissingAnimation() + animatableVC.dismissingAnimation() - if let referencedView = photoViewerController.referencedView { - photoViewerController.imageView.layer.cornerRadius = referencedView.layer.cornerRadius - photoViewerController.imageView.backgroundColor = referencedView.backgroundColor + if let referencedView = animatableVC.referencedView { + animatableVC.imageView.layer.cornerRadius = referencedView.layer.cornerRadius + animatableVC.imageView.backgroundColor = referencedView.backgroundColor } } @@ -140,7 +152,7 @@ final class MPAnimator: NSObject, MPViewerBaseAnimator { transitionContext.completeTransition(!isCancelled) if !isCancelled { - photoViewerController.dismissalAnimationDidFinish() + animatableVC.dismissalAnimationDidFinish() } // View controller appearance status @@ -149,7 +161,7 @@ final class MPAnimator: NSObject, MPViewerBaseAnimator { } // Layer animation - if let referencedView = photoViewerController.referencedView { + if let referencedView = animatableVC.referencedView { let animationGroup = CAAnimationGroup() animationGroup.timingFunction = CAMediaTimingFunction(name: .easeIn) animationGroup.duration = presentingDuration @@ -159,16 +171,16 @@ final class MPAnimator: NSObject, MPViewerBaseAnimator { let borderColorAnimation = CABasicAnimation(keyPath: "borderColor") borderColorAnimation.fromValue = UIColor.clear.cgColor borderColorAnimation.toValue = referencedView.layer.borderColor - photoViewerController.imageView.layer.borderColor = referencedView.layer.borderColor + animatableVC.imageView.layer.borderColor = referencedView.layer.borderColor // Border width let borderWidthAnimation = CABasicAnimation(keyPath: "borderWidth") borderWidthAnimation.fromValue = 0 borderWidthAnimation.toValue = referencedView.layer.borderWidth - photoViewerController.imageView.layer.borderWidth = referencedView.layer.borderWidth + animatableVC.imageView.layer.borderWidth = referencedView.layer.borderWidth animationGroup.animations = [borderColorAnimation, borderWidthAnimation] - photoViewerController.imageView.layer.add(animationGroup, forKey: nil) + animatableVC.imageView.layer.add(animationGroup, forKey: nil) } } diff --git a/Sources/General/UILayer/MediaViewer/MediaViewerViewController.swift b/Sources/General/UILayer/MediaViewer/MediaViewerViewController.swift index d0326a5..2007c57 100644 --- a/Sources/General/UILayer/MediaViewer/MediaViewerViewController.swift +++ b/Sources/General/UILayer/MediaViewer/MediaViewerViewController.swift @@ -44,7 +44,7 @@ extension MediaPreviewControllerDelegate { func toggleSelected(forModel model: MPPhotoModel) {} } -final class MediaViewerViewController: UIViewController { +final class MediaViewerViewController: UIViewController, MPViewControllerAnimatable { /// Indicates status bar animation style when changing hidden status /// Default value if UIStatusBarStyle.fade var statusBarAnimationStyle: UIStatusBarAnimation = .fade @@ -70,7 +70,7 @@ final class MediaViewerViewController: UIViewController { /// The view where photo viewer originally animates from. /// Provide this correctly so that you can have a nice effect. - weak private(set) var referencedView: UIView? { + weak var referencedView: UIView? { didSet { // Unhide old referenced view and hide the new one if oldValue !== referencedView {