Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

vipera-npm-registry / cordova-de-preferences-vipera   js

Repository URL to install this package:

Version: 0.0.6 

/ src / ios / CordovaDEPreferences.m

#import "CordovaDEPreferences.h"
#import <Cordova/CDV.h>

#define OLD_DE_LOCAL_STORAGE_PROTOCOL @"data://"
#define OLD_DE_LOCAL_STORAGE_KEY @"localStorage"
#define OLD_DE_LOCAL_STORAGE_DELETED_KEY @"OLD_DE_LOCAL_STORAGE_DELETED_KEY"

#define HTML_PREFERENCE_NAMESPACE        @"html_preference:"

#define UserDefaults    [NSUserDefaults standardUserDefaults]


@interface CordovaDEPreferences () <UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *uiWebView;

@property (nonatomic) BOOL oldLocalStorageRetrievedOrDeleted;
@property (nonatomic, strong) NSString *localStorageBase64;
@property (nonatomic, strong) CDVInvokedUrlCommand *oldLocalStorageCommand;

@end

@implementation CordovaDEPreferences

- (void)pluginInitialize
{
    [super pluginInitialize];
    
    self.oldLocalStorageRetrievedOrDeleted = NO;

    NSURL *htmlURL = [[NSBundle mainBundle] URLForResource:@"data" withExtension:@"html"];

    BOOL dataHtmlExists = [[NSFileManager defaultManager] fileExistsAtPath:[htmlURL path]];
    BOOL oldDELocalStorageDeleted = [UserDefaults boolForKey:OLD_DE_LOCAL_STORAGE_DELETED_KEY];
    
    // If the command to delete the local storage was received then just set it as loaded.
    if (oldDELocalStorageDeleted)
    {
        self.oldLocalStorageRetrievedOrDeleted = YES;
    }
    
    // If the old DE was not explicitly removed then load the uiwebview to retrieve the value.
    if (!oldDELocalStorageDeleted && dataHtmlExists)
    {
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:htmlURL];

        self.uiWebView = [[UIWebView alloc] init];
        self.uiWebView.delegate = self;
        [self.uiWebView loadRequest:urlRequest];
    }
}

- (NSString *) buildCompleteKeyWithKey:(NSString *) key
{
    return [HTML_PREFERENCE_NAMESPACE stringByAppendingString:key];
}

- (void) sendGetValue:(NSString *) retrievedValue forcommand:(CDVInvokedUrlCommand *) command
{
    CDVPluginResult * pluginResult  = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:retrievedValue];
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

- (void) resolveLocalStorageRequest
{
    // Wait for the old DE storage to be loaded before sending the response.
    // If the loclastorage has been deteted send the response. The value will be null.
    if (self.oldLocalStorageRetrievedOrDeleted && self.oldLocalStorageCommand)
    {
        [self sendGetValue:self.localStorageBase64 forcommand:self.oldLocalStorageCommand];
        self.oldLocalStorageCommand = nil;
    }
}

- (void)getValue:(CDVInvokedUrlCommand *)command
{
    @try
    {
        if ([command.arguments count] != 1)
        {
            [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Wrong number of parameters"] callbackId:command.callbackId];
            return;
        }
        
        NSString *key = command.arguments[0];
        if ([key isEqualToString:OLD_DE_LOCAL_STORAGE_KEY])
        {
            self.oldLocalStorageCommand = command;
            [self resolveLocalStorageRequest];
        }
        else
        {
            NSString *completeKey = [self buildCompleteKeyWithKey:key];
            NSString *retrievedValue = [UserDefaults objectForKey:completeKey];
            
            [self sendGetValue:retrievedValue forcommand:command];
        }
    }
    @catch (NSException *exception)
    {
        [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR] callbackId:command.callbackId];
    }
}

- (void)deleteValue:(CDVInvokedUrlCommand *)command
{
    @try
    {
        if ([command.arguments count] != 1)
        {
            [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Wrong number of parameters"] callbackId:command.callbackId];
            return;
        }
        
        NSString *key = command.arguments[0];
        
        // If we want to delete the local storage just set a flag to not load it again.
        // Also clear the local copy.
        if ([key isEqualToString:OLD_DE_LOCAL_STORAGE_KEY])
        {
            [UserDefaults setBool:YES forKey:OLD_DE_LOCAL_STORAGE_DELETED_KEY];
            [UserDefaults synchronize];
            self.localStorageBase64 = nil;
        }
        else
        {
            NSString *completeKey = [self buildCompleteKeyWithKey:key];
            [UserDefaults removeObjectForKey:completeKey];
            [UserDefaults synchronize];
        }
        
        CDVPluginResult * pluginResult  = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK];
        [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
    }
    @catch (NSException *exception)
    {
        [self.commandDelegate sendPluginResult:[CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR] callbackId:command.callbackId];
    }
}

- (void) localStroageRetrieved:(NSString *) localStorageBase64
{
    self.oldLocalStorageRetrievedOrDeleted = YES;
    self.localStorageBase64 = localStorageBase64;
    self.uiWebView = nil;
    [self resolveLocalStorageRequest];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    NSString *requestAbsolute = request.URL.absoluteString;
    if ([requestAbsolute hasPrefix:OLD_DE_LOCAL_STORAGE_PROTOCOL])
    {
        NSString *localStorageBase64 = [requestAbsolute stringByReplacingOccurrencesOfString:OLD_DE_LOCAL_STORAGE_PROTOCOL withString:@""];
        [self localStroageRetrieved:localStorageBase64];
//        NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:localStorageBase64 options:0];
//        NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
//        NSLog(@"%@", decodedString);
//
//        NSError *err = nil;
//        NSDictionary *localStoragedictionary = [NSJSONSerialization JSONObjectWithData:decodedData options:NSJSONReadingMutableContainers error:&err];
        
        return NO;
    }
    
    return YES;
}

@end