session-ios/SignalUtilitiesKit/Messaging/ThreadViewModel.swift

67 lines
2.2 KiB
Swift
Raw Normal View History

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
@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
@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-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)
let lastInteraction = thread.lastInteractionForInbox(transaction: transaction)
self.lastMessageForInbox = lastInteraction
self.lastMessageDate = lastInteraction?.dateForUI() ?? thread.creationDate
2018-05-05 04:32:29 +02:00
if let contactThread = thread as? TSContactThread {
self.contactSessionID = contactThread.contactSessionID()
2018-05-05 04:32:29 +02:00
} else {
self.contactSessionID = nil
2018-05-05 04:32:29 +02:00
}
if let groupThread = thread as? TSGroupThread {
2021-07-29 02:14:06 +02:00
self.isOnlyNotifyingForMentions = groupThread.isOnlyNotifyingForMentions
} else {
2021-07-29 02:14:06 +02:00
self.isOnlyNotifyingForMentions = false
}
2018-05-05 04:32:29 +02:00
self.unreadCount = thread.unreadMessageCount(transaction: transaction)
self.hasUnreadMessages = unreadCount > 0
self.hasUnreadMentions = thread.unreadMentionMessageCount(with: transaction) > 0
2018-05-05 04:32:29 +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
}