Manos Chorianopoulos

add steps service

......@@ -7,7 +7,7 @@
<key>Pods-SwiftWarplyFramework.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>0</integer>
</dict>
</dict>
</dict>
......
......@@ -1045,6 +1045,7 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_NSHumanReadableCopyright = "";
INFOPLIST_KEY_NSMotionUsageDescription = "We are using motion usage in order to track your step count.";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = (
......@@ -1081,6 +1082,7 @@
DYLIB_INSTALL_NAME_BASE = "@rpath";
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_NSHumanReadableCopyright = "";
INFOPLIST_KEY_NSMotionUsageDescription = "We are using motion usage in order to track your step count.";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
IPHONEOS_DEPLOYMENT_TARGET = 11.0;
LD_RUNPATH_SEARCH_PATHS = (
......
......@@ -7,7 +7,7 @@
<key>SwiftWarplyFramework.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
<integer>1</integer>
</dict>
</dict>
</dict>
......
......@@ -35,6 +35,16 @@ import SwiftEventBus
webview.configuration.userContentController.add(self, name: "Cosmote")
}
// MARK: - API Calls
func startTrackingSteps() {
swiftApi().startTrackingSteps(startTrackingStepsCallback)
}
func startTrackingStepsCallback (_ steps: Int) -> Void {
DispatchQueue.main.async {
}
}
// MARK: - WKNavigationDelegate
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
......@@ -129,11 +139,13 @@ import SwiftEventBus
let pacingEvent = swiftApi.WarplyPacingEventModel()
pacingEvent._isVisible = true
SwiftEventBus.post("pacing", sender: pacingEvent)
self.startTrackingSteps()
} else if (eventArray[1] == "steps" && eventArray[2] == "shortcutDisabled") {
let pacingEvent = swiftApi.WarplyPacingEventModel()
pacingEvent._isVisible = false
SwiftEventBus.post("pacing", sender: pacingEvent)
swiftApi().stopTrackingSteps()
} else if (eventArray[1] == "request" || eventArray[1] == "response") {
print("**************** WARPLY Webview Log START *****************")
......
......@@ -20,6 +20,8 @@
- (void) setToStage;
- (void) setLang:(NSString*) lang;
- (void) getSteps:(void(^)(CMPedometerData* stepsData)) completion;
- (void) startTrackingSteps:(void(^)(NSNumber * _Nullable numberOfSteps))callback;
- (void) stopTrackingSteps;
// - (UIViewController *) openCoupons:(UIView*) parentView;
//- (UIViewController *) openCoupon:(UIView*) parentView coupon:(NSDictionary*) coupon;
//- (UIViewController *) openCouponBarcode:(UIView*) parentView coupon:(NSDictionary*) coupon;
......@@ -85,6 +87,7 @@
- (void)consumerIntegrationAsync:(NSNumber*)nonTelco :(NSNumber*)acceptedConsent :(NSArray*)msisdnList :(NSString*)guid :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)redeemCouponSetAsync:(NSString*)uuid :(NSString*)communication_uuid :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)getPacingDetailsAsync:(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)setPacingDetailsAsync:(NSNumber*)steps :(NSString*)date :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)cosmoteSharingAsync:(NSString*)sharingId :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)cosmoteRetrieveSharingAsync:(NSString*)sharingId :(NSNumber*)accept :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)getCosmoteUserAsync:(NSString*)guid :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
......
......@@ -18,8 +18,9 @@ NSString *WARP_HOST = @"engage.warp.ly";
NSString *WARP_ERROR_DOMAIN = @"engage.warp.ly";
NSString *MERCHANT_ID;
NSString *LANG;
NSDate *date;
CMPedometer *pedometer;
// NSDate *date;
// CMPedometer *pedometer;
CMPedometer *_pedometer;
NSString *VERIFY_URL = @"/partners/cosmote/verify";
- (void)initialize:(NSDictionary *)launchOptions uuid:(NSString*)uuid merchantId:(NSString*)merchantId lang:(NSString*)lang{
......@@ -82,38 +83,68 @@ NSString *VERIFY_URL = @"/partners/cosmote/verify";
// }
}
- (void)queryDataFrom:(NSDate *)startDate toDate:(NSDate *)endDate {
[pedometer queryPedometerDataFromDate:startDate
toDate:endDate
withHandler:
^(CMPedometerData *pedometerData, NSError *error) {
// - (void)queryDataFrom:(NSDate *)startDate toDate:(NSDate *)endDate {
// [pedometer queryPedometerDataFromDate:startDate
// toDate:endDate
// withHandler:
// ^(CMPedometerData *pedometerData, NSError *error) {
NSLog(@"data:%@, error:%@", pedometerData, error);
// NSLog(@"data:%@, error:%@", pedometerData, error);
dispatch_async(dispatch_get_main_queue(), ^{
if (error) {
NSLog(@"Error = %@",error.userInfo);
} else {
}
});
}];
}
// dispatch_async(dispatch_get_main_queue(), ^{
// if (error) {
// NSLog(@"Error = %@",error.userInfo);
// } else {
// }
// });
// }];
// }
// - (void)recursiveQuery {
// NSDate *to = [NSDate date];
// NSDate *from = [to dateByAddingTimeInterval:-(24. * 3600.)];
// [self queryDataFrom:from toDate:to];
// }
// - (void) getSteps:(void(^)(CMPedometerData* stepsData)) completion {
// [pedometer startPedometerUpdatesFromDate:date
// withHandler:^(CMPedometerData *pedometerData, NSError *error) {
// dispatch_async(dispatch_get_main_queue(), ^{
// completion(pedometerData);
// });
// }];
// }
- (void)recursiveQuery {
NSDate *to = [NSDate date];
NSDate *from = [to dateByAddingTimeInterval:-(24. * 3600.)];
[self queryDataFrom:from toDate:to];
- (CMPedometer *)pedometer {
if (!_pedometer) {
_pedometer = [[CMPedometer alloc]init];
}
return _pedometer;
}
- (void) getSteps:(void(^)(CMPedometerData* stepsData)) completion {
[pedometer startPedometerUpdatesFromDate:date
withHandler:^(CMPedometerData *pedometerData, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
completion(pedometerData);
});
- (void)startTrackingSteps:(void(^)(NSNumber * _Nullable numberOfSteps))callback {
// start live tracking
[self.pedometer startPedometerUpdatesFromDate:[NSDate date] withHandler:^(CMPedometerData * _Nullable pedometerData, NSError * _Nullable error) {
// this block is called for each live update
if (pedometerData != nil) {
callback(pedometerData.numberOfSteps);
} else if (error) {
NSLog(@"startTrackingSteps error: %@", error);
}
}];
}
- (void)stopTrackingSteps {
// stop live tracking
[self.pedometer stopPedometerUpdates];
}
- (void) setToStage {
WARP_PRODUCTION_BASE_URL = @"https://engage-stage.warp.ly";
WARP_HOST = @"engage-stage.warp.ly";
......@@ -1421,6 +1452,17 @@ NSString *VERIFY_URL = @"/partners/cosmote/verify";
}];
}
- (void)setPacingDetailsAsync:(NSNumber*)steps :(NSString*)date :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
{
[[Warply sharedService] setPacingDetailsWithSuccessBlock:steps :date :^(NSDictionary *response) {
if (success) {
success(response);
}
} failureBlock:^(NSError *error) {
failure(error);
}];
}
- (void)cosmoteSharingAsync:(NSString*)sharingId :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
{
[[Warply sharedService] cosmoteSharingWithSuccessBlock:sharingId :^(NSDictionary *response) {
......
......@@ -338,6 +338,8 @@ WL_VERSION_INTERFACE()
- (void)getPacingDetailsWithSuccessBlock :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)setPacingDetailsWithSuccessBlock:(NSNumber*)steps :(NSString*)date :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
- (void)editProfileWithSuccessBlock:(NSString*)firstName andLastName:(NSString*)lastName andEmail:(NSString*)email andSalutation:(NSString*)salutation andMsisdn:(NSString*)msisdn
andNickname:(NSString*)nickname andGender:(NSString*)gender andBirthday:(NSString*)birthday andNameDay:(NSString*)nameday andTaxID:(NSString*)taxID andProfileMetadata:(NSDictionary*)profileMetadata optin:(NSNumber*) optin newsLetter:(NSNumber*)newsletter
andSMS:(NSNumber*)sms andSegmentation:(NSNumber*)segmentation andSMSSegmentation:(NSNumber*)smsSegmentation :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure;
......
......@@ -1231,6 +1231,47 @@ WL_VERSION_IMPLEMENTATION(WL_VERSION)
}];
}
- (void)setPacingDetailsWithSuccessBlock:(NSNumber*)steps :(NSString*)date :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
{
NSDictionary *postDictionary = @{@"consumer_data":@{@"method": @"steps", @"action": @"integration", @"operation": @"add", @"data":@{@"event": @"steps", @"type": @"counter", @"counter": steps, @"date": date}}};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:postDictionary options:0 error:NULL];
[self sendContext8:jsonData successBlock:^(NSDictionary *contextResponse) {
if (success) {
success(contextResponse);
}
NSLog(@"**************** WARPLY Response *****************" );
NSLog(@"%@", contextResponse );
} failureBlock:^(NSError *error) {
if (failure) {
NSDictionary* dict = [NSDictionary alloc];
dict = [error userInfo];
NSString* errorCode = [dict objectForKey:@"NSLocalizedDescription"];
if ([errorCode isEqual:@"Request failed: unauthorized (401)"]) {
[self refreshToken:^(NSDictionary *response) {
[self sendContext8:jsonData successBlock:^(NSDictionary *contextResponse) {
if (success) {
success(contextResponse);
}
NSLog(@"**************** WARPLY Response *****************" );
NSLog(@"%@", contextResponse );
} failureBlock:^(NSError *error) {
if (failure) {
failure(error);
}
}];
} failureBlock:^(NSError *error) {
if (failure) {
[_db executeUpdate:@"DROP TABLE requestVariables"];
failure(error);
}
NSLog(@"Error at token %@", error );
}];
}
NSLog(@"Error at set pacing details %@", error );
}
}];
}
- (void)consumerIntegrationWithSuccessBlock:(NSNumber*)nonTelco :(NSNumber*)acceptedConsent :(NSArray*)msisdnList :(NSString*)guid :(void(^)(NSDictionary *response))success failureBlock:(void(^)(NSError *error))failure
{
NSMutableDictionary* data = [[NSMutableDictionary alloc] init];
......
......@@ -22,8 +22,11 @@ public struct GlobalVariables {
static var ccmsCampaigns: Array<swiftApi.LoyaltyContextualOfferModel> = []
static var dealsSum: Float = 0.0
static var seasonalList: Array<swiftApi.LoyaltyGiftsForYouPackage> = []
static var savedSteps: Int = 0
}
var timer: DispatchSourceTimer?
public class swiftApi {
public init() {
......@@ -37,7 +40,103 @@ public class swiftApi {
public func setUserTag(_ newUserTag: String) -> Void {
UserDefaults.standard.set(newUserTag, forKey: "userTag")
}
public func getSteps() -> Int {
return UserDefaults.standard.integer(forKey: "steps")
}
public func setSteps(_ newValue: Int) -> Void {
UserDefaults.standard.set(newValue, forKey: "steps")
}
public func startTrackingSteps(_ getStepsCallback: @escaping (_ steps: Int) -> Void) -> Void {
let instanceOfMyApi = MyApi()
instanceOfMyApi.startTrackingSteps(startTrackingCallback)
swiftApi().startTimer()
func startTrackingCallback(_ steps: NSNumber?) -> Void {
if (steps != nil) {
swiftApi().setSteps((steps as! Int) - GlobalVariables.savedSteps)
}
getStepsCallback(steps as? Int ?? 0)
}
}
public func stopTrackingSteps() -> Void {
let instanceOfMyApi = MyApi()
instanceOfMyApi.stopTrackingSteps()
stopTimer()
let persistedSteps = swiftApi().getSteps()
if (persistedSteps > 0) {
let currentDateTime = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = dateFormatter.string(from: currentDateTime)
swiftApi().setPacingDetailsAsync(persistedSteps, dateString, setPacingDetailsAsyncCallback)
}
func setPacingDetailsAsyncCallback (_ responseData: swiftApi.GenericResponseModel?) -> Void {
if (responseData != nil) {
DispatchQueue.main.async {
if (responseData?.getStatus == 1) {
}
}
} else {
}
}
}
func startTimer() {
let queue = DispatchQueue(label: Bundle.main.bundleIdentifier! + ".timer")
timer = DispatchSource.makeTimerSource(queue: queue)
timer!.schedule(deadline: .now(), repeating: .seconds(10))
timer!.setEventHandler { [weak self] in
// do whatever stuff you want on the background queue here here
let persistedSteps = swiftApi().getSteps()
if (persistedSteps > 0) {
let currentDateTime = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let dateString = dateFormatter.string(from: currentDateTime)
swiftApi().setPacingDetailsAsync(persistedSteps, dateString, setPacingDetailsAsyncCallback)
}
DispatchQueue.main.async {
// update your model objects and/or UI here
}
}
timer!.resume()
func setPacingDetailsAsyncCallback (_ responseData: swiftApi.GenericResponseModel?) -> Void {
if (responseData != nil) {
DispatchQueue.main.async {
if (responseData?.getStatus == 1) {
}
}
} else {
}
}
}
func stopTimer() {
timer?.cancel()
timer = nil
}
public class DFY {
let couponCode: String?
let merchantId: String?
......@@ -3442,6 +3541,44 @@ public class swiftApi {
}
public func setPacingDetailsAsync(_ steps: Int, _ date: String, _ setPacingDetailsCallback: @escaping (_ responseData: GenericResponseModel?) -> Void) -> Void {
let instanceOfMyApi = MyApi()
instanceOfMyApi.setPacingDetailsAsync(steps as NSNumber, date, pacingCallback, failureBlock: pacingFailureCallback)
func pacingCallback(_ responseData: [AnyHashable: Any]?) -> Void {
if let responseDataDictionary = responseData as? [String: AnyObject] {
if (responseDataDictionary["status"] as? Int == 1) {
GlobalVariables.savedSteps += swiftApi().getSteps()
swiftApi().setSteps(0)
if let responseDataDictionary = responseData as? [String: Any] {
let tempResponse = GenericResponseModel(dictionary: responseDataDictionary)
setPacingDetailsCallback(tempResponse);
} else {
setPacingDetailsCallback(nil)
}
} else {
setPacingDetailsCallback(nil)
}
} else {
setPacingDetailsCallback(nil)
}
}
func pacingFailureCallback(_ error: Error?) -> Void {
print("setPacingDetails error: ")
setPacingDetailsCallback(nil)
}
}
public class SharingResponseModel {
private var modal_text: String
private var status: String
......