YHNative.m 2.22 KB
//
//  YHNative.m
//  YohoExplorerDemo
//
//  Created by gaoqiang xu on 4/7/15.
//  Copyright (c) 2015 gaoqiang xu. All rights reserved.
//

#import "YHNative.h"
#import <objc/message.h>

#import "YHNativeCamera.h"
#import "YHNaviteLBS.h"
#import "YHNativeMedia.h"
#import "YHNativeDevice.h"
#import "YHNativeMotion.h"
#import "YHNativeFS.h"
#import "YHNativeClickToShow.h"
#import "YHNative_Message.h"

@implementation YHNative

+ (Class)resolveActionName:(NSString *)name
{
    if ([name isEqualToString:YHNative_Camera]) {
        return [YHNativeCamera class];
        
    } else if ([name isEqualToString:YHNative_LBS]) {
        return [YHNaviteLBS class];
        
    } else if ([name isEqualToString:YHNative_Media]) {
        return [YHNativeMedia class];
        
    } else if ([name isEqualToString:YHNative_Device]) {
        return [YHNativeDevice class];
        
    } else if ([name isEqualToString:YHNative_Motion]) {
        return [YHNativeMotion class];
        
    } else if ([name isEqualToString:YHNative_FS]) {
        return [YHNativeFS class];
        
    } else if ([name isEqualToString:YHNative_ClickToShowImage]) {
        return [YHNativeClickToShow class];
    } else {
        
        return [YHNative_Message class];
    }
    
    return nil;
}

- (void)workWithMethod:(SEL)method options:(NSDictionary *)options success:(onSuccess)success failure:(onFailure)failure
{
    self.options = options;
    self.successCallBack = success;
    self.failureCallBack = failure;
    
    NSString *methodName = NSStringFromSelector(method);
    
    NSArray *functions = [self availableFunctions];
    NSArray *function = [functions filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF = %@", methodName]];
    
    if (function.count == 1 || [self isKindOfClass:[YHNative_Message class]]) {
        dispatch_async(dispatch_get_main_queue(), ^{
            ((void (*)(id, SEL))objc_msgSend)(self, method);
        });
    } else {
        NSString *message = [[NSString alloc] initWithFormat:@"No such action:%@ found on %@", methodName, NSStringFromClass([self class])];
        self.failureCallBack(message);
    }
}

- (NSArray *)availableFunctions
{
    return nil;
}

- (NSString *)actionName
{
    return nil;
}

@end