Manos Chorianopoulos

Add OffersViewController

......@@ -8,10 +8,47 @@
import UIKit
@objc public class OffersTableViewCell: UITableViewCell {
@IBOutlet weak var cellView: UIView!
@IBOutlet weak var campaignImage: UIImageView!
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var subtitleLabel: UILabel!
@IBOutlet weak var discountBGImage: UIImageView!
@IBOutlet weak var discountSmileImage: UIImageView!
@IBOutlet weak var discountLabel: UILabel!
var postImageURL: String? {
didSet {
if let url = postImageURL {
self.campaignImage.image = UIImage() // UIImage(named: "loading")
UIImage.loadImageUsingCacheWithUrlString(url) { image in
// set the image only when we are still displaying the content for the image we finished downloading
if url == self.postImageURL {
self.campaignImage.image = image
}
}
}
else {
self.campaignImage.image = nil
}
}
}
public override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
// Add shadow
self.layer.shadowColor = UIColor(red: 0.00, green: 0.00, blue: 0.00, alpha: 0.16).cgColor
self.layer.shadowOffset = CGSize(width: 0.0, height: 3.0)
self.layer.shadowOpacity = 1.0
self.layer.shadowRadius = 6.0
cellView.layer.cornerRadius = 5.0
cellView.clipsToBounds = true
discountBGImage.image = UIImage(named: "offer_circle_orange", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
discountSmileImage.image = UIImage(named: "logo_smile_white_new", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
}
public override func setSelected(_ selected: Bool, animated: Bool) {
......@@ -19,5 +56,22 @@ import UIKit
// Configure the view for the selected state
}
public override func layoutSubviews() {
super.layoutSubviews()
//set the values for top,left,bottom,right margins
let margins = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
contentView.frame = contentView.frame.inset(by: margins)
}
func configureCell(campaign: swiftApi.CampaignItemModel) {
// campaignImage.load(link: campaign.logo_url ?? "", placeholder: UIImage(), cache: URLCache())
self.postImageURL = campaign.logo_url ?? ""
titleLabel.text = campaign.title ?? ""
subtitleLabel.text = campaign.subtitle ?? ""
discountLabel.text = "10%"
}
}
......
......@@ -8,22 +8,117 @@
import UIKit
@objc public class OffersViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
public var campaigns:Array<swiftApi.CampaignItemModel> = [] {
didSet {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
public override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
getCampaignsRequest()
tableView.delegate = self
tableView.dataSource = self
tableView.contentInset.top = 25
}
// MARK: - Requests
func getCampaignsRequest() {
swiftApi().getCampaignsAsyncNew(language: "en", filters: [String : Any](), getCampaignsCallback, failureCallback: {errorCode in
print("========= getCampaignsCallback ERROR =========",errorCode)
})
}
func getCampaignsCallback (_ campaignsData: Array<swiftApi.CampaignItemModel>?) -> Void {
if (campaignsData != nil) {
DispatchQueue.main.async {
self.campaigns = campaignsData ?? []
self.tableView.reloadData()
}
} else {
print("========= getCampaignsCallback ERROR =========")
}
}
}
// MARK: - TableView
extension OffersViewController: UITableViewDelegate, UITableViewDataSource{
public func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.campaigns.count
}
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 288.0 + 20.0
// return UITableViewAutomaticDimension
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "OffersTableViewCellId", for: indexPath) as! OffersTableViewCell
cell.configureCell(campaign: campaigns[indexPath.row])
return cell
}
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let status = swiftApi().getNetworkStatus()
if (status == -1 || status == 0) {
self.showDialog("Δεν υπάρχει σύνδεση","Αυτή τη στιγμή βρίσκεσαι εκτός σύνδεσης. Παρακαλούμε βεβαιώσου ότι είσαι συνδεδεμένος στο διαδίκτυο και προσπάθησε ξανά.")
} else {
let storyboard = UIStoryboard(name: "Main", bundle: Bundle(for: MyEmptyClass.self))
let vc = storyboard.instantiateViewController(withIdentifier: "CampaignViewController") as! SwiftWarplyFramework.CampaignViewController
var url = ""
var params = ""
// url = swiftApi().constructCampaignUrl(campaigns[indexPath.row])
// params = swiftApi().constructCampaignParams(campaigns[indexPath.row])
url = campaigns[indexPath.row].index_url ?? ""
/*
// MARK: - Navigation
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
let data = try! encoder.encode(campaigns[indexPath.row])
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
print("Campaign_CLICK " + String(data: data, encoding: .utf8)!)
print("CampaignUrl url onclick: " + url)
vc.campaignUrl = url
vc.params = params
self.navigationController?.pushViewController(vc, animated: true)
}
}
*/
func showDialog(_ alertTitle: String, _ alertSubTitle: String) -> Void {
let alert = UIAlertController(title: alertTitle, message: alertSubTitle, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
switch action.style{
case .default:
print("default")
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}
}))
self.present(alert, animated: true, completion: nil)
}
}
......
......@@ -1456,16 +1456,10 @@ public class swiftApi {
func campaignsCallback(_ campaignsData: [AnyHashable: Any]?) -> Void {
var campaignsArray:Array<CampaignItemModel> = []
var carouselArray:Array<CampaignItemModel> = []
if let responseDataDictionary = campaignsData as? [String: AnyObject] {
if (responseDataDictionary["MAPP_CAMPAIGNING-status"] as? Int == 1) {
let dynatraceEvent = swiftApi.LoyaltySDKDynatraceEventModel()
dynatraceEvent._eventName = "custom_success_campaigns_loyalty"
dynatraceEvent._parameters = nil
// SwiftEventBus.post("dynatrace", sender: dynatraceEvent)
if let responseDataMapp = responseDataDictionary["MAPP_CAMPAIGNING"] as? [String: Any] {
if let responseDataCampaigns = responseDataMapp["campaigns"] as? [[String : Any]?] {
......@@ -1475,64 +1469,24 @@ public class swiftApi {
campaignsArray.append(tempCampaign)
}
}
}
} else {
getCampaignsCallback(nil)
}
swiftApi().getCampaignsPersonalizedAsync(language: language, filters: filters, {campaignsPersonalizedData in
campaignsArray = campaignsArray + (campaignsPersonalizedData ?? [])
swiftApi().setUniqueCampaignList(campaignsArray)
carouselArray = campaignsArray.filter {
let tempCampaign = $0
let isCarouselItem = (tempCampaign._carousel != nil) && (tempCampaign._carousel == "true")
return (isCarouselItem)
}
swiftApi().setCarouselList(carouselArray)
swiftApi().setUniqueCampaignList(campaignsArray)
var filteredCampaigns = campaignsArray.filter {
let tempCampaign = $0
let isCcmsOffer = (tempCampaign.ccms_offer != nil) && (tempCampaign.ccms_offer != "") && (tempCampaign.ccms_offer == "true")
let isTelco = (tempCampaign._type != nil && tempCampaign._type == "telco")
return (!(isCcmsOffer || isTelco))
}
// if (swiftApi().getConsumerInternal()?._answered == true) {
filteredCampaigns = filteredCampaigns.filter { $0.offer_category != "questionnaire" }
// }
let sortedCampaigns = campaignsArray.sorted {
($0._sorting ?? 0) < ($1._sorting ?? 0)
}
let sortedCampaigns = filteredCampaigns.sorted {
($0._sorting ?? 0) < ($1._sorting ?? 0)
getCampaignsCallback(sortedCampaigns)
}
getCampaignsCallback(sortedCampaigns);
// SwiftEventBus.post("campaigns_retrieved")
}, failureCallback: {errorCode in
failureCallback(errorCode)
})
} else {
getCampaignsCallback(nil)
}
} else {
let dynatraceEvent = swiftApi.LoyaltySDKDynatraceEventModel()
dynatraceEvent._eventName = "custom_error_campaigns_loyalty"
dynatraceEvent._parameters = nil
// SwiftEventBus.post("dynatrace", sender: dynatraceEvent)
getCampaignsCallback(nil)
}
} else {
let dynatraceEvent = swiftApi.LoyaltySDKDynatraceEventModel()
dynatraceEvent._eventName = "custom_error_campaigns_loyalty"
dynatraceEvent._parameters = nil
// SwiftEventBus.post("dynatrace", sender: dynatraceEvent)
getCampaignsCallback(nil)
}
}
......@@ -1542,11 +1496,6 @@ public class swiftApi {
print(error)
print("====================")
let dynatraceEvent = swiftApi.LoyaltySDKDynatraceEventModel()
dynatraceEvent._eventName = "custom_error_campaigns_loyalty"
dynatraceEvent._parameters = nil
// SwiftEventBus.post("dynatrace", sender: dynatraceEvent)
getCampaignsCallback(nil)
}
}
......