This commit is contained in:
fmodf 2024-07-09 14:37:51 +02:00
parent 103dd130ca
commit 3361b828ef
6 changed files with 120 additions and 14 deletions

View file

@ -7,4 +7,7 @@ enum ConversationAction: Codable {
case setReplyText(String)
case showAttachmentPicker(Bool)
case sendAttachment(Attachment)
case sendAttachmentDone
case sendAttachmentError(reason: String)
}

View file

@ -0,0 +1,22 @@
import Foundation
import GRDB
import Martin
import SwiftUI
enum AttachmentType: Stateable {
case movie
case image
case audio
case file
case location
case contact
}
struct Attachment: Stateable {
let id: String
let type: AttachmentType
let url: URL?
let data: [Data]?
let str: String?
let localPath: URL?
}

View file

@ -126,3 +126,58 @@ final class XMPPService: ObservableObject {
}
}
}
// open class HTTPFileUploadHelper {
//
// private static let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "HTTPFileUploadHelper")
//
// public static func upload(for context: Context, filename: String, inputStream: InputStream, filesize size: Int, mimeType: String, delegate: URLSessionDelegate?, completionHandler: @escaping (Result<URL,ShareError>)->Void) {
// let httpUploadModule = context.module(.httpFileUpload);
// httpUploadModule.findHttpUploadComponent(completionHandler: { result in
// switch result {
// case .success(let components):
// guard let component = components.first(where: { $0.maxSize > size }) else {
// completionHandler(.failure(.fileTooBig));
// return;
// }
// httpUploadModule.requestUploadSlot(componentJid: component.jid, filename: filename, size: size, contentType: mimeType, completionHandler: { result in
// switch result {
// case .success(let slot):
// var request = URLRequest(url: slot.putUri);
// slot.putHeaders.forEach({ (k,v) in
// request.addValue(v, forHTTPHeaderField: k);
// });
// request.httpMethod = "PUT";
// request.httpBodyStream = inputStream;
// request.addValue(String(size), forHTTPHeaderField: "Content-Length");
// request.addValue(mimeType, forHTTPHeaderField: "Content-Type");
// let session = URLSession(configuration: URLSessionConfiguration.default, delegate: delegate, delegateQueue: OperationQueue.main);
// session.dataTask(with: request) { (data, response, error) in
// let code = (response as? HTTPURLResponse)?.statusCode ?? 500;
// guard error == nil && (code == 200 || code == 201) else {
// logger.error("upload of file \(filename) failed, error: \(error as Any), response: \(response as Any)");
// completionHandler(.failure(.httpError));
// return;
// }
// if code == 200 {
// completionHandler(.failure(.invalidResponseCode(url: slot.getUri)));
// } else {
// completionHandler(.success(slot.getUri));
// }
// }.resume();
// case .failure(let error):
// logger.error("upload of file \(filename) failed, upload component returned error: \(error as Any)");
// completionHandler(.failure(.unknownError));
// }
// });
// case .failure(let error):
// completionHandler(.failure(error.errorCondition == .item_not_found ? .notSupported : .unknownError));
// }
// })
// }
//
// public enum UploadResult {
// case success(url: URL, filesize: Int, mimeType: String?)
// case failure(ShareError)
// }
// }

View file

@ -1,14 +1,14 @@
import Foundation
enum Const {
// Network
#if DEBUG
static let baseUrl = "staging.some.com/api"
#else
static let baseUrl = "prod.some.com/api"
#endif
static let requestTimeout = 15.0
static let networkLogging = true
// // Network
// #if DEBUG
// static let baseUrl = "staging.some.com/api"
// #else
// static let baseUrl = "prod.some.com/api"
// #endif
// static let requestTimeout = 15.0
// static let networkLogging = true
// App
static var appVersion: String {
@ -27,4 +27,7 @@ enum Const {
case narayana = "narayana.im"
case conversations = "conversations.im"
}
// Upload/download file folder
static let fileFolder = "ConversationsClassic"
}

View file

@ -1,6 +1,7 @@
import SwiftUI
struct AttachmentContactsPickerView: View {
@EnvironmentObject var store: AppStore
@State private var selectedContact: Roster?
var body: some View {

View file

@ -95,9 +95,15 @@ struct AttachmentMediaPickerView: View {
}
}
.fullScreenCover(isPresented: $showCameraPicker) {
CameraPicker(sourceType: .camera) { _ in
// TODO: Send captures photo/video
print("Image captured")
CameraPicker(sourceType: .camera) { data, type in
store.dispatch(.conversationAction(.sendAttachment(.init(
id: UUID().uuidString,
type: type,
url: nil,
data: [data],
str: nil,
localPath: nil
))))
showCameraPicker = false
}
.edgesIgnoringSafeArea(.all)
@ -355,7 +361,7 @@ struct CameraView: UIViewRepresentable {
struct CameraPicker: UIViewControllerRepresentable {
var sourceType: UIImagePickerController.SourceType
var completionHandler: (UIImage) -> Void
var completionHandler: (Data, AttachmentType) -> Void
func makeUIViewController(context: Context) -> UIImagePickerController {
let picker = UIImagePickerController()
@ -382,8 +388,24 @@ struct CameraPicker: UIViewControllerRepresentable {
}
func imagePickerController(_: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let image = info[.originalImage] as? UIImage {
parent.completionHandler(image)
if let mediaType = info[.mediaType] as? UTType {
switch mediaType {
case .image:
if let image = info[.originalImage] as? UIImage {
let data = image.jpegData(compressionQuality: 1.0) ?? Data()
parent.completionHandler(data, .image)
}
case .movie:
if let url = info[.mediaURL] as? URL {
let data = try? Data(contentsOf: url)
parent.completionHandler(data ?? Data(), .movie)
}
parent.completionHandler(Data(), .movie)
default:
break
}
}
}
}