2018-05-05 04:32:29 +02:00
|
|
|
//
|
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
@objc
|
|
|
|
public class ThreadViewModel: NSObject {
|
2018-05-25 22:51:40 +02:00
|
|
|
@objc public let hasUnreadMessages: Bool
|
|
|
|
@objc public let lastMessageDate: Date
|
|
|
|
@objc public let isGroupThread: Bool
|
|
|
|
@objc public let threadRecord: TSThread
|
|
|
|
@objc public let unreadCount: UInt
|
2021-05-05 02:00:39 +02:00
|
|
|
@objc public let contactSessionID: String?
|
2018-05-25 22:51:40 +02:00
|
|
|
@objc public let name: String
|
|
|
|
@objc public let isMuted: Bool
|
2021-11-17 05:51:53 +01:00
|
|
|
@objc public let isPinned: Bool
|
2021-07-29 02:14:06 +02:00
|
|
|
@objc public let isOnlyNotifyingForMentions: Bool
|
2021-08-30 05:22:40 +02:00
|
|
|
@objc public let hasUnreadMentions: Bool
|
2018-07-05 19:54:10 +02:00
|
|
|
|
2018-05-05 04:32:29 +02:00
|
|
|
var isContactThread: Bool {
|
|
|
|
return !isGroupThread
|
|
|
|
}
|
|
|
|
|
2018-05-25 22:51:40 +02:00
|
|
|
@objc public let lastMessageText: String?
|
2018-07-05 19:54:10 +02:00
|
|
|
@objc public let lastMessageForInbox: TSInteraction?
|
2018-05-05 04:32:29 +02:00
|
|
|
|
2018-05-25 22:51:40 +02:00
|
|
|
@objc
|
|
|
|
public init(thread: TSThread, transaction: YapDatabaseReadTransaction) {
|
2018-05-05 04:32:29 +02:00
|
|
|
self.threadRecord = thread
|
2018-10-03 23:41:43 +02:00
|
|
|
|
2018-05-05 04:32:29 +02:00
|
|
|
self.isGroupThread = thread.isGroupThread()
|
2022-01-18 03:35:53 +01:00
|
|
|
self.name = thread.name(with: transaction)
|
2018-05-05 04:32:29 +02:00
|
|
|
self.isMuted = thread.isMuted
|
2021-11-17 05:51:53 +01:00
|
|
|
self.isPinned = thread.isPinned
|
2018-05-05 04:32:29 +02:00
|
|
|
self.lastMessageText = thread.lastMessageText(transaction: transaction)
|
2018-10-03 23:41:43 +02:00
|
|
|
let lastInteraction = thread.lastInteractionForInbox(transaction: transaction)
|
|
|
|
self.lastMessageForInbox = lastInteraction
|
2021-04-19 03:36:05 +02:00
|
|
|
self.lastMessageDate = lastInteraction?.dateForUI() ?? thread.creationDate
|
2018-05-05 04:32:29 +02:00
|
|
|
|
|
|
|
if let contactThread = thread as? TSContactThread {
|
2021-05-05 02:00:39 +02:00
|
|
|
self.contactSessionID = contactThread.contactSessionID()
|
2018-05-05 04:32:29 +02:00
|
|
|
} else {
|
2021-05-05 02:00:39 +02:00
|
|
|
self.contactSessionID = nil
|
2018-05-05 04:32:29 +02:00
|
|
|
}
|
2021-07-26 07:42:45 +02:00
|
|
|
|
|
|
|
if let groupThread = thread as? TSGroupThread {
|
2021-07-29 02:14:06 +02:00
|
|
|
self.isOnlyNotifyingForMentions = groupThread.isOnlyNotifyingForMentions
|
2021-07-26 07:42:45 +02:00
|
|
|
} else {
|
2021-07-29 02:14:06 +02:00
|
|
|
self.isOnlyNotifyingForMentions = false
|
2021-07-26 07:42:45 +02:00
|
|
|
}
|
2018-05-05 04:32:29 +02:00
|
|
|
|
|
|
|
self.unreadCount = thread.unreadMessageCount(transaction: transaction)
|
|
|
|
self.hasUnreadMessages = unreadCount > 0
|
2022-03-11 05:48:12 +01:00
|
|
|
self.hasUnreadMentions = thread.unreadMentionMessageCount(with: transaction) > 0
|
2018-05-05 04:32:29 +02:00
|
|
|
}
|
2018-06-08 00:05:53 +02:00
|
|
|
|
|
|
|
@objc
|
|
|
|
override public func isEqual(_ object: Any?) -> Bool {
|
|
|
|
guard let otherThread = object as? ThreadViewModel else {
|
|
|
|
return super.isEqual(object)
|
|
|
|
}
|
|
|
|
|
|
|
|
return threadRecord.isEqual(otherThread.threadRecord)
|
|
|
|
}
|
2018-05-05 04:32:29 +02:00
|
|
|
}
|