98 lines
3.9 KiB
Swift
98 lines
3.9 KiB
Swift
import SwiftUI
|
|
|
|
struct SendMessageFormView: View {
|
|
@Environment(\.dismiss) private var dismiss
|
|
@ObservedObject var model: MessagesViewModel
|
|
@State private var phoneNumber = ""
|
|
@State private var content = ""
|
|
@State private var validationMessage: String?
|
|
|
|
private var trimmedPhoneNumber: String {
|
|
phoneNumber.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
}
|
|
|
|
private var trimmedContent: String {
|
|
content.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
}
|
|
|
|
private var canSend: Bool {
|
|
!trimmedPhoneNumber.isEmpty && !trimmedContent.isEmpty
|
|
}
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
ZStack {
|
|
AITheme.canvas.ignoresSafeArea()
|
|
ScrollView {
|
|
VStack(spacing: 12) {
|
|
AICard {
|
|
VStack(alignment: .leading, spacing: 14) {
|
|
VStack(alignment: .leading, spacing: 6) {
|
|
AIFieldLabel("实例")
|
|
Picker("实例", selection: Binding(
|
|
get: { model.selectedInstanceID ?? model.instances.first?.id },
|
|
set: { model.selectedInstanceID = $0 }
|
|
)) {
|
|
ForEach(model.instances) { instance in
|
|
Text(instance.name).tag(Optional(instance.id))
|
|
}
|
|
}
|
|
.pickerStyle(.menu)
|
|
.aiSelectSurface()
|
|
}
|
|
|
|
VStack(alignment: .leading, spacing: 6) {
|
|
AIFieldLabel("收件号码")
|
|
TextField("收件号码", text: $phoneNumber)
|
|
.textFieldStyle(AIFieldStyle())
|
|
#if os(iOS)
|
|
.keyboardType(.phonePad)
|
|
#endif
|
|
}
|
|
|
|
VStack(alignment: .leading, spacing: 6) {
|
|
AIFieldLabel("消息内容")
|
|
TextField("消息内容", text: $content, axis: .vertical)
|
|
.textFieldStyle(AIFieldStyle())
|
|
.lineLimit(4...8)
|
|
}
|
|
}
|
|
}
|
|
if let validationMessage {
|
|
AIStatusBanner(validationMessage, tint: AITheme.danger, systemImage: "exclamationmark.triangle")
|
|
}
|
|
}
|
|
.padding(16)
|
|
}
|
|
}
|
|
.navigationTitle("发送消息")
|
|
.aiBottomActionBar {
|
|
HStack(spacing: 10) {
|
|
Button("取消") { dismiss() }
|
|
.buttonStyle(AIButtonStyle(kind: .secondary, size: .regular, fillsWidth: true))
|
|
.frame(maxWidth: .infinity)
|
|
|
|
Button("发送") { send() }
|
|
.buttonStyle(AIButtonStyle(kind: .primary, size: .regular, fillsWidth: true))
|
|
.disabled(!canSend)
|
|
.frame(maxWidth: .infinity)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func send() {
|
|
Task {
|
|
do {
|
|
try await model.send(phoneNumber: trimmedPhoneNumber, content: trimmedContent)
|
|
phoneNumber = ""
|
|
content = ""
|
|
validationMessage = nil
|
|
dismiss()
|
|
} catch {
|
|
validationMessage = "发送失败,请检查号码、内容和实例登录状态。"
|
|
}
|
|
}
|
|
}
|
|
}
|