2022-01-21 11:09:33 +00:00
|
|
|
//
|
|
|
|
// ContentView.swift
|
|
|
|
// Shared
|
|
|
|
//
|
|
|
|
// Created by Evgeny Poberezkin on 17/01/2022.
|
|
|
|
//
|
|
|
|
|
2022-01-22 17:54:22 +00:00
|
|
|
//import SwiftUI
|
|
|
|
|
|
|
|
//struct ContentView: View {
|
|
|
|
// @State var messages: [String] = ["Start session:"]
|
|
|
|
// @State var text: String = ""
|
|
|
|
//
|
|
|
|
// func sendMessage() {
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// var body: some View {
|
|
|
|
// VStack {
|
|
|
|
// ScrollView {
|
|
|
|
// LazyVStack {
|
|
|
|
// ForEach(messages, id: \.self) { msg in
|
|
|
|
// MessageView(message: msg, sent: false)
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// .padding(10)
|
|
|
|
// }
|
|
|
|
// .frame(minWidth: 0,
|
|
|
|
// maxWidth: .infinity,
|
|
|
|
// minHeight: 0,
|
|
|
|
// maxHeight: .infinity,
|
|
|
|
// alignment: .topLeading)
|
|
|
|
// HStack {
|
|
|
|
// TextField("Message...", text: $text)
|
|
|
|
// .textFieldStyle(RoundedBorderTextFieldStyle())
|
|
|
|
// .frame(minHeight: CGFloat(30))
|
|
|
|
// Button(action: sendMessage) {
|
|
|
|
// Text("Send")
|
|
|
|
// }.disabled(text.isEmpty)
|
|
|
|
// }
|
|
|
|
// .frame(minHeight: CGFloat(30))
|
|
|
|
// .padding()
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
2022-01-21 11:09:33 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct ContentView: View {
|
2022-01-22 17:54:22 +00:00
|
|
|
|
|
|
|
var controller: controller
|
2022-01-21 11:09:33 +00:00
|
|
|
|
2022-01-22 17:54:22 +00:00
|
|
|
init(controller: controller) {
|
|
|
|
self.controller = controller
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@State private var logbuffer = [String]()
|
|
|
|
@State private var chatcmd: String = ""
|
|
|
|
@State private var chatlog: String = ""
|
|
|
|
@FocusState private var focused: Bool
|
|
|
|
|
|
|
|
func addLine(line: String) {
|
|
|
|
print(line)
|
|
|
|
logbuffer.append(line)
|
|
|
|
if(logbuffer.count > 50) { _ = logbuffer.dropFirst() }
|
|
|
|
chatlog = logbuffer.joined(separator: "\n")
|
2022-01-21 11:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
2022-01-22 17:54:22 +00:00
|
|
|
|
|
|
|
DispatchQueue.global().async {
|
|
|
|
while(true) {
|
|
|
|
let msg = String.init(cString: chat_recv_msg(controller))
|
|
|
|
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
addLine(line: msg)
|
2022-01-21 11:09:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-22 17:54:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return VStack {
|
|
|
|
ScrollView {
|
|
|
|
VStack(alignment: .leading) {
|
|
|
|
HStack { Spacer() }
|
|
|
|
Text(chatlog)
|
|
|
|
.lineLimit(nil)
|
|
|
|
.font(.system(.body, design: .monospaced))
|
|
|
|
}
|
|
|
|
.frame(maxWidth: .infinity)
|
2022-01-21 11:09:33 +00:00
|
|
|
}
|
2022-01-22 17:54:22 +00:00
|
|
|
|
|
|
|
TextField("Chat command", text: $chatcmd)
|
|
|
|
.focused($focused)
|
|
|
|
.onSubmit {
|
|
|
|
print(chatcmd)
|
|
|
|
var cCmd = chatcmd.cString(using: .utf8)!
|
|
|
|
print(String.init(cString: chat_send_cmd(controller, &cCmd)))
|
|
|
|
}
|
|
|
|
.textInputAutocapitalization(.never)
|
|
|
|
.disableAutocorrection(true)
|
|
|
|
.padding()
|
2022-01-21 11:09:33 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-22 17:54:22 +00:00
|
|
|
|
2022-01-21 11:09:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-22 17:54:22 +00:00
|
|
|
|
|
|
|
//struct ContentView_Previews: PreviewProvider {
|
|
|
|
// static var previews: some View {
|
|
|
|
// ContentView(text: "Hello!")
|
|
|
|
// }
|
|
|
|
//}
|