refactor(mobile): extract image viewer actions to protocol
- Create ImageCarouselViewControllerProtocol to define image interaction methods - Move save, copy, and share image methods from ImageViewerController to protocol implementation - Remove setupOptionsButton method from ImageViewerController - Add options menu directly in ImageCarouselViewController - Enable page control user interaction - Simplify image action handling across image viewer components Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
+52
-9
@@ -60,7 +60,7 @@ public class ImageCarouselViewController: UIPageViewController,
|
||||
pageControl.translatesAutoresizingMaskIntoConstraints = false
|
||||
pageControl.currentPageIndicatorTintColor = .white
|
||||
pageControl.pageIndicatorTintColor = .gray
|
||||
pageControl.isUserInteractionEnabled = false
|
||||
pageControl.isUserInteractionEnabled = true
|
||||
return pageControl
|
||||
}()
|
||||
|
||||
@@ -114,12 +114,63 @@ public class ImageCarouselViewController: UIPageViewController,
|
||||
target: self,
|
||||
action: #selector(dismiss(_:)))
|
||||
|
||||
let saveAction = UIAction(
|
||||
title: "Save to Photos",
|
||||
image: UIImage(systemName: "square.and.arrow.down")
|
||||
) { [weak self] _ in
|
||||
self?.saveImageToPhotos()
|
||||
}
|
||||
|
||||
let copyAction = UIAction(
|
||||
title: "Copy",
|
||||
image: UIImage(systemName: "doc.on.doc")
|
||||
) { [weak self] _ in
|
||||
self?.copyImageToClipboard()
|
||||
}
|
||||
|
||||
let shareAction = UIAction(
|
||||
title: "Share",
|
||||
image: UIImage(systemName: "square.and.arrow.up")
|
||||
) { [weak self] _ in
|
||||
self?.shareImage()
|
||||
}
|
||||
|
||||
let menu = UIMenu(title: "", children: [saveAction, copyAction, shareAction])
|
||||
|
||||
let optionsButton = UIBarButtonItem(
|
||||
image: UIImage(systemName: "ellipsis.circle")?.withAlpha(0.9).withTintColor(
|
||||
.white, renderingMode: .alwaysOriginal
|
||||
),
|
||||
primaryAction: nil,
|
||||
menu: menu
|
||||
)
|
||||
|
||||
navItem.leftBarButtonItem = closeBarButton
|
||||
navItem.rightBarButtonItem = optionsButton
|
||||
navBar.alpha = 0.0
|
||||
navBar.items = [navItem]
|
||||
|
||||
navBar.insert(to: view)
|
||||
}
|
||||
|
||||
private func saveImageToPhotos() {
|
||||
if let vc = viewControllers?.first as? ImageCarouselViewControllerProtocol {
|
||||
vc.saveImageToPhotos()
|
||||
}
|
||||
|
||||
}
|
||||
private func copyImageToClipboard() {
|
||||
if let vc = viewControllers?.first as? ImageCarouselViewControllerProtocol {
|
||||
vc.copyImageToClipboard()
|
||||
}
|
||||
}
|
||||
private func shareImage() {
|
||||
if let vc = viewControllers?.first as? ImageCarouselViewControllerProtocol {
|
||||
vc.shareImage()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private func addBackgroundView() {
|
||||
guard let backgroundView = backgroundView else { return }
|
||||
view.addSubview(backgroundView)
|
||||
@@ -203,14 +254,6 @@ public class ImageCarouselViewController: UIPageViewController,
|
||||
onClosePreview?()
|
||||
}
|
||||
|
||||
@objc
|
||||
func diTapRightNavBarItem(_ sender: UIBarButtonItem) {
|
||||
guard let onTap = onRightNavBarTapped,
|
||||
let _firstVC = viewControllers?.first as? ImageViewerController
|
||||
else { return }
|
||||
onTap(_firstVC.index)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - UIPageViewControllerDelegate
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
//
|
||||
// ImageCarouselViewControllerProtocol.swift
|
||||
// FollowNative
|
||||
//
|
||||
// Created by Innei on 2025/3/12.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
protocol ImageCarouselViewControllerProtocol {
|
||||
func saveImageToPhotos()
|
||||
func copyImageToClipboard()
|
||||
func shareImage()
|
||||
|
||||
}
|
||||
@@ -124,7 +124,7 @@ class ImageViewerController: UIViewController,
|
||||
}
|
||||
|
||||
addGestureRecognizers()
|
||||
setupOptionsButton()
|
||||
|
||||
}
|
||||
|
||||
override func viewDidAppear(_ animated: Bool) {
|
||||
@@ -231,51 +231,6 @@ class ImageViewerController: UIViewController,
|
||||
zoomInOrOut(at: pointInView)
|
||||
}
|
||||
|
||||
private func setupOptionsButton() {
|
||||
if let parent = parent as? ImageCarouselViewController {
|
||||
let saveAction = UIAction(
|
||||
title: "Save to Photos",
|
||||
image: UIImage(systemName: "square.and.arrow.down")
|
||||
) { [weak self] _ in
|
||||
self?.saveImageToPhotos()
|
||||
}
|
||||
|
||||
let copyAction = UIAction(
|
||||
title: "Copy",
|
||||
image: UIImage(systemName: "doc.on.doc")
|
||||
) { [weak self] _ in
|
||||
self?.copyImageToClipboard()
|
||||
}
|
||||
|
||||
let shareAction = UIAction(
|
||||
title: "Share",
|
||||
image: UIImage(systemName: "square.and.arrow.up")
|
||||
) { [weak self] _ in
|
||||
self?.shareImage()
|
||||
}
|
||||
|
||||
let menu = UIMenu(title: "", children: [saveAction, copyAction, shareAction])
|
||||
|
||||
let optionsButton = UIBarButtonItem(
|
||||
image: UIImage(systemName: "ellipsis.circle")?.withAlpha(0.9).withTintColor(
|
||||
.white, renderingMode: .alwaysOriginal
|
||||
),
|
||||
primaryAction: nil,
|
||||
menu: menu
|
||||
)
|
||||
|
||||
parent.setRightNavItem(item: optionsButton)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private func saveImageToPhotos() {
|
||||
guard let image = imageView.image else { return }
|
||||
|
||||
UIImageWriteToSavedPhotosAlbum(
|
||||
image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
|
||||
}
|
||||
|
||||
@objc func image(
|
||||
_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer
|
||||
) {
|
||||
@@ -288,31 +243,6 @@ class ImageViewerController: UIViewController,
|
||||
}
|
||||
}
|
||||
|
||||
private func copyImageToClipboard() {
|
||||
guard let image = imageView.image else { return }
|
||||
|
||||
UIPasteboard.general.image = image
|
||||
SPIndicator.present(title: "Copied", preset: .done, haptic: .success)
|
||||
}
|
||||
|
||||
private func shareImage() {
|
||||
guard let image = imageView.image else { return }
|
||||
|
||||
let activityViewController = UIActivityViewController(
|
||||
activityItems: [image],
|
||||
applicationActivities: nil)
|
||||
|
||||
// For iPad support
|
||||
if let popoverController = activityViewController.popoverPresentationController {
|
||||
popoverController.sourceView = imageView
|
||||
popoverController.sourceRect = CGRect(
|
||||
x: imageView.bounds.midX, y: imageView.bounds.midY, width: 0, height: 0)
|
||||
popoverController.permittedArrowDirections = []
|
||||
}
|
||||
|
||||
present(activityViewController, animated: true)
|
||||
}
|
||||
|
||||
func gestureRecognizerShouldBegin(
|
||||
_ gestureRecognizer: UIGestureRecognizer
|
||||
) -> Bool {
|
||||
@@ -402,3 +332,39 @@ extension ImageViewerController: UIScrollViewDelegate {
|
||||
updateConstraintsForSize(view.bounds.size)
|
||||
}
|
||||
}
|
||||
|
||||
extension ImageViewerController: ImageCarouselViewControllerProtocol {
|
||||
|
||||
public func saveImageToPhotos() {
|
||||
guard let image = imageView.image else { return }
|
||||
|
||||
UIImageWriteToSavedPhotosAlbum(
|
||||
image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
|
||||
}
|
||||
|
||||
public func copyImageToClipboard() {
|
||||
guard let image = imageView.image else { return }
|
||||
|
||||
UIPasteboard.general.image = image
|
||||
SPIndicator.present(title: "Copied", preset: .done, haptic: .success)
|
||||
}
|
||||
|
||||
public func shareImage() {
|
||||
guard let image = imageView.image else { return }
|
||||
|
||||
let activityViewController = UIActivityViewController(
|
||||
activityItems: [image],
|
||||
applicationActivities: nil)
|
||||
|
||||
// For iPad support
|
||||
if let popoverController = activityViewController.popoverPresentationController {
|
||||
popoverController.sourceView = imageView
|
||||
popoverController.sourceRect = CGRect(
|
||||
x: imageView.bounds.midX, y: imageView.bounds.midY, width: 0, height: 0)
|
||||
popoverController.permittedArrowDirections = []
|
||||
}
|
||||
|
||||
present(activityViewController, animated: true)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user