UI fixes on status + prepare for generic media upload

This commit is contained in:
Thomas Ricouard 2023-01-10 08:44:29 +01:00
parent 71ec57f915
commit f50a7f1556
3 changed files with 28 additions and 10 deletions

View file

@ -148,14 +148,19 @@ public class Client: ObservableObject, Equatable {
return urlSession.webSocketTask(with: request) return urlSession.webSocketTask(with: request)
} }
public func mediaUpload(mimeType: String, data: Data) async throws -> MediaAttachement { public func mediaUpload<Entity: Decodable>(endpoint: Endpoint,
let url = makeURL(endpoint: Media.medias, forceVersion: .v2) version: Version,
var request = makeURLRequest(url: url, httpMethod: "POST") method: String,
mimeType: String,
filename: String,
data: Data) async throws -> Entity {
let url = makeURL(endpoint: endpoint, forceVersion: version)
var request = makeURLRequest(url: url, httpMethod: method)
let boundary = UUID().uuidString let boundary = UUID().uuidString
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let httpBody = NSMutableData() let httpBody = NSMutableData()
httpBody.append("--\(boundary)\r\n".data(using: .utf8)!) httpBody.append("--\(boundary)\r\n".data(using: .utf8)!)
httpBody.append("Content-Disposition: form-data; name=\"file\"; filename=\"file.jpg\"\r\n".data(using: .utf8)!) httpBody.append("Content-Disposition: form-data; name=\"\(filename)\"; filename=\"file.jpg\"\r\n".data(using: .utf8)!)
httpBody.append("Content-Type: \(mimeType)\r\n".data(using: .utf8)!) httpBody.append("Content-Type: \(mimeType)\r\n".data(using: .utf8)!)
httpBody.append("\r\n".data(using: .utf8)!) httpBody.append("\r\n".data(using: .utf8)!)
httpBody.append(data) httpBody.append(data)
@ -163,7 +168,7 @@ public class Client: ObservableObject, Equatable {
request.httpBody = httpBody as Data request.httpBody = httpBody as Data
let (data, httpResponse) = try await urlSession.data(for: request) let (data, httpResponse) = try await urlSession.data(for: request)
logResponseOnError(httpResponse: httpResponse, data: data) logResponseOnError(httpResponse: httpResponse, data: data)
return try decoder.decode(MediaAttachement.self, from: data) return try decoder.decode(Entity.self, from: data)
} }
private func logResponseOnError(httpResponse: URLResponse, data: Data) { private func logResponseOnError(httpResponse: URLResponse, data: Data) {

View file

@ -329,6 +329,11 @@ public class StatusEditorViewModel: ObservableObject {
private func uploadMedia(data: Data) async throws -> MediaAttachement? { private func uploadMedia(data: Data) async throws -> MediaAttachement? {
guard let client else { return nil } guard let client else { return nil }
return try await client.mediaUpload(mimeType: "image/jpeg", data: data) return try await client.mediaUpload(endpoint: Media.medias,
version: .v2,
method: "POST",
mimeType: "image/jpeg",
filename: "file",
data: data)
} }
} }

View file

@ -65,6 +65,7 @@ public struct StatusRowView: View {
viewModel.displaySpoiler = false viewModel.displaySpoiler = false
} }
} }
.background(theme.primaryBackgroundColor)
.contextMenu { .contextMenu {
StatusRowContextMenu(viewModel: viewModel) StatusRowContextMenu(viewModel: viewModel)
} }
@ -200,13 +201,20 @@ public struct StatusRowView: View {
} }
if !status.mediaAttachments.isEmpty { if !status.mediaAttachments.isEmpty {
if theme.statusDisplayStyle == .compact {
HStack { HStack {
StatusMediaPreviewView(attachements: status.mediaAttachments, StatusMediaPreviewView(attachements: status.mediaAttachments,
sensitive: status.sensitive, sensitive: status.sensitive,
isNotifications: viewModel.isCompact) isNotifications: viewModel.isCompact)
.padding(.vertical, 4)
Spacer() Spacer()
} }
.padding(.vertical, 4)
} else {
StatusMediaPreviewView(attachements: status.mediaAttachments,
sensitive: status.sensitive,
isNotifications: viewModel.isCompact)
.padding(.vertical, 4)
}
} }
if let card = status.card, if let card = status.card,
viewModel.embededStatus?.url != status.card?.url, viewModel.embededStatus?.url != status.card?.url,