From bc9bce8896503aef544641a063bbb1cd38643e82 Mon Sep 17 00:00:00 2001
From: Bruno Muniz Azevedo Filho <bruno@elixxir.io>
Date: Mon, 27 Jun 2022 03:24:58 -0300
Subject: [PATCH] Working on file transfers

---
 .../Helpers/CellConfigurator.swift            |  8 ++++++-
 Sources/Integration/Client.swift              |  1 +
 .../Implementations/TransferManager.swift     |  4 +---
 .../Integration/Session/Session+Chat.swift    | 19 ++++++++++++++-
 Sources/Integration/Session/Session.swift     | 24 +++++++++++++++++++
 5 files changed, 51 insertions(+), 5 deletions(-)

diff --git a/Sources/ChatFeature/Helpers/CellConfigurator.swift b/Sources/ChatFeature/Helpers/CellConfigurator.swift
index 70923513..c59050a9 100644
--- a/Sources/ChatFeature/Helpers/CellConfigurator.swift
+++ b/Sources/ChatFeature/Helpers/CellConfigurator.swift
@@ -236,7 +236,13 @@ extension CellFactory {
                 Bubbler.build(imageBubble: cell.leftView, with: item, with: ft)
                 cell.canReply = false
                 cell.performReply = {}
-                cell.leftView.imageView.image = UIImage(data: ft.data!)
+
+                if let data = ft.data {
+                    cell.leftView.imageView.image = UIImage(data: data)
+                } else {
+                    cell.leftView.imageView.image = Asset.transferImagePlaceholder.image
+                }
+
                 return cell
             }
         )
diff --git a/Sources/Integration/Client.swift b/Sources/Integration/Client.swift
index 28ed1a64..7d53fb7e 100644
--- a/Sources/Integration/Client.swift
+++ b/Sources/Integration/Client.swift
@@ -22,6 +22,7 @@ public class Client {
     var messages: AnyPublisher<Message, Never> { messagesSubject.eraseToAnyPublisher() }
     var requests: AnyPublisher<Contact, Never> { requestsSubject.eraseToAnyPublisher() }
     var events: AnyPublisher<BackendEvent, Never> { eventsSubject.eraseToAnyPublisher() }
+    var transfers: AnyPublisher<FileTransfer, Never> { transfersSubject.eraseToAnyPublisher() }
     var requestsSent: AnyPublisher<Contact, Never> { requestsSentSubject.eraseToAnyPublisher() }
     var confirmations: AnyPublisher<Contact, Never> { confirmationsSubject.eraseToAnyPublisher() }
     var groupRequests: AnyPublisher<(Group, [Data], String?), Never> { groupRequestsSubject.eraseToAnyPublisher() }
diff --git a/Sources/Integration/Implementations/TransferManager.swift b/Sources/Integration/Implementations/TransferManager.swift
index 6b161800..8c38a91f 100644
--- a/Sources/Integration/Implementations/TransferManager.swift
+++ b/Sources/Integration/Implementations/TransferManager.swift
@@ -47,9 +47,7 @@ extension BindingsFileTransfer: TransferManagerInterface {
             callback(completed, sent, arrived, total, error)
         }
 
-        guard let file = FileManager.retrieve(name: url.lastPathComponent, type: url.pathExtension) else {
-            fatalError()
-        }
+        guard let file = try? Data(contentsOf: url) else { fatalError() }
 
         return try send(
             url.lastPathComponent,
diff --git a/Sources/Integration/Session/Session+Chat.swift b/Sources/Integration/Session/Session+Chat.swift
index a0835167..34af6aa2 100644
--- a/Sources/Integration/Session/Session+Chat.swift
+++ b/Sources/Integration/Session/Session+Chat.swift
@@ -40,10 +40,12 @@ extension Session {
         DispatchQueue.global().async { [weak self] in
             guard let self = self else { return }
 
-            var tid: Data!
+            var tid: Data?
 
             do {
                 tid = try manager.uploadFile(url: url, to: contact.id) { completed, send, arrived, total, error in
+                    guard let tid = tid else { return }
+
                     if completed {
                         self.endTransferWith(tid: tid)
                     } else {
@@ -55,8 +57,23 @@ extension Session {
                     }
                 }
 
+                guard let tid = tid else { return }
+
                 let content = url.pathExtension == "m4a" ? "a voice message" : "an image"
 
+                let transfer = FileTransfer(
+                    id: tid,
+                    contactId: contact.id,
+                    name: url.deletingPathExtension().lastPathComponent,
+                    type: url.pathExtension,
+                    data: try? Data(contentsOf: url),
+                    progress: 0.0,
+                    isIncoming: false,
+                    createdAt: Date()
+                )
+
+                _ = try? self.dbManager.saveFileTransfer(transfer)
+
                 let message = Message(
                     networkId: nil,
                     senderId: self.client.bindings.myId,
diff --git a/Sources/Integration/Session/Session.swift b/Sources/Integration/Session/Session.swift
index 8758ff86..2899e402 100644
--- a/Sources/Integration/Session/Session.swift
+++ b/Sources/Integration/Session/Session.swift
@@ -404,5 +404,29 @@ public final class Session: SessionType {
                     ))
                 }
             }.store(in: &cancellables)
+
+        client.transfers
+            .sink { [unowned self] in
+                _ = try? dbManager.saveFileTransfer($0)
+
+                let content = $0.type == "m4a" ? "a voice message" : "an image"
+
+                let message = Message(
+                    networkId: $0.id,
+                    senderId: $0.contactId,
+                    recipientId: myId,
+                    groupId: nil,
+                    date: $0.createdAt,
+                    status: .receiving,
+                    isUnread: true,
+                    text: "Sent you \(content)",
+                    replyMessageId: nil,
+                    roundURL: nil,
+                    fileTransferId: $0.id
+                )
+
+                _ = try? dbManager.saveMessage(message)
+            }
+            .store(in: &cancellables)
     }
 }
-- 
GitLab