feat: add copy photo action for status

This commit is contained in:
CMK 2021-07-06 19:53:47 +08:00
parent 7a0ceb8fc4
commit 66de0593df
7 changed files with 87 additions and 40 deletions

View File

@ -70,15 +70,16 @@
"cancel": "Cancel", "cancel": "Cancel",
"discard": "Discard", "discard": "Discard",
"try_again": "Try Again", "try_again": "Try Again",
"take_photo": "Take photo", "take_photo": "Take Photo",
"save_photo": "Save photo", "save_photo": "Save Photo",
"copy_photo": "Copy Photo",
"sign_in": "Sign In", "sign_in": "Sign In",
"sign_up": "Sign Up", "sign_up": "Sign Up",
"see_more": "See More", "see_more": "See More",
"preview": "Preview", "preview": "Preview",
"share": "Share", "share": "Share",
"share_user": "Share %s", "share_user": "Share %s",
"share_post": "Share post", "share_post": "Share Post",
"open_in_safari": "Open in Safari", "open_in_safari": "Open in Safari",
"find_people": "Find people to follow", "find_people": "Find people to follow",
"manually_search": "Manually search instead", "manually_search": "Manually search instead",

View File

@ -1005,7 +1005,8 @@ extension StatusSection {
private static func setupStatusMoreButtonMenu( private static func setupStatusMoreButtonMenu(
cell: StatusTableViewCell, cell: StatusTableViewCell,
dependency: NeedsDependency, dependency: NeedsDependency,
status: Status) { status: Status
) {
guard let userProvider = dependency as? UserProvider else { fatalError() } guard let userProvider = dependency as? UserProvider else { fatalError() }

View File

@ -110,6 +110,8 @@ internal enum L10n {
internal static let confirm = L10n.tr("Localizable", "Common.Controls.Actions.Confirm") internal static let confirm = L10n.tr("Localizable", "Common.Controls.Actions.Confirm")
/// Continue /// Continue
internal static let `continue` = L10n.tr("Localizable", "Common.Controls.Actions.Continue") internal static let `continue` = L10n.tr("Localizable", "Common.Controls.Actions.Continue")
/// Copy Photo
internal static let copyPhoto = L10n.tr("Localizable", "Common.Controls.Actions.CopyPhoto")
/// Delete /// Delete
internal static let delete = L10n.tr("Localizable", "Common.Controls.Actions.Delete") internal static let delete = L10n.tr("Localizable", "Common.Controls.Actions.Delete")
/// Discard /// Discard
@ -144,7 +146,7 @@ internal enum L10n {
} }
/// Save /// Save
internal static let save = L10n.tr("Localizable", "Common.Controls.Actions.Save") internal static let save = L10n.tr("Localizable", "Common.Controls.Actions.Save")
/// Save photo /// Save Photo
internal static let savePhoto = L10n.tr("Localizable", "Common.Controls.Actions.SavePhoto") internal static let savePhoto = L10n.tr("Localizable", "Common.Controls.Actions.SavePhoto")
/// See More /// See More
internal static let seeMore = L10n.tr("Localizable", "Common.Controls.Actions.SeeMore") internal static let seeMore = L10n.tr("Localizable", "Common.Controls.Actions.SeeMore")
@ -152,7 +154,7 @@ internal enum L10n {
internal static let settings = L10n.tr("Localizable", "Common.Controls.Actions.Settings") internal static let settings = L10n.tr("Localizable", "Common.Controls.Actions.Settings")
/// Share /// Share
internal static let share = L10n.tr("Localizable", "Common.Controls.Actions.Share") internal static let share = L10n.tr("Localizable", "Common.Controls.Actions.Share")
/// Share post /// Share Post
internal static let sharePost = L10n.tr("Localizable", "Common.Controls.Actions.SharePost") internal static let sharePost = L10n.tr("Localizable", "Common.Controls.Actions.SharePost")
/// Share %@ /// Share %@
internal static func shareUser(_ p1: Any) -> String { internal static func shareUser(_ p1: Any) -> String {
@ -164,7 +166,7 @@ internal enum L10n {
internal static let signUp = L10n.tr("Localizable", "Common.Controls.Actions.SignUp") internal static let signUp = L10n.tr("Localizable", "Common.Controls.Actions.SignUp")
/// Skip /// Skip
internal static let skip = L10n.tr("Localizable", "Common.Controls.Actions.Skip") internal static let skip = L10n.tr("Localizable", "Common.Controls.Actions.Skip")
/// Take photo /// Take Photo
internal static let takePhoto = L10n.tr("Localizable", "Common.Controls.Actions.TakePhoto") internal static let takePhoto = L10n.tr("Localizable", "Common.Controls.Actions.TakePhoto")
/// Try Again /// Try Again
internal static let tryAgain = L10n.tr("Localizable", "Common.Controls.Actions.TryAgain") internal static let tryAgain = L10n.tr("Localizable", "Common.Controls.Actions.TryAgain")

View File

@ -189,6 +189,35 @@ extension StatusTableViewCellDelegate where Self: StatusProvider {
}) })
.store(in: &self.context.disposeBag) .store(in: &self.context.disposeBag)
} }
let copyPhotoAction = UIAction(
title: L10n.Common.Controls.Actions.copyPhoto,
image: UIImage(systemName: "doc.on.doc"), identifier: nil, discoverabilityTitle: nil, attributes: [], state: .off
) { [weak self] _ in
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: copy photo", ((#file as NSString).lastPathComponent), #line, #function)
guard let self = self else { return }
self.attachment(of: status, index: i)
.setFailureType(to: Error.self)
.compactMap { attachment -> AnyPublisher<UIImage, Error>? in
guard let attachment = attachment, let url = URL(string: attachment.url) else { return nil }
return self.context.photoLibraryService.copyImage(url: url)
}
.switchToLatest()
.sink(receiveCompletion: { [weak self] completion in
guard let self = self else { return }
switch completion {
case .failure(let error):
guard let error = error as? PhotoLibraryService.PhotoLibraryError,
case .noPermission = error else { return }
let alertController = SettingService.openSettingsAlertController(title: L10n.Common.Alerts.SavePhotoFailure.title, message: L10n.Common.Alerts.SavePhotoFailure.message)
self.coordinator.present(scene: .alertController(alertController: alertController), from: self, transition: .alertController(animated: true, completion: nil))
case .finished:
break
}
}, receiveValue: { _ in
// do nothing
})
.store(in: &self.context.disposeBag)
}
let shareAction = UIAction( let shareAction = UIAction(
title: L10n.Common.Controls.Actions.share, image: UIImage(systemName: "square.and.arrow.up")!, identifier: nil, discoverabilityTitle: nil, attributes: [], state: .off title: L10n.Common.Controls.Actions.share, image: UIImage(systemName: "square.and.arrow.up")!, identifier: nil, discoverabilityTitle: nil, attributes: [], state: .off
) { [weak self] _ in ) { [weak self] _ in
@ -210,7 +239,7 @@ extension StatusTableViewCellDelegate where Self: StatusProvider {
}) })
.store(in: &self.context.disposeBag) .store(in: &self.context.disposeBag)
} }
let children = [savePhotoAction, shareAction] let children = [savePhotoAction, copyPhotoAction, shareAction]
return UIMenu(title: "", image: nil, children: children) return UIMenu(title: "", image: nil, children: children)
} }
contextMenuConfiguration.indexPath = indexPath contextMenuConfiguration.indexPath = indexPath

