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 {
instance?.version.hasPrefix("4") == true
}
public var isEditAltTextSupported: Bool {
instance?.version.hasPrefix("4.1") == true
}
private init() {}

View File

@ -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
}
}

View File

@ -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()

View File

@ -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: {

View File

@ -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))
}
}