WIP: disappearing message settings ui

pull/941/head
Ryan Zhao 3 years ago
parent d803ba20e7
commit d0f790421a

@ -114,6 +114,7 @@
7B4C75CD26BB92060000AC89 /* DeletedMessageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B4C75CC26BB92060000AC89 /* DeletedMessageView.swift */; };
7B50D64D28AC7CF80086CCEC /* silence.aiff in Resources */ = {isa = PBXBuildFile; fileRef = 7B50D64C28AC7CF80086CCEC /* silence.aiff */; };
7B5233C42900E90F00F8F375 /* PagedScrollView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B5233C32900E90F00F8F375 /* PagedScrollView.swift */; };
7B5233C6290636D700F8F375 /* _011_DisappearingMessageType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B5233C5290636D700F8F375 /* _011_DisappearingMessageType.swift */; };
7B7037432834B81F000DCF35 /* ReactionContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B7037422834B81F000DCF35 /* ReactionContainerView.swift */; };
7B7037452834BCC0000DCF35 /* ReactionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B7037442834BCC0000DCF35 /* ReactionView.swift */; };
7B7CB18E270D066F0079FF93 /* IncomingCallBanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7B7CB18D270D066F0079FF93 /* IncomingCallBanner.swift */; };
@ -1179,6 +1180,7 @@
7B4C75CC26BB92060000AC89 /* DeletedMessageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeletedMessageView.swift; sourceTree = "<group>"; };
7B50D64C28AC7CF80086CCEC /* silence.aiff */ = {isa = PBXFileReference; lastKnownFileType = audio.aiff; path = silence.aiff; sourceTree = "<group>"; };
7B5233C32900E90F00F8F375 /* PagedScrollView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagedScrollView.swift; sourceTree = "<group>"; };
7B5233C5290636D700F8F375 /* _011_DisappearingMessageType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = _011_DisappearingMessageType.swift; sourceTree = "<group>"; };
7B7037422834B81F000DCF35 /* ReactionContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReactionContainerView.swift; sourceTree = "<group>"; };
7B7037442834BCC0000DCF35 /* ReactionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReactionView.swift; sourceTree = "<group>"; };
7B7CB18D270D066F0079FF93 /* IncomingCallBanner.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = IncomingCallBanner.swift; sourceTree = "<group>"; };
@ -3540,6 +3542,7 @@
FD09B7E4288670BB00ED0B66 /* _008_EmojiReacts.swift */,
7BAA7B6528D2DE4700AE1489 /* _009_OpenGroupPermission.swift */,
FD7115F128C6CB3900B47552 /* _010_AddThreadIdToFTS.swift */,
7B5233C5290636D700F8F375 /* _011_DisappearingMessageType.swift */,
);
path = Migrations;
sourceTree = "<group>";
@ -5484,6 +5487,7 @@
FDC4387827B5C35400C60D73 /* SendMessageRequest.swift in Sources */,
FDC4381527B329CE00C60D73 /* NonceGenerator.swift in Sources */,
7B93D07027CF194000811CB6 /* ConfigurationMessage+Convenience.swift in Sources */,
7B5233C6290636D700F8F375 /* _011_DisappearingMessageType.swift in Sources */,
FD5C72FD284F0EC90029977D /* MessageReceiver+ExpirationTimers.swift in Sources */,
C3D9E3BE25676AD70040E4F3 /* (null) in Sources */,
C32C5A88256DBCF9003C73A2 /* MessageReceiver+ClosedGroups.swift in Sources */,

