From 978ec30074ec2482b0d3a9c21e3b19821242499f Mon Sep 17 00:00:00 2001 From: Ryan ZHAO <> Date: Thu, 7 Dec 2023 10:32:34 +1100 Subject: [PATCH] hide and display the following setting action accordingly --- .../ConversationVC+Interaction.swift | 33 +++++++++++-- .../Conversations/ConversationViewModel.swift | 13 ++++- .../Shared Models/MessageViewModel.swift | 47 ++++++++++++++----- 3 files changed, 75 insertions(+), 18 deletions(-) diff --git a/Session/Conversations/ConversationVC+Interaction.swift b/Session/Conversations/ConversationVC+Interaction.swift index 760ddd9e6..0802db694 100644 --- a/Session/Conversations/ConversationVC+Interaction.swift +++ b/Session/Conversations/ConversationVC+Interaction.swift @@ -897,20 +897,45 @@ extension ConversationVC: // For disappearing messages config update, show the following settings modal guard cellViewModel.variant != .infoDisappearingMessagesUpdate else { + let messageDisappearingConfig = cellViewModel.messageDisappearingConfiguration() + let expirationTimerString: String = floor(messageDisappearingConfig.durationSeconds).formatted(format: .long) + let expirationTypeString: String = (messageDisappearingConfig.type == .disappearAfterRead ? "DISAPPEARING_MESSAGE_STATE_READ".localized() : "DISAPPEARING_MESSAGE_STATE_SENT".localized()) + let modalBodyString: String = ( + messageDisappearingConfig.isEnabled ? + String( + format: "FOLLOW_SETTING_EXPLAINATION_TURNING_ON".localized(), + expirationTimerString, + expirationTypeString + ) : + "FOLLOW_SETTING_EXPLAINATION_TURNING_OFF".localized() + ) + let modalConfirmTitle: String = messageDisappearingConfig.isEnabled ? "DISAPPERING_MESSAGES_SAVE_TITLE".localized() : "" let confirmationModal: ConfirmationModal = ConfirmationModal( info: ConfirmationModal.Info( title: "FOLLOW_SETTING_TITLE".localized(), body: .attributedText( - NSAttributedString(string: "FOLLOW_SETTING_EXPLAINATION_TURNING_ON".localized()) + NSAttributedString(string: modalBodyString) + .adding( + attributes: [ .font: UIFont.boldSystemFont(ofSize: Values.smallFontSize) ], + range: (modalBodyString as NSString).range(of: expirationTypeString) + ) .adding( attributes: [ .font: UIFont.boldSystemFont(ofSize: Values.smallFontSize) ], - range: ("FOLLOW_SETTING_EXPLAINATION_TURNING_ON".localized() as NSString).range(of: cellViewModel.authorName) + range: (modalBodyString as NSString).range(of: expirationTimerString) + ) + .adding( + attributes: [ .font: UIFont.boldSystemFont(ofSize: Values.smallFontSize) ], + range: (modalBodyString as NSString).range(of: "DISAPPEARING_MESSAGES_OFF".localized().lowercased()) ) ), - confirmTitle: "modal_download_button_title".localized(), + confirmTitle: modalConfirmTitle, + confirmStyle: .danger, + cancelStyle: .textPrimary, dismissOnConfirm: false // Custom dismissal logic ) { [weak self] _ in - + dependencies.storage.writeAsync { db in + try messageDisappearingConfig.save(db) + } self?.dismiss(animated: true, completion: nil) } ) diff --git a/Session/Conversations/ConversationViewModel.swift b/Session/Conversations/ConversationViewModel.swift index 8189bb769..2f798876b 100644 --- a/Session/Conversations/ConversationViewModel.swift +++ b/Session/Conversations/ConversationViewModel.swift @@ -320,6 +320,16 @@ public class ConversationViewModel: OWSAudioPlayerDelegate { return SQL("LEFT JOIN \(RecipientState.self) ON \(recipientState[.interactionId]) = \(interaction[.id])") }() ), + PagedData.ObservedChanges( + table: DisappearingMessagesConfiguration.self, + columns: [ .isEnabled, .type, .durationSeconds ], + joinToPagedType: { + let interaction: TypedTableAlias = TypedTableAlias() + let disappearingMessagesConfiguration: TypedTableAlias = TypedTableAlias() + + return SQL("LEFT JOIN \(DisappearingMessagesConfiguration.self) ON \(disappearingMessagesConfiguration[.threadId]) = \(interaction[.threadId])") + }() + ), ], filterSQL: MessageViewModel.filterSQL(threadId: threadId), groupSQL: MessageViewModel.groupSQL, @@ -538,7 +548,8 @@ public class ConversationViewModel: OWSAudioPlayerDelegate { optimisticMessageId: optimisticMessageId, threadId: threadData.threadId, threadVariant: threadData.threadVariant, - threadDisappearingMessagesConfiguration: threadData.disappearingMessagesConfiguration, + threadExpirationType: threadData.disappearingMessagesConfiguration?.type, + threadExpirationTimer: threadData.disappearingMessagesConfiguration?.durationSeconds, threadOpenGroupServer: threadData.openGroupServer, threadOpenGroupPublicKey: threadData.openGroupPublicKey, threadContactNameInternal: threadData.threadContactName(), diff --git a/SessionMessagingKit/Shared Models/MessageViewModel.swift b/SessionMessagingKit/Shared Models/MessageViewModel.swift index d75cfa6d7..dee414612 100644 --- a/SessionMessagingKit/Shared Models/MessageViewModel.swift +++ b/SessionMessagingKit/Shared Models/MessageViewModel.swift @@ -19,7 +19,8 @@ public struct MessageViewModel: FetchableRecordWithRowId, Decodable, Equatable, case threadId case threadVariant case threadIsTrusted - case threadDisappearingMessagesConfiguration + case threadExpirationType + case threadExpirationTimer case threadOpenGroupServer case threadOpenGroupPublicKey case threadContactNameInternal @@ -101,7 +102,8 @@ public struct MessageViewModel: FetchableRecordWithRowId, Decodable, Equatable, public let threadId: String public let threadVariant: SessionThread.Variant public let threadIsTrusted: Bool - public let threadDisappearingMessagesConfiguration: DisappearingMessagesConfiguration? + public let threadExpirationType: DisappearingMessagesConfiguration.DisappearingMessageType? + public let threadExpirationTimer: TimeInterval? public let threadOpenGroupServer: String? public let threadOpenGroupPublicKey: String? private let threadContactNameInternal: String? @@ -210,7 +212,8 @@ public struct MessageViewModel: FetchableRecordWithRowId, Decodable, Equatable, threadId: self.threadId, threadVariant: self.threadVariant, threadIsTrusted: self.threadIsTrusted, - threadDisappearingMessagesConfiguration: self.threadDisappearingMessagesConfiguration, + threadExpirationType: self.threadExpirationType, + threadExpirationTimer: self.threadExpirationTimer, threadOpenGroupServer: self.threadOpenGroupServer, threadOpenGroupPublicKey: self.threadOpenGroupPublicKey, threadContactNameInternal: self.threadContactNameInternal, @@ -378,7 +381,8 @@ public struct MessageViewModel: FetchableRecordWithRowId, Decodable, Equatable, threadId: self.threadId, threadVariant: self.threadVariant, threadIsTrusted: self.threadIsTrusted, - threadDisappearingMessagesConfiguration: self.threadDisappearingMessagesConfiguration, + threadExpirationType: self.threadExpirationType, + threadExpirationTimer: self.threadExpirationTimer, threadOpenGroupServer: self.threadOpenGroupServer, threadOpenGroupPublicKey: self.threadOpenGroupPublicKey, threadContactNameInternal: self.threadContactNameInternal, @@ -489,8 +493,8 @@ public struct MessageViewModel: FetchableRecordWithRowId, Decodable, Equatable, // MARK: - DisappeaingMessagesUpdateControlMessage public extension MessageViewModel { - func canDoFollowingSetting() -> Bool { - let messageDisappearingConfig = DisappearingMessagesConfiguration + func messageDisappearingConfiguration() -> DisappearingMessagesConfiguration { + return DisappearingMessagesConfiguration .defaultWith(self.threadId) .with( isEnabled: (self.expiresInSeconds ?? 0) > 0, @@ -498,8 +502,21 @@ public extension MessageViewModel { type: (Int64(self.expiresStartedAtMs ?? 0) == self.timestampMs ? .disappearAfterSend : .disappearAfterRead ), lastChangeTimestampMs: nil ) - - return messageDisappearingConfig != self.threadDisappearingMessagesConfiguration + } + + func threadDisappearingConfiguration() -> DisappearingMessagesConfiguration { + return DisappearingMessagesConfiguration + .defaultWith(self.threadId) + .with( + isEnabled: (self.threadExpirationTimer ?? 0) > 0, + durationSeconds: self.threadExpirationTimer, + type: self.threadExpirationType, + lastChangeTimestampMs: nil + ) + } + + func canDoFollowingSetting() -> Bool { + return self.messageDisappearingConfiguration() != self.threadDisappearingConfiguration() } } @@ -604,7 +621,8 @@ public extension MessageViewModel { self.threadId = "INVALID_THREAD_ID" self.threadVariant = .contact self.threadIsTrusted = false - self.threadDisappearingMessagesConfiguration = nil + self.threadExpirationType = nil + self.threadExpirationTimer = nil self.threadOpenGroupServer = nil self.threadOpenGroupPublicKey = nil self.threadContactNameInternal = nil @@ -669,7 +687,8 @@ public extension MessageViewModel { optimisticMessageId: UUID, threadId: String, threadVariant: SessionThread.Variant, - threadDisappearingMessagesConfiguration: DisappearingMessagesConfiguration?, + threadExpirationType: DisappearingMessagesConfiguration.DisappearingMessageType?, + threadExpirationTimer: TimeInterval?, threadOpenGroupServer: String?, threadOpenGroupPublicKey: String?, threadContactNameInternal: String, @@ -691,7 +710,8 @@ public extension MessageViewModel { self.threadId = threadId self.threadVariant = threadVariant self.threadIsTrusted = false - self.threadDisappearingMessagesConfiguration = threadDisappearingMessagesConfiguration + self.threadExpirationType = threadExpirationType + self.threadExpirationTimer = threadExpirationTimer self.threadOpenGroupServer = threadOpenGroupServer self.threadOpenGroupPublicKey = threadOpenGroupPublicKey self.threadContactNameInternal = threadContactNameInternal @@ -835,7 +855,7 @@ public extension MessageViewModel { let linkPreviewAttachment: TypedTableAlias = TypedTableAlias(ViewModel.self, column: .linkPreviewAttachment) let readReceipt: TypedTableAlias = TypedTableAlias(name: "readReceipt") - let numColumnsBeforeLinkedRecords: Int = 26 + let numColumnsBeforeLinkedRecords: Int = 23 let finalGroupSQL: SQL = (groupSQL ?? "") let request: SQLRequest = """ SELECT @@ -843,7 +863,8 @@ public extension MessageViewModel { \(thread[.variant]) AS \(ViewModel.Columns.threadVariant), -- Default to 'true' for non-contact threads IFNULL(\(contact[.isTrusted]), true) AS \(ViewModel.Columns.threadIsTrusted), - \(disappearingMessagesConfig.allColumns), + \(disappearingMessagesConfig[.type]) AS \(ViewModel.Columns.threadExpirationType), + \(disappearingMessagesConfig[.durationSeconds]) AS \(ViewModel.Columns.threadExpirationTimer), \(openGroup[.server]) AS \(ViewModel.Columns.threadOpenGroupServer), \(openGroup[.publicKey]) AS \(ViewModel.Columns.threadOpenGroupPublicKey), IFNULL(\(threadProfile[.nickname]), \(threadProfile[.name])) AS \(ViewModel.Columns.threadContactNameInternal),