HomeViewController.swift 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. //
  2. // HomeViewController.swift
  3. // ADHTuanCan
  4. //
  5. // Created by 敖德亨 on 2023/9/28.
  6. //
  7. import UIKit
  8. //import MJRefresh
  9. import RxSwift
  10. import RxCocoa
  11. import RxAlamofire
  12. import Alamofire
  13. import HandyJSON
  14. import SwiftDate
  15. import SwiftyUserDefaults
  16. class HomeViewController: ADHBaseVC {
  17. var shopcartId : NSNumber?
  18. /// 是否是手动刷新
  19. var isMJLoad = false
  20. @IBOutlet var shopCar: UIView!
  21. /// 购物车价格
  22. @IBOutlet weak var shopCarPrice: UILabel!
  23. @IBOutlet weak var shopCarBotton: NSLayoutConstraint!
  24. @IBOutlet weak var saleLab: UILabel!
  25. @IBOutlet weak var saleView: UIView!
  26. /// 底部LAB偏移量
  27. @IBOutlet weak var bottomY1: NSLayoutConstraint!
  28. /// 底部LAB偏移量
  29. @IBOutlet weak var bottomY2: NSLayoutConstraint!
  30. let dateView = Bundle.main.loadNibNamed("HomeDateView", owner: nil, options: nil)?.last as? HomeDateView
  31. let everyBodyLove = Bundle.main.loadNibNamed("EverybodyLoveView", owner: nil, options: nil)?.last as? EverybodyLoveView
  32. let reBtnView = Bundle.main.loadNibNamed("HomeRecommendView", owner: nil, options: nil)?.last as? HomeRecommendView
  33. let searchView = Bundle.main.loadNibNamed("SearchView", owner: nil, options: nil)?.last as? SearchView
  34. //查看购物车View
  35. let checkShoppingCarView = Bundle.main.loadNibNamed("ShoppingCarDetailView", owner: nil, options: nil)?.last as? ShoppingCarDetailView
  36. //业务员必填页
  37. let SalesmanView = Bundle.main.loadNibNamed("SalesmanFirstShowView", owner: nil, options: nil)?.last as? SalesmanFirstShowView
  38. let timeRangeView = UINib.view(withType: timeManageSheetView.self)
  39. var viewList : [Any]?
  40. /// 当前选择的时间
  41. var nowTimeModel : HomeTimeItem?
  42. private var backImageView : UIImageView!
  43. let viewModel : HomeViewModel = HomeViewModel.init()
  44. let diposableBag = DisposeBag()
  45. /// 1为普团,2为白团
  46. let shopType = "2"
  47. ///套餐类型
  48. var runTypeId : String?
  49. ///早中晚类型
  50. var supplyTypeId : String?
  51. ///搜索内容
  52. var mealName : String?
  53. var isShowUserView = true
  54. override func viewWillAppear(_ animated: Bool) {
  55. super.viewWillAppear(animated)
  56. UIScrollView.appearance().isPagingEnabled = false
  57. }
  58. //去结算
  59. @IBAction func gotoShpCar(_ sender: UIButton) {
  60. let vc = CheckOrderViewController()
  61. vc.shopType = self.shopType
  62. vc.shopcartId = shopcartId
  63. vc.shopcartTimeId = self.nowTimeModel?.shopcartTimeId
  64. vc.nowTimeModel = self.nowTimeModel
  65. vc.reloadCallBackBlock = {[weak self] in
  66. self?.refreshData()
  67. }
  68. self.navigationController?.pushViewController(vc, animated: true)
  69. }
  70. //查看购物车
  71. @IBAction func checkShopCar(_ sender: UIButton) {
  72. if self.nowTimeModel.isNil{
  73. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择日期", Etext: "Please select a date"))
  74. return
  75. }
  76. self.checkShoppingCarView?.shopType = self.shopType
  77. self.checkShoppingCarView?.shopcartId = self.shopcartId
  78. self.checkShoppingCarView?.shopcartTimeId = self.nowTimeModel?.shopcartTimeId
  79. self.checkShoppingCarView?.nowTimeModel = self.nowTimeModel
  80. self.checkShoppingCarView?.show()
  81. }
  82. @IBAction func checkSaleDetail(_ sender: UIButton) {
  83. let saleView = Bundle.main.loadNibNamed("SaleDetailView", owner: nil, options: nil)?.last as? SaleDetailView
  84. saleView?.showWith(model: self.viewModel.shopCarPriceDetail)
  85. }
  86. override func viewDidLoad() {
  87. super.viewDidLoad()
  88. _ = NotificationCenter.default.rx.notification(Notification.Name(rawValue: "刷新首页")).takeUntil(self.rx.deallocated).subscribe(onNext:{[weak self] (notification) in
  89. self?.isMJLoad = false
  90. self?.refreshData()
  91. })
  92. _ = NotificationCenter.default.rx.notification(Notification.Name(rawValue: "刷新业务员首页")).takeUntil(self.rx.deallocated).subscribe(onNext:{[weak self] (notification) in
  93. self?.isMJLoad = false
  94. self?.refreshData()
  95. })
  96. self.scrollView.mj_header = MJRefreshNormalHeader.init(refreshingBlock: {[unowned self] in
  97. self.isMJLoad = true
  98. self.GetMealWithSupplyType(true)
  99. });
  100. self.scrollView.mj_footer = MJRefreshBackNormalFooter.init(refreshingBlock: {[unowned self] in
  101. self.isMJLoad = true
  102. self.GetMealWithSupplyType(false)
  103. });
  104. self.scrollView.ly_emptyView = LYEmptyView.emptyActionView(with: UIImage.init(named: "暂无数据"), titleStr: LanguagesUtil.createTextBy(Ctext: "暂无数据", Etext: "no data"), detailStr: "", btnTitleStr: LanguagesUtil.createTextBy(Ctext: "点击刷新", Etext: "Click to refresh"), btnClick: {[weak self] in
  105. self?.isMJLoad = true
  106. self?.GetMealWithSupplyType(true)
  107. })
  108. self.view.backgroundColor = kTBackgroundColor
  109. self.shopCar.isHidden = true
  110. shopCarBotton.constant = 0
  111. shopCar.bringSubviewToFront(self.scrollView)
  112. self.view.addSubview(self.scrollView)
  113. self.scrollView.mas_remakeConstraints { make in
  114. make?.top.left().right().bottom().mas_equalTo()(self.view)
  115. make?.height.offset()(kSCREEN_HEIGHT - kTabBarHeight)
  116. }
  117. // self.createUI()
  118. //获取餐品类型
  119. self.getHomeFoodTypes()
  120. // //获取时间段范围
  121. // self.getTimeRangDetail()
  122. }
  123. func refreshData(){
  124. self.getTimeRangDetail()
  125. }
  126. //MARK: createUI
  127. /// <#Description#>
  128. func createUI(){
  129. self.backImageView = UIImageView.init(image: UIImage.init(named: "首页背景")!)
  130. self.backImageView.isUserInteractionEnabled = true
  131. self.backImageView.addSubview(self.searchView!)
  132. self.searchView!.mas_remakeConstraints { make in
  133. make?.left.mas_equalTo()(self.backImageView)?.offset()(24)
  134. make?.right.mas_equalTo()(self.backImageView)?.offset()(-24)
  135. make?.top.mas_equalTo()(self.backImageView)?.offset()(self.backImageView.frame.size.height - 100)
  136. make?.height.offset()(48)
  137. }
  138. ///
  139. self.viewList = [self.backImageView!,self.dateView! ,self.everyBodyLove!,self.reBtnView!,self.collectionView]
  140. self.scrollView.addSubview(self.backImageView!)
  141. self.scrollView.addSubview(self.dateView!)
  142. self.scrollView.addSubview(self.everyBodyLove!)
  143. self.scrollView.addSubview(self.reBtnView!)
  144. self.scrollView.addSubview(self.collectionView)
  145. self.searchView?.callBackBlock = {[weak self] mealName in
  146. self?.mealName = mealName
  147. self?.GetMealWithSupplyType(true)
  148. }
  149. self.timeRangeView?.selectCheckDateBlock = {[weak self] startTime , endTime , days in
  150. self?.viewModel.homeEditorTimeRangModel.endDate = endTime
  151. self?.viewModel.homeEditorTimeRangModel.startDate = startTime
  152. self?.refreshTimeRang()
  153. }
  154. //选择时间段
  155. self.dateView?.timeRangeAction = {[weak self] in
  156. let monthFirstDate = NSDate.now
  157. let days : Int = monthFirstDate.day
  158. let startDate = monthFirstDate - days.days + 1.days
  159. self?.timeRangeView?.show(withStaStr: self?.viewModel.TimeRangDetail?.startDate ?? "", endStr: self?.viewModel.TimeRangDetail?.endDate ?? "",begin: startDate)
  160. }
  161. self.dateView?.selectBlock = {[weak self] model in
  162. self?.nowTimeModel = model
  163. self?.viewModel.SupplyTypeRequsetModel.shopcartTime = self?.nowTimeModel!.shopcartTime
  164. self?.isMJLoad = true
  165. self?.GetMealWithSupplyType(true)
  166. self?.reloadDateView()
  167. }
  168. //早中晚选择
  169. self.everyBodyLove?.supplyTypeAction = {[weak self] type in
  170. self?.supplyTypeId = type
  171. self?.isMJLoad = true
  172. self?.GetMealWithSupplyType(true)
  173. }
  174. //所有人都爱点击
  175. self.everyBodyLove?.touchUpActionBlock = {[unowned self] tag in
  176. if self.nowTimeModel.isNil{
  177. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择日期", Etext: "Please select a date"))
  178. return
  179. }
  180. let model : HomeFoodDetailModel = (self.viewModel.EveryBodyLoveList![tag])
  181. self.checkFoodDetail(model: model)
  182. }
  183. //下方套餐选择
  184. self.reBtnView?.runTypeAction = {[weak self] type in
  185. self?.runTypeId = type
  186. self?.isMJLoad = true
  187. self?.GetMealWithSupplyType(true)
  188. }
  189. self.checkShoppingCarView?.reFreshBlock = {[weak self] in
  190. self?.isMJLoad = true
  191. self?.getTimeRangDetail()
  192. }
  193. //刷新首页
  194. self.checkShoppingCarView?.reloadCallBackBlock = {[weak self] in
  195. self?.refreshData()
  196. }
  197. self.checkShoppingCarView?.checkOrderBlock = {[weak self] in
  198. let vc = CheckOrderViewController()
  199. vc.shopType = self?.shopType
  200. vc.shopcartId = self?.viewModel.TimeRangDetail?.shopcartId
  201. vc.shopcartTimeId = self?.checkShoppingCarView?.nowTimeModel?.shopcartTimeId
  202. vc.nowTimeModel = self?.checkShoppingCarView?.nowTimeModel
  203. vc.reloadCallBackBlock = {[weak self] in
  204. self?.refreshData()
  205. }
  206. self?.navigationController?.pushViewController(vc, animated: true)
  207. }
  208. self.resetConstraint()
  209. }
  210. //MARK: 刷新加载部分
  211. @objc func headerRefresh(){
  212. print("下拉刷新.")
  213. self.GetMealWithSupplyType(true)
  214. }
  215. //MARK: 获取餐品类型
  216. func getHomeFoodTypes(){
  217. self.viewModel.GetRunAndSupply.mealType = self.shopType
  218. self.hud.showLoading()
  219. self.viewModel.signalGetHomeFoodType()?.on(success: { dd in
  220. if self.viewModel.SupplyTypeList!.runTypeList!.count > 0{
  221. //下部套餐按钮渲染
  222. self.reBtnView?.configTitle(model: self.viewModel.SupplyTypeList!)
  223. //早中晚默认给到第一个类型
  224. self.runTypeId = self.viewModel.SupplyTypeList!.runTypeList![0].id
  225. }
  226. if self.viewModel.SupplyTypeList!.supplyTypeList!.count > 0{
  227. //早中晚按钮渲染
  228. self.everyBodyLove?.configTitle(model: self.viewModel.SupplyTypeList!)
  229. //早中晚默认给到第一个类型
  230. self.supplyTypeId = self.viewModel.SupplyTypeList!.supplyTypeList![0].id
  231. //获取餐品
  232. self.GetMealWithSupplyType(true)
  233. }
  234. self.hud.hidden()
  235. }, error: { error in
  236. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  237. })
  238. }
  239. /// 获取餐品类型
  240. /// - Parameter isFirstPage: 加载第一页
  241. func GetMealWithSupplyType(_ isFirstPage: Bool){
  242. self.hud.showLoading()
  243. self.viewModel.SupplyTypeRequsetModel.mealName = self.mealName
  244. self.viewModel.SupplyTypeRequsetModel.isGeneral = self.shopType
  245. self.viewModel.SupplyTypeRequsetModel.runTypeId = self.runTypeId
  246. self.viewModel.SupplyTypeRequsetModel.supplyTypeId = self.supplyTypeId
  247. self.viewModel.signalGetHomeFoodsWithType(isFirstPage)?.on(success: { dd in
  248. self.hud.hidden()
  249. self.collectionView.reloadData()
  250. self.getEveryBodyLove(isFirstPage)
  251. self.getTimeRangDetail()
  252. self.scrollView.mj_header.endRefreshing()
  253. self.scrollView.mj_footer.endRefreshing()
  254. }, error: { error in
  255. self.getEveryBodyLove(isFirstPage)
  256. self.scrollView.mj_header.endRefreshing()
  257. self.scrollView.mj_footer.endRefreshing()
  258. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  259. })
  260. }
  261. func getEveryBodyLove(_ isFirstPage: Bool){
  262. self.hud.showLoading()
  263. self.viewModel.getEveryBodyModel.mealName = self.mealName
  264. self.viewModel.getEveryBodyModel.isGeneral = self.shopType
  265. self.viewModel.getEveryBodyModel.runTypeId = self.runTypeId
  266. self.viewModel.getEveryBodyModel.supplyTypeId = self.supplyTypeId
  267. self.viewModel.signalGetEveryBodyLove(isFirstPage)?.on(success: { dd in
  268. self.hud.hidden()
  269. self.everyBodyLove?.configMode(datList: self.viewModel.EveryBodyLoveList)
  270. if let _ = self.viewList{
  271. self.resetConstraint()
  272. }else{
  273. self.createUI()
  274. }
  275. }, error: { error in
  276. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  277. })
  278. }
  279. //MARK: 获取时间范围
  280. func getTimeRangDetail(){
  281. self.viewModel.HomeTimeGetModel.shopType = self.shopType
  282. self.viewModel.signalGetHomeTime()?.on(success: { dd in
  283. self.hud.hidden()
  284. for item in self.viewModel.TimeRangDetail!.timeVoList! {
  285. let model : HomeTimeItem = item
  286. if model.day == self.nowTimeModel?.day{
  287. model.isSelect = true
  288. }
  289. }
  290. self.shopcartId = self.viewModel.TimeRangDetail?.shopcartId
  291. self.viewModel.homeEditorTimeRangModel.id = self.viewModel.TimeRangDetail?.id
  292. self.viewModel.homeEditorTimeRangModel.shopType = self.shopType
  293. // if !self.isMJLoad{
  294. // self.SalesmanView?.shopType = self.shopType
  295. // self.SalesmanView?.showWithShopcartId(shopcartId: self.shopcartId ?? 0, vc: self)
  296. // }
  297. DLog("旧购物车ID: \(self.shopcartId ?? 0)==> 新购物车ID:\(Defaults[\.userType] ?? "")")
  298. if "\(self.shopcartId ?? 0)" == Defaults[\.userType]{
  299. }else{
  300. Defaults[\.userType] = "\(self.shopcartId ?? 0)"
  301. self.SalesmanView?.shopType = self.shopType
  302. self.SalesmanView?.showWithShopcartId(shopcartId: self.shopcartId ?? 0, vc: self)
  303. }
  304. if let _ = self.viewList{
  305. self.resetConstraint()
  306. }else{
  307. self.createUI()
  308. }
  309. self.getShopCarPrice()
  310. self.reloadDateView()
  311. self.resetConstraint()
  312. }, error: { error in
  313. self.hud.showFailure("获取时间范围失败")
  314. })
  315. }
  316. /// 获取购物车价格详情
  317. func getShopCarPrice(){
  318. self.viewModel.getShopCarPriceRModel.id = self.shopcartId
  319. self.viewModel.signalGetShopCarPrice()?.on(success: { dd in
  320. if (self.viewModel.shopCarPriceDetail?.price ?? "") > 0{
  321. self.scrollView.mas_remakeConstraints { make in
  322. make?.top.left().right().bottom().mas_equalTo()(self.view)
  323. make?.bottom.mas_equalTo()(self.shopCar.mas_bottom)?.offset()(-64)
  324. make?.height.offset()(kSCREEN_HEIGHT - kTabBarHeight)
  325. }
  326. self.shopCar.isHidden = false
  327. }else{
  328. self.scrollView.mas_remakeConstraints { make in
  329. make?.top.left().right().bottom().mas_equalTo()(self.view)
  330. make?.height.offset()(kSCREEN_HEIGHT - kTabBarHeight)
  331. }
  332. self.shopCar.isHidden = true
  333. }
  334. self.shopCarPrice.text = "\(self.viewModel.shopCarPriceDetail?.price ?? "")"
  335. self.saleLab.text = LanguagesUtil.createTextBy(Ctext: "已优惠:", Etext: "Sale:") + "\(self.viewModel.shopCarPriceDetail?.discountAmount ?? "")"
  336. if self.viewModel.shopCarPriceDetail?.discountAmount > 0{
  337. self.saleView.isHidden = false
  338. self.bottomY1.constant = -10
  339. self.bottomY2.constant = -10
  340. }else{
  341. self.saleView.isHidden = true
  342. self.bottomY1.constant = 0
  343. self.bottomY2.constant = 0
  344. }
  345. }, error: { error in
  346. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  347. })
  348. }
  349. /// 刷新顶部时间范围
  350. func refreshTimeRang(){
  351. self.hud.showLoading()
  352. self.viewModel.signalHomeEditorTimeRang()?.on(success: { dd in
  353. self.hud.showSuccess()
  354. self.getTimeRangDetail()
  355. }, error: { error in
  356. self.hud.showFailure(error.msg ?? LanguagesUtil.createTextBy(Ctext: "网络错误", Etext: "network error"))
  357. })
  358. }
  359. //MARK: 刷新时间段
  360. func reloadDateView(){
  361. self.dateView?.configModel(dateModel: self.viewModel.TimeRangDetail!)
  362. if let _ = self.viewModel.TimeRangDetail?.timeVoList{
  363. for model in self.viewModel.TimeRangDetail!.timeVoList! {
  364. let item : HomeTimeItem = model
  365. if self.nowTimeModel?.shopcartTime == item.shopcartTime
  366. {
  367. self.nowTimeModel = item
  368. }
  369. }
  370. }
  371. }
  372. // 底部刷新
  373. func footerRefresh(){
  374. print("上拉刷新")
  375. }
  376. //MARK: 重设约束
  377. func resetConstraint(){
  378. // self.scrollView.addSubview(self.backImageView!)
  379. // self.scrollView.addSubview(self.dateView!)
  380. // self.scrollView.addSubview(self.everyBodyLove!)
  381. // self.scrollView.addSubview(self.collectionView)
  382. // self.scrollView.addSubview(self.reBtnView!)
  383. var contentSizeH : CGFloat = 0
  384. for i in 0..<viewList!.count{
  385. let itme : UIView = viewList![i] as! UIView
  386. itme.mas_remakeConstraints { make in
  387. make?.left.right().mas_equalTo()(self.scrollView)
  388. if i == 0{
  389. make?.width.offset()(kSCREEN_WIDTH)
  390. make?.top.mas_equalTo()(self.scrollView)
  391. }
  392. if i == 1{
  393. if self.viewModel.TimeRangDetail != nil{
  394. make?.height.offset()(178)
  395. }else{
  396. make?.height.offset()(0)
  397. }
  398. make?.top.mas_equalTo()(self.backImageView.mas_bottom)?.offset()(-24)
  399. }
  400. if i == 2{
  401. make?.top.mas_equalTo()(self.dateView!.mas_bottom)?.offset()
  402. if (self.viewModel.EveryBodyLoveList?.count ?? 0) == 0{
  403. make?.height.offset()(47)
  404. }else{
  405. make?.height.offset()(320+47+20)
  406. }
  407. }
  408. if i == 3{
  409. if self.viewModel.SupplyTypeList != nil{
  410. make?.height.offset()(76)
  411. }else{
  412. make?.height.offset()(0)
  413. }
  414. make?.top.mas_equalTo()(self.everyBodyLove!.mas_bottom)?.offset()
  415. }
  416. if i == 4{
  417. //self.viewModel.HomeFoodList?.count date数量 2两列 取整数取余数
  418. let itemSpac = (kSCREEN_WIDTH - 323) / 3 //行距
  419. let lin1 = (self.viewModel.HomeFoodList?.count ?? 0)/2
  420. let lin2 = (self.viewModel.HomeFoodList?.count ?? 0)%2
  421. if (self.viewModel.HomeFoodList?.count ?? 0) > 0{
  422. make?.height.offset()((206.5 + itemSpac) * CGFloat((lin1 + lin2)))
  423. }else{
  424. make?.height.offset()(0)
  425. }
  426. make?.top.mas_equalTo()(self.reBtnView!.mas_bottom)?.offset()
  427. make?.bottom.mas_equalTo()(self.scrollView)
  428. }
  429. }
  430. contentSizeH = contentSizeH + itme.frame.size.height
  431. }
  432. scrollView.contentSize = CGSize.init(width: 0, height: contentSizeH)
  433. }
  434. //MARK: scrollView
  435. private var scrollView : UIScrollView = {
  436. let scrollView = UIScrollView.init(frame: CGRect.zero)
  437. scrollView.alwaysBounceVertical = true
  438. scrollView.isScrollEnabled = true
  439. scrollView.showsVerticalScrollIndicator = false
  440. scrollView.backgroundColor = kTBackgroundColor
  441. scrollView.contentInsetAdjustmentBehavior = .never
  442. return scrollView
  443. }()
  444. //MARK: collectionView
  445. lazy var collectionView : UICollectionView = {
  446. //设置布局
  447. let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout.init()
  448. let itemSpac = (kSCREEN_WIDTH - 323) / 3
  449. layout.scrollDirection = .vertical //竖直
  450. layout.itemSize = CGSize.init(width: 161.5, height: 206.5)
  451. //行距
  452. layout.minimumInteritemSpacing = 20
  453. layout.minimumLineSpacing = itemSpac
  454. // layout.sectionInset = .init(top: 0, left: itemSpac, bottom: 0, right: itemSpac)
  455. if kIsIphoneX {
  456. layout.sectionInset = .init(top: 0, left: itemSpac, bottom: 0, right: itemSpac)
  457. }else{
  458. layout.sectionInset = .init(top: 0, left: 10, bottom: 0, right: 10)
  459. }
  460. let collectView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
  461. collectView.delegate = self
  462. collectView.dataSource = self
  463. collectView.backgroundColor = UIColor.white
  464. collectView.showsVerticalScrollIndicator = false
  465. collectView.isScrollEnabled = false
  466. collectView.register(withType: HomeFoodItem.self)
  467. return collectView
  468. }()
  469. func checkFoodDetail(model : HomeFoodDetailModel){
  470. if model.mealType == "2"{
  471. let vc = SetMealDetailVC()
  472. vc.mealId = model.mealId
  473. vc.mealType = model.mealType
  474. vc.runTypeId = model.runTypeId
  475. vc.supplyTypeId = model.supplyTypeId
  476. vc.shopcartTime = self.nowTimeModel?.shopcartTime
  477. vc.shopcartId = self.shopcartId
  478. vc.shopcartTimeId = self.nowTimeModel?.shopcartTimeId
  479. vc.reloadCallBackBlock = {[weak self] in
  480. self?.refreshData()
  481. }
  482. self.navigationController?.pushViewController(vc, animated: true)
  483. }else{
  484. let vc = SingleMealDetailController()
  485. vc.mealId = model.mealId
  486. vc.mealType = model.mealType
  487. vc.runTypeId = model.runTypeId
  488. vc.supplyTypeId = model.supplyTypeId
  489. vc.shopcartTime = self.nowTimeModel?.shopcartTime
  490. vc.shopcartId = self.shopcartId
  491. vc.shopcartTimeId = self.nowTimeModel?.shopcartTimeId
  492. vc.isGeneral = self.shopType
  493. vc.reloadCallBackBlock = {[weak self] in
  494. self?.refreshData()
  495. }
  496. self.navigationController?.pushViewController(vc, animated: true)
  497. }
  498. }
  499. }
  500. //MARK: Delegate
  501. extension HomeViewController : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout
  502. {
  503. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  504. return self.viewModel.HomeFoodList?.count ?? 0
  505. }
  506. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  507. let cell : HomeFoodItem = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeFoodItem", for: indexPath) as! HomeFoodItem
  508. cell.configData(dataModel: self.viewModel.HomeFoodList![indexPath.row])
  509. return cell;
  510. }
  511. func numberOfSections(in collectionView: UICollectionView) -> Int {
  512. return 1
  513. }
  514. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  515. if self.nowTimeModel.isNil{
  516. self.hud.showFailure(LanguagesUtil.createTextBy(Ctext: "请选择日期", Etext: "Please select a date"))
  517. return
  518. }
  519. let model = self.viewModel.HomeFoodList![indexPath.row]
  520. self.checkFoodDetail(model: model)
  521. }
  522. }