View File

@ -30,6 +30,7 @@ Please check your internet connection.";
"Common.Controls.Actions.Cancel" = "Cancel"; "Common.Controls.Actions.Cancel" = "Cancel";
"Common.Controls.Actions.Confirm" = "Confirm"; "Common.Controls.Actions.Confirm" = "Confirm";
"Common.Controls.Actions.Continue" = "Continue"; "Common.Controls.Actions.Continue" = "Continue";
"Common.Controls.Actions.CopyPhoto" = "Copy Photo";
"Common.Controls.Actions.Delete" = "Delete"; "Common.Controls.Actions.Delete" = "Delete";
"Common.Controls.Actions.Discard" = "Discard"; "Common.Controls.Actions.Discard" = "Discard";
"Common.Controls.Actions.Done" = "Done"; "Common.Controls.Actions.Done" = "Done";
@ -46,16 +47,16 @@ Please check your internet connection.";
"Common.Controls.Actions.Reply" = "Reply"; "Common.Controls.Actions.Reply" = "Reply";
"Common.Controls.Actions.ReportUser" = "Report %@"; "Common.Controls.Actions.ReportUser" = "Report %@";
"Common.Controls.Actions.Save" = "Save"; "Common.Controls.Actions.Save" = "Save";
"Common.Controls.Actions.SavePhoto" = "Save photo"; "Common.Controls.Actions.SavePhoto" = "Save Photo";
"Common.Controls.Actions.SeeMore" = "See More"; "Common.Controls.Actions.SeeMore" = "See More";
"Common.Controls.Actions.Settings" = "Settings"; "Common.Controls.Actions.Settings" = "Settings";
"Common.Controls.Actions.Share" = "Share"; "Common.Controls.Actions.Share" = "Share";
"Common.Controls.Actions.SharePost" = "Share post"; "Common.Controls.Actions.SharePost" = "Share Post";
"Common.Controls.Actions.ShareUser" = "Share %@"; "Common.Controls.Actions.ShareUser" = "Share %@";
"Common.Controls.Actions.SignIn" = "Sign In"; "Common.Controls.Actions.SignIn" = "Sign In";
"Common.Controls.Actions.SignUp" = "Sign Up"; "Common.Controls.Actions.SignUp" = "Sign Up";
"Common.Controls.Actions.Skip" = "Skip"; "Common.Controls.Actions.Skip" = "Skip";
"Common.Controls.Actions.TakePhoto" = "Take photo"; "Common.Controls.Actions.TakePhoto" = "Take Photo";
"Common.Controls.Actions.TryAgain" = "Try Again"; "Common.Controls.Actions.TryAgain" = "Try Again";
"Common.Controls.Actions.UnblockDomain" = "Unblock %@"; "Common.Controls.Actions.UnblockDomain" = "Unblock %@";
"Common.Controls.Friendship.Block" = "Block"; "Common.Controls.Friendship.Block" = "Block";

