// // + MessagesDataSource.swift // Wallet // // Created by Saveliy Stavitsky on 2/9/21. // Copyright © 2021 AM. All rights reserved. // import MessageKit import Foundation import UIKit extension CryptoChatControllerChat: MessagesDataSource { func currentSender() -> SenderType { let username = Accounts().current?.name ?? "" return Sender(senderId: username, displayName: username) } func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int { if loadedMsgsCount == 0 { loadedMsgsCount = min(messages.count, 100) } infoView.isHidden = loadedMsgsCount > 0 return loadedMsgsCount } func messageHeaderView(for indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageReusableView { messagesCollectionView.dequeueReusableHeaderView(InfoHeaderCollectionReusableView.self, for: indexPath) } func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType { let messageIndex = self.messages.count - self.loadedMsgsCount + indexPath.section if messageIndex > 0, messageIndex < self.messages.count { return messages[messages.count - loadedMsgsCount + indexPath.section] } else { return messages[0] } } func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? { let msg = messages[messages.count - loadedMsgsCount + indexPath.section] if !msg.isInSyncWithBlockchain { if (msg.timestamp < self.historyService.lastSyncTimestamp) || (msg.isUnsent == true) { return (msg.time.attributed + " • " + L10n.CryptoChat.Chat.notSent.attributed) .applying(attribute: .font, value: FontConvertible.Font.font(style: .regular, size: 12)) .applying(attribute: .foregroundColor, value: Asset.textBrick.color.withAlphaComponent(0.6)) } else { return (msg.time.attributed + " • " + L10n.CryptoChat.Chat.sending.attributed) .applying(attribute: .font, value: FontConvertible.Font.font(style: .regular, size: 12)) .applying(attribute: .foregroundColor, value: Asset.textDeepWater.color.withAlphaComponent(0.4)) } } else { if isFromCurrentSender(message: message) { return msg.time.attributed .applying(attribute: .font, value: FontConvertible.Font.font(style: .regular, size: 12)) .applying(attribute: .foregroundColor, value: Asset.textDeepWater.color.withAlphaComponent(0.4)) } else { return msg.time.attributed .applying(attribute: .font, value: FontConvertible.Font.font(style: .regular, size: 12)) .applying(attribute: .foregroundColor, value: Asset.textPebble.color) } } } func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? { messages[messages.count - loadedMsgsCount + indexPath.section].day.attributed .applying(attribute: .font, value: Font.font(style: .regular, size: 14)) .applying(attribute: .foregroundColor, value: Asset.textGranite.color) } func cellBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? { L10n.CryptoChat.Chat.unreadMsgs.attributed .applying(attribute: .font, value: Font.font(style: .bold, size: 12)) .applying(attribute: .foregroundColor, value: Asset.textDeepWater.color) } }