SimpleX-Chat/apps/ios/Shared/Views/UserSettings/PreferencesView.swift
JRoberts 07ef6e4090
ios: marked deleted chat items, full deletion preference; android: types (#1473)
* ios: marked deleted chat items; full deletion preference

* text_, menu, backend

* android types

* more android types

* fix

* refactor ios

* restore previews

* box

* refactor menu

* revert unnecessary content.text changes

* Update apps/ios/Shared/Views/Chat/ChatItem/CIVoiceView.swift

Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>

* revert layered framed items

* clever framed view

* improve look

* restore previews

* restore previews

* refactor

* refactoring, almost looks good

* look

* add previews

* more previews

* remove preview of legacy item

* ChatItemDeleted

* flip if

* remove text_

* refactor

* abstract pref property

* move marked deleted

* revert pref change

* undo menu

* fix - change to constants

* undo pref logic

Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com>
2022-12-03 15:40:31 +04:00

78 lines
2.6 KiB
Swift

//
// 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 {
@EnvironmentObject var chatModel: ChatModel
@State var profile: LocalProfile
@State var preferences: FullPreferences
@State var currentPreferences: FullPreferences
var body: some View {
VStack {
List {
featureSection(.fullDelete, $preferences.fullDelete.allow)
featureSection(.voice, $preferences.voice.allow)
Section {
Button("Reset") { preferences = currentPreferences }
Button("Save (and notify contacts)") { savePreferences() }
}
.disabled(currentPreferences == preferences)
}
}
}
private func featureSection(_ feature: ChatFeature, _ 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)
}
}
private func savePreferences() {
Task {
do {
var p = fromLocalProfile(profile)
p.preferences = fullPreferencesToPreferences(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
}
}
} catch {
logger.error("PreferencesView apiUpdateProfile error: \(responseError(error))")
}
}
}
}
struct PreferencesView_Previews: PreviewProvider {
static var previews: some View {
PreferencesView(
profile: LocalProfile(profileId: 1, displayName: "alice", fullName: "", localAlias: ""),
preferences: FullPreferences.sampleData,
currentPreferences: FullPreferences.sampleData
)
}
}