2021-04-29 13:49:46 +02:00
|
|
|
//
|
|
|
|
// PhotoLibraryService.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-4-29.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
2021-05-06 09:05:24 +02:00
|
|
|
import Photos
|
2021-07-07 11:51:47 +02:00
|
|
|
import AlamofireImage
|
2021-04-29 13:49:46 +02:00
|
|
|
|
|
|
|
final class PhotoLibraryService: NSObject {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-06 09:05:24 +02:00
|
|
|
extension PhotoLibraryService {
|
|
|
|
|
|
|
|
enum PhotoLibraryError: Error {
|
|
|
|
case noPermission
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:49:46 +02:00
|
|
|
extension PhotoLibraryService {
|
|
|
|
|
|
|
|
func saveImage(url: URL) -> AnyPublisher<UIImage, Error> {
|
2021-07-06 14:02:30 +02:00
|
|
|
guard PHPhotoLibrary.authorizationStatus(for: .addOnly) != .denied else {
|
|
|
|
return Fail(error: PhotoLibraryError.noPermission).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2021-07-06 13:53:47 +02:00
|
|
|
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> {
|
2021-04-29 13:49:46 +02:00
|
|
|
let impactFeedbackGenerator = UIImpactFeedbackGenerator(style: .light)
|
|
|
|
let notificationFeedbackGenerator = UINotificationFeedbackGenerator()
|
2021-07-06 13:53:47 +02:00
|
|
|
|
2021-07-07 11:51:47 +02:00
|
|
|
return Future<UIImage, Error> { promise in
|
|
|
|
ImageDownloader.default.download(URLRequest(url: url), completion: { response in
|
|
|
|
switch response.result {
|
2021-04-29 13:49:46 +02:00
|
|
|
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)
|
2021-07-07 11:51:47 +02:00
|
|
|
promise(.failure(error))
|
|
|
|
case .success(let image):
|
2021-04-29 13:49:46 +02:00
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: download image %s success", ((#file as NSString).lastPathComponent), #line, #function, url.debugDescription)
|
2021-07-07 11:51:47 +02:00
|
|
|
promise(.success(image))
|
2021-04-29 13:49:46 +02:00
|
|
|
}
|
|
|
|
})
|
2021-07-07 11:51:47 +02:00
|
|
|
}
|
|
|
|
.handleEvents(receiveSubscription: { _ in
|
|
|
|
impactFeedbackGenerator.impactOccurred()
|
|
|
|
}, receiveCompletion: { completion in
|
|
|
|
switch completion {
|
|
|
|
case .failure:
|
|
|
|
notificationFeedbackGenerator.notificationOccurred(.error)
|
|
|
|
case .finished:
|
|
|
|
notificationFeedbackGenerator.notificationOccurred(.success)
|
2021-04-29 13:49:46 +02:00
|
|
|
}
|
2021-07-07 11:51:47 +02:00
|
|
|
})
|
|
|
|
.eraseToAnyPublisher()
|
2021-04-29 13:49:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func save(image: UIImage, withNotificationFeedback: Bool = false) {
|
|
|
|
UIImageWriteToSavedPhotosAlbum(
|
|
|
|
image,
|
|
|
|
self,
|
|
|
|
#selector(PhotoLibraryService.image(_:didFinishSavingWithError:contextInfo:)),
|
|
|
|
nil
|
|
|
|
)
|
|
|
|
|
|
|
|
// assert no error
|
|
|
|
if withNotificationFeedback {
|
|
|
|
let notificationFeedbackGenerator = UINotificationFeedbackGenerator()
|
|
|
|
notificationFeedbackGenerator.notificationOccurred(.success)
|
|
|
|
}
|
|
|
|
}
|
2021-07-06 14:02:30 +02:00
|
|
|
|
|
|
|
func copy(image: UIImage, withNotificationFeedback: Bool = false) {
|
|
|
|
UIPasteboard.general.image = image
|
|
|
|
|
|
|
|
// assert no error
|
|
|
|
if withNotificationFeedback {
|
|
|
|
let notificationFeedbackGenerator = UINotificationFeedbackGenerator()
|
|
|
|
notificationFeedbackGenerator.notificationOccurred(.success)
|
|
|
|
}
|
|
|
|
}
|
2021-04-29 13:49:46 +02:00
|
|
|
|
|
|
|
@objc private func image(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
|
|
|
|
// TODO: notify banner
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|