// // Singleton.m // iSales-NPD // // Created by Jack on 2016/10/12. // Copyright © 2016年 United Software Applications, Inc. All rights reserved. // #import "RASingleton.h" #import "NotificationNameCenter.h" #import "AppDelegate.h" #import "RAUtils.h" @interface RASingleton () @property (nonatomic,strong) NSMutableDictionary *globalParameters; @end @implementation RASingleton #pragma mark - Global Parameter - (NSMutableDictionary *)globalParameters { if (!_globalParameters) { _globalParameters = [NSMutableDictionary dictionary]; } return _globalParameters; } - (void)setGlobalParameter:(id)param forKey:(NSString *)key { if (param == nil || key == nil) { return; } [self.globalParameters setObject:param forKey:key]; } - (id)globalParameterForKey:(NSString *)key { if (key == nil) { return nil; } return [self.globalParameters objectForKey:key]; } - (void)setUser:(NSString *)user { _user = user; [self setGlobalParameter:_user forKey:@"user"]; } - (void)setPassword:(NSString *)password { _password = password; [self setGlobalParameter:_password forKey:@"password"]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; [self removeObserver:self forKeyPath:@"stack_contents"]; } //- (instancetype)init { // if (self = [super init]) { // // [self addObserver:self forKeyPath:@"scan_cart" // options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) // context:@"content changed"]; // // } // return self; //} + (instancetype)sharedInstance { static RASingleton *singleton = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ singleton = [[RASingleton alloc] init]; [[NSNotificationCenter defaultCenter] addObserver:singleton selector:@selector(handleLogin:) name:User_LoginOK_Notification object:nil]; // self addObserver:self forKeyPath:self.scan_cart options:<#(NSKeyValueObservingOptions)#> context:<#(nullable void *)#> }); return singleton; } //- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context //{ // if(self.enable_OfflineOrder) // { // if([keyPath isEqualToString:@"scan_cart"]) // { // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); // NSString *documents = [paths objectAtIndex:0]; // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; // // // NSString *orderdir = [documents stringByAppendingPathComponent:appDelegate.order_code]; // // NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"]; // // // // [RAUtils dicttofile:cartpath dict:self.scan_cart]; // // // self.scan_cart addObserver:<#(nonnull NSObject *)#> forKeyPath:<#(nonnull NSString *)#> options:<#(NSKeyValueObservingOptions)#> context:<#(nullable void *)#> // // } // } // //} - (void)setGlobal_lock:(BOOL)global_lock { _global_lock = global_lock; if (global_lock) { [[NSNotificationCenter defaultCenter] postNotificationName:Lock_Permission_Notification object:nil]; } else { [[NSNotificationCenter defaultCenter] postNotificationName:unLock_Permission_Notification object:nil]; } } - (void)resetGlobalLock { _global_lock = NO; } - (void)handleLogin:(NSNotification *)notification { self.currentOrderIsMerged = NO; } - (NSString *)homeClickedItemName { if (!_homeClickedItemName) { _homeClickedItemName = @""; } return _homeClickedItemName; } - (NSString *)deliveryString { if (!_deliveryString) { _deliveryString = @""; } return _deliveryString; } @end