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

@ -147,15 +147,20 @@ public class Client: ObservableObject, Equatable {
let request = makeURLRequest(url: url, httpMethod: "GET")
return urlSession.webSocketTask(with: request)
}
public func mediaUpload(mimeType: String, data: Data) async throws -> MediaAttachement {
let url = makeURL(endpoint: Media.medias, forceVersion: .v2)
var request = makeURLRequest(url: url, httpMethod: "POST")
public func mediaUpload<Entity: Decodable>(endpoint: Endpoint,
version: Version,
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
request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let httpBody = NSMutableData()
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("\r\n".data(using: .utf8)!)
httpBody.append(data)
@ -163,7 +168,7 @@ public class Client: ObservableObject, Equatable {
request.httpBody = httpBody as Data
let (data, httpResponse) = try await urlSession.data(for: request)
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) {

View file

@ -329,6 +329,11 @@ public class StatusEditorViewModel: ObservableObject {
private func uploadMedia(data: Data) async throws -> MediaAttachement? {
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
}
}
.background(theme.primaryBackgroundColor)
.contextMenu {
StatusRowContextMenu(viewModel: viewModel)
}
@ -200,12 +201,19 @@ public struct StatusRowView: View {
}
if !status.mediaAttachments.isEmpty {
HStack {
if theme.statusDisplayStyle == .compact {
HStack {
StatusMediaPreviewView(attachements: status.mediaAttachments,
sensitive: status.sensitive,
isNotifications: viewModel.isCompact)
Spacer()
}
.padding(.vertical, 4)
} else {
StatusMediaPreviewView(attachements: status.mediaAttachments,
sensitive: status.sensitive,
isNotifications: viewModel.isCompact)
.padding(.vertical, 4)
Spacer()
.padding(.vertical, 4)
}
}
if let card = status.card,