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.
 
 
 
 

195 lines
6.8 KiB

//
// RewardViewController.swift
// GMERemittance
//
// Created by Sujal on 3/14/18.
// Copyright © 2018 Gobal Money Express Co. Ltd. All rights reserved.
//
import UIKit
import SDWebImage
class RewardViewController: UIViewController {
@IBOutlet weak var rewardCollectionView: UICollectionView!
@IBOutlet weak var labelRewardPoint: UILabel!
let itemsPerRow: CGFloat = 2
let insetValue: CGFloat = 7
var availablePoints: String!
static var notificationCode: String = ""
private var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
private var rewardviewmodel = RewardViewModel()
public var fromNotification: Int?
public static var rewardConnectionTimeOutCheck = 0
override func viewDidAppear(_ animated: Bool) {
setUpNetworkListener()
setUpAnotherLoginListener(genericviewmodel: rewardviewmodel)
}
override func viewDidLoad() {
super.viewDidLoad()
if RewardViewController.notificationCode == "p700" {
setUpNavBar(id: 207, title: "Reward")
}else {
setUpNavBar(id: 201, title: "Reward")
}
setUpListener()
availablePoints = UserDefaults.standard.object(forKey: "com.gmeremit.rewardPoint") as! String
labelRewardPoint.text = availablePoints
rewardCollectionView.delegate = self
rewardCollectionView.dataSource = self
showActivityIndicator(activityIndicator: activityIndicator)
disableUserInteractions()
rewardviewmodel.rewardConnectionTimeOut.value = nil
/**
connection timeout
*/
rewardviewmodel.rewardConnectionTimeOut.bind { [unowned self] in
guard $0 != nil else {
return
}
self.enableUserInteractions()
self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
if self.fromNotification != 0{
if RewardViewController.rewardConnectionTimeOutCheck == 0{
RewardViewController.rewardConnectionTimeOutCheck = RewardViewController.rewardConnectionTimeOutCheck+1
self.popUpMessage(value: 37)
}
}
}
rewardviewmodel.fetchRewards()
}
/**
Internet Check
*/
func setUpNetworkListener() {
rewardviewmodel.internetConnection.bind { [unowned self] in
guard $0 != nil else {
return
}
self.enableUserInteractions()
self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
self.popUpMessage(value: 15)
}
}
/**
Update reward data in view
*/
func setUpListener() {
rewardviewmodel.rewardsAvailable.bind { [unowned self] in
guard $0 != nil else {
return
}
self.dismissActivityIndicator(activityIndicator: self.activityIndicator)
self.enableUserInteractions()
if $0 == 0 {
self.popUpMessageError(value: 10, message: self.rewardviewmodel.getErrorMessage())
} else if $0 == 1 {
self.rewardCollectionView.reloadData()
}
self.rewardviewmodel.rewardsAvailable.value = nil
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let redeemViewController
= segue.destination as! RedeemViewController
redeemViewController.rewardviewmodel = self.rewardviewmodel
}
}
extension RewardViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return rewardviewmodel.getRewardCount()
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let rewardCell = collectionView.dequeueReusableCell(withReuseIdentifier: "rewardCollectionViewCell", for: indexPath) as! RewardCollectionViewCell
if let rewardImageString = rewardviewmodel.getRewardImage(index: indexPath.row) {
if let rewardImageUrl = URL(string: rewardImageString) {
rewardCell.imageViewReward.sd_setImage(with: rewardImageUrl, placeholderImage: nil, options: [.progressiveDownload,.scaleDownLargeImages], completed: nil)
}
}
rewardCell.labelRewardName.text = rewardviewmodel.getRewardName(index: indexPath.row)
let redeemPoint = rewardviewmodel.getRedeemPoint(index: indexPath.row)
rewardCell.labelRewardPoint.text = redeemPoint + " " + "Points"
if Float(redeemPoint)! <= Float(availablePoints)! {
rewardCell.buttonRedeem.backgroundColor = UIColor(hex: 0xec1c24)
} else {
rewardCell.buttonRedeem.isUserInteractionEnabled = false
}
rewardCell.delegate = self
return rewardCell
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
let widthPerItem = (collectionView.frame.width - insetValue * 2 - insetValue) / itemsPerRow
return CGSize(width: widthPerItem, height: 1.1 * widthPerItem)
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: insetValue, left: insetValue, bottom: insetValue, right: insetValue)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return insetValue
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return insetValue
}
}
extension RewardViewController: RedeemRewardDelegate {
func redeemReward(_ sender: RewardCollectionViewCell) {
guard let indexPath = rewardCollectionView.indexPath(for: sender) else {
return
}
rewardviewmodel.setSelectedRewardIndex(index: indexPath.row)
}
}