// // MessageSetting.swift // copinism // // Created by 黄仕杰 on 2024/6/8. // import SwiftUI struct MessageSettingsView: View { @State private var isNotificationEnabled = false @State private var isSpamFilterEnabled = false @State private var isDiscussionEnabled = false var body: some View { NavigationView { Form { if isNotificationEnabled { Section(header: Text("消息设置")) { NavigationLink(destination: Text("接受消息通知")) { Text("接受消息通知") Spacer() Text("已开启") .font(.subheadline) .foregroundColor(.gray) } } } else { Section(header: Text("消息设置")) { VStack(alignment: .leading) { Text("消息通知已关闭") .font(.headline) .padding(.bottom, 2) Text("你已在系统中关闭通知。点击「去开启」前往系统设置-通知中,打开「允许通知」") .font(.subheadline) .foregroundColor(.gray) Button(action: { if let appSettings = URL(string: UIApplication.openSettingsURLString) { UIApplication.shared.open(appSettings, options: [:], completionHandler: nil) } }) { Text("去开启") .foregroundColor(.blue) } } .padding() } } Section(header: Text("消息通知设置")) { Toggle(isOn: $isSpamFilterEnabled) { Text("一键免打扰") Spacer() Text("只接收xxxx消息") .font(.subheadline) .foregroundColor(.gray) } Toggle(isOn: $isDiscussionEnabled) { Text("讨论消息") Spacer() Text("(讨论回复、点赞等)") .font(.subheadline) .foregroundColor(.gray) } } } .navigationTitle("消息设置") .navigationBarTitleDisplayMode(.inline) .onAppear() { isNotificationEnabled = UserDefaults.standard.bool(forKey: "notify") NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: .main) { _ in isNotificationEnabled = UserDefaults.standard.bool(forKey: "notify") } } .onDisappear() { NotificationCenter.default.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil) } } } } #Preview { MessageSettingsView() }