SimpleX-Chat/apps/ios/Shared/Views/UserSettings/PreferencesView.swift

81 lines
2.7 KiB
Swift
Raw Normal View History

//
// 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
@Environment(\.dismiss) var dismiss: DismissAction
@State var profile: LocalProfile
@State var preferences: FullPreferences
@State var currentPreferences: FullPreferences
var body: some View {
VStack {
List {
2022-11-16 20:26:43 +04:00
featureSection(.fullDelete, $preferences.fullDelete.allow)
featureSection(.voice, $preferences.voice.allow)
Section {
2022-11-16 20:26:43 +04:00
Button("Reset", role: .destructive) { preferences = currentPreferences }
Button("Save (and notify contacts)") { savePreferences() }
}
2022-11-16 20:26:43 +04:00
.disabled(currentPreferences == preferences)
}
}
}
private func featureSection(_ feature: Feature, _ allowFeature: Binding<FeatureAllowed>) -> some View {
Section {
settingsRow(feature.icon) {
Picker(feature.text, selection: allowFeature) {
ForEach(FeatureAllowed.values) { allow in
Text(allow.text)
}
}
.frame(height: 36)
}
} footer: {
Text(feature.allowDescription(allowFeature.wrappedValue))
.frame(height: 36, alignment: .topLeading)
}
}
2022-11-16 20:26:43 +04:00
private func savePreferences() {
Task {
do {
var p = fromLocalProfile(profile)
p.preferences = toPreferences(preferences)
if let newProfile = try await apiUpdateProfile(profile: p) {
await MainActor.run {
if let profileId = chatModel.currentUser?.profile.profileId {
chatModel.currentUser?.profile = toLocalProfile(profileId, newProfile, "")
chatModel.currentUser?.fullPreferences = preferences
}
currentPreferences = preferences
dismiss()
}
}
} catch {
logger.error("PreferencesView apiUpdateProfile error: \(responseError(error))")
}
}
}
}
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
)
}
}