@ -42,8 +42,7 @@ class ThreadDisappearingMessagesViewModel: SessionTableViewModel<ThreadDisappear
private let threadId: String
private let threadVariant: SessionThread.Variant
private let config: DisappearingMessagesConfiguration
private var storedSelection: TimeInterval
private var currentSelection: CurrentValueSubject<TimeInterval, Never>
private var currentSelection: CurrentValueSubject<DisappearingMessagesConfiguration, Error>
// MARK: - Initialization
@ -57,30 +56,7 @@ class ThreadDisappearingMessagesViewModel: SessionTableViewModel<ThreadDisappear
self.threadId = threadId
self.threadVariant = threadVariant
self.config = config
self.storedSelection = (config.isEnabled ? config.durationSeconds : 0)
self.currentSelection = CurrentValueSubject(self.storedSelection)
}
// MARK: - Navigation
override var rightNavItems: AnyPublisher<[NavItem]?, Never> {
currentSelection
.removeDuplicates()
.map { [weak self] currentSelection in (self?.storedSelection != currentSelection) }
.map { isChanged in
guard isChanged else { return [] }
return [
NavItem(
id: .save,
systemItem: .save,
accessibilityIdentifier: "Save button"
) { [weak self] in
self?.saveChanges()
// self?.dismissScreen()
}
]
}
.eraseToAnyPublisher()
self.currentSelection = CurrentValueSubject(self.config)
}
// MARK: - Content
@ -100,61 +76,86 @@ class ThreadDisappearingMessagesViewModel: SessionTableViewModel<ThreadDisappear
/// this is due to the behaviour of `ValueConcurrentObserver.asyncStartObservation` which triggers it's own
/// fetch (after the ones in `ValueConcurrentObserver.asyncStart`/`ValueConcurrentObserver.syncStart`)
/// just in case the database has changed between the two reads - unfortunately it doesn't look like there is a way to prevent this
private lazy var _observableSettingsData: ObservableData = ValueObservation
.trackingConstantRegion { [weak self, config] db -> [SectionModel] in
return [
SectionModel(
model: .type,
elements: [
SessionCell.Info(
id: .off,
title: "DISAPPEARING_MESSAGES_OFF".localized(),
rightAccessory: .radio(
isSelected: { (self?.currentSelection.value == 0) }
),
onTap: { self?.currentSelection.send(0) }
),
SessionCell.Info(
id: .disappearAfterRead,
title: "DISAPPERING_MESSAGES_TYPE_AFTER_READ_TITLE".localized(),
subtitle: "DISAPPERING_MESSAGES_TYPE_AFTER_READ_DESCRIPTION".localized(),
rightAccessory: .radio(
isSelected: { (self?.currentSelection.value != 0) }
),
onTap: { self?.currentSelection.send(24 * 60 * 60) }
),
SessionCell.Info(
id: .disappearAfterSend,
title: "DISAPPERING_MESSAGES_TYPE_AFTER_SEND_TITLE".localized(),
subtitle: "DISAPPERING_MESSAGES_TYPE_AFTER_SEND_DESCRIPTION".localized(),
rightAccessory: .radio(
isSelected: { (self?.currentSelection.value != 0) }
),
onTap: { self?.currentSelection.send(24 * 60 * 60) }
)
]
)
].appending(
(self?.currentSelection.value == 0) ? nil :
private lazy var _observableSettingsData: ObservableData = {
self.currentSelection
.map { [weak self] currentSelection in
return [
SectionModel(
model: .timer,
model: .type,
elements: [
SessionCell.Info(
id: .currentSetting,
title: (self?.currentSelection.value.formatted(format: .long) ?? ""),
rightAccessory: .icon(
UIImage(named: "ic_chevron_down")?
.withRenderingMode(.alwaysTemplate)
id: .off,
title: "DISAPPEARING_MESSAGES_OFF".localized(),
rightAccessory: .radio(
isSelected: { (currentSelection.isEnabled == false) }
),
onTap: { }
onTap: {
// let updatedConfig: DisappearingMessagesConfiguration = currentSelection
// .with(
// isEnabled: false,
// durationSeconds: 0,
// type: nil
// )
// self?.currentSelection.send(updatedConfig)
}
),
SessionCell.Info(
id: .disappearAfterRead,
title: "DISAPPERING_MESSAGES_TYPE_AFTER_READ_TITLE".localized(),
subtitle: "DISAPPERING_MESSAGES_TYPE_AFTER_READ_DESCRIPTION".localized(),
rightAccessory: .radio(
isSelected: { (currentSelection.type == DisappearingMessagesConfiguration.DisappearingMessageType.disappearAfterRead) }
),
onTap: {
// let updatedConfig: DisappearingMessagesConfiguration = currentSelection
// .with(
// isEnabled: true,
// durationSeconds: (24 * 60 * 60),
// type: DisappearingMessagesConfiguration.DisappearingMessageType.disappearAfterRead
// )
// self?.currentSelection.send(updatedConfig)
}
),
SessionCell.Info(
id: .disappearAfterSend,
title: "DISAPPERING_MESSAGES_TYPE_AFTER_SEND_TITLE".localized(),
subtitle: "DISAPPERING_MESSAGES_TYPE_AFTER_SEND_DESCRIPTION".localized(),
rightAccessory: .radio(
isSelected: { (currentSelection.type == DisappearingMessagesConfiguration.DisappearingMessageType.disappearAfterSend) }
),
onTap: {
// let updatedConfig: DisappearingMessagesConfiguration = currentSelection
// .with(
// isEnabled: true,
// durationSeconds: (24 * 60 * 60),
// type: DisappearingMessagesConfiguration.DisappearingMessageType.disappearAfterSend
// )
// self?.currentSelection.send(updatedConfig)
}
)
]
)
)
}
.removeDuplicates()
.publisher(in: dependencies.storage, scheduling: dependencies.scheduler)
].appending(
(currentSelection.isEnabled == false) ? nil :
SectionModel(
model: .timer,
elements: [
SessionCell.Info(
id: .currentSetting,
title: currentSelection.durationSeconds.formatted(format: .long),
rightAccessory: .icon(
UIImage(named: "ic_chevron_down")?
.withRenderingMode(.alwaysTemplate)
),
onTap: { }
)
]
)
)
}
.removeDuplicates()
.eraseToAnyPublisher()
}()
// MARK: - Functions
public override func updateSettings(_ updatedSettings: [SectionModel]) {
@ -163,12 +164,7 @@ class ThreadDisappearingMessagesViewModel: SessionTableViewModel<ThreadDisappear
private func saveChanges() {
let threadId: String = self.threadId
let currentSelection: TimeInterval = self.currentSelection.value
let updatedConfig: DisappearingMessagesConfiguration = self.config
.with(
isEnabled: (currentSelection != 0),
durationSeconds: currentSelection
)
let updatedConfig: DisappearingMessagesConfiguration = self.currentSelection.value
guard self.config != updatedConfig else { return }

@ -0,0 +1,22 @@
// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import GRDB
import SessionUtilitiesKit
enum _011_DisappearingMessageType: Migration {
static let target: TargetMigrations.Identifier = .messagingKit
static let identifier: String = "DisappearingMessageType"
static let needsConfigSync: Bool = false
static let minExpectedRunDuration: TimeInterval = 0.1
static func migrate(_ db: GRDB.Database) throws {
// try db.alter(table: DisappearingMessagesConfiguration.self) { t in
// t.add(.permissions, .integer)
// .defaults(to: OpenGroup.Permissions.all)
// }
Storage.update(progress: 1, for: self, in: target) // In case this is the last migration
}
}

@ -14,6 +14,12 @@ public struct DisappearingMessagesConfiguration: Codable, Identifiable, Equatabl
case threadId
case isEnabled
case durationSeconds
case type
}
public enum DisappearingMessageType: Int, Codable, Hashable, DatabaseValueConvertible {
case disappearAfterRead
case disappearAfterSend
}
public var id: String { threadId } // Identifiable
@ -21,6 +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
// MARK: - Relationships
@ -38,18 +45,21 @@ public extension DisappearingMessagesConfiguration {
return DisappearingMessagesConfiguration(
threadId: threadId,
isEnabled: false,
durationSeconds: defaultDuration
durationSeconds: defaultDuration,
type: nil
)
}
func with(
isEnabled: Bool? = nil,
durationSeconds: TimeInterval? = nil
durationSeconds: TimeInterval? = nil,
type: DisappearingMessageType? = nil
) -> DisappearingMessagesConfiguration {
return DisappearingMessagesConfiguration(
threadId: threadId,
isEnabled: (isEnabled ?? self.isEnabled),
durationSeconds: (durationSeconds ?? self.durationSeconds)
durationSeconds: (durationSeconds ?? self.durationSeconds),
type: (isEnabled == false) ? nil : (type ?? self.type)
)
}
}

Loading…
Cancel
Save