younger_times
2023-07-10 31b6be2dfcc5a7e9300c90b2216dbd579153c3b0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    //
    //  HomeVC.swift
    //  WanPai
    //
    //  Created by 杨锴 on 2023/6/8.
    //
 
import UIKit
import JQTools
import RxSwift
import SDWebImage
 
    //http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4
    //http://vjs.zencdn.net/v/oceans.mp4
    //https://media.w3.org/2010/05/sintel/trailer.mp4
 
class HomeVC: BaseVC,Refreshable{
    @IBOutlet weak var collectionView: BaseCollectionView!
    @IBOutlet weak var label_vipInfo: UILabel!
 
    private var layout:WaterFallFlowLayout!
    @IBOutlet weak var label_store: UILabel!
    private var items = Array<HomeStoreConfigModel>()
    private var storeModel:HomeStoreModel?
 
    override func viewDidLoad() {
        super.viewDidLoad()
 
        locationTool.startLocation { [weak self] local in
            locationTool.stopLocation()
            self?.getStoreInfo()
        } errorClouse: { [weak self] error in
            alertError(msg: "定位获取失败")
            self?.label_store.text = "定位获取失败"
            self?.getStoreInfo()
        }
 
        refreshStatusBind(to: collectionView,header: { [weak self] () in
            guard let weakSelf = self,weakSelf.storeModel != nil else {
                self?.refreshStatus.onNext(.endHeaderRefresh);return
            }
            Services.homeStoreConfig(storeId: weakSelf.storeModel!.storeId).subscribe(onNext: {data in
                if let models = data.data{
                    self?.items = models
                    let group = DispatchGroup()
 
                    for m in models{
                        let queue = DispatchQueue(label: "1")
                        queue.async(group: group) {
                            group.enter()
                            SDWebImageDownloader.shared.downloadImage(with: URL(string: m.backgroundImage)) { image, data, error, status in
                                if let i = image{
                                    m.radio = i.size.width / i.size.height
                                }
                                group.leave()
                            }
                        }
                    }
 
                    group.notify(queue: .main){
                        self?.refreshStatus.onNext(.endHeaderRefresh)
                        self?.collectionView.reloadData()
                    }
                }
            }).disposed(by: weakSelf.disposeBag)
        })
    }
    
    override func setUI() {
        view.backgroundColor = UIColor(hexStr: "EEF0F3")
        
        layout = WaterFallFlowLayout()
        layout.cols = 2
        layout.sectionInset = UIEdgeInsets(top: 14, left: 34, bottom: 14, right: 34)
        layout.delegate = self
        collectionView.collectionViewLayout = layout
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .white
        collectionView.register(UINib(nibName: "HomeCCell", bundle: nil), forCellWithReuseIdentifier: "_HomeCCell")
        collectionView.jq_setEmptyView("无数据", image: UIImage(named: "icon_empty"), foregroundColor: .gray, clouse: nil)
    }
 
    private func getStoreInfo(){
        Services.homeInfo().subscribe(onNext: {[weak self] data in
            if let m = data.data{
                self?.label_store.text = m.name.isEmpty ? "门店获取失败":m.name
                var text = "欢迎来到 快乐运动俱乐部"
                if m.isVip == 0{text.append("\n加入我们 成为会员")}
                self?.label_vipInfo.text = text
 
                if m.storeId != 0{
                    self?.storeModel = m
                    self?.refreshStatus.onNext(.beingHeaderRefresh)
                }else{
                    self?.defaultData()
                }
            }
        }) { [weak self] error in
            self?.label_store.text = "门店获取失败"
            self?.defaultData()
        }.disposed(by: disposeBag)
    }
 
    private func defaultData(){
        for index in 1...8{
            let type = HomeItemType(rawValue: index)!
            let m = HomeStoreConfigModel()
            m.type = type
            m.isOpen = 1
            m.sort = index
            m.radio = type.defaultImg.size.width / type.defaultImg.size.height
            items.append(m)
        }
        collectionView.reloadData()
    }
    
    @IBAction func joinMemberAction(_ sender: UIButton) {
        let joinMemberVC = JoinMemberIntroduceVC()
        push(vc: joinMemberVC)
    }
    
    
    @IBAction func chooseStoresAction(_ sender: TapBtn) {
        StoresChooseView.show()
    }
 
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .lightContent
    }
}
 
extension HomeVC:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row == 0{
            let vc = CourseListVC()
            push(vc: vc)
        }
        
        if indexPath.row == 1{
            let vc = ActivityListVC()
            push(vc: vc)
        }
 
        if indexPath.row == 2{
            let vc = CourseOnlineListVC()
            vc.title = "看视频得奖励"
            push(vc: vc)
        }
 
        if indexPath.row == 3{
            let vc = CourseOnlineListVC()
            vc.title = "线上课得积分"
            push(vc: vc)
        }
 
        if indexPath.row == 4{
            let vc = YardListVC()
            push(vc: vc)
        }
 
        if indexPath.row == 5{
            let vc = WelfareFreeVC()
            push(vc: vc)
        }
 
        if indexPath.row == 7{
            let vc = WelfareWeeklyListVC()
            push(vc: vc)
        }
    }
    
}
 
extension HomeVC:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_HomeCCell", for: indexPath) as! HomeCCell
        let item = items[indexPath.row]
        cell.img.sd_setImage(with: URL(string: item.backgroundImage), placeholderImage: item.type.defaultImg, options: .highPriority)
        return cell
    }
 
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return items.count
    }
    
}
 
extension HomeVC: WaterFallLayoutDelegate{
    func waterFlowLayout(_ waterFlowLayout: WaterFallFlowLayout, itemHeight indexPath: IndexPath) -> CGFloat {
        let item = items[indexPath.row]
 
        if (item.radio ?? 0) >= 0.8{
            return 185
        }else{
            return 288
        }
    }
}