disappearing message settings UI for closed groups

pull/941/head
ryanzhao 2 years ago
parent 9c165000c2
commit d01cda454c

@ -18,22 +18,23 @@ class ThreadDisappearingMessagesViewModel: SessionTableViewModel<ThreadDisappear
public enum Section: SessionTableSection {
case type
case timer
case timerWithOff
var title: String? {
switch self {
case .type: return "DISAPPERING_MESSAGES_TYPE_TITLE".localized()
case .timer: return "DISAPPERING_MESSAGES_TIMER_TITLE".localized()
case .timerWithOff: return nil
}
}
var style: SessionTableSectionStyle { return .title }
}
public enum Item: Differentiable {
case off
case disappearAfterRead
case disappearAfterSend
case currentSetting
public struct Item: Equatable, Hashable, Differentiable {
let title: String
public var differenceIdentifier: String { title }
}
// MARK: - Variables
@ -79,79 +80,135 @@ class ThreadDisappearingMessagesViewModel: SessionTableViewModel<ThreadDisappear
private lazy var _observableSettingsData: ObservableData = {
self.currentSelection
.map { [weak self] currentSelection in
return [
SectionModel(
model: .type,
elements: [
SessionCell.Info(
id: .off,
title: "DISAPPEARING_MESSAGES_OFF".localized(),
rightAccessory: .radio(
isSelected: { (self?.currentSelection.value.isEnabled == false) }
),
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: { (self?.currentSelection.value.type == .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: { (self?.currentSelection.value.type == .disappearAfterSend) }
),
onTap: {
let updatedConfig: DisappearingMessagesConfiguration = currentSelection
.with(
isEnabled: true,
durationSeconds: (24 * 60 * 60),
type: DisappearingMessagesConfiguration.DisappearingMessageType.disappearAfterSend
)
self?.currentSelection.send(updatedConfig)
}
)
]
)
].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)
guard let threadVariant = self?.threadVariant else { return [] }
switch threadVariant {
case .contact:
return [
SectionModel(
model: .type,
elements: [
SessionCell.Info(
id: Item(title: "DISAPPEARING_MESSAGES_OFF".localized()),
title: "DISAPPEARING_MESSAGES_OFF".localized(),
rightAccessory: .radio(
isSelected: { (self?.currentSelection.value.isEnabled == false) }
),
onTap: {
let updatedConfig: DisappearingMessagesConfiguration = currentSelection
.with(
isEnabled: false,
durationSeconds: 0,
type: nil
)
self?.currentSelection.send(updatedConfig)
}
),
SessionCell.Info(
id: Item(title: "DISAPPERING_MESSAGES_TYPE_AFTER_READ_TITLE".localized()),
title: "DISAPPERING_MESSAGES_TYPE_AFTER_READ_TITLE".localized(),
subtitle: "DISAPPERING_MESSAGES_TYPE_AFTER_READ_DESCRIPTION".localized(),
rightAccessory: .radio(
isSelected: { (self?.currentSelection.value.isEnabled == true) && (self?.currentSelection.value.type == .disappearAfterRead) }
),
onTap: {
let updatedConfig: DisappearingMessagesConfiguration = currentSelection
.with(
isEnabled: true,
durationSeconds: (24 * 60 * 60),
type: DisappearingMessagesConfiguration.DisappearingMessageType.disappearAfterRead
)
self?.currentSelection.send(updatedConfig)
}
),
onTap: { }
SessionCell.Info(
id: Item(title: "DISAPPERING_MESSAGES_TYPE_AFTER_SEND_TITLE".localized()),
title: "DISAPPERING_MESSAGES_TYPE_AFTER_SEND_TITLE".localized(),
subtitle: "DISAPPERING_MESSAGES_TYPE_AFTER_SEND_DESCRIPTION".localized(),
rightAccessory: .radio(
isSelected: { (self?.currentSelection.value.isEnabled == true) && (self?.currentSelection.value.type == .disappearAfterSend) }
),
onTap: {
let updatedConfig: DisappearingMessagesConfiguration = currentSelection
.with(
isEnabled: true,
durationSeconds: (24 * 60 * 60),
type: DisappearingMessagesConfiguration.DisappearingMessageType.disappearAfterSend
)
self?.currentSelection.send(updatedConfig)
}
)
]
)
].appending(
(currentSelection.isEnabled == false) ? nil :
SectionModel(
model: .timer,
elements: [
SessionCell.Info(
id: Item(title: "Current Setting"),
title: currentSelection.durationSeconds.formatted(format: .long),
rightAccessory: .icon(
UIImage(named: "ic_chevron_down")?
.withRenderingMode(.alwaysTemplate)
),
onTap: {
}
)
]
)
]
)
)
case .closedGroup:
return [
SectionModel(
model: .timerWithOff,
elements: [
SessionCell.Info(
id: Item(title: "DISAPPEARING_MESSAGES_OFF".localized()),
title: "DISAPPEARING_MESSAGES_OFF".localized(),
rightAccessory: .radio(
isSelected: { (self?.currentSelection.value.isEnabled == false) }
),
onTap: {
let updatedConfig: DisappearingMessagesConfiguration = currentSelection
.with(
isEnabled: false,
durationSeconds: 0,
type: nil
)
self?.currentSelection.send(updatedConfig)
}
)
].appending(
contentsOf: DisappearingMessagesConfiguration
.validDurationsSeconds(.disappearAfterRead)
.map { duration in
let title: String = duration.formatted(format: .long)
return SessionCell.Info(
id: Item(title: title),
title: title,
rightAccessory: .radio(
isSelected: { (self?.currentSelection.value.isEnabled == true) && (self?.currentSelection.value.durationSeconds == duration) }
),
onTap: {
let updatedConfig: DisappearingMessagesConfiguration = currentSelection
.with(
isEnabled: true,
durationSeconds: duration,
type: .disappearAfterRead
)
self?.currentSelection.send(updatedConfig)
}
)
}
)
)
]
case . openGroup:
return [] // Should not happen
}
}
.removeDuplicates()
.eraseToAnyPublisher()

