RASingleton.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // Singleton.m
  3. // iSales-NPD
  4. //
  5. // Created by Jack on 2016/10/12.
  6. // Copyright © 2016年 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "RASingleton.h"
  9. #import "NotificationNameCenter.h"
  10. #import "AppDelegate.h"
  11. #import "RAUtils.h"
  12. @interface RASingleton ()
  13. @property (nonatomic,strong) NSMutableDictionary *globalParameters;
  14. @end
  15. @implementation RASingleton
  16. #pragma mark - Global Parameter
  17. - (NSMutableDictionary *)globalParameters {
  18. if (!_globalParameters) {
  19. _globalParameters = [NSMutableDictionary dictionary];
  20. }
  21. return _globalParameters;
  22. }
  23. - (void)setGlobalParameter:(id)param forKey:(NSString *)key {
  24. if (param == nil || key == nil) {
  25. return;
  26. }
  27. [self.globalParameters setObject:param forKey:key];
  28. }
  29. - (id)globalParameterForKey:(NSString *)key {
  30. if (key == nil) {
  31. return nil;
  32. }
  33. return [self.globalParameters objectForKey:key];
  34. }
  35. - (void)setUser:(NSString *)user {
  36. _user = user;
  37. [self setGlobalParameter:_user forKey:@"user"];
  38. }
  39. - (void)setPassword:(NSString *)password {
  40. _password = password;
  41. [self setGlobalParameter:_password forKey:@"password"];
  42. }
  43. - (void)dealloc {
  44. [[NSNotificationCenter defaultCenter] removeObserver:self];
  45. [self removeObserver:self forKeyPath:@"stack_contents"];
  46. }
  47. //- (instancetype)init {
  48. // if (self = [super init]) {
  49. //
  50. // [self addObserver:self forKeyPath:@"scan_cart"
  51. // options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
  52. // context:@"content changed"];
  53. //
  54. // }
  55. // return self;
  56. //}
  57. + (instancetype)sharedInstance {
  58. static RASingleton *singleton = nil;
  59. static dispatch_once_t onceToken;
  60. dispatch_once(&onceToken, ^{
  61. singleton = [[RASingleton alloc] init];
  62. [[NSNotificationCenter defaultCenter] addObserver:singleton selector:@selector(handleLogin:) name:User_LoginOK_Notification object:nil];
  63. // self addObserver:self forKeyPath:self.scan_cart options:<#(NSKeyValueObservingOptions)#> context:<#(nullable void *)#>
  64. });
  65. return singleton;
  66. }
  67. //- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  68. //{
  69. // if(self.enable_OfflineOrder)
  70. // {
  71. // if([keyPath isEqualToString:@"scan_cart"])
  72. // {
  73. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  74. // NSString *documents = [paths objectAtIndex:0];
  75. // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  76. //
  77. //
  78. // NSString *orderdir = [documents stringByAppendingPathComponent:appDelegate.order_code];
  79. //
  80. // NSString *cartpath = [orderdir stringByAppendingPathComponent:@"cart.json"];
  81. //
  82. //
  83. //
  84. // [RAUtils dicttofile:cartpath dict:self.scan_cart];
  85. //
  86. //
  87. // self.scan_cart addObserver:<#(nonnull NSObject *)#> forKeyPath:<#(nonnull NSString *)#> options:<#(NSKeyValueObservingOptions)#> context:<#(nullable void *)#>
  88. //
  89. // }
  90. // }
  91. //
  92. //}
  93. - (void)setGlobal_lock:(BOOL)global_lock {
  94. _global_lock = global_lock;
  95. if (global_lock) {
  96. [[NSNotificationCenter defaultCenter] postNotificationName:Lock_Permission_Notification object:nil];
  97. } else {
  98. [[NSNotificationCenter defaultCenter] postNotificationName:unLock_Permission_Notification object:nil];
  99. }
  100. }
  101. - (void)resetGlobalLock {
  102. _global_lock = NO;
  103. }
  104. - (void)handleLogin:(NSNotification *)notification {
  105. self.currentOrderIsMerged = NO;
  106. }
  107. - (NSString *)homeClickedItemName {
  108. if (!_homeClickedItemName) {
  109. _homeClickedItemName = @"";
  110. }
  111. return _homeClickedItemName;
  112. }
  113. - (NSString *)deliveryString {
  114. if (!_deliveryString) {
  115. _deliveryString = @"";
  116. }
  117. return _deliveryString;
  118. }
  119. @end