diff --git a/xikolo-common/Data/Helper/UserProfileHelper.swift b/xikolo-common/Data/Helper/UserProfileHelper.swift index 6bb0277f2..a8f309a9b 100644 --- a/xikolo-common/Data/Helper/UserProfileHelper.swift +++ b/xikolo-common/Data/Helper/UserProfileHelper.swift @@ -103,6 +103,9 @@ open class UserProfileHelper { DispatchQueue.main.async { NotificationCenter.default.post(name: NotificationKeys.loginStateChangedKey, object: nil) + #if OPENSAP + AppDelegate.instance().checkForVoucher() + #endif } } diff --git a/xikolo-ios/AppDelegate.swift b/xikolo-ios/AppDelegate.swift index e9aa15eda..3b8751248 100644 --- a/xikolo-ios/AppDelegate.swift +++ b/xikolo-ios/AppDelegate.swift @@ -6,6 +6,7 @@ // Copyright © 2015 HPI. All rights reserved. // +import BrightFutures import Firebase import UIKit import SDWebImage @@ -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) { @@ -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 { + let promise = Promise() + + 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