Skip to content
Snippets Groups Projects
Commit 32a363d5 authored by Bruno Muniz's avatar Bruno Muniz :apple:
Browse files

Fixed bubbles and cell configuration

parent 80b177b0
Branches
Tags
2 merge requests!40v1.1.2b166,!38Using new database structure
...@@ -329,26 +329,23 @@ extension GroupChatController: UICollectionViewDataSource { ...@@ -329,26 +329,23 @@ extension GroupChatController: UICollectionViewDataSource {
let canReply: () -> Bool = { item.status == .sent || item.status == .received } let canReply: () -> Bool = { item.status == .sent || item.status == .received }
let performReply: () -> Void = { [weak self] in let performReply: () -> Void = { [weak self] in
//self?.viewModel.didRequestReply(item) self?.viewModel.didRequestReply(item)
} }
let name: (Data) -> String = viewModel.getName(from:) let name: (Data) -> String = viewModel.getName(from:)
let text: (Data) -> String = viewModel.getText(from:)
let showRound: (String?) -> Void = viewModel.showRoundFrom(_:) let showRound: (String?) -> Void = viewModel.showRoundFrom(_:)
let replyContent: (Data) -> (String, String) = viewModel.getReplyContent(for:)
if item.status == .received { if item.status == .received {
if item.replyMessageId != nil { if item.replyMessageId != nil {
let cell: IncomingGroupReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) let cell: IncomingGroupReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
// Bubbler.buildReplyGroup( Bubbler.buildReplyGroup(
// bubble: cell.leftView, bubble: cell.leftView,
// with: item, with: item,
// reply: .init( reply: replyContent(item.replyMessageId!),
// text: text(item.payload.reply!.messageId), sender: name(item.senderId)
// sender: name(item.payload.reply!.senderId) )
// ),
// sender: name(item.sender)
// )
cell.canReply = canReply() cell.canReply = canReply()
cell.performReply = performReply cell.performReply = performReply
...@@ -357,7 +354,12 @@ extension GroupChatController: UICollectionViewDataSource { ...@@ -357,7 +354,12 @@ extension GroupChatController: UICollectionViewDataSource {
return cell return cell
} else { } else {
let cell: IncomingGroupTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) let cell: IncomingGroupTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
// Bubbler.buildGroup(bubble: cell.leftView, with: item, with: name(item.sender)) Bubbler.buildGroup(
bubble: cell.leftView,
with: item,
with: name(item.senderId)
)
cell.canReply = canReply() cell.canReply = canReply()
cell.performReply = performReply cell.performReply = performReply
cell.leftView.didTapShowRound = { showRound(item.roundURL) } cell.leftView.didTapShowRound = { showRound(item.roundURL) }
...@@ -368,15 +370,12 @@ extension GroupChatController: UICollectionViewDataSource { ...@@ -368,15 +370,12 @@ extension GroupChatController: UICollectionViewDataSource {
if item.replyMessageId != nil { if item.replyMessageId != nil {
let cell: OutgoingFailedGroupReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) let cell: OutgoingFailedGroupReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
// Bubbler.buildReplyGroup( Bubbler.buildReplyGroup(
// bubble: cell.rightView, bubble: cell.rightView,
// with: item, with: item,
// reply: .init( reply: replyContent(item.replyMessageId!),
// text: text(item.payload.reply!.messageId), sender: name(item.senderId)
// sender: name(item.payload.reply!.senderId) )
// ),
// sender: name(item.sender)
// )
cell.canReply = canReply() cell.canReply = canReply()
cell.performReply = performReply cell.performReply = performReply
...@@ -385,7 +384,12 @@ extension GroupChatController: UICollectionViewDataSource { ...@@ -385,7 +384,12 @@ extension GroupChatController: UICollectionViewDataSource {
} else { } else {
let cell: OutgoingFailedGroupTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) let cell: OutgoingFailedGroupTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
// Bubbler.buildGroup(bubble: cell.rightView, with: item, with: name(item.sender)) Bubbler.buildGroup(
bubble: cell.rightView,
with: item,
with: name(item.senderId)
)
cell.canReply = canReply() cell.canReply = canReply()
cell.performReply = performReply cell.performReply = performReply
...@@ -395,15 +399,12 @@ extension GroupChatController: UICollectionViewDataSource { ...@@ -395,15 +399,12 @@ extension GroupChatController: UICollectionViewDataSource {
if item.replyMessageId != nil { if item.replyMessageId != nil {
let cell: OutgoingGroupReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) let cell: OutgoingGroupReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
// Bubbler.buildReplyGroup( Bubbler.buildReplyGroup(
// bubble: cell.rightView, bubble: cell.rightView,
// with: item, with: item,
// reply: .init( reply: replyContent(item.replyMessageId!),
// text: text(item.payload.reply!.messageId), sender: name(item.senderId)
// sender: name(item.payload.reply!.senderId) )
// ),
// sender: name(item.sender)
// )
cell.canReply = canReply() cell.canReply = canReply()
cell.performReply = performReply cell.performReply = performReply
...@@ -413,7 +414,12 @@ extension GroupChatController: UICollectionViewDataSource { ...@@ -413,7 +414,12 @@ extension GroupChatController: UICollectionViewDataSource {
} else { } else {
let cell: OutgoingGroupTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) let cell: OutgoingGroupTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
// Bubbler.buildGroup(bubble: cell.rightView, with: item, with: name(item.sender)) Bubbler.buildGroup(
bubble: cell.rightView,
with: item,
with: name(item.senderId)
)
cell.canReply = canReply() cell.canReply = canReply()
cell.performReply = performReply cell.performReply = performReply
cell.rightView.didTapShowRound = { showRound(item.roundURL) } cell.rightView.didTapShowRound = { showRound(item.roundURL) }
...@@ -537,7 +543,7 @@ extension GroupChatController: UICollectionViewDelegate { ...@@ -537,7 +543,7 @@ extension GroupChatController: UICollectionViewDelegate {
} }
let reply = UIAction(title: Localized.Chat.BubbleMenu.reply, state: .off) { [weak self] _ in let reply = UIAction(title: Localized.Chat.BubbleMenu.reply, state: .off) { [weak self] _ in
//self?.viewModel.didRequestReply(item) self?.viewModel.didRequestReply(item)
} }
let delete = UIAction(title: Localized.Chat.BubbleMenu.delete, state: .off) { [weak self] _ in let delete = UIAction(title: Localized.Chat.BubbleMenu.delete, state: .off) { [weak self] _ in
......
...@@ -489,10 +489,9 @@ extension SingleChatController: UICollectionViewDataSource { ...@@ -489,10 +489,9 @@ extension SingleChatController: UICollectionViewDataSource {
cellForItemAt indexPath: IndexPath cellForItemAt indexPath: IndexPath
) -> UICollectionViewCell { ) -> UICollectionViewCell {
let name: (Data) -> String = viewModel.getName(from:)
let text: (Data) -> String = viewModel.getText(from:)
let showRound: (String?) -> Void = viewModel.showRoundFrom(_:) let showRound: (String?) -> Void = viewModel.showRoundFrom(_:)
let item = sections[indexPath.section].elements[indexPath.item] let item = sections[indexPath.section].elements[indexPath.item]
let replyContent: (Data) -> (String, String) = viewModel.getReplyContent(for:)
let performReply: () -> Void = { [weak self] in self?.viewModel.didRequestReply(item) } let performReply: () -> Void = { [weak self] in self?.viewModel.didRequestReply(item) }
let factory = CellFactory.combined(factories: [ let factory = CellFactory.combined(factories: [
...@@ -503,9 +502,9 @@ extension SingleChatController: UICollectionViewDataSource { ...@@ -503,9 +502,9 @@ extension SingleChatController: UICollectionViewDataSource {
.incomingText(performReply: performReply, showRound: showRound), .incomingText(performReply: performReply, showRound: showRound),
.outgoingText(performReply: performReply, showRound: showRound), .outgoingText(performReply: performReply, showRound: showRound),
.outgoingFailedText(performReply: performReply), .outgoingFailedText(performReply: performReply),
.incomingReply(performReply: performReply, name: name, text: text, showRound: showRound), .incomingReply(performReply: performReply, replyContent: replyContent, showRound: showRound),
.outgoingReply(performReply: performReply, name: name, text: text, showRound: showRound), .outgoingReply(performReply: performReply, replyContent: replyContent, showRound: showRound),
.outgoingFailedReply(performReply: performReply, name: name, text: text) .outgoingFailedReply(performReply: performReply, replyContent: replyContent)
]) ])
return factory(item: item, collectionView: collectionView, indexPath: indexPath) return factory(item: item, collectionView: collectionView, indexPath: indexPath)
......
import UIKit import UIKit
import Shared import Shared
import XXModels
final class Bubbler { final class Bubbler {
static func build( static func build(
audioBubble: AudioMessageView, audioBubble: AudioMessageView,
with item: ChatItem with item: Message
) { ) {
audioBubble.dateLabel.text = item.date.asHoursAndMinutes() audioBubble.dateLabel.text = item.date.asHoursAndMinutes()
...@@ -41,7 +42,7 @@ final class Bubbler { ...@@ -41,7 +42,7 @@ final class Bubbler {
static func build( static func build(
imageBubble: ImageMessageView, imageBubble: ImageMessageView,
with item: ChatItem with item: Message
) { ) {
// let progress = item.payload.attachment!.progress // let progress = item.payload.attachment!.progress
// imageBubble.progressLabel.text = String(format: "%.1f%%", progress * 100) // imageBubble.progressLabel.text = String(format: "%.1f%%", progress * 100)
...@@ -80,9 +81,9 @@ final class Bubbler { ...@@ -80,9 +81,9 @@ final class Bubbler {
static func build( static func build(
bubble: StackMessageView, bubble: StackMessageView,
with item: ChatItem with item: Message
) { ) {
bubble.textView.text = item.payload.text bubble.textView.text = item.text
bubble.senderLabel.removeFromSuperview() bubble.senderLabel.removeFromSuperview()
bubble.dateLabel.text = item.date.asHoursAndMinutes() bubble.dateLabel.text = item.date.asHoursAndMinutes()
...@@ -135,14 +136,14 @@ final class Bubbler { ...@@ -135,14 +136,14 @@ final class Bubbler {
static func buildReply( static func buildReply(
bubble: ReplyStackMessageView, bubble: ReplyStackMessageView,
with item: ChatItem, with item: Message,
reply: ReplyModel reply: (contactTitle: String, messageText: String)
) { ) {
bubble.dateLabel.text = item.date.asHoursAndMinutes() bubble.dateLabel.text = item.date.asHoursAndMinutes()
bubble.textView.text = item.payload.text bubble.textView.text = item.text
bubble.replyView.message.text = reply.text bubble.replyView.message.text = reply.messageText
bubble.replyView.title.text = reply.sender bubble.replyView.title.text = reply.contactTitle
let roundButtonColor: UIColor let roundButtonColor: UIColor
...@@ -195,4 +196,112 @@ final class Bubbler { ...@@ -195,4 +196,112 @@ final class Bubbler {
) )
bubble.roundButton.setAttributedTitle(attrString, for: .normal) bubble.roundButton.setAttributedTitle(attrString, for: .normal)
} }
static func buildReplyGroup(
bubble: ReplyStackMessageView,
with item: Message,
reply: (contactTitle: String, messageText: String),
sender: String
) {
bubble.dateLabel.text = item.date.asHoursAndMinutes()
bubble.textView.text = item.text
bubble.replyView.message.text = reply.messageText
bubble.replyView.title.text = reply.contactTitle
let roundButtonColor: UIColor
switch item.status {
case .received, .receiving:
bubble.senderLabel.text = sender
bubble.backgroundColor = Asset.neutralWhite.color
bubble.textView.textColor = Asset.neutralActive.color
bubble.dateLabel.textColor = Asset.neutralDisabled.color
roundButtonColor = Asset.neutralDisabled.color
bubble.replyView.container.backgroundColor = Asset.brandDefault.color
bubble.replyView.space.backgroundColor = Asset.brandPrimary.color
bubble.lockerImageView.removeFromSuperview()
bubble.revertBottomStackOrder()
case .sendingFailed, .sendingTimedOut:
bubble.senderLabel.removeFromSuperview()
bubble.backgroundColor = Asset.accentDanger.color
bubble.textView.textColor = Asset.neutralWhite.color
bubble.dateLabel.textColor = Asset.neutralWhite.color
roundButtonColor = Asset.neutralWhite.color
bubble.replyView.space.backgroundColor = Asset.neutralWhite.color
bubble.replyView.container.backgroundColor = Asset.brandLight.color
case .sent, .sending:
bubble.senderLabel.removeFromSuperview()
bubble.textView.textColor = Asset.neutralWhite.color
bubble.backgroundColor = Asset.brandBubble.color
bubble.dateLabel.textColor = Asset.neutralWhite.color
roundButtonColor = Asset.neutralWhite.color
bubble.replyView.space.backgroundColor = Asset.neutralWhite.color
bubble.replyView.container.backgroundColor = Asset.brandLight.color
case .receivingFailed:
fatalError()
}
let attrString = NSAttributedString(
string: "show mix",
attributes: [
.underlineStyle: NSUnderlineStyle.single.rawValue,
.underlineColor: roundButtonColor,
.foregroundColor: roundButtonColor,
.font: Fonts.Mulish.regular.font(size: 12.0) as Any
]
)
bubble.roundButton.setAttributedTitle(attrString, for: .normal)
}
static func buildGroup(
bubble: StackMessageView,
with item: Message,
with senderName: String
) {
bubble.textView.text = item.text
bubble.dateLabel.text = item.date.asHoursAndMinutes()
let roundButtonColor: UIColor
switch item.status {
case .received, .receiving:
bubble.senderLabel.text = senderName
bubble.backgroundColor = Asset.neutralWhite.color
bubble.textView.textColor = Asset.neutralActive.color
bubble.dateLabel.textColor = Asset.neutralDisabled.color
roundButtonColor = Asset.neutralDisabled.color
bubble.lockerImageView.removeFromSuperview()
bubble.revertBottomStackOrder()
case .sendingFailed, .sendingTimedOut:
bubble.senderLabel.removeFromSuperview()
bubble.backgroundColor = Asset.accentDanger.color
bubble.textView.textColor = Asset.neutralWhite.color
bubble.dateLabel.textColor = Asset.neutralWhite.color
roundButtonColor = Asset.neutralWhite.color
case .sent, .sending:
bubble.senderLabel.removeFromSuperview()
bubble.backgroundColor = Asset.brandBubble.color
bubble.textView.textColor = Asset.neutralWhite.color
bubble.dateLabel.textColor = Asset.neutralWhite.color
roundButtonColor = Asset.neutralWhite.color
case .receivingFailed:
fatalError()
}
let attrString = NSAttributedString(
string: "show mix",
attributes: [
.underlineStyle: NSUnderlineStyle.single.rawValue,
.underlineColor: roundButtonColor,
.foregroundColor: roundButtonColor,
.font: Fonts.Mulish.regular.font(size: 12.0) as Any
]
)
bubble.roundButton.setAttributedTitle(attrString, for: .normal)
}
} }
import UIKit import UIKit
import Shared import Shared
import Combine import Combine
import XXModels
import Voxophone import Voxophone
import AVFoundation import AVFoundation
struct CellFactory { struct CellFactory {
var canBuild: (ChatItem) -> Bool var canBuild: (Message) -> Bool
var build: (ChatItem, UICollectionView, IndexPath) -> UICollectionViewCell var build: (Message, UICollectionView, IndexPath) -> UICollectionViewCell
func callAsFunction( func callAsFunction(
item: ChatItem, item: Message,
collectionView: UICollectionView, collectionView: UICollectionView,
indexPath: IndexPath indexPath: IndexPath
) -> UICollectionViewCell { ) -> UICollectionViewCell {
...@@ -236,14 +237,13 @@ extension CellFactory { ...@@ -236,14 +237,13 @@ extension CellFactory {
extension CellFactory { extension CellFactory {
static func outgoingReply( static func outgoingReply(
performReply: @escaping () -> Void, performReply: @escaping () -> Void,
name: @escaping (Data) -> String, replyContent: @escaping (Data) -> (String, String),
text: @escaping (Data) -> String,
showRound: @escaping (String?) -> Void showRound: @escaping (String?) -> Void
) -> Self { ) -> Self {
.init( .init(
canBuild: { item in canBuild: { item in
(item.status == .sent || item.status == .sending) (item.status == .sent || item.status == .sending)
&& item.payload.reply != nil && item.replyMessageId != nil
}, build: { item, collectionView, indexPath in }, build: { item, collectionView, indexPath in
let cell: OutgoingReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) let cell: OutgoingReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
...@@ -251,10 +251,7 @@ extension CellFactory { ...@@ -251,10 +251,7 @@ extension CellFactory {
Bubbler.buildReply( Bubbler.buildReply(
bubble: cell.rightView, bubble: cell.rightView,
with: item, with: item,
reply: .init( reply: replyContent(item.replyMessageId!)
text: text(item.payload.reply!.messageId),
sender: name(item.payload.reply!.senderId)
)
) )
cell.canReply = item.status == .sent cell.canReply = item.status == .sent
...@@ -267,14 +264,13 @@ extension CellFactory { ...@@ -267,14 +264,13 @@ extension CellFactory {
static func incomingReply( static func incomingReply(
performReply: @escaping () -> Void, performReply: @escaping () -> Void,
name: @escaping (Data) -> String, replyContent: @escaping (Data) -> (String, String),
text: @escaping (Data) -> String,
showRound: @escaping (String?) -> Void showRound: @escaping (String?) -> Void
) -> Self { ) -> Self {
.init( .init(
canBuild: { item in canBuild: { item in
item.status == .received item.status == .received
&& item.payload.reply != nil && item.replyMessageId != nil
}, build: { item, collectionView, indexPath in }, build: { item, collectionView, indexPath in
let cell: IncomingReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) let cell: IncomingReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
...@@ -282,10 +278,7 @@ extension CellFactory { ...@@ -282,10 +278,7 @@ extension CellFactory {
Bubbler.buildReply( Bubbler.buildReply(
bubble: cell.leftView, bubble: cell.leftView,
with: item, with: item,
reply: .init( reply: replyContent(item.replyMessageId!)
text: text(item.payload.reply!.messageId),
sender: name(item.payload.reply!.senderId)
)
) )
cell.canReply = item.status == .received cell.canReply = item.status == .received
cell.performReply = performReply cell.performReply = performReply
...@@ -298,13 +291,12 @@ extension CellFactory { ...@@ -298,13 +291,12 @@ extension CellFactory {
static func outgoingFailedReply( static func outgoingFailedReply(
performReply: @escaping () -> Void, performReply: @escaping () -> Void,
name: @escaping (Data) -> String, replyContent: @escaping (Data) -> (String, String)
text: @escaping (Data) -> String
) -> Self { ) -> Self {
.init( .init(
canBuild: { item in canBuild: { item in
(item.status == .sendingFailed || item.status == .sendingTimedOut) (item.status == .sendingFailed || item.status == .sendingTimedOut)
&& item.payload.reply != nil && item.replyMessageId != nil
}, build: { item, collectionView, indexPath in }, build: { item, collectionView, indexPath in
let cell: OutgoingFailedReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) let cell: OutgoingFailedReplyCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
...@@ -312,10 +304,7 @@ extension CellFactory { ...@@ -312,10 +304,7 @@ extension CellFactory {
Bubbler.buildReply( Bubbler.buildReply(
bubble: cell.rightView, bubble: cell.rightView,
with: item, with: item,
reply: .init( reply: replyContent(item.replyMessageId!)
text: text(item.payload.reply!.messageId),
sender: name(item.payload.reply!.senderId)
)
) )
cell.canReply = false cell.canReply = false
...@@ -334,7 +323,7 @@ extension CellFactory { ...@@ -334,7 +323,7 @@ extension CellFactory {
.init( .init(
canBuild: { item in canBuild: { item in
item.status == .received item.status == .received
&& item.payload.reply == nil && item.replyMessageId == nil
}, build: { item, collectionView, indexPath in }, build: { item, collectionView, indexPath in
let cell: IncomingTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) let cell: IncomingTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
...@@ -356,7 +345,7 @@ extension CellFactory { ...@@ -356,7 +345,7 @@ extension CellFactory {
.init( .init(
canBuild: { item in canBuild: { item in
(item.status == .sending || item.status == .sent) (item.status == .sending || item.status == .sent)
&& item.payload.reply == nil && item.replyMessageId == nil
}, build: { item, collectionView, indexPath in }, build: { item, collectionView, indexPath in
let cell: OutgoingTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) let cell: OutgoingTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
...@@ -375,7 +364,7 @@ extension CellFactory { ...@@ -375,7 +364,7 @@ extension CellFactory {
.init( .init(
canBuild: { item in canBuild: { item in
(item.status == .sendingFailed || item.status == .sendingTimedOut) (item.status == .sendingFailed || item.status == .sendingTimedOut)
&& item.payload.reply == nil && item.replyMessageId == nil
}, build: { item, collectionView, indexPath in }, build: { item, collectionView, indexPath in
let cell: OutgoingFailedTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath) let cell: OutgoingFailedTextCell = collectionView.dequeueReusableCell(forIndexPath: indexPath)
...@@ -412,9 +401,9 @@ struct ActionFactory { ...@@ -412,9 +401,9 @@ struct ActionFactory {
} }
static func build( static func build(
from item: ChatItem, from item: Message,
action: Action, action: Action,
closure: @escaping (ChatItem) -> Void closure: @escaping (Message) -> Void
) -> UIAction? { ) -> UIAction? {
switch action { switch action {
......
...@@ -88,6 +88,19 @@ final class GroupChatViewModel { ...@@ -88,6 +88,19 @@ final class GroupChatViewModel {
stagedReply = nil stagedReply = nil
} }
func getReplyContent(for messageId: Data) -> (String, String) {
guard let message = try? session.dbManager.fetchMessages(.init(networkId: messageId)).first else {
return ("[DELETED]", "[DELETED]")
}
guard let contact = try? session.dbManager.fetchContacts(.init(id: [message.senderId])).first else {
return ("You", message.text)
}
let contactTitle = (contact.nickname ?? contact.username) ?? "Fetching username..."
return (contactTitle, message.text)
}
func getName(from senderId: Data) -> String { func getName(from senderId: Data) -> String {
guard let contact = try? session.dbManager.fetchContacts(.init(id: [senderId])).first else { guard let contact = try? session.dbManager.fetchContacts(.init(id: [senderId])).first else {
return "You" return "You"
...@@ -96,17 +109,22 @@ final class GroupChatViewModel { ...@@ -96,17 +109,22 @@ final class GroupChatViewModel {
return (contact.nickname ?? contact.username) ?? "Fetching username..." return (contact.nickname ?? contact.username) ?? "Fetching username..."
} }
func getText(from messageId: Data) -> String { func didRequestReply(_ message: Message) {
guard let message = try? session.dbManager.fetchMessages(.init(networkId: messageId)).first else { guard let networkId = message.networkId else { return }
return "[DELETED]"
let senderTitle: String = {
if message.senderId == session.myId {
return "You"
} else {
guard let contact = try? session.dbManager.fetchContacts(.init(id: [message.senderId])).first else {
return "Error"
} }
return message.text return (contact.nickname ?? contact.username) ?? "Fetching username..."
} }
}()
func didRequestReply(_ message: Message) {
guard let networkId = message.networkId else { return }
stagedReply = Reply(messageId: networkId, senderId: message.senderId) stagedReply = Reply(messageId: networkId, senderId: message.senderId)
replySubject.send((getName(from: message.senderId), message.text)) replySubject.send((senderTitle, message.text))
} }
} }
...@@ -181,6 +181,8 @@ final class SingleChatViewModel { ...@@ -181,6 +181,8 @@ final class SingleChatViewModel {
} }
func didRequestReply(_ message: Message) { func didRequestReply(_ message: Message) {
guard let networkId = message.networkId else { return }
let senderTitle: String = { let senderTitle: String = {
if message.senderId == session.myId { if message.senderId == session.myId {
return "You" return "You"
...@@ -190,15 +192,20 @@ final class SingleChatViewModel { ...@@ -190,15 +192,20 @@ final class SingleChatViewModel {
}() }()
replySubject.send((senderTitle, message.text)) replySubject.send((senderTitle, message.text))
stagedReply = Reply(messageId: message.networkId!, senderId: message.senderId) stagedReply = Reply(messageId: networkId, senderId: message.senderId)
} }
func getText(from messageId: Data) -> String { func getReplyContent(for messageId: Data) -> (String, String) {
guard let message = try? session.dbManager.fetchMessages(.init(networkId: messageId)).first else { guard let message = try? session.dbManager.fetchMessages(.init(networkId: messageId)).first else {
return "[DELETED]" return ("[DELETED]", "[DELETED]")
}
guard let contact = try? session.dbManager.fetchContacts(.init(id: [message.senderId])).first else {
return ("You", message.text)
} }
return message.text let contactTitle = (contact.nickname ?? contact.username) ?? "Fetching username..."
return (contactTitle, message.text)
} }
func showRoundFrom(_ roundURL: String?) { func showRoundFrom(_ roundURL: String?) {
...@@ -217,10 +224,6 @@ final class SingleChatViewModel { ...@@ -217,10 +224,6 @@ final class SingleChatViewModel {
sectionsRelay.value.flatMap(\.elements).first(where: { $0.id == id }) sectionsRelay.value.flatMap(\.elements).first(where: { $0.id == id })
} }
func getName(from senderId: Data) -> String {
senderId == session.myId ? "You" : contact.nickname ?? contact.username!
}
func itemAt(indexPath: IndexPath) -> Message? { func itemAt(indexPath: IndexPath) -> Message? {
guard sectionsRelay.value.count > indexPath.section else { return nil } guard sectionsRelay.value.count > indexPath.section else { return nil }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment