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
|
|
|
|
@Environment(\.dismiss) var dismiss: DismissAction
|
|
|
|
@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-11-16 20:26:43 +04:00
|
|
|
featureSection(.fullDelete, $preferences.fullDelete.allow)
|
|
|
|
featureSection(.voice, $preferences.voice.allow)
|
2022-11-14 10:12:17 +00:00
|
|
|
|
|
|
|
Section {
|
2022-11-16 20:26:43 +04:00
|
|
|
Button("Reset", role: .destructive) { preferences = currentPreferences }
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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))")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|