From 54914d69c6ba746b2b8818488ba9aadbbf4a316f Mon Sep 17 00:00:00 2001 From: ryanzhao Date: Wed, 26 Oct 2022 17:16:23 +1100 Subject: [PATCH] WIP: fetch disappearing message config from database --- Session/Conversations/ConversationVC.swift | 6 ++-- .../Conversations/ConversationViewModel.swift | 1 + .../ConversationTitleView.swift | 31 ++++++++++++++++--- .../DisappearingMessageConfiguration.swift | 4 +-- .../SessionThreadViewModel.swift | 18 ++++++++++- 5 files changed, 51 insertions(+), 9 deletions(-) diff --git a/Session/Conversations/ConversationVC.swift b/Session/Conversations/ConversationVC.swift index 8bb622e84..e2a61e723 100644 --- a/Session/Conversations/ConversationVC.swift +++ b/Session/Conversations/ConversationVC.swift @@ -556,7 +556,8 @@ final class ConversationVC: BaseVC, ConversationSearchControllerDelegate, UITabl viewModel.threadData.threadIsNoteToSelf != updatedThreadData.threadIsNoteToSelf || viewModel.threadData.threadMutedUntilTimestamp != updatedThreadData.threadMutedUntilTimestamp || viewModel.threadData.threadOnlyNotifyForMentions != updatedThreadData.threadOnlyNotifyForMentions || - viewModel.threadData.userCount != updatedThreadData.userCount + viewModel.threadData.userCount != updatedThreadData.userCount || + viewModel.threadData.disappearingMessagesConfiguration != updatedThreadData.disappearingMessagesConfiguration { titleView.update( with: updatedThreadData.displayName, @@ -564,7 +565,8 @@ final class ConversationVC: BaseVC, ConversationSearchControllerDelegate, UITabl threadVariant: updatedThreadData.threadVariant, mutedUntilTimestamp: updatedThreadData.threadMutedUntilTimestamp, onlyNotifyForMentions: (updatedThreadData.threadOnlyNotifyForMentions == true), - userCount: updatedThreadData.userCount + userCount: updatedThreadData.userCount, + disappearingMessagesConfig: updatedThreadData.disappearingMessagesConfiguration ) } diff --git a/Session/Conversations/ConversationViewModel.swift b/Session/Conversations/ConversationViewModel.swift index cdb434a40..f822f7096 100644 --- a/Session/Conversations/ConversationViewModel.swift +++ b/Session/Conversations/ConversationViewModel.swift @@ -137,6 +137,7 @@ public class ConversationViewModel: OWSAudioPlayerDelegate { .conversationQuery(threadId: threadId, userPublicKey: userPublicKey) .fetchOne(db) + return threadViewModel .map { $0.with(recentReactionEmoji: recentReactionEmoji) } } diff --git a/Session/Conversations/Views & Modals/ConversationTitleView.swift b/Session/Conversations/Views & Modals/ConversationTitleView.swift index fef986a5d..7b015582e 100644 --- a/Session/Conversations/Views & Modals/ConversationTitleView.swift +++ b/Session/Conversations/Views & Modals/ConversationTitleView.swift @@ -110,7 +110,8 @@ final class ConversationTitleView: UIView { threadVariant: threadVariant, mutedUntilTimestamp: nil, onlyNotifyForMentions: false, - userCount: (threadVariant != .contact ? 0 : nil) + userCount: (threadVariant != .contact ? 0 : nil), + disappearingMessagesConfig: nil ) } @@ -138,7 +139,8 @@ final class ConversationTitleView: UIView { threadVariant: SessionThread.Variant, mutedUntilTimestamp: TimeInterval?, onlyNotifyForMentions: Bool, - userCount: Int? + userCount: Int?, + disappearingMessagesConfig: DisappearingMessagesConfiguration? ) { guard Thread.isMainThread else { DispatchQueue.main.async { [weak self] in @@ -148,7 +150,8 @@ final class ConversationTitleView: UIView { threadVariant: threadVariant, mutedUntilTimestamp: mutedUntilTimestamp, onlyNotifyForMentions: onlyNotifyForMentions, - userCount: userCount + userCount: userCount, + disappearingMessagesConfig: disappearingMessagesConfig ) } return @@ -157,7 +160,8 @@ final class ConversationTitleView: UIView { let shouldHaveSubtitle: Bool = ( Date().timeIntervalSince1970 <= (mutedUntilTimestamp ?? 0) || onlyNotifyForMentions || - userCount != nil + userCount != nil || + disappearingMessagesConfig?.isEnabled == true ) self.titleLabel.text = name @@ -219,6 +223,25 @@ final class ConversationTitleView: UIView { } // TODO: Disappearing message settings + if let config = disappearingMessagesConfig, config.isEnabled == true { + let imageAttachment = NSTextAttachment() + imageAttachment.image = UIImage(named: "ic_timer")?.withTint(textPrimary) + imageAttachment.bounds = CGRect( + x: 0, + y: -2, + width: Values.smallFontSize, + height: Values.smallFontSize + ) + + self?.notificationSettingsLabel.attributedText = NSAttributedString(attachment: imageAttachment) + .appending(string: " ") + .appending(string: config.type == .disappearAfterRead ? "DISAPPERING_MESSAGES_TYPE_AFTER_READ_TITLE".localized() : "DISAPPERING_MESSAGES_TYPE_AFTER_SEND_TITLE".localized()) + .appending(string: " - ") + .appending(string: config.durationString) + self?.disappearingMessageSettingLabel.isHidden = false + slides.append(self?.disappearingMessageSettingLabel) + } + self?.pagedScrollView.update( with: slides.compactMap{ $0 }, slideSize: CGSize( diff --git a/SessionMessagingKit/Database/Models/DisappearingMessageConfiguration.swift b/SessionMessagingKit/Database/Models/DisappearingMessageConfiguration.swift index 369407960..9221a8d15 100644 --- a/SessionMessagingKit/Database/Models/DisappearingMessageConfiguration.swift +++ b/SessionMessagingKit/Database/Models/DisappearingMessageConfiguration.swift @@ -4,7 +4,7 @@ import Foundation import GRDB import SessionUtilitiesKit -public struct DisappearingMessagesConfiguration: Codable, Identifiable, Equatable, FetchableRecord, PersistableRecord, TableRecord, ColumnExpressible { +public struct DisappearingMessagesConfiguration: Codable, Identifiable, Equatable, Hashable, FetchableRecord, PersistableRecord, TableRecord, ColumnExpressible { public static var databaseTableName: String { "disappearingMessagesConfiguration" } internal static let threadForeignKey = ForeignKey([Columns.threadId], to: [SessionThread.Columns.id]) private static let thread = belongsTo(SessionThread.self, using: threadForeignKey) @@ -27,7 +27,7 @@ public struct DisappearingMessagesConfiguration: Codable, Identifiable, Equatabl public let threadId: String public let isEnabled: Bool public let durationSeconds: TimeInterval - public var type: DisappearingMessageType? = nil + public var type: DisappearingMessageType? // MARK: - Relationships diff --git a/SessionMessagingKit/Shared Models/SessionThreadViewModel.swift b/SessionMessagingKit/Shared Models/SessionThreadViewModel.swift index 2280a8542..7a2e111a1 100644 --- a/SessionMessagingKit/Shared Models/SessionThreadViewModel.swift +++ b/SessionMessagingKit/Shared Models/SessionThreadViewModel.swift @@ -32,6 +32,7 @@ public struct SessionThreadViewModel: FetchableRecordWithRowId, Decodable, Equat public static let threadContactIsTypingKey: SQL = SQL(stringLiteral: CodingKeys.threadContactIsTyping.stringValue) public static let threadUnreadCountKey: SQL = SQL(stringLiteral: CodingKeys.threadUnreadCount.stringValue) public static let threadUnreadMentionCountKey: SQL = SQL(stringLiteral: CodingKeys.threadUnreadMentionCount.stringValue) + public static let disappearingMessagesConfigurationKey: SQL = SQL(stringLiteral: CodingKeys.disappearingMessagesConfiguration.stringValue) public static let contactProfileKey: SQL = SQL(stringLiteral: CodingKeys.contactProfile.stringValue) public static let closedGroupNameKey: SQL = SQL(stringLiteral: CodingKeys.closedGroupName.stringValue) public static let closedGroupUserCountKey: SQL = SQL(stringLiteral: CodingKeys.closedGroupUserCount.stringValue) @@ -106,6 +107,8 @@ public struct SessionThreadViewModel: FetchableRecordWithRowId, Decodable, Equat // Thread display info + public let disappearingMessagesConfiguration: DisappearingMessagesConfiguration? + private let contactProfile: Profile? private let closedGroupProfileFront: Profile? private let closedGroupProfileBack: Profile? @@ -239,7 +242,8 @@ public extension SessionThreadViewModel { threadIsNoteToSelf: Bool = false, contactProfile: Profile? = nil, currentUserIsClosedGroupMember: Bool? = nil, - unreadCount: UInt = 0 + unreadCount: UInt = 0, + disappearingMessagesConfiguration: DisappearingMessagesConfiguration? = nil ) { self.rowId = -1 self.threadId = (threadId ?? SessionThreadViewModel.invalidId) @@ -263,6 +267,8 @@ public extension SessionThreadViewModel { // Thread display info + self.disappearingMessagesConfiguration = disappearingMessagesConfiguration + self.contactProfile = contactProfile self.closedGroupProfileFront = nil self.closedGroupProfileBack = nil @@ -323,6 +329,7 @@ public extension SessionThreadViewModel { threadContactIsTyping: self.threadContactIsTyping, threadUnreadCount: self.threadUnreadCount, threadUnreadMentionCount: self.threadUnreadMentionCount, + disappearingMessagesConfiguration: self.disappearingMessagesConfiguration, contactProfile: self.contactProfile, closedGroupProfileFront: self.closedGroupProfileFront, closedGroupProfileBack: self.closedGroupProfileBack, @@ -376,6 +383,7 @@ public extension SessionThreadViewModel { threadContactIsTyping: self.threadContactIsTyping, threadUnreadCount: self.threadUnreadCount, threadUnreadMentionCount: self.threadUnreadMentionCount, + disappearingMessagesConfiguration: self.disappearingMessagesConfiguration, contactProfile: self.contactProfile, closedGroupProfileFront: self.closedGroupProfileFront, closedGroupProfileBack: self.closedGroupProfileBack, @@ -703,6 +711,7 @@ public extension SessionThreadViewModel { /// but including this warning just in case there is a discrepancy) static func conversationQuery(threadId: String, userPublicKey: String) -> AdaptedFetchRequest> { let thread: TypedTableAlias = TypedTableAlias() + let disappearingMessagesConfiguration: TypedTableAlias = TypedTableAlias() let contact: TypedTableAlias = TypedTableAlias() let closedGroup: TypedTableAlias = TypedTableAlias() let groupMember: TypedTableAlias = TypedTableAlias() @@ -745,6 +754,8 @@ public extension SessionThreadViewModel { \(thread[.messageDraft]) AS \(ViewModel.threadMessageDraftKey), \(Interaction.self).\(ViewModel.threadUnreadCountKey), + + \(ViewModel.disappearingMessagesConfigurationKey).*, \(ViewModel.contactProfileKey).*, \(closedGroup[.name]) AS \(ViewModel.closedGroupNameKey), @@ -761,6 +772,7 @@ public extension SessionThreadViewModel { \(SQL("\(userPublicKey)")) AS \(ViewModel.currentUserPublicKeyKey) FROM \(SessionThread.self) + LEFT JOIN \(DisappearingMessagesConfiguration.self) ON \(disappearingMessagesConfiguration[.threadId]) = \(thread[.id]) LEFT JOIN \(Contact.self) ON \(contact[.id]) = \(thread[.id]) LEFT JOIN ( -- Fetch all interaction-specific data in a subquery to be more efficient @@ -811,6 +823,7 @@ public extension SessionThreadViewModel { static func conversationSettingsQuery(threadId: String, userPublicKey: String) -> AdaptedFetchRequest> { let thread: TypedTableAlias = TypedTableAlias() + let disappearingMessagesConfiguration: TypedTableAlias = TypedTableAlias() let contact: TypedTableAlias = TypedTableAlias() let closedGroup: TypedTableAlias = TypedTableAlias() let groupMember: TypedTableAlias = TypedTableAlias() @@ -838,6 +851,8 @@ public extension SessionThreadViewModel { \(contact[.isBlocked]) AS \(ViewModel.threadIsBlockedKey), \(thread[.mutedUntilTimestamp]) AS \(ViewModel.threadMutedUntilTimestampKey), \(thread[.onlyNotifyForMentions]) AS \(ViewModel.threadOnlyNotifyForMentionsKey), + + \(ViewModel.disappearingMessagesConfigurationKey).*, \(ViewModel.contactProfileKey).*, \(ViewModel.closedGroupProfileFrontKey).*, @@ -852,6 +867,7 @@ public extension SessionThreadViewModel { \(SQL("\(userPublicKey)")) AS \(ViewModel.currentUserPublicKeyKey) FROM \(SessionThread.self) + LEFT JOIN \(DisappearingMessagesConfiguration.self) ON \(disappearingMessagesConfiguration[.threadId]) = \(thread[.id]) LEFT JOIN \(Contact.self) ON \(contact[.id]) = \(thread[.id]) LEFT JOIN \(Profile.self) AS \(ViewModel.contactProfileKey) ON \(ViewModel.contactProfileKey).\(profileIdColumnLiteral) = \(thread[.id]) LEFT JOIN \(OpenGroup.self) ON \(openGroup[.threadId]) = \(thread[.id])