View File

@ -30,6 +30,7 @@ Please check your internet connection.";
"Common.Controls.Actions.Cancel" = "Cancel"; "Common.Controls.Actions.Cancel" = "Cancel";
"Common.Controls.Actions.Confirm" = "Confirm"; "Common.Controls.Actions.Confirm" = "Confirm";
"Common.Controls.Actions.Continue" = "Continue"; "Common.Controls.Actions.Continue" = "Continue";
"Common.Controls.Actions.CopyPhoto" = "Copy Photo";
"Common.Controls.Actions.Delete" = "Delete"; "Common.Controls.Actions.Delete" = "Delete";
"Common.Controls.Actions.Discard" = "Discard"; "Common.Controls.Actions.Discard" = "Discard";
"Common.Controls.Actions.Done" = "Done"; "Common.Controls.Actions.Done" = "Done";
@ -46,16 +47,16 @@ Please check your internet connection.";
"Common.Controls.Actions.Reply" = "Reply"; "Common.Controls.Actions.Reply" = "Reply";
"Common.Controls.Actions.ReportUser" = "Report %@"; "Common.Controls.Actions.ReportUser" = "Report %@";
"Common.Controls.Actions.Save" = "Save"; "Common.Controls.Actions.Save" = "Save";
"Common.Controls.Actions.SavePhoto" = "Save photo"; "Common.Controls.Actions.SavePhoto" = "Save Photo";
"Common.Controls.Actions.SeeMore" = "See More"; "Common.Controls.Actions.SeeMore" = "See More";
"Common.Controls.Actions.Settings" = "Settings"; "Common.Controls.Actions.Settings" = "Settings";
"Common.Controls.Actions.Share" = "Share"; "Common.Controls.Actions.Share" = "Share";
"Common.Controls.Actions.SharePost" = "Share post"; "Common.Controls.Actions.SharePost" = "Share Post";
"Common.Controls.Actions.ShareUser" = "Share %@"; "Common.Controls.Actions.ShareUser" = "Share %@";
"Common.Controls.Actions.SignIn" = "Sign In"; "Common.Controls.Actions.SignIn" = "Sign In";
"Common.Controls.Actions.SignUp" = "Sign Up"; "Common.Controls.Actions.SignUp" = "Sign Up";
"Common.Controls.Actions.Skip" = "Skip"; "Common.Controls.Actions.Skip" = "Skip";
"Common.Controls.Actions.TakePhoto" = "Take photo"; "Common.Controls.Actions.TakePhoto" = "Take Photo";
"Common.Controls.Actions.TryAgain" = "Try Again"; "Common.Controls.Actions.TryAgain" = "Try Again";
"Common.Controls.Actions.UnblockDomain" = "Unblock %@"; "Common.Controls.Actions.UnblockDomain" = "Unblock %@";
"Common.Controls.Friendship.Block" = "Block"; "Common.Controls.Friendship.Block" = "Block";

