#import "DEAppRatingPlugin.h"
#import <DEAppRating/DEAppRating.h>
#define ERR_CODE_INVALID_PARAMETER_ERROR @"INVALID_PARAMETER_ERROR"
#define ERR_CODE_NOT_YET_INITIALIZED @"NOT_YET_INITIALIZED"
#define ERR_CODE_GENERIC_ERROR @"GENERIC_ERROR"
#define DE_RATING_STATUS_STR_NEVER_ASKED @"NEVER_ASKED"
#define DE_RATING_STATUS_STR_OK @"OK"
#define DE_RATING_STATUS_DECLINED @"DECLINED"
#define DE_RATING_STATUS_SENT @"SENT"
#define DE_RATING_TYPE_POSITIVE @"POSITIVE"
#define DE_RATING_TYPE_NEGATIVE @"NEGATIVE"
#define DE_RATING_TYPE_UNKNOWN @"UNKNOWN"
@interface DEAppRatingPlugin()
{
NSString *_domain;
NSString *_app;
NSString *_assetVersion;
}
@end
@implementation DEAppRatingPlugin
- (void)pluginInitialize
{
NSLog(@"Initializing Plugin AppRating...");
}
- (void)initialize:(CDVInvokedUrlCommand*)command
{
// Retrieve the asset version
_domain = command.arguments[0];
_app = command.arguments[1];
_assetVersion = command.arguments[2];
[[DEAppRatingManager sharedInstance] initWithDomain:_domain andApplication:_app];
//check for optional parameters
NSString *motifDomain = [self getSafeStringSettingsFor:@"appRating.motif.domain" withDefaultValue:@"default"];
NSString *motifApp = [self getSafeStringSettingsFor:@"appRating.motif.app" withDefaultValue:@"vipera"];
NSString *motifService = [self getSafeStringSettingsFor:@"appRating.motif.service" withDefaultValue:@"feedback"];
NSString *motifOperation = [self getSafeStringSettingsFor:@"appRating.motif.operation" withDefaultValue:@"appRating"];
[[DEAppRatingManager sharedInstance] setMotifOperationWith:motifDomain andApp:motifApp andService:motifService andName:motifOperation];
CDVPluginResult* result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
- (NSString*)getSafeStringSettingsFor:(NSString*)settingName withDefaultValue:(NSString*)defaultValue
{
NSDictionary *settings = self.commandDelegate.settings;
NSString *configuredValue = [settings valueForKey:[settingName lowercaseString]];
if (configuredValue==nil){
return defaultValue;
} else {
return configuredValue;
}
}
- (void)getApiModel:(CDVInvokedUrlCommand*)command
{
NSString *apiModel = [[DEAppRatingManager sharedInstance] apiModel];
CDVPluginResult* result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK
messageAsString:apiModel];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
- (void) setUserName:(CDVInvokedUrlCommand*)command
{
NSString *userName = command.arguments[0];
[[DEAppRatingManager sharedInstance] setUserName:userName];
CDVPluginResult* result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
- (void) canRequireFeedback:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* result = nil;
@try {
BOOL canRequireFeedback = [[DEAppRatingManager sharedInstance] canRequireFeedback];
result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK
messageAsBool:canRequireFeedback];
} @catch ( NSException *e ) {
NSLog(@"canRequireFeedback Error: %@",e);
NSDictionary *errorObj = @{
@"errorCode" : [self errorCodeFromException:e],
@"errorData" : e.description
};
result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_ERROR
messageAsDictionary:errorObj];
}
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
- (void) startRating:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* result = nil;
@try {
BOOL canRequireFeedback = [[DEAppRatingManager sharedInstance] canRequireFeedback];
if (!canRequireFeedback){
//Can't get another feedback
NSDictionary *resultObj = [self uiFeedbackResultData:DEAppRatingUIAlreadyAsked andRatingStatus:[[DEAppRatingManager sharedInstance] currentFeedbackStatus] andType:DEAppRatingUnknown andData:nil];
CDVPluginResult* result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:resultObj];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
return;
}
DEAppRatingUI *appRating = [[DEAppRatingUI alloc]init];
NSDictionary *extraInfo = [self buildExtraInfo];
[appRating startRatingWithCompletion:^(DEAppRatingUIResult uiResult, DEAppRatingStatus status, DEAppRatingType type, NSDictionary *data) {
NSDictionary *resultObj = [self uiFeedbackResultData:uiResult andRatingStatus:status andType:type andData:data];
CDVPluginResult* result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK
messageAsDictionary:resultObj];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
} andExtraInfo:extraInfo];
} @catch ( NSException *e ) {
NSDictionary *errorObj = @{
@"errorCode" : [self errorCodeFromException:e],
@"errorData" : e.description
};
result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_ERROR
messageAsDictionary:errorObj];
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
}
-(NSString*) uiResultToString:(DEAppRatingUIResult) uiResult
{
switch (uiResult) {
case DEAppRatingUIOk:
return @"OK";
break;
case DEAppRatingUIAlreadyAsked:
return @"AlreadyAsked";
break;
default:
return @"Unknown";
break;
}
}
- (NSDictionary*)uiFeedbackResultData:(DEAppRatingUIResult)uiResult andRatingStatus:(DEAppRatingStatus)status andType:(DEAppRatingType)type andData:(NSDictionary*)data
{
NSDictionary *ret = nil;
if (data==nil){
ret = @{
@"uiResult" : [self uiResultToString:uiResult],
@"ratingStatus" : [self statusToString:status],
@"ratingType" : [self typeToString:type]
};
} else {
ret = @{
@"uiResult" : [self uiResultToString:uiResult],
@"ratingStatus" : [self statusToString:status],
@"ratingType" : [self typeToString:type],
@"data" : data
};
}
return ret;
}
- (NSString*)statusToString:(DEAppRatingStatus)status
{
return [DEAppRatingManager fromStatusToString:status];
}
- (NSString*)typeToString:(DEAppRatingType)type
{
return [DEAppRatingManager fromTypeToString:type];
}
- (void) updateFeedbackStatus:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* result = nil;
@try {
NSString *statusStr = command.arguments[0];
DEAppRatingStatus status = [self statusFromString:statusStr];
[[DEAppRatingManager sharedInstance] updateFeedbackStatus:status];
result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK];
}
@catch(NSException *e) {
NSLog(@"updateFeedbackStatus Error: %@",e);
NSDictionary *errorObj = @{
@"errorCode" : [self errorCodeFromException:e],
@"errorData" : e.description
};
result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_ERROR
messageAsDictionary:errorObj];
}
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
- (DEAppRatingType) typeFromString:(NSString*)typeStr
{
if ([typeStr caseInsensitiveCompare:DE_RATING_TYPE_POSITIVE]== NSOrderedSame ){
return DEAppRatingPositive;
}
else if ([typeStr caseInsensitiveCompare:DE_RATING_TYPE_NEGATIVE]== NSOrderedSame ){
return DEAppRatingNegative;
}
else if ([typeStr caseInsensitiveCompare:DE_RATING_TYPE_UNKNOWN]== NSOrderedSame ){
return DEAppRatingUnknown;
}
else {
return DEAppRatingUnknown;
}
}
- (DEAppRatingStatus) statusFromString:(NSString*)statusStr
{
if ([statusStr caseInsensitiveCompare:DE_RATING_STATUS_STR_NEVER_ASKED]== NSOrderedSame ){
return DEAppRatingNeverAsked;
}
else if ([statusStr caseInsensitiveCompare:DE_RATING_STATUS_STR_OK]== NSOrderedSame ){
return DEAppRatingOk;
}
else if ([statusStr caseInsensitiveCompare:DE_RATING_STATUS_DECLINED]== NSOrderedSame ){
return DEAppRatingDeclined;
}
else if ([statusStr caseInsensitiveCompare:DE_RATING_STATUS_SENT]== NSOrderedSame ){
return DEAppRatingSent;
} else {
NSException* invalidStatusException = [NSException
exceptionWithName:@"InvalidStatusException"
reason:@"Invalid status"
userInfo:nil];
@throw invalidStatusException;
}
}
- (void) getFeedbackStatus:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* result = nil;
@try {
DEAppRatingStatus currentStatus = [[DEAppRatingManager sharedInstance] currentFeedbackStatus];
result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK
messageAsString:[self statusToString:currentStatus]];
} @catch ( NSException *e ) {
NSLog(@"getFeedbackStatus Error: %@",e);
NSDictionary *errorObj = @{
@"errorCode" : [self errorCodeFromException:e],
@"errorData" : e.description
};
result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_ERROR
messageAsDictionary:errorObj];
}
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
- (void) sendFeedbackWithMessage:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* result = nil;
@try {
NSString *message = command.arguments[0];
NSNumber *rating = command.arguments[1];
NSDictionary *extraInfo = [self buildExtraInfo];
[[DEAppRatingManager sharedInstance] sendFeedbackWithMessage:message andRating:[rating intValue] andExtraInfo:extraInfo];
result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK];
} @catch ( NSException *e ) {
NSLog(@"sendFeedbackWithMessage Error: %@",e);
NSDictionary *errorObj = @{
@"errorCode" : [self errorCodeFromException:e],
@"errorData" : e.description
};
result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_ERROR
messageAsDictionary:errorObj];
}
[self.commandDelegate sendPluginResult:result callbackId:command.callbackId];
}
- (void) declineRequest:(CDVInvokedUrlCommand*)command
{
CDVPluginResult* result = nil;
@try {
NSString *typeStr = command.arguments[0];
DEAppRatingType type = [self typeFromString:typeStr];
NSDictionary *extraInfo = [self buildExtraInfo];
[[DEAppRatingManager sharedInstance] declineRequest:type andExtraInfo:extraInfo];
result = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK];
} @catch ( NSException *e ) {
NSLog(@"getFeedbackStatus Error: %@",e);
NSDictionary *errorObj = @{
@"errorCode" : [self errorCodeFromException:e],
@"errorData" : e.description
};
result = [CDVPluginResult
Loading ...