宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
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
//
//  MyPointsVC.swift
//  OKProject
//
//  Created by Sweet on 2020/12/25.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
import MJRefresh
class MyPointsVC: YYViewController {
    var cuurentPoints = 0
   
    let viewModel = PointsViewModel()
    var dataSoure = [PoinstModel]()
    @IBOutlet weak var collectionView: UICollectionView!
    var page = 1
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        collectionView.mj_header?.beginRefreshing()
        collectionView.reloadData()
       
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "我的积分"
        let layout = UICollectionViewFlowLayout.init()
        layout.scrollDirection = .vertical
        layout.itemSize = CGSize(width: (screenW) / 2 , height: 190)
        layout.minimumLineSpacing=0
        layout.minimumInteritemSpacing=0
        layout.headerReferenceSize = CGSize(width: screenW, height: 120)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.setCollectionViewLayout(layout, animated: true)
        collectionView.register(UINib(nibName: "PointsCollectionViewCell", bundle: nil ), forCellWithReuseIdentifier: "PointsCollectionViewCell")
        
        collectionView.register(UINib(nibName: "PointsCollectionReusableView", bundle: nil ), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "PointsCollectionReusableView")
        collectionView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.page = 1
            self.loadData()
        })
        
        collectionView.mj_footer = MJRefreshBackNormalFooter(refreshingBlock: {
            self.page += 1
            self.loadData()
        })
        navigationItem.rightBarButtonItem = UIBarButtonItem.yy_creat(title: "兑换记录", target: self , action: #selector(rightAction)).item
    }
    @objc func rightAction(){
        let vc  = ChangeListVC()
        yy_push(vc: vc )
    }
    func loadData(){
        
        viewModel.getPoinstData(page: page).subscribe { (r) in
            self.collectionView.mj_header?.endRefreshing()
            self.collectionView.mj_footer?.endRefreshing()
            switch r{
            case.success(let data):
                guard let data = data else { return  }
                if self.page == 1 {
                    self.dataSoure = data
                }else{
                    self.dataSoure.append(contentsOf: data)
                }
                self.collectionView.reloadData()
                break
            case.failure(let error):
                self.collectionView.mj_header?.endRefreshing()
                self.collectionView.mj_footer?.endRefreshing()
                alert(text: error.localizedDescription)
                break
            }
        } onError: { (error) in
            
        } onCompleted: {
            
        } onDisposed: {
            
        }.disposed(by: disposeBag)
        
    }
   
    
}
extension MyPointsVC:UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataSoure.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cellString = "PointsCollectionViewCell"
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellString, for: indexPath) as!  PointsCollectionViewCell
        cell.onModel(model: dataSoure[indexPath.row])
        return cell
    }
    //设置 区头和区尾
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader  {
            let headerView:PointsCollectionReusableView = collectionView.dequeueReusableSupplementaryView(ofKind:UICollectionView.elementKindSectionHeader , withReuseIdentifier: "PointsCollectionReusableView", for: indexPath) as! PointsCollectionReusableView
            headerView.numLb.text = "\(app.userInfo.integral)"
            return headerView
        }
        return UICollectionReusableView()
    }
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: screenW, height: 120)
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = dataSoure[indexPath.row]
        let vc  = PointsDeatilVC()
        vc.model = model
        yy_push(vc: vc)
    }
}