Skip to content

Instantly share code, notes, and snippets.

@wizard1066
Created April 17, 2020 11:42
Show Gist options
  • Save wizard1066/62547cea25873a5fc8c7e43dc098d9a8 to your computer and use it in GitHub Desktop.
Save wizard1066/62547cea25873a5fc8c7e43dc098d9a8 to your computer and use it in GitHub Desktop.
ddwtp17
import SwiftUI
import Combine
enum MyAppPage {
case Menu
case SecondPage
}
struct ListView: View {
@Binding var name:String
@State var device: String
@State var isSelected: Bool
var body: some View {
Text(device)
.listRowBackground(self.isSelected ? Color.yellow: Color.clear)
.onTapGesture {
self.name = self.device
self.isSelected = !self.isSelected
}
}
}
final class MyAppEnvironmentData: ObservableObject {
@Published var currentPage : MyAppPage? = .Menu
}
struct NavigationTest: View {
var body: some View {
NavigationView {
PageOne()
}
}
}
struct PageOne: View {
@EnvironmentObject var env : MyAppEnvironmentData
@ObservedObject var mobile = BonjourBrowser()
@State var name: String = ""
@State var telegram:String = ""
@State var udpCode = UDPNetwork()
@State var tcpCode = TCPNetwork()
@State var message:String = ""
@State var startSvr = false
@State var searchSvr = false
@State var connectSvr = false
@State var stopStr = false
@State var showingAlert = false
@State var background = Color.yellow
// maximum 32 players in the room
@State var isSelected = [Bool](repeating: false, count: 32)
@State var index = 0
var body: some View {
let navlink = NavigationLink(destination: PageTwo(),
tag: .SecondPage,
selection: $env.currentPage,
label: { EmptyView() })
return VStack {
List {
ForEach(mobile.devices, id: \.self) { each in
ListView(name: self.$name, device: each.device, isSelected: false)
}
}
.font(Fonts.avenirNextCondensedBold(size: 16))
.frame(width: 256, height: 128, alignment: .center)
Text("Dominoes").font(.largeTitle)
.padding()
.onAppear(perform: {
self.udpCode.bonjourUDP(UIDevice.current.name)
DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
self.mobile.seek(typeOf: serviceUDPName)
})
})
.onReceive(resetPublisher) { (_) in
self.name = ""
}
.onReceive(alertPublisher, perform: { (_) in
self.showingAlert = true
})
.alert(isPresented: $showingAlert) {
Alert(title: Text("No client selected"), message: Text("Sorry, You need to select a client first"), dismissButton: .default(Text("Try Again!")))
}
navlink
.frame(width:0, height:0)
EmptyView()
Button("Play") {
self.udpCode.bonjourToUDP(self.name)
self.env.currentPage = .SecondPage
DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
self.udpCode.sendUDP("Hello World")
})
}
.padding()
.border(Color.primary)
}
}
}
struct PageTwo: View {
@State var message: String = "Phew"
@EnvironmentObject var env : MyAppEnvironmentData
var body: some View {
VStack {
Text("Page Two").font(.largeTitle).padding()
Text(message)
.font(Fonts.avenirNextCondensedBold(size: 16))
.padding()
.onReceive(talkingPublisher) { ( data ) in
self.message = "received " + data
}
Text("Go Back")
.padding()
.border(Color.primary)
.onTapGesture {
self.env.currentPage = .Menu
}
}.navigationBarBackButtonHidden(true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment