2021-04-29 13:49:46 +02:00
|
|
|
//
|
|
|
|
// PhotoLibraryService.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-4-29.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
2021-05-06 09:05:24 +02:00
|
|
|
import Photos
|
2021-07-21 13:45:24 +02:00
|
|
|
import Alamofire
|
2021-07-07 11:51:47 +02:00
|
|
|
import AlamofireImage
|
2021-04-29 13:49:46 +02:00
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
public final class PhotoLibraryService: NSObject {
|
2021-04-29 13:49:46 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-06 09:05:24 +02:00
|
|
|
extension PhotoLibraryService {
|
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
public enum PhotoLibraryError: Error {
|
2021-05-06 09:05:24 +02:00
|
|
|
case noPermission
|
2021-07-21 13:45:24 +02:00
|
|
|
case badPayload
|
|
|
|
}
|
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
public enum ImageSource {
|
2021-07-21 13:45:24 +02:00
|
|
|
case url(URL)
|
|
|
|
case image(UIImage)
|
2021-05-06 09:05:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-04-29 13:49:46 +02:00
|
|
|
extension PhotoLibraryService {
|
2021-07-06 14:02:30 +02:00
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
public func save(imageSource source: ImageSource) -> AnyPublisher<Void, Error> {
|
2021-07-21 13:45:24 +02:00
|
|
|
let impactFeedbackGenerator = UIImpactFeedbackGenerator(style: .light)
|
|
|
|
let notificationFeedbackGenerator = UINotificationFeedbackGenerator()
|
|
|
|
|
|
|
|
|
|
|
|
let imageDataPublisher: AnyPublisher<Data, Error> = {
|
|
|
|
switch source {
|
|
|
|
case .url(let url):
|
|
|
|
return PhotoLibraryService.fetchImageData(url: url)
|
|
|
|
case .image(let image):
|
|
|
|
return PhotoLibraryService.fetchImageData(image: image)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return imageDataPublisher
|
|
|
|
.flatMap { data in
|
|
|
|
PhotoLibraryService.save(imageData: data)
|
|
|
|
}
|
|
|
|
.handleEvents(receiveSubscription: { _ in
|
|
|
|
impactFeedbackGenerator.impactOccurred()
|
|
|
|
}, receiveCompletion: { completion in
|
|
|
|
switch completion {
|
|
|
|
case .failure:
|
|
|
|
notificationFeedbackGenerator.notificationOccurred(.error)
|
|
|
|
case .finished:
|
|
|
|
notificationFeedbackGenerator.notificationOccurred(.success)
|
|
|
|
}
|
2021-07-06 13:53:47 +02:00
|
|
|
})
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2021-07-21 13:45:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
extension PhotoLibraryService {
|
|
|
|
|
2022-09-30 13:28:09 +02:00
|
|
|
public func copy(imageSource source: ImageSource) -> AnyPublisher<Void, Error> {
|
2021-07-21 13:45:24 +02:00
|
|
|
|
|
|
|
let impactFeedbackGenerator = UIImpactFeedbackGenerator(style: .light)
|
|
|
|
let notificationFeedbackGenerator = UINotificationFeedbackGenerator()
|
|
|
|
|
|
|
|
let imageDataPublisher: AnyPublisher<Data, Error> = {
|
|
|
|
switch source {
|
|
|
|
case .url(let url):
|
|
|
|
return PhotoLibraryService.fetchImageData(url: url)
|
|
|
|
case .image(let image):
|
|
|
|
return PhotoLibraryService.fetchImageData(image: image)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return imageDataPublisher
|
|
|
|
.flatMap { data in
|
|
|
|
PhotoLibraryService.copy(imageData: data)
|
|
|
|
}
|
|
|
|
.handleEvents(receiveSubscription: { _ in
|
|
|
|
impactFeedbackGenerator.impactOccurred()
|
|
|
|
}, receiveCompletion: { completion in
|
|
|
|
switch completion {
|
|
|
|
case .failure:
|
|
|
|
notificationFeedbackGenerator.notificationOccurred(.error)
|
|
|
|
case .finished:
|
|
|
|
notificationFeedbackGenerator.notificationOccurred(.success)
|
|
|
|
}
|
2021-07-06 13:53:47 +02:00
|
|
|
})
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
2021-07-21 13:45:24 +02:00
|
|
|
}
|
2021-07-06 13:53:47 +02:00
|
|
|
|
2021-07-21 13:45:24 +02:00
|
|
|
extension PhotoLibraryService {
|
2021-07-06 13:53:47 +02:00
|
|
|
|
2021-07-21 13:45:24 +02:00
|
|
|
static func fetchImageData(url: URL) -> AnyPublisher<Data, Error> {
|
|
|
|
AF.request(url).publishData()
|
|
|
|
.tryMap { response in
|
2021-07-07 11:51:47 +02:00
|
|
|
switch response.result {
|
2021-07-21 13:45:24 +02:00
|
|
|
case .success(let data):
|
|
|
|
return data
|
2021-04-29 13:49:46 +02:00
|
|
|
case .failure(let error):
|
2021-07-21 13:45:24 +02:00
|
|
|
throw error
|
2021-04-29 13:49:46 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-21 13:45:24 +02:00
|
|
|
.eraseToAnyPublisher()
|
2021-04-29 13:49:46 +02:00
|
|
|
}
|
2021-07-21 13:45:24 +02:00
|
|
|
|
|
|
|
static func fetchImageData(image: UIImage) -> AnyPublisher<Data, Error> {
|
|
|
|
return Future<Data, Error> { promise in
|
|
|
|
DispatchQueue.global().async {
|
|
|
|
let imageData = image.pngData()
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
if let imageData = imageData {
|
|
|
|
promise(.success(imageData))
|
|
|
|
} else {
|
|
|
|
promise(.failure(PhotoLibraryError.badPayload))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-29 13:49:46 +02:00
|
|
|
}
|
2021-07-21 13:45:24 +02:00
|
|
|
.eraseToAnyPublisher()
|
2021-04-29 13:49:46 +02:00
|
|
|
}
|
2021-07-06 14:02:30 +02:00
|
|
|
|
2021-07-21 13:45:24 +02:00
|
|
|
static func save(imageData: Data) -> AnyPublisher<Void, Error> {
|
|
|
|
guard PHPhotoLibrary.authorizationStatus(for: .addOnly) != .denied else {
|
|
|
|
return Fail(error: PhotoLibraryError.noPermission).eraseToAnyPublisher()
|
|
|
|
}
|
2021-07-06 14:02:30 +02:00
|
|
|
|
2021-07-21 13:45:24 +02:00
|
|
|
return Future<Void, Error> { promise in
|
|
|
|
PHPhotoLibrary.shared().performChanges {
|
|
|
|
PHAssetCreationRequest.forAsset().addResource(with: .photo, data: imageData, options: nil)
|
|
|
|
} completionHandler: { isSuccess, error in
|
|
|
|
if let error = error {
|
|
|
|
promise(.failure(error))
|
|
|
|
} else {
|
|
|
|
promise(.success(Void()))
|
|
|
|
}
|
|
|
|
}
|
2021-07-06 14:02:30 +02:00
|
|
|
}
|
2021-07-21 13:45:24 +02:00
|
|
|
.eraseToAnyPublisher()
|
2021-07-06 14:02:30 +02:00
|
|
|
}
|
2021-07-21 13:45:24 +02:00
|
|
|
|
|
|
|
static func copy(imageData: Data) -> AnyPublisher<Void, Error> {
|
|
|
|
Future<Void, Error> { promise in
|
|
|
|
DispatchQueue.global().async {
|
|
|
|
let image = UIImage(data: imageData, scale: UIScreen.main.scale)
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
if let image = image {
|
|
|
|
UIPasteboard.general.image = image
|
|
|
|
promise(.success(Void()))
|
|
|
|
} else {
|
|
|
|
promise(.failure(PhotoLibraryError.badPayload))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
2021-04-29 13:49:46 +02:00
|
|
|
}
|
2021-07-21 13:45:24 +02:00
|
|
|
|
2021-04-29 13:49:46 +02:00
|
|
|
}
|