From a8e6dc187c524972833351788f9c81c38233ec72 Mon Sep 17 00:00:00 2001 From: Sophia Tung Date: Wed, 11 Sep 2024 23:26:53 -0700 Subject: [PATCH] Add iCloud Sync beta toggle --- Mammoth/AppDelegate.swift | 1 + Mammoth/Localizable.xcstrings | 11 +++++ Mammoth/Managers/CloudSyncManager.swift | 8 ++++ Mammoth/Models/GlobalStruct.swift | 1 + Mammoth/Screens/Settings/SettingsTypes.swift | 6 ++- .../Settings/SettingsViewController.swift | 41 +++++++++++++++++-- 6 files changed, 63 insertions(+), 5 deletions(-) diff --git a/Mammoth/AppDelegate.swift b/Mammoth/AppDelegate.swift index 4940c33d8..39e127590 100644 --- a/Mammoth/AppDelegate.swift +++ b/Mammoth/AppDelegate.swift @@ -231,6 +231,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD GlobalStruct.openLinksInReaderView = UserDefaults.standard.value(forKey: "openLinksInReaderView") as? Bool ?? false GlobalStruct.preferredBrowser = UserDefaults.standard.string(forKey: "PreferredBrowser") ?? "In-App Browser" GlobalStruct.appLock = UserDefaults.standard.value(forKey: "appLock") as? Bool ?? false + GlobalStruct.cloudSync = UserDefaults.standard.value(forKey: "cloudSync") as? Bool ?? false GlobalStruct.shareAnalytics = UserDefaults.standard.value(forKey: "shareAnalytics") as? Bool ?? true GlobalStruct.tab2 = UserDefaults.standard.value(forKey: "tab2") as? Bool ?? true diff --git a/Mammoth/Localizable.xcstrings b/Mammoth/Localizable.xcstrings index 924bda384..6c934136d 100644 --- a/Mammoth/Localizable.xcstrings +++ b/Mammoth/Localizable.xcstrings @@ -65757,6 +65757,17 @@ } } }, + "settings.cloudsync" : { + "comment" : "iCloud Sync settings title string", + "localizations" : { + "en" : { + "stringUnit" : { + "state" : "translated", + "value" : "iCloud Sync - beta" + } + } + } + }, "settings.composer" : { "comment" : "Button in settings.", "extractionState" : "manual", diff --git a/Mammoth/Managers/CloudSyncManager.swift b/Mammoth/Managers/CloudSyncManager.swift index e0b563c64..6b0aca624 100644 --- a/Mammoth/Managers/CloudSyncManager.swift +++ b/Mammoth/Managers/CloudSyncManager.swift @@ -42,6 +42,8 @@ class CloudSyncManager { } public func enableSaving(forFeedType feedType: NewsFeedTypes) { + if !GlobalStruct.cloudSync { return } + switch feedType { case .following: shouldSaveFollowing = true @@ -84,6 +86,8 @@ class CloudSyncManager { } public func saveSyncStatus(for type: NewsFeedTypes, scrollPosition: NewsFeedScrollPosition) { + if !GlobalStruct.cloudSync { return } + switch type { case .following: if !shouldSaveFollowing { @@ -119,6 +123,8 @@ class CloudSyncManager { } public func cloudSavedPosition(for type: NewsFeedTypes) -> NewsFeedScrollPosition? { + if !GlobalStruct.cloudSync { return nil } + let (itemKey, dateKey) = keys(for: type) guard !itemKey.isEmpty, !dateKey.isEmpty else { return nil } @@ -136,6 +142,8 @@ class CloudSyncManager { } private func setSyncStatus(for type: NewsFeedTypes, scrollPosition: NewsFeedScrollPosition) { + if !GlobalStruct.cloudSync { return } + let (itemKey, dateKey) = keys(for: type) guard !itemKey.isEmpty, !dateKey.isEmpty else { return } diff --git a/Mammoth/Models/GlobalStruct.swift b/Mammoth/Models/GlobalStruct.swift index 3127a9f0d..20dcfb3d5 100644 --- a/Mammoth/Models/GlobalStruct.swift +++ b/Mammoth/Models/GlobalStruct.swift @@ -153,6 +153,7 @@ public struct GlobalStruct { static var openLinksInReaderView: Bool = false static var preferredBrowser: String = "In-App Browser" static var appLock: Bool = false + static var cloudSync: Bool = false static var shareAnalytics: Bool = true // composer diff --git a/Mammoth/Screens/Settings/SettingsTypes.swift b/Mammoth/Screens/Settings/SettingsTypes.swift index 467c969e7..b36ae2e5a 100644 --- a/Mammoth/Screens/Settings/SettingsTypes.swift +++ b/Mammoth/Screens/Settings/SettingsTypes.swift @@ -37,6 +37,8 @@ public enum SettingsItem { case appLock + case cloudSync + case clearData var title: String { @@ -55,6 +57,7 @@ public enum SettingsItem { case .openLinks: return NSLocalizedString("settings.openLinks", comment: "") case .readerView: return NSLocalizedString("settings.readerView", comment: "") case .appLock: return NSLocalizedString("settings.appLock", comment: "") + case .cloudSync: return NSLocalizedString("settings.cloudsync", comment: "iCloud Sync settings title string") case .development: return NSLocalizedString("settings.development", comment: "") case .analytics: return NSLocalizedString("settings.analytics", comment: "") case .sourceCode: return NSLocalizedString("settings.sourceCode", comment: "") @@ -78,6 +81,7 @@ public enum SettingsItem { case .openLinks: return "\u{f08e}" case .readerView: return "\u{e1d8}" case .appLock: return "\u{f023}" + case .cloudSync: return "\u{f2f1}" case .development: return "\u{f121}" case .analytics: return "\u{f681}" case .sourceCode: return "\u{f121}" @@ -96,5 +100,5 @@ public enum SettingsItem { public struct SettingsSection { var items: [SettingsItem] - var footerTitle: String? = nil + var footerTitle: String? } diff --git a/Mammoth/Screens/Settings/SettingsViewController.swift b/Mammoth/Screens/Settings/SettingsViewController.swift index 9e25129d3..d66e0973d 100644 --- a/Mammoth/Screens/Settings/SettingsViewController.swift +++ b/Mammoth/Screens/Settings/SettingsViewController.swift @@ -34,12 +34,15 @@ class SettingsViewController: UIViewController { .pushNotifications, .soundsAndHaptics, .siriShortcuts - ].compactMap{$0}), + ].compactMap {$0}), SettingsSection(items: [ .openLinks, GlobalStruct.preferredBrowser == "In-App Browser" ? .readerView : nil, .appLock - ].compactMap{$0}), + ].compactMap {$0}), + SettingsSection(items: [ + .cloudSync + ].compactMap {$0}), SettingsSection(items: [ .getInTouch, .subscriptions, @@ -64,12 +67,15 @@ class SettingsViewController: UIViewController { .pushNotifications, .soundsAndHaptics, .siriShortcuts - ].compactMap{$0}), + ].compactMap {$0}), SettingsSection(items: [ .openLinks, GlobalStruct.preferredBrowser == "In-App Browser" ? .readerView : nil, .appLock - ].compactMap{$0}), + ].compactMap {$0}), + SettingsSection(items: [ + .cloudSync + ].compactMap {$0}), SettingsSection(items: [ .getInTouch, .development @@ -226,6 +232,15 @@ class SettingsViewController: UIViewController { } } + @objc func switchCloudSync(_ sender: UISwitch) { + if sender.isOn { + GlobalStruct.cloudSync = true + UserDefaults.standard.setValue(true, forKey: "cloudSync") + } else { + GlobalStruct.cloudSync = false + UserDefaults.standard.setValue(false, forKey: "cloudSync") + } + } } // MARK: - UITableView @@ -304,6 +319,24 @@ extension SettingsViewController: UITableViewDataSource, UITableViewDelegate { cell.accessoryType = .none cell.selectionStyle = .none cell.textLabel?.textAlignment = .left + } else if item == .cloudSync { + let switchView = UISwitch(frame: .zero) + if UserDefaults.standard.value(forKey: "cloudSync") as? Bool != nil { + if UserDefaults.standard.value(forKey: "cloudSync") as? Bool == false { + switchView.setOn(false, animated: false) + } else { + switchView.setOn(true, animated: false) + } + } else { + switchView.setOn(false, animated: false) + } + switchView.onTintColor = .custom.gold + + switchView.addTarget(self, action: #selector(switchCloudSync), for: .valueChanged) + cell.accessoryView = switchView + cell.accessoryType = .none + cell.selectionStyle = .none + cell.textLabel?.textAlignment = .left } else if item == .clearData { cell.accessoryView = nil cell.accessoryType = .none