NodeView.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. //
  2. // NodeView.swift
  3. // naiyoup
  4. //
  5. // Created by C Auto on 2021/6/4.
  6. //
  7. import SwiftUI
  8. import SwiftUIX
  9. import SwiftyPing
  10. import Combine
  11. class RowData: ObservableObject {
  12. //var didChange = PassthroughSubject<Void, Never>()
  13. @Published var value: String = ""
  14. @Published var valueColor: String = ""
  15. //@Published var isPing = false
  16. init() {
  17. //update()
  18. }
  19. func update(hosts : Int) {
  20. log.debug(hosts)
  21. if(hosts <= 49 && hosts >= 1){
  22. self.value = "流畅"
  23. self.valueColor = "#00FF00"
  24. } else if(hosts >= 50 || hosts >= 100){
  25. self.value = "繁忙"
  26. self.valueColor = "#FF0000"
  27. } else if(hosts <= 0){
  28. self.value = "故障"
  29. self.valueColor = "#696969"
  30. }
  31. // let once = try? SwiftyPing(host: hosts, configuration: PingConfiguration(interval: 0.5, with: 5), queue: DispatchQueue.global())
  32. // once?.observer = { (response) in
  33. // //let duration = response.duration
  34. // DispatchQueue.main.async {
  35. // let ms = Int(response.duration * 1000)
  36. // self.value = "\(ms) ms"
  37. // log.debug(self.value)
  38. // //self.isPing = false
  39. // }
  40. //
  41. // }
  42. // once?.targetCount = 1
  43. // try? once?.startPinging()
  44. //self.isPing = true
  45. }
  46. }
  47. struct PingView: View {
  48. var name: Int? = 0
  49. @ObservedObject var loader: RowData
  50. init(name: Int?, loader: RowData) {
  51. self.name = name
  52. self.loader = loader
  53. }
  54. var body: some View {
  55. HStack {
  56. Text(self.loader.value)
  57. .foregroundColor(Color.init(hex: self.loader.valueColor))
  58. .padding()
  59. }
  60. .onAppear {
  61. loader.update(hosts: self.name ?? 0)
  62. }
  63. }
  64. }
  65. struct NodelCell: View {
  66. var name = ""
  67. var host = 0
  68. var body: some View {
  69. HStack {
  70. Text(name)
  71. .padding()
  72. Spacer()
  73. //1-30 流畅 31-100 繁忙 0 故障 // Ping once
  74. PingView(name: host , loader: RowData())
  75. }
  76. .contentShape(Rectangle())
  77. .onAppear {
  78. }
  79. }
  80. }
  81. struct ListRemoveSeparator: ViewModifier {
  82. func body(content: Content) -> some View {
  83. content
  84. .onAppear(perform: {
  85. UITableView.appearance().tableFooterView = UIView()
  86. UITableView.appearance().separatorStyle = .none
  87. })
  88. .onDisappear(perform: {
  89. UITableView.appearance().tableFooterView = nil
  90. UITableView.appearance().separatorStyle = .singleLine
  91. })
  92. }
  93. }
  94. struct NodeView: View {
  95. // 追加代码1:声明属性presentationMode
  96. @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode>
  97. @Binding var hosts : String
  98. //@EnvironmentObject var nodelViewModel : NodelViewModel
  99. @ObservedObject var nodelViewModel = NodelViewModel.nodel
  100. @ObservedObject var rowdata = RowData()
  101. //@Binding var nodelModel : NodelModel
  102. @State var ping_msg = "点我检测延迟"
  103. var screenSize: CGRect = UIScreen.main.bounds
  104. var itmesize = 250
  105. var minimumInteritemSpacing : CGFloat = 5.0
  106. var itmesCount : CGFloat = 2.0
  107. var cellWidth : CGFloat {
  108. (screenSize.width / itmesCount) - (minimumInteritemSpacing * 2) - 1
  109. }
  110. //itemCount = (CollectionView.width - sectionInset.left - sectionInset.right + minimumInteritemSpacing) / (itemSize + minimumInteritemSpacing)
  111. var body: some View {
  112. ZStack {
  113. if self.nodelViewModel.loading == true
  114. {
  115. ActivityIndicator(isAnimating: self.nodelViewModel.loading)
  116. .configure { $0.color = .blue}
  117. .padding()
  118. } else if self.nodelViewModel.isAlt == true {
  119. VStack{
  120. Text("\(self.nodelViewModel.errmsg)")
  121. .foregroundColor(Color.black)
  122. Text("点击重试")
  123. .foregroundColor(Color.black)
  124. .onTapGesture {
  125. self.nodelViewModel.GetNode()
  126. }
  127. }
  128. } else {
  129. Color("NodeItem")
  130. List{
  131. ForEach(0..<self.nodelViewModel.nodeList.count,id:\.self){ index in
  132. NodelCell(name: self.nodelViewModel.nodeList[index].name ?? "", host: self.nodelViewModel.nodeList[index].online_users)
  133. .frame(height: 60)
  134. .onTapGesture {
  135. self.nodelViewModel.nodeModel = self.nodelViewModel.nodeList[index]
  136. self.hosts = self.nodelViewModel.nodeModel?.name ?? "智能选线"
  137. //self.isPop = false
  138. self.presentationMode.wrappedValue.dismiss()
  139. }
  140. .background(Color.init(hex: "#FFFFFF"))
  141. }
  142. }
  143. }
  144. }
  145. .onAppear {
  146. let controller = UIApplication.shared.windows[0].rootViewController as? MyHontingController
  147. controller?.statusBarStyle = .lightContent
  148. nodelViewModel.GetNode()
  149. }
  150. .navigationBarTitle(Text("节点"), displayMode: .inline)
  151. //.navigationBarHidden(true)
  152. }
  153. }