@ -24,7 +24,8 @@ public enum SNMessagingKit { // Just to make the external API nice
[
_008_EmojiReacts.self,
_009_OpenGroupPermission.self,
_010_AddThreadIdToFTS.self
_010_AddThreadIdToFTS.self,
_011_DisappearingMessageType.self
]
]
)

@ -11,10 +11,18 @@ enum _011_DisappearingMessageType: Migration {
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)
// }
try db.alter(table: DisappearingMessagesConfiguration.self) { t in
t.add(.type, .integer)
}
_ = try DisappearingMessagesConfiguration
.filter(DisappearingMessagesConfiguration.Columns.isEnabled == true)
.updateAll(
db,
DisappearingMessagesConfiguration.Columns.type.set(
to: DisappearingMessagesConfiguration.DisappearingMessageType.disappearAfterRead
)
)
Storage.update(progress: 1, for: self, in: target) // In case this is the last migration
}

@ -134,6 +134,27 @@ extension DisappearingMessagesConfiguration {
public static var maxDurationSeconds: TimeInterval = {
return (validDurationsSeconds.max() ?? 0)
}()
public static func validDurationsSeconds(_ type: DisappearingMessageType) -> [TimeInterval] {
switch type {
case .disappearAfterRead:
return [
(5 * 60),
(1 * 60 * 60),
(12 * 60 * 60),
(24 * 60 * 60),
(7 * 24 * 60 * 60),
(2 * 7 * 24 * 60 * 60)
]
case .disappearAfterSend:
return [
(12 * 60 * 60),
(24 * 60 * 60),
(7 * 24 * 60 * 60),
(2 * 7 * 24 * 60 * 60)
]
}
}
}
// MARK: - Objective-C Support

Loading…
Cancel
Save