Allow editing of alt-text for media (#796)

* Allow editing of alt-text for media

* Fix

---------

Co-authored-by: Thomas Ricouard <ricouard77@gmail.com>
This commit is contained in:
Joe Barbere 2023-02-11 15:38:33 -05:00 committed by GitHub
parent 2b2d042085
commit fcf20b1675
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 6 deletions

View File

@ -17,6 +17,10 @@ public class CurrentInstance: ObservableObject {
public var isEditSupported: Bool { public var isEditSupported: Bool {
instance?.version.hasPrefix("4") == true instance?.version.hasPrefix("4") == true
} }
public var isEditAltTextSupported: Bool {
instance?.version.hasPrefix("4.1") == true
}
private init() {} private init() {}

View File

@ -84,6 +84,7 @@ public struct StatusData: Encodable {
public let mediaIds: [String]? public let mediaIds: [String]?
public let poll: PollData? public let poll: PollData?
public let language: String? public let language: String?
public let mediaAttributes: [MediaAttribute]?
public struct PollData: Encodable { public struct PollData: Encodable {
public let options: [String] public let options: [String]
@ -96,6 +97,20 @@ public struct StatusData: Encodable {
self.expires_in = expires_in 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, public init(status: String,
visibility: Visibility, visibility: Visibility,
@ -103,7 +118,8 @@ public struct StatusData: Encodable {
spoilerText: String? = nil, spoilerText: String? = nil,
mediaIds: [String]? = nil, mediaIds: [String]? = nil,
poll: PollData? = nil, poll: PollData? = nil,
language: String? = nil) language: String? = nil,
mediaAttributes: [MediaAttribute]? = nil)
{ {
self.status = status self.status = status
self.visibility = visibility self.visibility = visibility
@ -112,5 +128,6 @@ public struct StatusData: Encodable {
self.mediaIds = mediaIds self.mediaIds = mediaIds
self.poll = poll self.poll = poll
self.language = language self.language = language
self.mediaAttributes = mediaAttributes
} }
} }

View File

@ -1,4 +1,5 @@
import DesignSystem import DesignSystem
import Env
import Models import Models
import Shimmer import Shimmer
import SwiftUI import SwiftUI
@ -6,6 +7,7 @@ import SwiftUI
struct StatusEditorMediaEditView: View { struct StatusEditorMediaEditView: View {
@Environment(\.dismiss) private var dismiss @Environment(\.dismiss) private var dismiss
@EnvironmentObject private var theme: Theme @EnvironmentObject private var theme: Theme
@EnvironmentObject private var currentInstance: CurrentInstance
@ObservedObject var viewModel: StatusEditorViewModel @ObservedObject var viewModel: StatusEditorViewModel
let container: StatusEditorMediaContainer let container: StatusEditorMediaContainer
@ -53,8 +55,14 @@ struct StatusEditorMediaEditView: View {
ToolbarItem(placement: .navigationBarTrailing) { ToolbarItem(placement: .navigationBarTrailing) {
Button("action.done") { Button("action.done") {
if !imageDescription.isEmpty { if !imageDescription.isEmpty {
Task { if currentInstance.isEditAltTextSupported && viewModel.mode.isEditing {
await viewModel.addDescription(container: container, description: imageDescription) Task {
await viewModel.editDescription(container: container, description: imageDescription)
}
} else {
Task {
await viewModel.addDescription(container: container, description: imageDescription)
}
} }
} }
dismiss() dismiss()

View File

@ -7,6 +7,7 @@ import SwiftUI
struct StatusEditorMediaView: View { struct StatusEditorMediaView: View {
@EnvironmentObject private var theme: Theme @EnvironmentObject private var theme: Theme
@EnvironmentObject private var currentInstance: CurrentInstance
@ObservedObject var viewModel: StatusEditorViewModel @ObservedObject var viewModel: StatusEditorViewModel
@State private var editingContainer: StatusEditorMediaContainer? @State private var editingContainer: StatusEditorMediaContainer?
@ -126,7 +127,7 @@ struct StatusEditorMediaView: View {
@ViewBuilder @ViewBuilder
private func makeImageMenu(container: StatusEditorMediaContainer) -> some View { private func makeImageMenu(container: StatusEditorMediaContainer) -> some View {
if container.mediaAttachment != nil { if container.mediaAttachment != nil {
if !viewModel.mode.isEditing { if currentInstance.isEditAltTextSupported || !viewModel.mode.isEditing {
Button { Button {
editingContainer = container editingContainer = container
} label: { } label: {

View File

@ -175,7 +175,8 @@ public class StatusEditorViewModel: NSObject, ObservableObject {
spoilerText: spoilerOn ? spoilerText : nil, spoilerText: spoilerOn ? spoilerText : nil,
mediaIds: mediasImages.compactMap { $0.mediaAttachment?.id }, mediaIds: mediasImages.compactMap { $0.mediaAttachment?.id },
poll: pollData, poll: pollData,
language: selectedLanguage) language: selectedLanguage,
mediaAttributes: mediaAttributes)
switch mode { switch mode {
case .new, .replyTo, .quote, .mention, .shareExtension: case .new, .replyTo, .quote, .mention, .shareExtension:
postStatus = try await client.post(endpoint: Statuses.postStatus(json: data)) postStatus = try await client.post(endpoint: Statuses.postStatus(json: data))
@ -658,7 +659,15 @@ public class StatusEditorViewModel: NSObject, ObservableObject {
gifTransferable: nil, gifTransferable: nil,
mediaAttachment: media, mediaAttachment: media,
error: nil) 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))
} }
} }