View File

@ -9,7 +9,7 @@ import os.log
import UIKit import UIKit
import Combine import Combine
import Photos import Photos
import AlamofireImage import Nuke
final class PhotoLibraryService: NSObject { final class PhotoLibraryService: NSObject {
@ -26,39 +26,51 @@ extension PhotoLibraryService {
extension PhotoLibraryService { extension PhotoLibraryService {
func saveImage(url: URL) -> AnyPublisher<UIImage, Error> { func saveImage(url: URL) -> AnyPublisher<UIImage, Error> {
return processImage(url: url)
.handleEvents(receiveOutput: { image in
self.save(image: image)
})
.eraseToAnyPublisher()
}
func copyImage(url: URL) -> AnyPublisher<UIImage, Error> {
return processImage(url: url)
.handleEvents(receiveOutput: { image in
UIPasteboard.general.image = image
})
.eraseToAnyPublisher()
}
func processImage(url: URL) -> AnyPublisher<UIImage, Error> {
let impactFeedbackGenerator = UIImpactFeedbackGenerator(style: .light) let impactFeedbackGenerator = UIImpactFeedbackGenerator(style: .light)
let notificationFeedbackGenerator = UINotificationFeedbackGenerator() let notificationFeedbackGenerator = UINotificationFeedbackGenerator()
return Future<UIImage, Error> { promise in guard PHPhotoLibrary.authorizationStatus(for: .addOnly) != .denied else {
guard PHPhotoLibrary.authorizationStatus(for: .addOnly) != .denied else { return Fail(error: PhotoLibraryError.noPermission).eraseToAnyPublisher()
promise(.failure(PhotoLibraryError.noPermission)) }
return
}
ImageDownloader.default.download(URLRequest(url: url), completion: { [weak self] response in return ImagePipeline.shared.imagePublisher(with: url)
guard let self = self else { return } .handleEvents(receiveSubscription: { _ in
switch response.result { impactFeedbackGenerator.impactOccurred()
}, receiveOutput: { response in
self.save(image: response.image)
}, receiveCompletion: { completion in
switch completion {
case .failure(let error): case .failure(let error):
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: download image %s fail: %s", ((#file as NSString).lastPathComponent), #line, #function, url.debugDescription, error.localizedDescription) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: download image %s fail: %s", ((#file as NSString).lastPathComponent), #line, #function, url.debugDescription, error.localizedDescription)
promise(.failure(error))
case .success(let image): notificationFeedbackGenerator.notificationOccurred(.error)
case .finished:
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: download image %s success", ((#file as NSString).lastPathComponent), #line, #function, url.debugDescription) os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: download image %s success", ((#file as NSString).lastPathComponent), #line, #function, url.debugDescription)
self.save(image: image)
promise(.success(image)) notificationFeedbackGenerator.notificationOccurred(.success)
} }
}) })
} .map { response in
.handleEvents(receiveSubscription: { _ in return response.image
impactFeedbackGenerator.impactOccurred()
}, receiveCompletion: { completion in
switch completion {
case .failure:
notificationFeedbackGenerator.notificationOccurred(.error)
case .finished:
notificationFeedbackGenerator.notificationOccurred(.success)
} }
}) .mapError { error in error as Error }
.eraseToAnyPublisher() .eraseToAnyPublisher()
} }
func save(image: UIImage, withNotificationFeedback: Bool = false) { func save(image: UIImage, withNotificationFeedback: Bool = false) {