IndexUIView.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // IndexUIView.swift
  3. // fiveConstant
  4. //
  5. // Created by 李建 on 2023/1/20.
  6. //
  7. import SwiftUI
  8. import SwiftyUserDefaults
  9. struct IndexUIView: View {
  10. @StateObject var userInfoData: UserInfoData
  11. @State var roomList: [RoomInfo] = []
  12. @EnvironmentObject var progressState: ProgressState;
  13. @State var isShowEmpty = false
  14. var body: some View {
  15. VStack {
  16. // 用户头像及欢迎
  17. HStack {
  18. VStack(alignment: .leading) {
  19. Text("欢迎回来,\(Defaults[\.userName] ?? "")")
  20. .bold()
  21. Text("感谢使用永续绿建五恒系统")
  22. }
  23. Spacer()
  24. UserPhoto(url: $userInfoData.photo).frame(width: 50, height: 50)
  25. .onTapGesture {
  26. Defaults.removeAll()
  27. }
  28. }
  29. // 天气
  30. ZStack {
  31. RoundedRectangle(cornerRadius: 8)
  32. .fill(Color("SecondColor"))
  33. .frame(maxHeight: 100)
  34. HStack {
  35. VStack {
  36. HStack() {
  37. Text("27")
  38. .font(.largeTitle)
  39. Text("℃")
  40. Text("优")
  41. .font(.callout)
  42. .fontWeight(.bold)
  43. }
  44. Text("9.12 星期一 睛")
  45. .font(.caption)
  46. }
  47. Spacer()
  48. // 中间部分
  49. VStack(alignment: .leading) {
  50. HStack {
  51. Image("icon_location")
  52. Text("济南市")
  53. }
  54. HStack {
  55. Image("icon_humidity")
  56. Text("59%")
  57. }
  58. }
  59. .font(.caption)
  60. }
  61. .padding()
  62. }
  63. .foregroundColor(Color.white)
  64. .padding(.bottom, 30)
  65. HStack {
  66. Text("我的房间")
  67. .font(.title3)
  68. .bold()
  69. Spacer()
  70. }
  71. if roomList.count > 0 {
  72. ScrollView {
  73. LazyVGrid(columns: Array(repeating: GridItem(.fixed(170)), count: 2), spacing: 20) {
  74. ForEach(roomList, id: \.record_id) { list in
  75. NavigationLink(destination: {
  76. RoomControlView(roomInfo: list, controller: RoomController(roomId: list.record_id!, gatewayId: "YX20230203", controlNumber: list.control_number!))
  77. }, label: {
  78. RoomCard(roomData: list)
  79. })
  80. }
  81. }
  82. }
  83. } else if isShowEmpty {
  84. ZStack {
  85. Image("img_no_room")
  86. .resizable()
  87. .frame(width: 274, height: 280)
  88. VStack(alignment: .center, spacing: 13) {
  89. Text("您还没有添加房间")
  90. .font(.footnote)
  91. .foregroundColor(.gray)
  92. Button {
  93. } label: {
  94. HStack {
  95. Image(systemName: "plus").font(.caption)
  96. .foregroundColor(Color("MainColor"))
  97. Text("添加房间")
  98. .font(.caption)
  99. .foregroundColor(Color("MainColor"))
  100. }
  101. .padding(8)
  102. .overlay(RoundedRectangle(cornerRadius: 50).stroke(Color("MainColor")))
  103. }
  104. }.padding(.top, 150)
  105. }.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
  106. }
  107. Spacer()
  108. }
  109. .padding()
  110. .onAppear {
  111. HttpRequest<[RoomInfo]>.loadData(target: UserApi.myRooms) { returnData in
  112. roomList = returnData
  113. if roomList.count == 0 {
  114. isShowEmpty = true
  115. } else {
  116. isShowEmpty = false
  117. }
  118. }
  119. }
  120. }
  121. }
  122. struct IndexUIView_Previews: PreviewProvider {
  123. static var previews: some View {
  124. IndexUIView(userInfoData: UserInfoData(), roomList: [])
  125. }
  126. }