From 6a82bf9e010db3329250f1051580e6af8fbb4ac9 Mon Sep 17 00:00:00 2001 From: aringenbach Date: Wed, 6 Apr 2022 15:00:56 +0200 Subject: [PATCH] Add presence offline mode setting --- Config/BuildSettings.swift | 5 ++ Riot/Assets/en.lproj/Vector.strings | 4 ++ Riot/Generated/Strings.swift | 12 +++++ .../MatrixKit/Models/Account/MXKAccount.m | 22 +++++++-- .../MatrixKit/Models/Account/MXKAccountData.h | 7 +++ .../MatrixKit/Models/Account/MXKAccountData.m | 11 +++++ .../Modules/Settings/SettingsViewController.m | 47 +++++++++++++++++++ changelog.d/5995.change | 1 + 8 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 changelog.d/5995.change diff --git a/Config/BuildSettings.swift b/Config/BuildSettings.swift index 5695d6723..ef19f87c6 100644 --- a/Config/BuildSettings.swift +++ b/Config/BuildSettings.swift @@ -427,4 +427,9 @@ final class BuildSettings: NSObject { // Do not enable live location sharing atm return false } + + // MARK: - Presence + + /// A setting to enable the presence configuration settings section. + static let presenceSettingsAllowConfiguration: Bool = false } diff --git a/Riot/Assets/en.lproj/Vector.strings b/Riot/Assets/en.lproj/Vector.strings index 8f6405336..501fba8c1 100644 --- a/Riot/Assets/en.lproj/Vector.strings +++ b/Riot/Assets/en.lproj/Vector.strings @@ -750,6 +750,10 @@ Tap the + to start adding people."; "settings_enable_room_message_bubbles" = "Message bubbles"; +"settings_presence" = "Presence"; +"settings_presence_offline_mode" = "Offline Mode"; +"settings_presence_offline_mode_description" = "If enabled, you will always appear offline to other users, even when using the application."; + // Security settings "security_settings_title" = "Security"; "security_settings_crypto_sessions" = "MY SESSIONS"; diff --git a/Riot/Generated/Strings.swift b/Riot/Generated/Strings.swift index 85169e4ca..5bdea1a03 100644 --- a/Riot/Generated/Strings.swift +++ b/Riot/Generated/Strings.swift @@ -6895,6 +6895,18 @@ public class VectorL10n: NSObject { public static var settingsPinRoomsWithUnread: String { return VectorL10n.tr("Vector", "settings_pin_rooms_with_unread") } + /// Presence + public static var settingsPresence: String { + return VectorL10n.tr("Vector", "settings_presence") + } + /// Offline Mode + public static var settingsPresenceOfflineMode: String { + return VectorL10n.tr("Vector", "settings_presence_offline_mode") + } + /// If enabled, you will always appear offline to other users, even when using the application. + public static var settingsPresenceOfflineModeDescription: String { + return VectorL10n.tr("Vector", "settings_presence_offline_mode_description") + } /// Privacy Policy public static var settingsPrivacyPolicy: String { return VectorL10n.tr("Vector", "settings_privacy_policy") diff --git a/Riot/Modules/MatrixKit/Models/Account/MXKAccount.m b/Riot/Modules/MatrixKit/Models/Account/MXKAccount.m index 728a90fa1..38d7e5e05 100644 --- a/Riot/Modules/MatrixKit/Models/Account/MXKAccount.m +++ b/Riot/Modules/MatrixKit/Models/Account/MXKAccount.m @@ -715,6 +715,7 @@ static NSArray *initialSyncSilentErrorsHTTPStatusCodes; // Instantiate new session mxSession = [[MXSession alloc] initWithMatrixRestClient:mxRestClient]; + mxSession.preferredSyncPresence = self.preferredSyncPresence; // Check whether an antivirus url is defined. if (_antivirusServerURL) @@ -1007,7 +1008,7 @@ static NSArray *initialSyncSilentErrorsHTTPStatusCodes; // Update user presence MXWeakify(self); - [self setUserPresence:MXPresenceUnavailable andStatusMessage:nil completion:^{ + [self setUserPresence:MXPresenceOffline andStatusMessage:nil completion:^{ MXStrongifyAndReturnIfNil(self); [self cancelPauseBackgroundTask]; }]; @@ -1045,8 +1046,10 @@ static NSArray *initialSyncSilentErrorsHTTPStatusCodes; case MXSessionStatePauseRequested: { // Resume SDK and update user presence + MXWeakify(self); [mxSession resume:^{ - [self setUserPresence:MXPresenceOnline andStatusMessage:nil completion:nil]; + MXStrongifyAndReturnIfNil(self); + [self setUserPresence:self.preferredSyncPresence andStatusMessage:nil completion:nil]; [self refreshAPNSPusher]; [self refreshPushKitPusher]; @@ -1513,7 +1516,7 @@ static NSArray *initialSyncSilentErrorsHTTPStatusCodes; MXLogDebug(@"[MXKAccount] %@: The session is ready. Matrix SDK session has been started in %0.fms.", self.mxCredentials.userId, [[NSDate date] timeIntervalSinceDate:self->openSessionStartDate] * 1000); - [self setUserPresence:MXPresenceOnline andStatusMessage:nil completion:nil]; + [self setUserPresence:self.preferredSyncPresence andStatusMessage:nil completion:nil]; } failure:^(NSError *error) { MXStrongifyAndReturnIfNil(self); @@ -2159,4 +2162,17 @@ static NSArray *initialSyncSilentErrorsHTTPStatusCodes; } } +#pragma mark - Presence + +- (void)setPreferredSyncPresence:(MXPresence)preferredSyncPresence +{ + [super setPreferredSyncPresence:preferredSyncPresence]; + + if (self.mxSession) + { + self.mxSession.preferredSyncPresence = preferredSyncPresence; + [self setUserPresence:preferredSyncPresence andStatusMessage:nil completion:nil]; + } +} + @end diff --git a/Riot/Modules/MatrixKit/Models/Account/MXKAccountData.h b/Riot/Modules/MatrixKit/Models/Account/MXKAccountData.h index 3098f66d1..ed49eb55c 100644 --- a/Riot/Modules/MatrixKit/Models/Account/MXKAccountData.h +++ b/Riot/Modules/MatrixKit/Models/Account/MXKAccountData.h @@ -32,6 +32,7 @@ @protected BOOL _isSoftLogout; @protected BOOL _hasPusherForPushNotifications; @protected BOOL _hasPusherForPushKitNotifications; +@protected MXPresence _preferredSyncPresence; } /** @@ -89,6 +90,12 @@ */ @property (nonatomic, readonly) BOOL hasPusherForPushKitNotifications; +/** + The account's preferred Presence status to share while the application is in foreground. + Defaults to MXPresenceOnline. + */ +@property (nonatomic) MXPresence preferredSyncPresence; + /** Enable In-App notifications based on Remote notifications rules. diff --git a/Riot/Modules/MatrixKit/Models/Account/MXKAccountData.m b/Riot/Modules/MatrixKit/Models/Account/MXKAccountData.m index 4d59e2a4b..35e6ca9d5 100644 --- a/Riot/Modules/MatrixKit/Models/Account/MXKAccountData.m +++ b/Riot/Modules/MatrixKit/Models/Account/MXKAccountData.m @@ -80,6 +80,15 @@ _warnedAboutEncryption = [coder decodeBoolForKey:@"warnedAboutEncryption"]; + if ([coder decodeIntForKey:@"preferredSyncPresence"]) + { + _preferredSyncPresence = [coder decodeInt64ForKey:@"preferredSyncPresence"]; + } + else + { + _preferredSyncPresence = MXPresenceOnline; + } + _others = [coder decodeObjectForKey:@"others"]; } @@ -143,6 +152,8 @@ [coder encodeBool:_warnedAboutEncryption forKey:@"warnedAboutEncryption"]; + [coder encodeInt64:_preferredSyncPresence forKey:@"preferredSyncPresence"]; + [coder encodeObject:_others forKey:@"others"]; } diff --git a/Riot/Modules/Settings/SettingsViewController.m b/Riot/Modules/Settings/SettingsViewController.m index 4cff1dac3..a8e55a443 100644 --- a/Riot/Modules/Settings/SettingsViewController.m +++ b/Riot/Modules/Settings/SettingsViewController.m @@ -62,6 +62,7 @@ typedef NS_ENUM(NSUInteger, SECTION_TAG) SECTION_TAG_IGNORED_USERS, SECTION_TAG_INTEGRATIONS, SECTION_TAG_USER_INTERFACE, + SECTION_TAG_PRESENCE, SECTION_TAG_ADVANCED, SECTION_TAG_ABOUT, SECTION_TAG_LABS, @@ -137,6 +138,11 @@ typedef NS_ENUM(NSUInteger, IDENTITY_SERVER) IDENTITY_SERVER_INDEX }; +typedef NS_ENUM(NSUInteger, PRESENCE) +{ + PRESENCE_OFFLINE_MODE = 0, +}; + typedef NS_ENUM(NSUInteger, ADVANCED) { ADVANCED_SHOW_NSFW_ROOMS_INDEX = 0, @@ -520,6 +526,16 @@ TableViewSectionsDelegate> [tmpSections addObject: sectionUserInterface]; + if(BuildSettings.presenceSettingsAllowConfiguration) + { + Section *sectionPresence = [Section sectionWithTag:SECTION_TAG_PRESENCE]; + [sectionPresence addRowWithTag:PRESENCE_OFFLINE_MODE]; + sectionPresence.headerTitle = VectorL10n.settingsPresence; + sectionPresence.footerTitle = VectorL10n.settingsPresenceOfflineModeDescription; + + [tmpSections addObject:sectionPresence]; + } + Section *sectionAdvanced = [Section sectionWithTag:SECTION_TAG_ADVANCED]; sectionAdvanced.headerTitle = [VectorL10n settingsAdvanced]; @@ -2287,6 +2303,24 @@ TableViewSectionsDelegate> cell.selectionStyle = UITableViewCellSelectionStyleDefault; } } + else if (section == SECTION_TAG_PRESENCE) + { + if (row == PRESENCE_OFFLINE_MODE) + { + MXKTableViewCellWithLabelAndSwitch* labelAndSwitchCell = [self getLabelAndSwitchCell:tableView forIndexPath:indexPath]; + + labelAndSwitchCell.mxkLabel.text = VectorL10n.settingsPresenceOfflineMode; + + MXKAccount *account = MXKAccountManager.sharedManager.accounts.firstObject; + + labelAndSwitchCell.mxkSwitch.on = account.preferredSyncPresence == MXPresenceOffline; + labelAndSwitchCell.mxkSwitch.onTintColor = ThemeService.shared.theme.tintColor; + labelAndSwitchCell.mxkSwitch.enabled = YES; + [labelAndSwitchCell.mxkSwitch addTarget:self action:@selector(togglePresenceOfflineMode:) forControlEvents:UIControlEventTouchUpInside]; + + cell = labelAndSwitchCell; + } + } else if (section == SECTION_TAG_ADVANCED) { if (row == ADVANCED_SHOW_NSFW_ROOMS_INDEX) @@ -3907,6 +3941,19 @@ TableViewSectionsDelegate> self.deactivateAccountViewController = deactivateAccountViewController; } +- (void)togglePresenceOfflineMode:(UISwitch *)sender +{ + MXKAccount *account = MXKAccountManager.sharedManager.accounts.firstObject; + if (sender.isOn) + { + account.preferredSyncPresence = MXPresenceOffline; + } + else + { + account.preferredSyncPresence = MXPresenceOnline; + } +} + - (void)toggleNSFWPublicRoomsFiltering:(UISwitch *)sender { RiotSettings.shared.showNSFWPublicRooms = sender.isOn; diff --git a/changelog.d/5995.change b/changelog.d/5995.change new file mode 100644 index 000000000..7d40008cb --- /dev/null +++ b/changelog.d/5995.change @@ -0,0 +1 @@ +Presence: add an optional setting for offline mode