core, mobile: logic for enabling disappearing messages (#1588)

* core: logic for enabled for disappearing messages

* refactor

* update feature enabled in UI
This commit is contained in:
Evgeny Poberezkin
2022-12-16 10:27:59 +00:00
committed by GitHub
parent 6b8705e9f4
commit 8786e2147a
6 changed files with 30 additions and 15 deletions
@@ -2101,10 +2101,10 @@ data class FeatureEnabled(
get() = if (forUser) SimplexGreen else if (forContact) WarningYellow else HighOrLowlight
companion object {
fun enabled(user: ChatPreference, contact: ChatPreference): FeatureEnabled =
fun enabled(asymmetric: Boolean, user: ChatPreference, contact: ChatPreference): FeatureEnabled =
when {
user.allow == FeatureAllowed.ALWAYS && contact.allow == FeatureAllowed.NO -> FeatureEnabled(forUser = false, forContact = true)
user.allow == FeatureAllowed.NO && contact.allow == FeatureAllowed.ALWAYS -> FeatureEnabled(forUser = true, forContact = false)
user.allow == FeatureAllowed.ALWAYS && contact.allow == FeatureAllowed.NO -> FeatureEnabled(forUser = false, forContact = asymmetric)
user.allow == FeatureAllowed.NO && contact.allow == FeatureAllowed.ALWAYS -> FeatureEnabled(forUser = asymmetric, forContact = false)
contact.allow == FeatureAllowed.NO -> FeatureEnabled(forUser = false, forContact = false)
user.allow == FeatureAllowed.NO -> FeatureEnabled(forUser = false, forContact = false)
else -> FeatureEnabled(forUser = true, forContact = true)
@@ -2138,6 +2138,11 @@ enum class ChatFeature: Feature {
@SerialName("fullDelete") FullDelete,
@SerialName("voice") Voice;
val asymmetric: Boolean get() = when (this) {
TimedMessages -> false
else -> true
}
override val text: String
get() = when(this) {
TimedMessages -> generalGetString(R.string.timed_messages)
@@ -112,6 +112,7 @@ private fun FeatureSection(
onSelected: (ContactFeatureAllowed) -> Unit
) {
val enabled = FeatureEnabled.enabled(
feature.asymmetric,
user = ChatPreference(allow = allowFeature.value.allowed),
contact = pref.contactPreference
)
@@ -50,6 +50,7 @@ struct ContactPreferencesView: View {
private func featureSection(_ feature: ChatFeature, _ userDefault: FeatureAllowed, _ pref: ContactUserPreference, _ allowFeature: Binding<ContactFeatureAllowed>) -> some View {
let enabled = FeatureEnabled.enabled(
asymmetric: feature.asymmetric,
user: Preference(allow: allowFeature.wrappedValue.allowed),
contact: pref.contactPreference
)
+10 -3
View File
@@ -231,10 +231,10 @@ public struct FeatureEnabled: Decodable {
self.forContact = forContact
}
public static func enabled(user: Preference, contact: Preference) -> FeatureEnabled {
public static func enabled(asymmetric: Bool, user: Preference, contact: Preference) -> FeatureEnabled {
switch (user.allow, contact.allow) {
case (.always, .no): return FeatureEnabled(forUser: false, forContact: true)
case (.no, .always): return FeatureEnabled(forUser: true, forContact: false)
case (.always, .no): return FeatureEnabled(forUser: false, forContact: asymmetric)
case (.no, .always): return FeatureEnabled(forUser: asymmetric, forContact: false)
case (_, .no): return FeatureEnabled(forUser: false, forContact: false)
case (.no, _): return FeatureEnabled(forUser: false, forContact: false)
default: return FeatureEnabled(forUser: true, forContact: true)
@@ -278,6 +278,13 @@ public enum ChatFeature: String, Decodable, Feature {
public var id: Self { self }
public var asymmetric: Bool {
switch self {
case .timedMessages: return false
default: return true
}
}
public var text: String {
switch self {
case .timedMessages: return NSLocalizedString("Disappearing messages", comment: "chat feature")
+3 -4
View File
@@ -305,11 +305,10 @@ ciTimedToTTL timed_ = timed_ >>= \CITimed {ttl} -> Just ttl
contactCITimedTTL :: Contact -> Maybe Int
contactCITimedTTL Contact {mergedPreferences = ContactUserPreferences {timedMessages = ContactUserPreference {enabled, userPreference}}}
| forUser enabled && forContact enabled = case userPreference of
CUPContact TimedMessagesPreference {ttl = Just t} -> Just t
CUPUser TimedMessagesPreference {ttl = Just t} -> Just t
_ -> Nothing
| forUser enabled && forContact enabled = ttl
| otherwise = Nothing
where
TimedMessagesPreference {ttl} = preference (userPreference :: ContactUserPref TimedMessagesPreference)
groupCITimedTTL :: GroupInfo -> Maybe Int
groupCITimedTTL GroupInfo {fullGroupPreferences = FullGroupPreferences {timedMessages = TimedMessagesGroupPreference {enable, ttl}}}
+7 -5
View File
@@ -787,10 +787,10 @@ instance ToJSON PrefEnabled where
toJSON = J.genericToJSON J.defaultOptions
toEncoding = J.genericToEncoding J.defaultOptions
prefEnabled :: FeatureI f => FeaturePreference f -> FeaturePreference f -> PrefEnabled
prefEnabled user contact = case (getField @"allow" user, getField @"allow" contact) of
(FAAlways, FANo) -> PrefEnabled {forUser = False, forContact = True}
(FANo, FAAlways) -> PrefEnabled {forUser = True, forContact = False}
prefEnabled :: FeatureI f => Bool -> FeaturePreference f -> FeaturePreference f -> PrefEnabled
prefEnabled asymmetric user contact = case (getField @"allow" user, getField @"allow" contact) of
(FAAlways, FANo) -> PrefEnabled {forUser = False, forContact = asymmetric}
(FANo, FAAlways) -> PrefEnabled {forUser = asymmetric, forContact = False}
(_, FANo) -> PrefEnabled False False
(FANo, _) -> PrefEnabled False False
_ -> PrefEnabled True True
@@ -819,12 +819,14 @@ contactUserPreferences user userPreferences contactPreferences connectedIncognit
pref :: FeatureI f => SChatFeature f -> ContactUserPreference (FeaturePreference f)
pref f =
ContactUserPreference
{ enabled = prefEnabled userPref ctPref,
{ enabled = prefEnabled (asymmetric f) userPref ctPref,
-- incognito contact cannot have default user preference used
userPreference = if connectedIncognito then CUPContact ctUserPref else maybe (CUPUser userPref) CUPContact ctUserPref_,
contactPreference = ctPref
}
where
asymmetric SCFTimedMessages = False
asymmetric _ = True
ctUserPref = getPreference f userPreferences
ctUserPref_ = chatPrefSel f userPreferences
userPref = getPreference f ctUserPrefs