Skip to content

Commit

Permalink
Show alert for openSAP birthday voucher
Browse files Browse the repository at this point in the history
  • Loading branch information
mathebox committed Apr 13, 2018
1 parent 47dd5b4 commit f1e787b
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
3 changes: 3 additions & 0 deletions xikolo-common/Data/Helper/UserProfileHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ open class UserProfileHelper {

DispatchQueue.main.async {
NotificationCenter.default.post(name: NotificationKeys.loginStateChangedKey, object: nil)
#if OPENSAP
AppDelegate.instance().checkForVoucher()
#endif
}
}

Expand Down
82 changes: 82 additions & 0 deletions xikolo-ios/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright © 2015 HPI. All rights reserved.
//

import BrightFutures
import Firebase
import UIKit
import SDWebImage
Expand Down Expand Up @@ -94,6 +95,9 @@ class AppDelegate : AbstractAppDelegate {

func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
#if OPENSAP
self.checkForVoucher()
#endif
}

override func applicationWillTerminate(_ application: UIApplication) {
Expand Down Expand Up @@ -159,3 +163,81 @@ extension AppDelegate : AbstractLoginViewControllerDelegate {
self.tabBarController?.selectedIndex = 0
}
}

#if OPENSAP
extension AppDelegate {

func checkForVoucher() {
self.requestVoucher().onSuccess { awarded in
guard awarded else { return }

let title = "It's our fifth anniversary"
let message = "You've earned a free openSAP reactivation code! Your code and more details will be delivered to your inbox shortly."
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Thanks", style: .default))
self.tabBarController?.present(alert, animated: true)
}
}

func requestVoucher() -> Future<Bool, XikoloError> {
let promise = Promise<Bool, XikoloError>()

let urlString = Routes.API_V2_URL + "/osap_5th_birthday.json"
guard let url = URL(string: urlString) else {
promise.failure(.invalidURL(urlString))
return promise.future
}

var request = URLRequest(url: url)
request.httpMethod = "GET"

request.setValue(Routes.HEADER_USER_PLATFORM_VALUE, forHTTPHeaderField: Routes.HEADER_USER_PLATFORM)
for (key, value) in NetworkHelper.getRequestHeaders() {
request.setValue(value, forHTTPHeaderField: key)
}

let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let err = error {
promise.failure(.network(err))
return
}

guard let urlResponse = response as? HTTPURLResponse else {
promise.failure(.api(.invalidResponse))
return
}

guard 200 ... 299 ~= urlResponse.statusCode else {
promise.failure(.api(.responseError(statusCode: urlResponse.statusCode, headers: urlResponse.allHeaderFields)))
return
}

guard let responseData = data else {
promise.failure(.api(.noData))
return
}

do {
guard let json = try JSONSerialization.jsonObject(with: responseData, options: []) as? [String: Any] else {
promise.failure(.api(.serializationError(.invalidDocumentStructure)))
return
}

guard let voucher = json["voucher"] as? String else {
promise.failure(.invalidData)
return
}

return promise.success(voucher == "awarded")
} catch {
promise.failure(.api(.serializationError(.jsonSerializationError(error))))
}
}

task.resume()

return promise.future
}

}
#endif

0 comments on commit f1e787b

Please sign in to comment.