From fcf20b16753bad85f8f6e78e9f144f0a51c075b5 Mon Sep 17 00:00:00 2001 From: Joe Barbere Date: Sat, 11 Feb 2023 15:38:33 -0500 Subject: [PATCH] Allow editing of alt-text for media (#796) * Allow editing of alt-text for media * Fix --------- Co-authored-by: Thomas Ricouard --- .../Env/Sources/Env/CurrentInstance.swift | 4 ++++ .../Sources/Network/Endpoint/Statuses.swift | 19 ++++++++++++++++++- .../StatusEditorMediaEditView.swift | 12 ++++++++++-- .../Components/StatusEditorMediaView.swift | 3 ++- .../Status/Editor/StatusEditorViewModel.swift | 13 +++++++++++-- 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/Packages/Env/Sources/Env/CurrentInstance.swift b/Packages/Env/Sources/Env/CurrentInstance.swift index 77fef15e..5d2274f5 100644 --- a/Packages/Env/Sources/Env/CurrentInstance.swift +++ b/Packages/Env/Sources/Env/CurrentInstance.swift @@ -17,6 +17,10 @@ public class CurrentInstance: ObservableObject { public var isEditSupported: Bool { instance?.version.hasPrefix("4") == true } + + public var isEditAltTextSupported: Bool { + instance?.version.hasPrefix("4.1") == true + } private init() {} diff --git a/Packages/Network/Sources/Network/Endpoint/Statuses.swift b/Packages/Network/Sources/Network/Endpoint/Statuses.swift index 9ea6c331..b9702a1d 100644 --- a/Packages/Network/Sources/Network/Endpoint/Statuses.swift +++ b/Packages/Network/Sources/Network/Endpoint/Statuses.swift @@ -84,6 +84,7 @@ public struct StatusData: Encodable { public let mediaIds: [String]? public let poll: PollData? public let language: String? + public let mediaAttributes: [MediaAttribute]? public struct PollData: Encodable { public let options: [String] @@ -96,6 +97,20 @@ public struct StatusData: Encodable { self.expires_in = expires_in } } + + public struct MediaAttribute: Encodable { + public let id: String + public let description: String? + public let thumbnail: String? + public let focus: String? + + public init(id: String, description: String?, thumbnail: String?, focus: String?) { + self.id = id + self.description = description + self.thumbnail = thumbnail + self.focus = focus + } + } public init(status: String, visibility: Visibility, @@ -103,7 +118,8 @@ public struct StatusData: Encodable { spoilerText: String? = nil, mediaIds: [String]? = nil, poll: PollData? = nil, - language: String? = nil) + language: String? = nil, + mediaAttributes: [MediaAttribute]? = nil) { self.status = status self.visibility = visibility @@ -112,5 +128,6 @@ public struct StatusData: Encodable { self.mediaIds = mediaIds self.poll = poll self.language = language + self.mediaAttributes = mediaAttributes } } diff --git a/Packages/Status/Sources/Status/Editor/Components/StatusEditorMediaEditView.swift b/Packages/Status/Sources/Status/Editor/Components/StatusEditorMediaEditView.swift index bd93e71a..8d14e226 100644 --- a/Packages/Status/Sources/Status/Editor/Components/StatusEditorMediaEditView.swift +++ b/Packages/Status/Sources/Status/Editor/Components/StatusEditorMediaEditView.swift @@ -1,4 +1,5 @@ import DesignSystem +import Env import Models import Shimmer import SwiftUI @@ -6,6 +7,7 @@ import SwiftUI struct StatusEditorMediaEditView: View { @Environment(\.dismiss) private var dismiss @EnvironmentObject private var theme: Theme + @EnvironmentObject private var currentInstance: CurrentInstance @ObservedObject var viewModel: StatusEditorViewModel let container: StatusEditorMediaContainer @@ -53,8 +55,14 @@ struct StatusEditorMediaEditView: View { ToolbarItem(placement: .navigationBarTrailing) { Button("action.done") { if !imageDescription.isEmpty { - Task { - await viewModel.addDescription(container: container, description: imageDescription) + if currentInstance.isEditAltTextSupported && viewModel.mode.isEditing { + Task { + await viewModel.editDescription(container: container, description: imageDescription) + } + } else { + Task { + await viewModel.addDescription(container: container, description: imageDescription) + } } } dismiss() diff --git a/Packages/Status/Sources/Status/Editor/Components/StatusEditorMediaView.swift b/Packages/Status/Sources/Status/Editor/Components/StatusEditorMediaView.swift index 8eca5d89..8f9df62f 100644 --- a/Packages/Status/Sources/Status/Editor/Components/StatusEditorMediaView.swift +++ b/Packages/Status/Sources/Status/Editor/Components/StatusEditorMediaView.swift @@ -7,6 +7,7 @@ import SwiftUI struct StatusEditorMediaView: View { @EnvironmentObject private var theme: Theme + @EnvironmentObject private var currentInstance: CurrentInstance @ObservedObject var viewModel: StatusEditorViewModel @State private var editingContainer: StatusEditorMediaContainer? @@ -126,7 +127,7 @@ struct StatusEditorMediaView: View { @ViewBuilder private func makeImageMenu(container: StatusEditorMediaContainer) -> some View { if container.mediaAttachment != nil { - if !viewModel.mode.isEditing { + if currentInstance.isEditAltTextSupported || !viewModel.mode.isEditing { Button { editingContainer = container } label: { diff --git a/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift b/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift index 2d492e31..37419954 100644 --- a/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift +++ b/Packages/Status/Sources/Status/Editor/StatusEditorViewModel.swift @@ -175,7 +175,8 @@ public class StatusEditorViewModel: NSObject, ObservableObject { spoilerText: spoilerOn ? spoilerText : nil, mediaIds: mediasImages.compactMap { $0.mediaAttachment?.id }, poll: pollData, - language: selectedLanguage) + language: selectedLanguage, + mediaAttributes: mediaAttributes) switch mode { case .new, .replyTo, .quote, .mention, .shareExtension: postStatus = try await client.post(endpoint: Statuses.postStatus(json: data)) @@ -658,7 +659,15 @@ public class StatusEditorViewModel: NSObject, ObservableObject { gifTransferable: nil, mediaAttachment: media, error: nil) - } catch {} + } catch { print(error) } + } + } + + private var mediaAttributes: [StatusData.MediaAttribute] = [] + func editDescription(container: StatusEditorMediaContainer, description: String) async { + guard let attachment = container.mediaAttachment else { return } + if indexOf(container: container) != nil { + mediaAttributes.append(StatusData.MediaAttribute(id: attachment.id, description: description, thumbnail: nil, focus: nil)) } }