-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Post Composer, uploading avatar/banner (#818)
- Loading branch information
Showing
19 changed files
with
821 additions
and
520 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
// | ||
|
||
import SwiftUI | ||
import PhotosUI | ||
|
||
struct ImageUploadResponse: Codable { | ||
public let msg: String? | ||
|
84 changes: 0 additions & 84 deletions
84
Mlem/Views/Shared/Components/Image Upload/ImageUploadView.swift
This file was deleted.
Oops, something went wrong.
186 changes: 186 additions & 0 deletions
186
Mlem/Views/Shared/Components/Image Upload/LinkAttachmentModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
// | ||
// LinkAttachmentModel.swift | ||
// Mlem | ||
// | ||
// Created by Sjmarf on 17/12/2023. | ||
// | ||
|
||
import SwiftUI | ||
import Dependencies | ||
import PhotosUI | ||
|
||
class LinkAttachmentModel: ObservableObject { | ||
@Dependency(\.pictrsRepository) private var pictrsRepository: PictrsRespository | ||
@Dependency(\.apiClient) private var apiClient: APIClient | ||
@Dependency(\.errorHandler) private var errorHandler: ErrorHandler | ||
|
||
var uploadTask: Task<(), any Error>? | ||
|
||
@AppStorage("promptUser.permission.privacy.allowImageUploads") var askedForPermissionToUploadImages: Bool = false | ||
@AppStorage("confirmImageUploads") var confirmImageUploads: Bool = false | ||
|
||
@Published var url: String = "" | ||
@Published var imageModel: PictrsImageModel? | ||
|
||
init(url: String) { | ||
self.url = url | ||
} | ||
|
||
@Published var photosPickerItem: PhotosPickerItem? | ||
@Published var showingUploadConfirmation: Bool = false | ||
@Published var showingPhotosPicker: Bool = false | ||
@Published var showingFilePicker: Bool = false | ||
|
||
func attachImageAction() { | ||
showingPhotosPicker = true | ||
} | ||
|
||
func attachFileAction() { | ||
showingFilePicker = true | ||
} | ||
|
||
func pasteFromClipboardAction() { | ||
pasteFromClipboard() | ||
} | ||
|
||
func removeLinkAction() { | ||
url = "" | ||
deletePictrs() | ||
} | ||
|
||
func prepareToUpload(photo: PhotosPickerItem) async { | ||
do { | ||
if let data = try await photo.loadTransferable(type: Data.self) { | ||
DispatchQueue.main.async { | ||
self.prepareToUpload(data: data) | ||
} | ||
} else { | ||
imageModel = .init(state: .failed("Invalid image format")) | ||
} | ||
} catch { | ||
imageModel = .init(state: .failed(String(describing: error))) | ||
} | ||
} | ||
|
||
func prepareToUpload(result: Result<URL, Error>) { | ||
switch result { | ||
case .success(let url): | ||
do { | ||
guard url.startAccessingSecurityScopedResource() else { | ||
imageModel = .init(state: .failed("Invalid permissions")) | ||
return | ||
} | ||
let data = try Data(contentsOf: url) | ||
url.stopAccessingSecurityScopedResource() | ||
prepareToUpload(data: data) | ||
} catch { | ||
url.stopAccessingSecurityScopedResource() | ||
imageModel = .init(state: .failed(String(describing: error))) | ||
} | ||
case .failure(let error): | ||
imageModel = .init(state: .failed(String(describing: error))) | ||
} | ||
} | ||
|
||
func prepareToUpload(data: Data) { | ||
imageModel = .init() | ||
self.imageModel?.state = .readyToUpload(data: data) | ||
if let uiImage = UIImage(data: data) { | ||
self.imageModel?.image = Image(uiImage: uiImage) | ||
} | ||
if self.askedForPermissionToUploadImages == false || self.confirmImageUploads { | ||
self.showingUploadConfirmation = true | ||
} else { | ||
self.uploadImage() | ||
} | ||
} | ||
|
||
func pasteFromClipboard() { | ||
Task { | ||
if UIPasteboard.general.hasImages, let content = UIPasteboard.general.image { | ||
if let data = content.pngData() { | ||
DispatchQueue.main.async { | ||
self.prepareToUpload(data: data) | ||
} | ||
} | ||
} else if UIPasteboard.general.hasURLs, let content = UIPasteboard.general.url { | ||
DispatchQueue.main.async { | ||
self.url = content.absoluteString | ||
} | ||
} | ||
} | ||
} | ||
|
||
func uploadImage() { | ||
guard let imageModel = imageModel else { return } | ||
Task(priority: .userInitiated) { | ||
self.uploadTask = try await pictrsRepository.uploadImage( | ||
imageModel: imageModel, | ||
onUpdate: { newValue in | ||
DispatchQueue.main.async { | ||
self.imageModel = newValue | ||
switch newValue.state { | ||
case .uploaded(let file): | ||
if let file = file { | ||
do { | ||
var components = URLComponents() | ||
components.scheme = try self.apiClient.session.instanceUrl.scheme | ||
components.host = try self.apiClient.session.instanceUrl.host | ||
components.path = "/pictrs/image/\(file.file)" | ||
self.url = components.url?.absoluteString ?? "" | ||
} catch { | ||
self.imageModel?.state = .failed(nil) | ||
} | ||
} | ||
default: | ||
self.url = "" | ||
} | ||
} | ||
} | ||
) | ||
} | ||
} | ||
|
||
func deletePictrs(compareUrl: String? = nil) { | ||
if let task = self.uploadTask { | ||
task.cancel() | ||
} | ||
switch self.imageModel?.state { | ||
case .uploaded(file: let file): | ||
if let file = file { | ||
self.photosPickerItem = nil | ||
Task { | ||
do { | ||
if let compareUrl { | ||
var components = URLComponents() | ||
components.scheme = try self.apiClient.session.instanceUrl.scheme | ||
components.host = try self.apiClient.session.instanceUrl.host | ||
components.path = "/pictrs/image/\(file.file)" | ||
if let imageModelUrl = components.url?.absoluteString, compareUrl != imageModelUrl { | ||
try await pictrsRepository.deleteImage(file: file) | ||
print("Deleted from pictrs") | ||
DispatchQueue.main.async { | ||
self.imageModel = nil | ||
} | ||
} | ||
} else { | ||
try await pictrsRepository.deleteImage(file: file) | ||
print("Deleted from pictrs") | ||
DispatchQueue.main.async { | ||
self.imageModel = nil | ||
} | ||
} | ||
} catch { | ||
print("ERROR", error) | ||
errorHandler.handle(error) | ||
} | ||
} | ||
} | ||
default: | ||
self.imageModel = nil | ||
} | ||
if url == "" && self.imageModel != nil { | ||
self.imageModel = nil | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Mlem/Views/Shared/Components/Image Upload/LinkAttachmentProxy.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// | ||
// LinkAttachmentProxy.swift | ||
// Mlem | ||
// | ||
// Created by Sjmarf on 17/12/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
struct LinkAttachmentProxy { | ||
private let model: LinkAttachmentModel | ||
|
||
var imageModel: PictrsImageModel? { | ||
return model.imageModel | ||
} | ||
|
||
func attachImageAction() { | ||
model.showingPhotosPicker = true | ||
} | ||
|
||
func attachFileAction() { | ||
model.showingFilePicker = true | ||
} | ||
|
||
func pasteFromClipboardAction() { | ||
model.pasteFromClipboard() | ||
} | ||
|
||
func removeLinkAction() { | ||
model.url = "" | ||
} | ||
|
||
init(model: LinkAttachmentModel) { | ||
self.model = model | ||
} | ||
} |
Oops, something went wrong.