//
|
// 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)
|
}
|
}
|