Resetting the app after deletion and cleanup settings (#372)

* Fixes #361 - Reset everything if the app has been deleted since the last run

* Cleaned up and renamed the ElementSettings to ApplicationSettings. Removed its singleton and moved it to the service locator.

* Moved the ApplicationSettings to the Application folder

* Merged together the BuildSettings and ApplicationSettings

* Reset the UserDefaults too when resetting the application state

* Rename ServiceLocator.applicationSettings to just settings

* Fix indentation

* Rename ApplicationSettings to AppSettings

* Various tweaks following code review

* Fix unit tests
This commit is contained in:
Stefan Ceriu
2022-12-16 10:02:22 +02:00
committed by GitHub
parent 5b90f37f2e
commit 6f974a4422
39 changed files with 259 additions and 272 deletions
@@ -1,82 +0,0 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import SwiftUI
/// Store Element specific app settings.
final class ElementSettings: ObservableObject {
// MARK: - Constants
public enum UserDefaultsKeys: String {
case timelineStyle
case enableAnalytics
case isIdentifiedForAnalytics
case slidingSyncProxyBaseURLString
case enableInAppNotifications
case pusherProfileTag
}
static let shared = ElementSettings()
/// UserDefaults to be used on reads and writes.
static var store: UserDefaults {
guard let userDefaults = UserDefaults(suiteName: InfoPlistReader.target.appGroupIdentifier) else {
fatalError("Fail to load shared UserDefaults")
}
return userDefaults
}
private init() {
// no-op
}
// MARK: - Analytics
/// Whether the user has already been shown the PostHog analytics prompt.
var hasSeenAnalyticsPrompt: Bool {
Self.store.object(forKey: UserDefaultsKeys.enableAnalytics.rawValue) != nil
}
/// `true` when the user has opted in to send analytics.
@AppStorage(UserDefaultsKeys.enableAnalytics.rawValue, store: store)
var enableAnalytics = false
/// Indicates if the device has already called identify for this session to PostHog.
/// This is separate to `enableAnalytics` as logging out leaves analytics
/// enabled, but requires the next account to be identified separately.
@AppStorage(UserDefaultsKeys.isIdentifiedForAnalytics.rawValue, store: store)
var isIdentifiedForAnalytics = false
// MARK: - Room Screen
@AppStorage(UserDefaultsKeys.timelineStyle.rawValue, store: store)
var timelineStyle = BuildSettings.defaultRoomTimelineStyle
// MARK: - Client
@AppStorage(UserDefaultsKeys.slidingSyncProxyBaseURLString.rawValue, store: store)
var slidingSyncProxyBaseURLString = BuildSettings.defaultSlidingSyncProxyBaseURLString
// MARK: - Notifications
@AppStorage(UserDefaultsKeys.enableInAppNotifications.rawValue, store: store)
var enableInAppNotifications = true
@AppStorage(UserDefaultsKeys.pusherProfileTag.rawValue, store: store)
/// Tag describing which set of device specific rules a pusher executes.
var pusherProfileTag: String?
}
@@ -37,7 +37,7 @@ enum PermalinkBuilder {
throw PermalinkBuilderError.invalidUserIdentifier
}
let urlString = "\(BuildSettings.permalinkBaseURL)/#/\(userIdentifier)"
let urlString = "\(ServiceLocator.shared.settings.permalinkBaseURL)/#/\(userIdentifier)"
guard let url = URL(string: urlString) else {
throw PermalinkBuilderError.failedConstructingURL
@@ -75,7 +75,7 @@ enum PermalinkBuilder {
throw PermalinkBuilderError.failedAddingPercentEncoding
}
let urlString = "\(BuildSettings.permalinkBaseURL)/#/\(roomId)/\(eventId)"
let urlString = "\(ServiceLocator.shared.settings.permalinkBaseURL)/#/\(roomId)/\(eventId)"
guard let url = URL(string: urlString) else {
throw PermalinkBuilderError.failedConstructingURL
@@ -91,7 +91,7 @@ enum PermalinkBuilder {
throw PermalinkBuilderError.failedAddingPercentEncoding
}
let urlString = "\(BuildSettings.permalinkBaseURL)/#/\(identifier)"
let urlString = "\(ServiceLocator.shared.settings.permalinkBaseURL)/#/\(identifier)"
guard let url = URL(string: urlString) else {
throw PermalinkBuilderError.failedConstructingURL