younger_times
2023-07-24 858bd6df13a6a6415d12d8e60141575574646f58
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
//  WelfareVC.swift
//  WanPai
//
//  Created by 杨锴 on 2023/6/8.
//
 
import UIKit
import JQTools
import QMUIKit
 
class WelfareVC: BaseVC {
 
    @IBOutlet weak var img_userProfile: UIImageView!
    @IBOutlet weak var label_username: UILabel!
    @IBOutlet weak var btn_vip: QMUIButton!
    @IBOutlet weak var label_coin: UILabel!
    @IBOutlet weak var label_score: UILabel!
    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var coinCollectionView: UICollectionView!
    @IBOutlet weak var btn_coupon: UIButton!
    @IBOutlet weak var btn_shoppping: UIButton!
    @IBOutlet weak var btn_weekly: UIButton!
    @IBOutlet weak var btn_todayFree: UIButton!
 
    private var timer:Timer?
    private var timerOffsetX:Double = 0
    private let cellW = JQ_ScreenW * 0.3794
    private var benefitHomeModel:BenefitHomeModel?
 
    override func viewDidLoad() {
        super.viewDidLoad()
 
        Services.benefitHome().subscribe(onNext: {[weak self] data in
            if let model = data.data{
                self?.benefitHomeModel = model
                self?.img_userProfile.sd_setImage(with: URL(string: model.userHeadImg))
                self?.label_username.text = model.userName
                self?.btn_vip.isHidden = model.isMember == "年度会员"
                self?.label_coin.text = "\(model.wpCoin)"
                self?.label_score.text = "\(model.userIntegral)"
                self?.coinCollectionView.reloadData()
 
                if let coupon = model.image?.myConpons{
                    self?.btn_coupon.sd_setImage(with: URL(string: coupon), for: .normal, placeholderImage: nil,context: nil)
                }
 
                if let onlineShop = model.image?.onlineShop{
                    self?.btn_shoppping.sd_setImage(with: URL(string: onlineShop), for: .normal, placeholderImage: nil, context: nil)
                }
 
                if let weeksBenefit = model.image?.weeksBenefit{
                    self?.btn_weekly.sd_setImage(with: URL(string: weeksBenefit), for: .normal, placeholderImage: nil, context: nil)
                }
 
                if let todayFree = model.image?.todayFree{
                    self?.btn_todayFree.sd_setImage(with: URL(string: todayFree), for: .normal, placeholderImage: nil, context: nil)
                }
 
                if model.commodities.count > 0{
                    self?.autoScroll()
                }
            }
        }) { error in
 
        }.disposed(by: disposeBag)
    }
 
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        timer?.invalidate()
    }
 
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        navigationController?.isNavigationBarHidden = false
    }
 
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationController?.isNavigationBarHidden = true
    }
 
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if timer != nil{
            recoverTimer()
        }else{
            autoScroll()
        }
    }
 
    override func setUI() {
        scrollView.contentInsetAdjustmentBehavior = .never
        coinCollectionView.delegate  = self
        coinCollectionView.dataSource = self
        coinCollectionView.register(UINib(nibName: "WelfareCoinCCell", bundle: nil), forCellWithReuseIdentifier: "_WelfareCoinCCell")
    }
 
    @IBAction func userProfileAction(_ sender: UIButton) {
        let vc = ProfileVC()
        push(vc: vc)
    }
 
    @IBAction func billAction(_ sender: UIButton) {
        let vc = WelfareBillListVC()
        push(vc: vc)
    }
 
    @IBAction func couponsAction(_ sender: UIButton) {
        let vc = WelfareCouponsListVC()
        push(vc: vc)
    }
 
    @IBAction func rechargeAction(_ sender: UIButton) {
        let vc = RechargeRecordVC(coin: benefitHomeModel?.wpCoin ?? 0)
        push(vc: vc)
    }
 
    @IBAction func welfareWeeklyAction(_ sender: UIButton) {
        let vc = WelfareWeeklyListVC()
        push(vc: vc)
    }
    
    @IBAction func freeTodayAction(_ sender: UIButton) {
        let vc = WelfareFreeVC(StoreWalfareModel())
        push(vc: vc)
    }
 
    @IBAction func storeAction(_ sender: Any) {
        let vc = CoinStoreCenterVC()
        push(vc: vc)
    }
 
    
    private func autoScroll(){
        guard benefitHomeModel != nil else {return}
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(runTimer), userInfo: nil, repeats: true)
        timer?.fire()
        RunLoop.current.add(timer!, forMode: .common)
    }
 
    @objc func runTimer(){
        if timerOffsetX >= (JQ_ScreenW - cellW * Double(benefitHomeModel!.commodities.count - 3)){
            timerOffsetX = 0
            coinCollectionView.setContentOffset(CGPoint.zero, animated: true)
            timer?.invalidate()
            DispatchQueue.main.asyncAfter(deadline: .now()+2.5) {
                self.recoverTimer()
            }
        }
        self.timerOffsetX += 1
        self.coinCollectionView.setContentOffset(CGPoint(x: timerOffsetX, y: 0), animated: true)
    }
 
    private func recoverTimer(){
        guard benefitHomeModel != nil else {return}
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(runTimer), userInfo: nil, repeats: true)
    }
 
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        timer?.invalidate()
    }
 
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        self.timerOffsetX = max(0,scrollView.contentOffset.x)
        self.timer?.invalidate()
        DispatchQueue.main.asyncAfter(deadline: .now()+2.5) {
            self.recoverTimer()
        }
    }
 
    override var preferredStatusBarStyle: UIStatusBarStyle{
        return .darkContent
    }
}
 
extension WelfareVC:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = benefitHomeModel!.commodities[indexPath.row]
        let vc = WelfareRedeemGoodsDetailVC()
        push(vc: vc)
    }
}
 
extension WelfareVC:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return benefitHomeModel?.commodities.count ?? 0
    }
 
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
       let model = benefitHomeModel!.commodities[indexPath.row]
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_WelfareCoinCCell", for: indexPath) as! WelfareCoinCCell
        cell.label_name.text = model.commodityName
        cell.label_price.text = model.commodityPrice.currency()
        cell.img_cover.sd_setImage(with: URL(string: model.commodityImg), placeholderImage: UIImage(named: "test_1"))
        return cell
    }
}
 
extension WelfareVC:UICollectionViewDelegateFlowLayout{
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 0
    }
 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
 
        return CGSize(width: cellW, height: cellW)
    }
}