You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

210 lines
5.6 KiB

//
// RewardViewController.swift
// GME Remit
//
// Created by InKwon Devik Kim on 11/04/2019.
//Copyright © 2019 Gobal Money Express Co. Ltd. All rights reserved.
//
import UIKit
import XLPagerTabStrip
class RewardViewController: UIViewController {
// MARK: Properties
var presenter: RewardModuleInterface?
private var models: [RewardProduct]? {
didSet {
collectionView.reloadData()
}
}
private var rewardPoint: String? {
didSet {
rewardPointLabel.text = rewardPoint
}
}
private lazy var refreshControl = UIRefreshControl()
// MARK: IBOutlets
@IBOutlet weak var collectionView: UICollectionView!
@IBOutlet weak var rewardPointLabel: UILabel!
@IBOutlet weak var rewardPointTitleLabel: UILabel!
@IBOutlet weak var rewardNoteLabel: UILabel!
@IBOutlet weak var rewardPointContainerView: UIView!
@IBOutlet weak var layoutSegment: UISegmentedControl!
// MARK: VC's Life cycle
override func viewDidLoad() {
super.viewDidLoad()
setup()
layoutSegment.addTarget(self, action: #selector(selectLayout(_:)), for: .valueChanged)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
title = "reward_group_title_text".localized()
collectionView.setContentOffset(.zero, animated: false)
rewardPoint = GMEDB.shared.user.string(.rewardPoint)?.likeCommaMoney()
collectionView.reloadData()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationItem.title = ""
}
// MARK: IBActions
@objc func selectLayout(_ sender: UISegmentedControl) {
setCollectionViewLayout(sender.selectedSegmentIndex)
}
// MARK: Other Functions
private func setup() {
// all setup should be done here
setDelegate()
setCollectionViewLayout(0)
addRefreshControlCollectionView()
setMultiLanguage()
rewardPointContainerView.hero.id = "pointsView"
presenter?.viewIsReady()
layoutSegment.tintColor = .themeRed
layoutSegment.backgroundColor = .clear
collectionView.layer.cornerRadius = 5
}
private func setDelegate() {
collectionView.dataSource = self
}
private func setMultiLanguage() {
rewardPointTitleLabel.text = "reward_points_text".localized()
rewardNoteLabel.text = "reward_note_text".localized()
}
private func setCollectionViewLayout(_ type: Int) {
let width: CGFloat
let height: CGFloat
switch type {
case 0:
width = (view.frame.width - 30) / 2 - 3.5
height = (view.frame.width - 30) / 2 - 3.5 + 30
case 1:
width = (view.frame.width - 30)
height = (( width * 260 ) / 320) + 5
default:
width = (view.frame.width - 30) / 2 - 3.5
height = (view.frame.width - 30) / 2 - 3.5 + 30
}
let cellSize = CGSize(
width: width,
height: height
)
let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .vertical
layout.itemSize = cellSize
layout.sectionInset = UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
layout.minimumLineSpacing = 5.0
layout.minimumInteritemSpacing = 5.0
collectionView.setCollectionViewLayout(layout, animated: true)
collectionView.reloadData()
collectionView.collectionViewLayout.invalidateLayout()
}
private func addRefreshControlCollectionView() {
let colorOption = [NSAttributedString.Key.foregroundColor : UIColor.themeWhite]
let title = NSAttributedString(string: "pull to refresh", attributes: colorOption)
refreshControl.attributedTitle = title
refreshControl.backgroundColor = .themeBlue
refreshControl.tintColor = .themeWhite
refreshControl.addTarget(self, action: #selector(refresh), for: .valueChanged)
if #available(iOS 10.0, *) {
collectionView.refreshControl = refreshControl
} else {
collectionView.addSubview(refreshControl)
}
}
@objc
private func refresh() {
self.presenter?.viewIsReady()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {[weak self] in
guard let `self` = self else {return}
if #available(iOS 10.0, *) {
self.collectionView.refreshControl?.endRefreshing()
} else {
self.refreshControl.endRefreshing()
}
}
}
}
// MARK: RewardViewInterface
extension RewardViewController: RewardViewInterface {
func setRewardProducts(models: [RewardProduct]?) {
self.models = models
}
func failure(error: Error) {
alertWithOk(message: error.localizedDescription)
}
func goRedeemViewController(with model: RewardProduct?) {
presenter?.goRedeemViewController(with: model)
}
func startLoading() {
showProgressHud()
}
func endLoading() {
hideProgressHud()
}
}
extension RewardViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return models?.count ?? 0
}
func collectionView(
_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath
) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(
withReuseIdentifier: "RewardItemCollectionViewCell",
for: indexPath
) as? RewardItemCollectionViewCell else {
return UICollectionViewCell()
}
cell.setModel(with: models?[indexPath.row], delegate: self)
return cell
}
}
// MARK: - XLPagerTabStrip's IndicatorInfoProvider
extension RewardViewController: IndicatorInfoProvider {
func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
return IndicatorInfo.init(title: "products_text".localized())
}
}