2022-11-14 10:12:17 +00:00
|
|
|
//
|
|
|
|
// PreferencesView.swift
|
|
|
|
// SimpleX (iOS)
|
|
|
|
//
|
|
|
|
// Created by Evgeny on 13/11/2022.
|
|
|
|
// Copyright © 2022 SimpleX Chat. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import SimpleXChat
|
|
|
|
|
|
|
|
struct PreferencesView: View {
|
2022-11-16 20:26:43 +04:00
|
|
|
@EnvironmentObject var chatModel: ChatModel
|
2024-07-03 22:42:13 +01:00
|
|
|
@EnvironmentObject var theme: AppTheme
|
2022-11-16 20:26:43 +04:00
|
|
|
@State var profile: LocalProfile
|
|
|
|
@State var preferences: FullPreferences
|
|
|
|
@State var currentPreferences: FullPreferences
|
2022-11-14 10:12:17 +00:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack {
|
|
|
|
List {
|
2022-12-21 12:59:45 +00:00
|
|
|
timedMessagesFeatureSection($preferences.timedMessages.allow)
|
2022-12-03 15:40:31 +04:00
|
|
|
featureSection(.fullDelete, $preferences.fullDelete.allow)
|
2023-06-19 22:47:49 +01:00
|
|
|
featureSection(.reactions, $preferences.reactions.allow)
|
2022-11-16 20:26:43 +04:00
|
|
|
featureSection(.voice, $preferences.voice.allow)
|
2023-05-13 11:22:31 +02:00
|
|
|
featureSection(.calls, $preferences.calls.allow)
|
2022-11-14 10:12:17 +00:00
|
|
|
|
|
|
|
Section {
|
2022-11-17 12:59:13 +04:00
|
|
|
Button("Reset") { preferences = currentPreferences }
|
2022-11-16 20:26:43 +04:00
|
|
|
Button("Save (and notify contacts)") { savePreferences() }
|
2022-11-14 10:12:17 +00:00
|
|
|
}
|
2022-11-16 20:26:43 +04:00
|
|
|
.disabled(currentPreferences == preferences)
|
2022-11-14 10:12:17 +00:00
|
|
|
}
|
|
|
|
}
|
2024-09-10 09:31:53 +01:00
|
|
|
.onDisappear {
|
|
|
|
if currentPreferences != preferences {
|
|
|
|
showAlert(
|
|
|
|
title: NSLocalizedString("Your chat preferences", comment: "alert title"),
|
|
|
|
message: NSLocalizedString("Chat preferences were changed.", comment: "alert message"),
|
|
|
|
buttonTitle: NSLocalizedString("Save", comment: "alert button"),
|
|
|
|
buttonAction: savePreferences,
|
|
|
|
cancelButton: true
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-11-14 10:12:17 +00:00
|
|
|
}
|
|
|
|
|
2022-11-29 15:19:20 +00:00
|
|
|
private func featureSection(_ feature: ChatFeature, _ allowFeature: Binding<FeatureAllowed>) -> some View {
|
2022-11-14 10:12:17 +00:00
|
|
|
Section {
|
2024-07-03 22:42:13 +01:00
|
|
|
settingsRow(feature.icon, color: theme.colors.secondary) {
|
2022-11-14 10:12:17 +00:00
|
|
|
Picker(feature.text, selection: allowFeature) {
|
|
|
|
ForEach(FeatureAllowed.values) { allow in
|
|
|
|
Text(allow.text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.frame(height: 36)
|
|
|
|
}
|
|
|
|
}
|
2024-07-03 22:42:13 +01:00
|
|
|
footer: { featureFooter(feature, allowFeature).foregroundColor(theme.colors.secondary) }
|
2022-12-21 12:59:45 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private func timedMessagesFeatureSection(_ allowFeature: Binding<FeatureAllowed>) -> some View {
|
|
|
|
Section {
|
|
|
|
let allow = Binding(
|
|
|
|
get: { allowFeature.wrappedValue == .always || allowFeature.wrappedValue == .yes },
|
|
|
|
set: { yes, _ in allowFeature.wrappedValue = yes ? .yes : .no }
|
|
|
|
)
|
2024-07-03 22:42:13 +01:00
|
|
|
settingsRow(ChatFeature.timedMessages.icon, color: theme.colors.secondary) {
|
2022-12-21 12:59:45 +00:00
|
|
|
Toggle(ChatFeature.timedMessages.text, isOn: allow)
|
|
|
|
}
|
|
|
|
}
|
2024-07-03 22:42:13 +01:00
|
|
|
footer: { featureFooter(.timedMessages, allowFeature).foregroundColor(theme.colors.secondary) }
|
2022-12-21 12:59:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func featureFooter(_ feature: ChatFeature, _ allowFeature: Binding<FeatureAllowed>) -> some View {
|
2023-05-13 11:22:31 +02:00
|
|
|
Text(feature.allowDescription(allowFeature.wrappedValue))
|
2022-11-14 10:12:17 +00:00
|
|
|
}
|
2022-11-16 20:26:43 +04:00
|
|
|
|
|
|
|
private func savePreferences() {
|
|
|
|
Task {
|
|
|
|
do {
|
|
|
|
var p = fromLocalProfile(profile)
|
2022-11-25 21:05:14 +04:00
|
|
|
p.preferences = fullPreferencesToPreferences(preferences)
|
2023-08-23 20:43:06 +03:00
|
|
|
if let (newProfile, updatedContacts) = try await apiUpdateProfile(profile: p) {
|
2022-11-16 20:26:43 +04:00
|
|
|
await MainActor.run {
|
2023-01-23 15:48:29 +00:00
|
|
|
chatModel.updateCurrentUser(newProfile, preferences)
|
2023-08-23 20:43:06 +03:00
|
|
|
updatedContacts.forEach(chatModel.updateContact)
|
2022-11-16 20:26:43 +04:00
|
|
|
currentPreferences = preferences
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
logger.error("PreferencesView apiUpdateProfile error: \(responseError(error))")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-14 10:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct PreferencesView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2022-11-16 20:26:43 +04:00
|
|
|
PreferencesView(
|
|
|
|
profile: LocalProfile(profileId: 1, displayName: "alice", fullName: "", localAlias: ""),
|
|
|
|
preferences: FullPreferences.sampleData,
|
|
|
|
currentPreferences: FullPreferences.sampleData
|
|
|
|
)
|
2022-11-14 10:12:17 +00:00
|
|
|
}
|
|
|
|
}
|