import SwiftUI struct EditPrivacyView: View { @State private var profilePermissions = ProfilePermissionsResponse() private var privacyScopeOptions: [PrivacyScope] { PrivacyScope.allCases } private var forceAutoDeleteBinding: Binding { Binding( get: { profilePermissions.maxMessageAutoDeleteSeconds ?? 30 }, set: { profilePermissions.maxMessageAutoDeleteSeconds = $0 } ) } private var autoDeleteAccountEnabled: Binding { Binding( get: { profilePermissions.autoDeleteAfterDays != nil }, set: { newValue in profilePermissions.autoDeleteAfterDays = newValue ? (profilePermissions.autoDeleteAfterDays ?? 30) : nil } ) } private var autoDeleteAccountBinding: Binding { Binding( get: { profilePermissions.autoDeleteAfterDays ?? 30 }, set: { profilePermissions.autoDeleteAfterDays = min(max($0, 1), 365) } ) } var body: some View { Form { Section(header: Text("Профиль и поиск")) { Toggle("Разрешить поиск профиля", isOn: $profilePermissions.isSearchable) Toggle("Разрешить пересылку сообщений", isOn: $profilePermissions.allowMessageForwarding) Toggle("Принимать сообщения от незнакомцев", isOn: $profilePermissions.allowMessagesFromNonContacts) } Section(header: Text("Видимость и контент")) { Toggle("Показывать фото не-контактам", isOn: $profilePermissions.showProfilePhotoToNonContacts) Toggle("Показывать био не-контактам", isOn: $profilePermissions.showBioToNonContacts) Toggle("Показывать сторисы не-контактам", isOn: $profilePermissions.showStoriesToNonContacts) Picker("Видимость статуса 'был в сети'", selection: $profilePermissions.lastSeenVisibility) { ForEach(privacyScopeOptions) { scope in Text(scope.title).tag(scope.rawValue) } } .pickerStyle(.segmented) } Section(header: Text("Приглашения и звонки")) { Picker("Кто может приглашать в паблики", selection: $profilePermissions.publicInvitePermission) { ForEach(privacyScopeOptions) { scope in Text(scope.title).tag(scope.rawValue) } } .pickerStyle(.segmented) Picker("Кто может приглашать в беседы", selection: $profilePermissions.groupInvitePermission) { ForEach(privacyScopeOptions) { scope in Text(scope.title).tag(scope.rawValue) } } .pickerStyle(.segmented) Picker("Кто может звонить", selection: $profilePermissions.callPermission) { ForEach(privacyScopeOptions) { scope in Text(scope.title).tag(scope.rawValue) } } .pickerStyle(.segmented) } Section(header: Text("Чаты и хранение")) { Toggle("Разрешить хранить чаты на сервере (Обычный)", isOn: $profilePermissions.allowServerChats) Toggle("Принудительное автоудаление в ЛС (Приватный)", isOn: $profilePermissions.forceAutoDeleteMessagesInPrivate) if profilePermissions.forceAutoDeleteMessagesInPrivate { Stepper(value: forceAutoDeleteBinding, in: 5...86400, step: 5) { Text("Таймер автоудаления: \(formattedAutoDeleteSeconds(forceAutoDeleteBinding.wrappedValue))") } } } Section(header: Text("Автоудаление аккаунта")) { Toggle("Включить автоудаление аккаунта", isOn: autoDeleteAccountEnabled) if autoDeleteAccountEnabled.wrappedValue { Stepper(value: autoDeleteAccountBinding, in: 1...365) { Text("Удалять аккаунт через \(autoDeleteAccountBinding.wrappedValue) дн.") } } } Section { Button("Сохранить изменения") { print("Параметры приватности: \(profilePermissions)") } .frame(maxWidth: .infinity, alignment: .center) } Section { Button(role: .destructive) { profilePermissions = ProfilePermissionsResponse() print("Настройки приватности сброшены к значениям по умолчанию") } label: { Text("Сбросить по умолчанию") .frame(maxWidth: .infinity, alignment: .center) } } } .navigationTitle("Настройки приватности") .onChange(of: profilePermissions.forceAutoDeleteMessagesInPrivate) { newValue in if newValue { profilePermissions.maxMessageAutoDeleteSeconds = profilePermissions.maxMessageAutoDeleteSeconds ?? 30 } else { profilePermissions.maxMessageAutoDeleteSeconds = nil } } } private func formattedAutoDeleteSeconds(_ value: Int) -> String { switch value { case ..<60: return "\(value) сек." case 60..<3600: let minutes = value / 60 return "\(minutes) мин." default: let hours = Double(value) / 3600.0 return String(format: "%.1f ч.", hours) } } } private enum PrivacyScope: Int, CaseIterable, Identifiable { case everyone = 0 case contacts = 1 case nobody = 2 var id: Int { rawValue } var title: String { switch self { case .everyone: return "Все" case .contacts: return "Контакты" case .nobody: return "Никто" } } } struct ProfilePermissionsResponse: Codable { var isSearchable: Bool = true var allowMessageForwarding: Bool = true var allowMessagesFromNonContacts: Bool = true var showProfilePhotoToNonContacts: Bool = true var lastSeenVisibility: Int = PrivacyScope.everyone.rawValue var showBioToNonContacts: Bool = true var showStoriesToNonContacts: Bool = true var allowServerChats: Bool = true var publicInvitePermission: Int = PrivacyScope.everyone.rawValue var groupInvitePermission: Int = PrivacyScope.everyone.rawValue var callPermission: Int = PrivacyScope.everyone.rawValue var forceAutoDeleteMessagesInPrivate: Bool = false var maxMessageAutoDeleteSeconds: Int? = nil var autoDeleteAfterDays: Int? = nil }