Files
simplex-chat/apps/ios/Shared/Views/UserSettings/SettingsView.swift
T
Narasimha-sc bf905eb545 ui: settings navigation reorganization (#7005)
* android, desktop: settings navigation reorganization

Restructure the root Settings screen into two top-level sections and
fold previously-scattered items into three new sub-screens.

Root:
- Appearance, Your privacy, Chat data
- Help & support, Migrate to another device, Advanced settings

Your privacy (renamed from Privacy & security): keeps Device,
link previews / remove tracking, auto-accept images, blur media,
contact requests from groups. Adds a "More privacy" sub-screen.

More privacy (new): show last messages, message draft, encrypt local
files, protect IP address (with original dynamic footer preserved),
notification preview mode (moved from Notifications), and delivery
receipts.

Help & support (new): merges Help and Support SimpleX Chat sections
into Help / About (with App version) / Contact / Support the project.

Advanced settings (new): Network & servers, Notifications (Android),
Audio & video calls; then Developer tools, Restart and Shutdown
(Android), App update channel (desktop). Notifications is hidden on
desktop because the screen is empty after Show preview moves to
More privacy.

Chat data is the new top-level menu label for the existing
Database passphrase & export screen.

* android, desktop: trim settings reorg diff

- Remove 5 dead strings from base/strings.xml (privacy_and_security,
  database_passphrase_and_export, settings_section_title_chat_database,
  settings_section_title_support, settings_section_title_app) — no
  Kotlin references after the reorg.
- Drop the now single-variant CurrentPage enum in
  NotificationsSettingsView; replace with a direct callback.
- Read userDisplayName locally in HelpAndSupportView instead of
  threading it through SettingsLayout/SettingsView/Preview.

* android, desktop: remove orphan locale strings

Companion to 442a368c9 which removed 5 dead keys from base/strings.xml.
The :common:adjustFormatting task enforces that every locale string has
a corresponding base entry, so these orphans broke the build. Removed
across 35 locale files (154 lines).

Keys removed:
- privacy_and_security
- database_passphrase_and_export
- settings_section_title_chat_database
- settings_section_title_support
- settings_section_title_app

* Revert "android, desktop: remove orphan locale strings"

This reverts commit 0ad5fc9308.

* android, desktop: restore base strings for removed keys

Counterpart to revert of 0ad5fc930: re-add the 5 base entries that
442a368c9 had deleted so the locale files (restored by the prior revert)
are no longer orphaned. Translation keys must not be removed once
introduced — the values can change but the keys stay.

Keys restored to base with their master English values:
- database_passphrase_and_export
- privacy_and_security
- settings_section_title_chat_database
- settings_section_title_support
- settings_section_title_app

* android, desktop: keep share-button helpers in SettingsView

ContributeItem, RateAppItem, StarOnGithubItem were moved from
SettingsView.kt to HelpAndSupportView.kt as part of the reorg.
Move them back: just drop the `private` modifier (one-word edit
per function) so HelpAndSupportView can call them in place. Saves
~60 lines of diff churn vs the move + matches the file's existing
pattern where helpers like AppVersionItem, ChatPreferencesItem,
ChatLockItem, etc. are all public top-level @Composable.

* android, desktop: inline HelpAndSupportView into SettingsView.kt

HelpAndSupportView is the only call site of SettingsView.kt's
share-button helpers; placing it as a top-level @Composable in
SettingsView.kt keeps the help/about/contact/support flow next to
the other settings entry points and removes the need for a new file.
Three imports (BuildConfigCommon, SimpleXInfo, WhatsNewView) that
the reorg was deleting from SettingsView.kt stay in place. Saves
~35 lines of diff and one new file.

* android, desktop: inline AdvancedSettingsView into SettingsView.kt

Same treatment as HelpAndSupportView in the previous commit:
AdvancedSettingsView is only reached from SettingsLayout, so the
function and its expect declaration live as top-level @Composable
in SettingsView.kt instead of a new file. NetworkAndServersView
import that the reorg was deleting from SettingsView.kt stays.
The .android.kt / .desktop.kt actuals are unchanged and keep
implementing the (now relocated) expect. Saves ~15 lines and a file.

* ios: settings navigation reorganization

Mirror the multiplatform reorg on iOS:

Root SettingsView: collapse the 5 sections into 2 unlabeled groups —
{Appearance, Your privacy, Chat data} and {Help & support, Migrate to
another device, Advanced settings}. "Privacy & security" becomes
"Your privacy"; the database row label becomes "Chat data".

PrivacySettings: keeps Device, link previews / remove tracking,
auto-accept images, blur media, contact requests from groups. Adds a
"More privacy" link.

MorePrivacy (new, inlined in PrivacySettings.swift): show last
messages, message draft, encrypt local files, protect IP address
(with original dynamic footer preserved), notification preview mode
(moved from NotificationsView), delivery receipts. Own state and
private helpers for the moved set* functions.

HelpAndSupportView (new, inlined in SettingsView.swift): merges Help
and Support sections into Help / About (with App version) / Contact /
Support the project.

AdvancedSettingsView (new, inlined in SettingsView.swift): Network &
servers, Notifications, Audio & video calls, Developer tools. iOS has
no Restart/Shutdown (Android-only) or App update channel (desktop).

NotificationsView: "Show preview" navigation removed — it now lives
in MorePrivacy. notificationsIcon() promoted to a free function so
AdvancedSettingsView can render the notifications status badge.

* android, desktop: keep platform file names as SettingsView.{android,desktop}.kt

Revert the file renames from {Advanced}SettingsView.{android,desktop}.kt.
Function rename SettingsSectionApp → AdvancedSettingsAppSection stays
inside each file; only the file path returns to its original name. No
behavior change; diff stat now shows two in-place modifications instead
of renames.

* ios: keep PrivacySettings/SettingsView state in place, use inline destinations

Restructure the iOS reorg to avoid moving state, helpers, and the alert
enum out of PrivacySettings — and to avoid moving notificationsIcon
out of SettingsView. The Help & Support, Advanced Settings, and
More Privacy "screens" become private computed properties on their
parent struct, so all @AppStorage, @State, set* helpers, and the
PrivacySettingsViewAlert enum stay UNCHANGED from master. NavigationLink
destinations reference the computed properties directly.

Net iOS diff vs master: 220+/154- (was 361+/259-) — saves ~245 lines.

* simplex settings

* android, desktop: mirror iOS settings reorganization

- inline Advanced settings section into the main settings list (Network &
  servers, Notifications, Audio & video calls, App version); remove the
  separate Advanced settings page
- reorder first section: Appearance, Your privacy, Help & support, Chat data,
  Migrate; merge About SimpleX Chat into the Help section
- move the developer/maintenance section under App version (VersionInfoView);
  load core version inside the view so it always opens (Developer tools and
  Shutdown stay reachable even if the version request fails)
- keep "Developer tools" label (not renamed to "Developer")
- replace the Restart row with Cancel/Restart/Shutdown options in the
  Shutdown dialog
- split DatabaseView: "Chat data" page (messages TTL, Database passphrase &
  export, Files & media) and a sub-page with passphrase/export/import/delete
  and the Run chat toggle; rename title to "Chat data"
- align delivery receipts alert wording with the renamed "Your privacy" settings

* android, desktop: simplify settings reorg internals

- VersionInfoView: drop the section/card wrapping, keep the original plain
  version-text layout; load core version in-view so the screen always opens
- DatabaseView: make the "Database passphrase & export" sub-page a
  self-contained DatabaseManagementView that owns its own state, mirroring the
  DatabaseView/DatabaseLayout pattern instead of threading params through a modal

* android, desktop: show App version screen as a card screen

VersionInfoView now hosts a settings section (Developer tools / updates), so
open it with cardScreen = true like the other settings screens — otherwise the
section renders without the card box around it.

* android, desktop: show "Rate the app" only on mobile

The action opens a Play Store link, which does nothing on desktop (the
market:// scheme has no handler and the web fallback never fires). Gate it to
Android, like the Contribute item.

* android, desktop: move Shutdown to settings above app version

Move the Shutdown action out of the app version screen into the main
advanced settings section, just above the app version row. It stays
Android only (desktop is closed via the window) through an
AppShutdownItem expect/actual.

* android, desktop: show app version info in its own card

Wrap the version info block on the app version screen in a section card,
matching iOS and the rest of the card-screen settings.

* fix background

---------

Co-authored-by: Evgeny Poberezkin <evgeny@poberezkin.com>
2026-06-07 23:38:05 +01:00

565 lines
23 KiB
Swift

//
// SettingsView.swift
// SimpleX
//
// Created by Evgeny Poberezkin on 31/01/2022.
// Copyright © 2022 SimpleX Chat. All rights reserved.
//
// Spec: spec/client/navigation.md
import SwiftUI
import StoreKit
import SimpleXChat
let simplexTeamURL = URL(string: "simplex:/a#lrdvu2d8A1GumSmoKb2krQmtKhWXq-tyGpHuM7aMwsw?h=smp6.simplex.im")!
let appVersion = Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String
let appBuild = Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String
let DEFAULT_SHOW_LA_NOTICE = "showLocalAuthenticationNotice"
let DEFAULT_LA_NOTICE_SHOWN = "localAuthenticationNoticeShown"
let DEFAULT_PERFORM_LA = "performLocalAuthentication" // deprecated, moved to app group
let DEFAULT_LA_MODE = "localAuthenticationMode"
let DEFAULT_LA_LOCK_DELAY = "localAuthenticationLockDelay"
let DEFAULT_LA_SELF_DESTRUCT = "localAuthenticationSelfDestruct"
let DEFAULT_LA_SELF_DESTRUCT_DISPLAY_NAME = "localAuthenticationSelfDestructDisplayName"
let DEFAULT_NOTIFICATION_ALERT_SHOWN = "notificationAlertShown"
let DEFAULT_WEBRTC_POLICY_RELAY = "webrtcPolicyRelay"
let DEFAULT_WEBRTC_ICE_SERVERS = "webrtcICEServers"
let DEFAULT_CALL_KIT_CALLS_IN_RECENTS = "callKitCallsInRecents"
let DEFAULT_PRIVACY_ACCEPT_IMAGES = "privacyAcceptImages" // unused. Use GROUP_DEFAULT_PRIVACY_ACCEPT_IMAGES instead
let DEFAULT_PRIVACY_LINK_PREVIEWS = "privacyLinkPreviews" // deprecated, moved to app group
let DEFAULT_PRIVACY_SIMPLEX_LINK_MODE = "privacySimplexLinkMode"
let DEFAULT_PRIVACY_SHOW_CHAT_PREVIEWS = "privacyShowChatPreviews"
let DEFAULT_PRIVACY_SAVE_LAST_DRAFT = "privacySaveLastDraft"
let DEFAULT_PRIVACY_PROTECT_SCREEN = "privacyProtectScreen"
let DEFAULT_PRIVACY_DELIVERY_RECEIPTS_SET = "privacyDeliveryReceiptsSet"
let DEFAULT_PRIVACY_MEDIA_BLUR_RADIUS = "privacyMediaBlurRadius"
let DEFAULT_EXPERIMENTAL_CALLS = "experimentalCalls"
let DEFAULT_CHAT_ARCHIVE_NAME = "chatArchiveName"
let DEFAULT_CHAT_ARCHIVE_TIME = "chatArchiveTime"
let DEFAULT_CHAT_V3_DB_MIGRATION = "chatV3DBMigration"
let DEFAULT_SHOULD_IMPORT_APP_SETTINGS = "shouldImportAppSettings"
let DEFAULT_DEVELOPER_TOOLS = "developerTools"
let DEFAULT_ENCRYPTION_STARTED = "encryptionStarted"
let DEFAULT_ENCRYPTION_STARTED_AT = "encryptionStartedAt"
let DEFAULT_ACCENT_COLOR_RED = "accentColorRed" // deprecated, only used for migration
let DEFAULT_ACCENT_COLOR_GREEN = "accentColorGreen" // deprecated, only used for migration
let DEFAULT_ACCENT_COLOR_BLUE = "accentColorBlue" // deprecated, only used for migration
let DEFAULT_USER_INTERFACE_STYLE = "userInterfaceStyle" // deprecated, only used for migration
let DEFAULT_PROFILE_IMAGE_CORNER_RADIUS = "profileImageCornerRadius"
let DEFAULT_CHAT_ITEM_ROUNDNESS = "chatItemRoundness"
let DEFAULT_CHAT_ITEM_TAIL = "chatItemTail"
let DEFAULT_ONE_HAND_UI_CARD_SHOWN = "oneHandUICardShown"
let DEFAULT_ADDRESS_CREATION_CARD_SHOWN = "addressCreationCardShown"
let DEFAULT_TOOLBAR_MATERIAL = "toolbarMaterial"
let DEFAULT_CONNECT_VIA_LINK_TAB = "connectViaLinkTab"
let DEFAULT_LIVE_MESSAGE_ALERT_SHOWN = "liveMessageAlertShown"
let DEFAULT_SHOW_HIDDEN_PROFILES_NOTICE = "showHiddenProfilesNotice"
let DEFAULT_SHOW_MUTE_PROFILE_ALERT = "showMuteProfileAlert"
let DEFAULT_SHOW_REPORTS_IN_SUPPORT_CHAT_ALERT = "showReportsInSupportChatAlert"
let DEFAULT_WHATS_NEW_VERSION = "defaultWhatsNewVersion"
let DEFAULT_ONBOARDING_STAGE = "onboardingStage"
let DEFAULT_MIGRATION_TO_STAGE = "migrationToStage"
let DEFAULT_MIGRATION_FROM_STAGE = "migrationFromStage"
let DEFAULT_CUSTOM_DISAPPEARING_MESSAGE_TIME = "customDisappearingMessageTime"
let DEFAULT_SHOW_UNREAD_AND_FAVORITES = "showUnreadAndFavorites"
let DEFAULT_DEVICE_NAME_FOR_REMOTE_ACCESS = "deviceNameForRemoteAccess"
let DEFAULT_CONFIRM_REMOTE_SESSIONS = "confirmRemoteSessions"
let DEFAULT_CONNECT_REMOTE_VIA_MULTICAST = "connectRemoteViaMulticast"
let DEFAULT_CONNECT_REMOTE_VIA_MULTICAST_AUTO = "connectRemoteViaMulticastAuto"
let DEFAULT_SHOW_DELETE_CONVERSATION_NOTICE = "showDeleteConversationNotice"
let DEFAULT_SHOW_DELETE_CONTACT_NOTICE = "showDeleteContactNotice"
let DEFAULT_SHOW_SENT_VIA_RPOXY = "showSentViaProxy"
let DEFAULT_SHOW_SUBSCRIPTION_PERCENTAGE = "showSubscriptionPercentage"
let DEFAULT_CURRENT_THEME = "currentTheme"
let DEFAULT_SYSTEM_DARK_THEME = "systemDarkTheme"
let DEFAULT_CURRENT_THEME_IDS = "currentThemeIds"
let DEFAULT_THEME_OVERRIDES = "themeOverrides"
let DEFAULT_NETWORK_PROXY = "networkProxy"
let ANDROID_DEFAULT_CALL_ON_LOCK_SCREEN = "androidCallOnLockScreen"
let defaultChatItemRoundness: Double = 0.75
let appDefaults: [String: Any] = [
DEFAULT_SHOW_LA_NOTICE: false,
DEFAULT_LA_NOTICE_SHOWN: false,
DEFAULT_PERFORM_LA: false,
DEFAULT_LA_MODE: LAMode.system.rawValue,
DEFAULT_LA_LOCK_DELAY: 30,
DEFAULT_LA_SELF_DESTRUCT: false,
DEFAULT_NOTIFICATION_ALERT_SHOWN: false,
DEFAULT_WEBRTC_POLICY_RELAY: true,
DEFAULT_CALL_KIT_CALLS_IN_RECENTS: false,
DEFAULT_PRIVACY_ACCEPT_IMAGES: true,
DEFAULT_PRIVACY_LINK_PREVIEWS: true,
DEFAULT_PRIVACY_SIMPLEX_LINK_MODE: SimpleXLinkMode.description.rawValue,
DEFAULT_PRIVACY_SHOW_CHAT_PREVIEWS: true,
DEFAULT_PRIVACY_SAVE_LAST_DRAFT: true,
DEFAULT_PRIVACY_PROTECT_SCREEN: false,
DEFAULT_PRIVACY_DELIVERY_RECEIPTS_SET: false,
DEFAULT_PRIVACY_MEDIA_BLUR_RADIUS: 0,
DEFAULT_EXPERIMENTAL_CALLS: false,
DEFAULT_CHAT_V3_DB_MIGRATION: V3DBMigrationState.offer.rawValue,
DEFAULT_DEVELOPER_TOOLS: false,
DEFAULT_ENCRYPTION_STARTED: false,
DEFAULT_PROFILE_IMAGE_CORNER_RADIUS: defaultProfileImageCorner,
DEFAULT_CHAT_ITEM_ROUNDNESS: defaultChatItemRoundness,
DEFAULT_CHAT_ITEM_TAIL: true,
DEFAULT_ONE_HAND_UI_CARD_SHOWN: false,
DEFAULT_ADDRESS_CREATION_CARD_SHOWN: false,
DEFAULT_TOOLBAR_MATERIAL: ToolbarMaterial.defaultMaterial,
DEFAULT_CONNECT_VIA_LINK_TAB: ConnectViaLinkTab.scan.rawValue,
DEFAULT_LIVE_MESSAGE_ALERT_SHOWN: false,
DEFAULT_SHOW_HIDDEN_PROFILES_NOTICE: true,
DEFAULT_SHOW_MUTE_PROFILE_ALERT: true,
DEFAULT_SHOW_REPORTS_IN_SUPPORT_CHAT_ALERT: true,
DEFAULT_ONBOARDING_STAGE: OnboardingStage.onboardingComplete.rawValue,
DEFAULT_CUSTOM_DISAPPEARING_MESSAGE_TIME: 300,
DEFAULT_SHOW_UNREAD_AND_FAVORITES: false,
DEFAULT_CONFIRM_REMOTE_SESSIONS: false,
DEFAULT_CONNECT_REMOTE_VIA_MULTICAST: true,
DEFAULT_CONNECT_REMOTE_VIA_MULTICAST_AUTO: true,
DEFAULT_SHOW_DELETE_CONVERSATION_NOTICE: true,
DEFAULT_SHOW_DELETE_CONTACT_NOTICE: true,
DEFAULT_SHOW_SENT_VIA_RPOXY: false,
DEFAULT_SHOW_SUBSCRIPTION_PERCENTAGE: false,
ANDROID_DEFAULT_CALL_ON_LOCK_SCREEN: AppSettingsLockScreenCalls.show.rawValue,
DEFAULT_THEME_OVERRIDES: "{}",
DEFAULT_CURRENT_THEME: DefaultTheme.SYSTEM_THEME_NAME,
DEFAULT_SYSTEM_DARK_THEME: DefaultTheme.DARK.themeName,
DEFAULT_CURRENT_THEME_IDS: "{}"
]
// only Bool defaults can be used here,
// or hintDefaultsUnchanged and resetHintDefaults need to be changed
let hintDefaults = [
DEFAULT_LA_NOTICE_SHOWN,
DEFAULT_ONE_HAND_UI_CARD_SHOWN,
DEFAULT_ADDRESS_CREATION_CARD_SHOWN,
DEFAULT_LIVE_MESSAGE_ALERT_SHOWN,
DEFAULT_SHOW_HIDDEN_PROFILES_NOTICE,
DEFAULT_SHOW_MUTE_PROFILE_ALERT,
DEFAULT_SHOW_REPORTS_IN_SUPPORT_CHAT_ALERT,
DEFAULT_SHOW_DELETE_CONVERSATION_NOTICE,
DEFAULT_SHOW_DELETE_CONTACT_NOTICE
]
let hintGroupDefaults = [
GROUP_DEFAULT_PRIVACY_LINK_PREVIEWS_SHOW_ALERT
]
// not used anymore
enum ConnectViaLinkTab: String {
case scan
case paste
}
enum SimpleXLinkMode: String, Identifiable {
case description
case full
case browser
static var values: [SimpleXLinkMode] = [.description, .full]
public var id: Self { self }
var text: LocalizedStringKey {
switch self {
case .description: return "Description"
case .full: return "Full link"
case .browser: return "Via browser"
}
}
}
private var indent: CGFloat = 36
let chatArchiveTimeDefault = DateDefault(defaults: UserDefaults.standard, forKey: DEFAULT_CHAT_ARCHIVE_TIME)
let encryptionStartedDefault = BoolDefault(defaults: UserDefaults.standard, forKey: DEFAULT_ENCRYPTION_STARTED)
let encryptionStartedAtDefault = DateDefault(defaults: UserDefaults.standard, forKey: DEFAULT_ENCRYPTION_STARTED_AT)
let connectViaLinkTabDefault = EnumDefault<ConnectViaLinkTab>(defaults: UserDefaults.standard, forKey: DEFAULT_CONNECT_VIA_LINK_TAB, withDefault: .scan)
let privacySimplexLinkModeDefault = EnumDefault<SimpleXLinkMode>(defaults: UserDefaults.standard, forKey: DEFAULT_PRIVACY_SIMPLEX_LINK_MODE, withDefault: .description)
let privacyLocalAuthModeDefault = EnumDefault<LAMode>(defaults: UserDefaults.standard, forKey: DEFAULT_LA_MODE, withDefault: .system)
let privacyDeliveryReceiptsSet = BoolDefault(defaults: UserDefaults.standard, forKey: DEFAULT_PRIVACY_DELIVERY_RECEIPTS_SET)
let onboardingStageDefault = EnumDefault<OnboardingStage>(defaults: UserDefaults.standard, forKey: DEFAULT_ONBOARDING_STAGE, withDefault: .onboardingComplete)
let customDisappearingMessageTimeDefault = IntDefault(defaults: UserDefaults.standard, forKey: DEFAULT_CUSTOM_DISAPPEARING_MESSAGE_TIME)
let showDeleteConversationNoticeDefault = BoolDefault(defaults: UserDefaults.standard, forKey: DEFAULT_SHOW_DELETE_CONVERSATION_NOTICE)
let showDeleteContactNoticeDefault = BoolDefault(defaults: UserDefaults.standard, forKey: DEFAULT_SHOW_DELETE_CONTACT_NOTICE)
let showReportsInSupportChatAlertDefault = BoolDefault(defaults: UserDefaults.standard, forKey: DEFAULT_SHOW_REPORTS_IN_SUPPORT_CHAT_ALERT)
/// after importing new database, this flag will be set and unset only after importing app settings in `initializeChat` */
let shouldImportAppSettingsDefault = BoolDefault(defaults: UserDefaults.standard, forKey: DEFAULT_SHOULD_IMPORT_APP_SETTINGS)
let currentThemeDefault = StringDefault(defaults: UserDefaults.standard, forKey: DEFAULT_CURRENT_THEME, withDefault: DefaultTheme.SYSTEM_THEME_NAME)
let systemDarkThemeDefault = StringDefault(defaults: UserDefaults.standard, forKey: DEFAULT_SYSTEM_DARK_THEME, withDefault: DefaultTheme.DARK.themeName)
let currentThemeIdsDefault = CodableDefault<[String: String]>(defaults: UserDefaults.standard, forKey: DEFAULT_CURRENT_THEME_IDS, withDefault: [:] )
let themeOverridesDefault: CodableDefault<[ThemeOverrides]> = CodableDefault(defaults: UserDefaults.standard, forKey: DEFAULT_THEME_OVERRIDES, withDefault: [])
func setGroupDefaults() {
privacyAcceptImagesGroupDefault.set(UserDefaults.standard.bool(forKey: DEFAULT_PRIVACY_ACCEPT_IMAGES))
appLocalAuthEnabledGroupDefault.set(UserDefaults.standard.bool(forKey: DEFAULT_PERFORM_LA))
privacyLinkPreviewsGroupDefault.set(UserDefaults.standard.bool(forKey: DEFAULT_PRIVACY_LINK_PREVIEWS))
profileImageCornerRadiusGroupDefault.set(UserDefaults.standard.double(forKey: DEFAULT_PROFILE_IMAGE_CORNER_RADIUS))
}
public class StringDefault {
var defaults: UserDefaults
var key: String
var defaultValue: String
public init(defaults: UserDefaults = UserDefaults.standard, forKey: String, withDefault: String) {
self.defaults = defaults
self.key = forKey
self.defaultValue = withDefault
}
public func get() -> String {
defaults.string(forKey: key) ?? defaultValue
}
public func set(_ value: String) {
defaults.set(value, forKey: key)
defaults.synchronize()
}
}
public class CodableDefault<T: Codable> {
var defaults: UserDefaults
var key: String
var defaultValue: T
public init(defaults: UserDefaults = UserDefaults.standard, forKey: String, withDefault: T) {
self.defaults = defaults
self.key = forKey
self.defaultValue = withDefault
}
var cache: T? = nil
public func get() -> T {
if let cache {
return cache
} else if let value = defaults.string(forKey: key) {
let res = decodeJSON(value) ?? defaultValue
cache = res
return res
}
return defaultValue
}
public func set(_ value: T) {
defaults.set(encodeJSON(value), forKey: key)
cache = value
//defaults.synchronize()
}
}
let networkProxyDefault: CodableDefault<NetworkProxy> = CodableDefault(defaults: UserDefaults.standard, forKey: DEFAULT_NETWORK_PROXY, withDefault: NetworkProxy.def)
struct SettingsView: View {
@Environment(\.colorScheme) var colorScheme
@Environment(\.dismiss) var dismiss
@EnvironmentObject var chatModel: ChatModel
@EnvironmentObject var sceneDelegate: SceneDelegate
@EnvironmentObject var theme: AppTheme
@State private var showProgress: Bool = false
var body: some View {
ZStack {
settingsView()
if showProgress {
progressView()
}
}
}
func settingsView() -> some View {
List {
Section(header: Text(verbatim: "").foregroundColor(theme.colors.secondary)) {
if UIApplication.shared.supportsAlternateIcons {
NavigationLink {
AppearanceSettings()
.navigationTitle("Appearance")
.modifier(ThemedBackground(grouped: true))
} label: {
settingsRow("sun.max", color: theme.colors.secondary) { Text("Appearance") }
}
.disabled(chatModel.chatRunning != true)
}
NavigationLink {
PrivacySettings()
.navigationTitle("Your privacy")
.modifier(ThemedBackground(grouped: true))
} label: {
settingsRow("lock", color: theme.colors.secondary) { Text("Your privacy") }
}
.disabled(chatModel.chatRunning != true)
NavigationLink {
helpAndSupportView
} label: {
settingsRow("questionmark", color: theme.colors.secondary) { Text("Help & support") }
}
chatDatabaseRow()
NavigationLink {
MigrateFromDevice(showProgressOnSettings: $showProgress)
.toolbar {
// Redaction broken for `.navigationTitle` - using a toolbar item instead.
ToolbarItem(placement: .principal) {
Text("Migrate device").font(.headline)
}
}
.modifier(ThemedBackground(grouped: true))
.navigationBarTitleDisplayMode(.large)
} label: {
settingsRow("tray.and.arrow.up", color: theme.colors.secondary) { Text("Migrate to another device") }
}
}
Section(header: Text("Advanced settings").foregroundColor(theme.colors.secondary)) {
NavigationLink {
NetworkAndServers()
.navigationTitle("Network & servers")
.modifier(ThemedBackground(grouped: true))
} label: {
settingsRow("externaldrive.connected.to.line.below", color: theme.colors.secondary) { Text("Network & servers") }
}
.disabled(chatModel.chatRunning != true)
NavigationLink {
NotificationsView()
.navigationTitle("Notifications")
.modifier(ThemedBackground(grouped: true))
} label: {
HStack {
notificationsIcon()
Text("Notifications")
}
}
.disabled(chatModel.chatRunning != true)
NavigationLink {
CallSettings()
.navigationTitle("Your calls")
.modifier(ThemedBackground(grouped: true))
} label: {
settingsRow("video", color: theme.colors.secondary) { Text("Audio & video calls") }
}
.disabled(chatModel.chatRunning != true)
NavigationLink {
VersionView()
.navigationBarTitle("App version")
.modifier(ThemedBackground())
} label: {
Text(verbatim: "v\(appVersion ?? "?")")
}
}
}
.navigationTitle("Your settings")
.modifier(ThemedBackground(grouped: true))
.onDisappear {
chatModel.showingTerminal = false
chatModel.terminalItems = []
}
}
@ViewBuilder
private var helpAndSupportView: some View {
List {
let user = chatModel.currentUser
Section(header: Text("Help").foregroundColor(theme.colors.secondary)) {
if let user = user {
NavigationLink {
ChatHelp(dismissSettingsSheet: dismiss)
.navigationTitle("Welcome \(user.displayName)!")
.modifier(ThemedBackground())
.frame(maxHeight: .infinity, alignment: .top)
} label: {
settingsRow("questionmark", color: theme.colors.secondary) { Text("How to use it") }
}
}
NavigationLink {
WhatsNewView(viaSettings: true, updatedConditions: false)
.modifier(ThemedBackground())
.navigationBarTitleDisplayMode(.inline)
} label: {
settingsRow("plus", color: theme.colors.secondary) { Text("What's new") }
}
NavigationLink {
SimpleXInfo(onboarding: false)
.navigationBarTitle("", displayMode: .inline)
.modifier(ThemedBackground())
.frame(maxHeight: .infinity, alignment: .top)
} label: {
settingsRow("info", color: theme.colors.secondary) { Text("About SimpleX Chat") }
}
}
Section(header: Text("Contact").foregroundColor(theme.colors.secondary)) {
settingsRow("number", color: theme.colors.secondary) {
Button("Send questions and ideas") {
dismiss()
DispatchQueue.main.async {
UIApplication.shared.open(simplexTeamURL)
}
}
}
.disabled(chatModel.chatRunning != true)
settingsRow("envelope", color: theme.colors.secondary) { Text("[Send us email](mailto:chat@simplex.chat)") }
}
Section(header: Text("Support the project").foregroundColor(theme.colors.secondary)) {
settingsRow("keyboard", color: theme.colors.secondary) {
ExternalLink("Contribute", destination: URL(string: "https://github.com/simplex-chat/simplex-chat#contribute")!)
}
settingsRow("star", color: theme.colors.secondary) {
Button("Rate the app") {
if let scene = sceneDelegate.windowScene {
SKStoreReviewController.requestReview(in: scene)
}
}
}
ExternalLink(destination: URL(string: "https://github.com/simplex-chat/simplex-chat")!) {
ZStack(alignment: .leading) {
Image(colorScheme == .dark ? "github_light" : "github")
.resizable()
.frame(width: 24, height: 24)
.opacity(0.5)
.colorMultiply(theme.colors.secondary)
Text("Star on GitHub")
.padding(.leading, indent)
}
}
}
}
.navigationTitle("Help & support")
.modifier(ThemedBackground(grouped: true))
}
private func chatDatabaseRow() -> some View {
NavigationLink {
DatabaseView(dismissSettingsSheet: dismiss, chatItemTTL: chatModel.chatItemTTL)
.navigationTitle("Chat data")
.modifier(ThemedBackground(grouped: true))
} label: {
let color: Color = chatModel.chatDbEncrypted == false ? .orange : theme.colors.secondary
settingsRow("internaldrive", color: color) {
HStack {
Text("Chat data")
Spacer()
if chatModel.chatRunning == false {
Image(systemName: "exclamationmark.octagon.fill").foregroundColor(.red)
}
}
}
}
}
private func progressView() -> some View {
VStack {
ProgressView().scaleEffect(2)
}
.frame(maxWidth: .infinity, maxHeight: .infinity )
}
private enum NotificationAlert {
case enable
case error(LocalizedStringKey, String)
}
private func notificationsIcon() -> some View {
let icon: String
let color: Color
switch (chatModel.tokenStatus) {
case .new:
icon = "bolt"
color = theme.colors.secondary
case .registered:
icon = "bolt.fill"
color = theme.colors.secondary
case .invalid: fallthrough
case .invalidBad: fallthrough
case .invalidTopic: fallthrough
case .invalidExpired: fallthrough
case .invalidUnregistered:
icon = "bolt.slash"
color = theme.colors.secondary
case .confirmed:
icon = "bolt.fill"
color = .yellow
case .active:
icon = "bolt.fill"
color = .green
case .expired:
icon = "bolt.slash.fill"
color = theme.colors.secondary
case .none:
icon = "bolt"
color = theme.colors.secondary
}
return Image(systemName: icon)
.padding(.trailing, 9)
.foregroundColor(color)
}
}
func settingsRow<Content : View>(_ icon: String, color: Color/* = .secondary*/, content: @escaping () -> Content) -> some View {
ZStack(alignment: .leading) {
Image(systemName: icon).frame(maxWidth: 24, maxHeight: 24, alignment: .center)
.symbolRenderingMode(.monochrome)
.foregroundColor(color)
content().padding(.leading, indent)
}
}
struct ProfilePreview: View {
var profileOf: NamedChat
var color = Color(uiColor: .tertiarySystemGroupedBackground)
var body: some View {
HStack {
ProfileImage(imageStr: profileOf.image, size: 44, color: color)
.padding(.trailing, 6)
profileName(profileOf).lineLimit(1)
}
}
}
func profileName(_ profileOf: NamedChat) -> Text {
var t = Text(profileOf.displayName).fontWeight(.semibold).font(.title2)
if profileOf.fullName != "" && profileOf.fullName != profileOf.displayName {
t = t + Text(verbatim: " (" + profileOf.fullName + ")")
// .font(.callout)
}
return t
}
struct SettingsView_Previews: PreviewProvider {
static var previews: some View {
let chatModel = ChatModel()
chatModel.currentUser = User.sampleData
return SettingsView()
.environmentObject(chatModel)
}
}