Main.swift 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // Main.swift
  3. // naiyoup
  4. //
  5. // Created by C Auto on 2021/6/2.
  6. //
  7. import SwiftUI
  8. import Combine
  9. import SwiftyUserDefaults
  10. struct RouteView : View {
  11. @Binding var IsQuan : Bool
  12. var body: some View {
  13. HStack{
  14. Toggle( self.IsQuan ? "智能路由" : "全局", isOn: self.$IsQuan)
  15. .foregroundColor(Color.white)
  16. }
  17. .padding()
  18. .frame(width: 200,height: 59)
  19. .background(Color("SelectBackgrouunColor"))
  20. .cornerRadius(20)
  21. .shadow(radius: 10)
  22. }
  23. }
  24. struct SwicthNodeView :View {
  25. @Binding var Host : String
  26. var body: some View{
  27. HStack{
  28. Image(systemName: "bolt.horizontal.icloud")
  29. .foregroundColor(Color.white)
  30. Text(Host)
  31. .foregroundColor(Color.white)
  32. Spacer()
  33. }
  34. .padding()
  35. .frame(width: 200,height: 59)
  36. .background(Color("SelectBackgrouunColor"))
  37. .cornerRadius(20)
  38. .shadow(radius: 10)
  39. }
  40. }
  41. struct ProUserView :View{
  42. var body: some View{
  43. HStack{
  44. Image(systemName: "person")
  45. .foregroundColor(Color.white)
  46. Text("个人中心")
  47. .foregroundColor(Color.white)
  48. Spacer()
  49. }
  50. .padding()
  51. .frame(width: 200,height: 59)
  52. .background(Color("SelectBackgrouunColor"))
  53. .cornerRadius(20)
  54. .shadow(radius: 10)
  55. }
  56. }
  57. struct WebNavigationView :View {
  58. var body: some View{
  59. HStack{
  60. Image(systemName: "paperplane.fill")
  61. .foregroundColor(Color.white)
  62. Text("网站导航")
  63. .foregroundColor(Color.white)
  64. Spacer()
  65. }
  66. .padding()
  67. .frame(width: 200,height: 59)
  68. .background(Color("SelectBackgrouunColor"))
  69. .cornerRadius(20)
  70. .shadow(radius: 10)
  71. }
  72. }
  73. struct MainView: View {
  74. @State var isQuan : Bool = true
  75. @State var isPop : Bool = false
  76. @State var isMePop : Bool = false
  77. @State var isConnectd: Bool = false
  78. @State var isYsBool : Bool = false
  79. //@State var nodelModel : NodelModel? = nil
  80. var nodelViewModel = NodelViewModel.nodel
  81. @ObservedObject var mainViewModel = MainViewModel.main
  82. @State var hosts : String = "智能选线"
  83. init() {
  84. //UINavigationBar.appearance().backgroundImage(for: <#T##UIBarPosition#>, barMetrics: <#T##UIBarMetrics#>)
  85. UIApplication.statusBarBackgroundColor = UIColor.init(named: "BackgroundColor")
  86. UINavigationBar.appearance().backgroundColor = UIColor.init(named: "BackgroundColor")
  87. UINavigationBar.appearance().tintColor = UIColor.white
  88. UINavigationBar.appearance().barTintColor = UIColor.init(named: "BackgroundColor")
  89. UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.white]
  90. UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.white]
  91. UINavigationBar.appearance().isTranslucent = false
  92. }
  93. var body: some View {
  94. NavigationView{
  95. ZStack{
  96. Color("BackgroundColor").edgesIgnoringSafeArea(.all)
  97. VStack(spacing: 15){
  98. //Spacer()
  99. Text(mainViewModel.vpnstatus)
  100. .foregroundColor(Color("vStatus"))
  101. if self.mainViewModel.authting == true
  102. {
  103. ActivityIndicator(isAnimating: self.mainViewModel.authting)
  104. .configure { $0.color = .white}
  105. .frame(width: 200, height: 200, alignment: .center)
  106. .padding()
  107. } else {
  108. Image(mainViewModel.serviceOpen ? "btnswitchon" : "btnswitchoff")
  109. .resizable()
  110. .frame(width: 200, height: 200, alignment: .center)
  111. .padding()
  112. .onTapGesture {
  113. //链接vpn
  114. self.ConnectVPN()
  115. }
  116. .alert(isPresented: $isYsBool) {
  117. Alert(title: Text("关于隐私"),
  118. message: Text("本应用不会向任何无关第三方提供、出售、出租、分享或交易您的个人信息,除非事先得到您的许可,或该第三方和本应用(含本应用关联公司)单独或共同为您提供服务,且在该服务结束后,其将被禁止访问包括其以前能够访问的所有这些资料。\n本应用亦不允许任何第三方以任何手段收集、编辑、出售或者无偿传播您的个人信息。任何本应用平台用户如从事上述活动,一经发现,本应用有权立即终止与该用户的服务协议。\n为服务用户的目的,本应用可能通过使用您的个人信息,向您提供您感兴趣的信息,包括但不限于向您发出产品和服务信息,或者与本应用合作伙伴共享信息以便他们向您发送有关其产品和服务的信息(后者需要您的事先同意)。"),
  119. primaryButton: .destructive(Text("确定")) {
  120. Defaults[\.vpnys] = true
  121. self.isYsBool = false
  122. ConnectVPN()
  123. },
  124. secondaryButton: .cancel(Text("取消")))
  125. }
  126. }
  127. //.onChange(of: <#T##Equatable#>, perform: <#T##(Equatable) -> Void#>)
  128. RouteView(IsQuan: $isQuan)
  129. NavigationLink(
  130. destination: LazyView(NodeView(hosts: self.$hosts)),
  131. label: {
  132. SwicthNodeView(Host: self.$hosts)
  133. // .onChange(of: self.isPop, perform: { value in
  134. //
  135. // if let node = self.nodelViewModel.nodeModel {
  136. // self.hosts = node.name ?? "智能选线"
  137. // }
  138. // })
  139. })
  140. NavigationLink(
  141. destination: LazyView(MeView()),
  142. isActive: self.$isMePop,
  143. label: {
  144. ProUserView()
  145. })
  146. //WebNavigationView()
  147. Spacer()
  148. }
  149. .padding()
  150. .onAppear {
  151. print("Home On Appear")
  152. let controller = UIApplication.shared.windows[0].rootViewController as? MyHontingController
  153. controller?.statusBarStyle = .lightContent
  154. UITableView.appearance().tableFooterView = UIView()
  155. UITableView.appearance().separatorStyle = .none
  156. }
  157. .onDisappear(perform: {
  158. UITableView.appearance().tableFooterView = nil
  159. UITableView.appearance().separatorStyle = .singleLine
  160. })
  161. }
  162. }
  163. .navigationBarHidden(true)
  164. .navigationViewStyle(StackNavigationViewStyle())
  165. }
  166. func ConnectVPN() {
  167. if !Defaults[\.vpnys] {
  168. self.isYsBool = true
  169. return
  170. }
  171. if mainViewModel.serviceOpen {
  172. mainViewModel.closeService(completion: {
  173. self.isConnectd = false
  174. mainViewModel.vpnstatus = "未连接"
  175. })
  176. } else {
  177. guard mainViewModel.GetLoginModel() != nil else {
  178. return
  179. }
  180. mainViewModel.route = self.isQuan
  181. if let node = self.nodelViewModel.nodeModel {
  182. // mainViewModel.serverip = node.host ?? ""
  183. // mainViewModel.port = node.port ?? 0
  184. // mainViewModel.encrypt = node.method ?? ""
  185. // mainViewModel.password = node.passwd ?? ""
  186. // mainViewModel.route = self.isQuan
  187. // //开始链接
  188. // mainViewModel.openService{ e in
  189. //
  190. // log.debug(e)
  191. // }
  192. mainViewModel.GetAuthNode(nodel: node, success: { name in
  193. self.hosts = name ?? "智能选线"
  194. }, completion: { e in
  195. log.debug(e)
  196. })
  197. } else {
  198. // mainViewModel.GetAuthNode { name in
  199. // self.hosts = name ?? "智能选线"
  200. // } completion: { e in
  201. // log.debug(e)
  202. // }
  203. mainViewModel.GetAuthNode(nodel: nil, success: { name in
  204. self.hosts = name ?? "智能选线"
  205. }, completion: { e in
  206. log.debug(e)
  207. })
  208. }
  209. }
  210. }
  211. }
  212. struct Main_Previews: PreviewProvider {
  213. static var previews: some View {
  214. MainView()
  215. }
  216. }