Signal-iOS/SignalMessaging/ViewModels/ThreadViewModel.swift
Michael Kirk a344a0a2ca Fix crash with old empty threads creationDate is nullable
creationDate didn't always exist - we added it and did not backfill it. We
could backfill with a made up date, e.g. [NSDate
dateWithTimeIntervalSince1970:0], but I'm wary of putting surprising bogus data
in the db. Instead I've using an old date during sorting where it's not
surprising.
2019-05-24 15:21:13 -04:00

62 lines
2.0 KiB
Swift

//
// Copyright (c) 2019 Open Whisper Systems. All rights reserved.
//
import Foundation
@objc
public class ThreadViewModel: NSObject {
@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 contactIdentifier: String?
@objc public let name: String
@objc public let isMuted: Bool
var isContactThread: Bool {
return !isGroupThread
}
@objc public let lastMessageText: String?
@objc public let lastMessageForInbox: TSInteraction?
@objc
public init(thread: TSThread, transaction: SDSAnyReadTransaction) {
self.threadRecord = thread
self.isGroupThread = thread.isGroupThread()
self.name = thread.name()
self.isMuted = thread.isMuted
self.lastMessageText = thread.lastMessageText(transaction: transaction)
let lastInteraction = thread.lastInteractionForInbox(transaction: transaction)
self.lastMessageForInbox = lastInteraction
self.lastMessageDate = lastInteraction?.receivedAtDate()
if let contactThread = thread as? TSContactThread {
self.contactIdentifier = contactThread.contactIdentifier()
} else {
self.contactIdentifier = nil
}
if let threadUniqueId = thread.uniqueId,
let unreadCount = try? InteractionFinder(threadUniqueId: threadUniqueId).unreadCount(transaction: transaction) {
self.unreadCount = unreadCount
} else {
owsFailDebug("unreadCount was unexpectedly nil")
self.unreadCount = 0
}
self.hasUnreadMessages = unreadCount > 0
}
@objc
override public func isEqual(_ object: Any?) -> Bool {
guard let otherThread = object as? ThreadViewModel else {
return super.isEqual(object)
}
return threadRecord.isEqual(otherThread.threadRecord)
}
}