Advertisement
Guest User

Untitled

a guest
Mar 8th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. - (void)showActionSheet
  2. {
  3. BlockActionSheet *sheet = [BlockActionSheet sheetWithTitle:@"Choose one"];
  4. __weak BlockActionSheet *weakSheet = sheet;
  5.  
  6. [sheet addButtonWithTitle:@"Blue" atIndex:0 block:^{
  7. [self downloadFileFromServerSuccessBlock:^{
  8. //YAY
  9. } failureBlock:^{
  10. BlockAlertView *alert = [BlockAlertView alertWithTitle:@"Failure" message:@"Something Went Wrong"];
  11. [alert addButtonWithTitle:@"Try Again" block:^{
  12. [weakSheet showInView:self.view];
  13. }];
  14. [alert setCancelButtonWithTitle:@"Cancel" block:nil];
  15. }];
  16. }];
  17.  
  18. [sheet addButtonWithTitle:@"Black" atIndex:1 block:^{
  19. //something else
  20. }];
  21.  
  22. [sheet setCancelButtonWithTitle:@"Cancel" block:nil];
  23.  
  24. [sheet showInView:self.view];
  25. }
  26.  
  27. /*
  28. * This function is getting called from a login view controller.
  29. */
  30.  
  31. - (void)sendAuthentication
  32. {
  33. /*
  34. * A RACSubscribable will invoke the where^ block when it calls [sendNext:];
  35. * That call is coming from the ThaweQBRequestController.
  36. * If the where^ block return YES then the subscribeNext^ block will get called.
  37. * asMaybes wraps the object I send back in a RACMaybe object which has a convenience method [hasObject];
  38. * The subscribeNext^ block has an object coming into it, that object is an array in this case that I sent
  39. * from the ThaweQBRequestController.
  40. */
  41.  
  42. RACSubscribable *sub = [[ThaweQBRequestController logInWithUsername:self.thaweusername password:self.thawepassword] asMaybes];
  43.  
  44. [[sub where:^(id x) {
  45. return [x hasObject];
  46. }]
  47. subscribeNext:^(id _) {
  48.  
  49. NSArray *array = [_ object];
  50.  
  51. NSString *errcode = [array objectAtIndex:0];
  52.  
  53. if (errcode.boolValue == YES)
  54. //YAY SUCCESS!!!
  55.  
  56. } else {
  57. //UH OH
  58. }
  59. // Update UI, the array has the username and password in it if I want to display or whatever is clever.
  60. }];
  61. }
  62.  
  63. + (RACAsyncSubject *)logInWithUsername:(NSString *)username password:(NSString *)password
  64. {
  65. /*
  66. * First we have our loginCommand. It will invoke the block we give it on a background thread.
  67. * This block returns a Subscribable. We can subscribe other blocks to be invoked when the aync call is done.
  68. * In this case we are creating loginResult to retrieve the async call.
  69. */
  70.  
  71. RACAsyncCommand *loginCommand = [RACAsyncCommand command];
  72.  
  73. /*
  74. * Now we have our AsyncSubject. We are instantiated it here and are going to pass it back to the login view controller;
  75. * When our async call finishes and we filter through the results we will call [subject sendNext:] to invoke blocks we created in
  76. * the login view controller;
  77. * We will filter the results uses the loginResult blocks.
  78. */
  79. RACAsyncSubject *subject = [RACAsyncSubject subject];
  80.  
  81. /*
  82. * loginResult is a "Subscribable". Every time it gets a [sendNext:] call it runs the blocks assosiated with it.
  83. * These [sendNext:] calls are coming from our network code.
  84. * 'repeat' means that even after the async block is invoked it keeps a reference to it incase we want to use it again.
  85. * 'asMaybes' wraps a RACMaybe object around the object you send to the loginResult blocks. The benefit of a RACMaybe is
  86. * that it has some convienence methods like 'hasError' and 'hasObject'.
  87. */
  88.  
  89. RACSubscribable *loginResult = [[[loginCommand
  90. addAsyncBlock:^(id _) {
  91. return [self authenticateUser:username password:password];
  92. }]
  93. repeat]
  94. asMaybes];
  95.  
  96. /*
  97. * The first block, where^, get called every time loginResult calls [sendNext:].
  98. * If it returns YES then the select^ block gets called. The select^ block invokes the subscribeNext^ block and sends it the
  99. * error.
  100. */
  101.  
  102. [[[loginResult
  103. where:^(id x) {
  104. return [x hasError];
  105. }]
  106. select:^(id x) {
  107. return [x error];
  108. }]
  109. subscribeNext:^(id x) {
  110. NSLog(@"network error omg: %@", x);
  111. }];
  112.  
  113. /*
  114. * Same as above except this time we are looking for instances when we have an object instead of an NSError.
  115. * The object we are getting is being returned from our network code. In this case it is an integer, 0 means we had a successfull login.
  116. * Now we can call [subject sencNext:] to inform our login view controller of the pass or fail of the authentication.
  117. * [sendCompleted] is a cleanup call, allowing ReactiveCocoa to dispose of our blocks and free up memory.
  118. */
  119.  
  120. [loginResult
  121. where:^(id x) {
  122. return [x hasObject];
  123. }]
  124. subscribeNext:^(id _) {
  125. NSNumber *number;
  126. NSString *errcode = [_ object];
  127. if (errcode.intValue == 0) number = [NSNumber numberWithBool:YES] ?: [NSNumber numberWithBool:NO];
  128. [subject sendNext:[NSArray arrayWithObjects:number, username, password, nil]];
  129. [subject sendCompleted];
  130. }];
  131.  
  132. /*
  133. * [execute:] starts the whole thing off. This call invokes the loginCommand block that returns the async call to the loginResult subscribable.
  134. */
  135.  
  136. [loginCommand execute:@"This value gets transfered to the addAsyncBlock:^(id _) block above."];
  137.  
  138. return subject;
  139. }
  140.  
  141. /*
  142. * This function uses a QuickBase wrapper I made to make server request and whatnot.
  143. * This function is called when the [loginCommand execute:] call in the previous function gets called
  144. * and invokes the loginCommand block which returns the async request.
  145. * That request is what this function is returning.
  146. */
  147.  
  148. + (RACAsyncSubject *)authenticateUser:(NSString *)username password:(NSString *)password
  149. {
  150. QBRequest *request = [[QBRequest alloc] init];
  151.  
  152. [request setQuickBaseAction:QBActionTypeAuthenticate];
  153.  
  154. [request setURLString:URLstring withDatabaseID:nil];
  155.  
  156. [request setApplicationToken:appToken];
  157.  
  158. return [request sendAndPersist:NO username:username password:password];
  159. }
  160.  
  161. - (RACAsyncSubject *)sendAndPersist:(BOOL)persist username:(NSString *)username password:(NSString *)password
  162. {
  163. self.subject = [RACAsyncSubject subject];
  164.  
  165. /*
  166. * These next two blocks are called when my network request is done.
  167. * My AsyncSubject is a property so that I can reference it later when I parse
  168. * throught the response and figure out whether I logged in correctly or not.
  169. * In the case that the network call itself fails, then the AysncSubject calls
  170. * [sendError:] which will invoke those NSError capturing blocks in the ThaweQBRequestController.
  171. */
  172.  
  173. [anOp onCompletion:^(MKNetworkOperation *completedOperation) {
  174.  
  175. dispatch_async(background_parseSave_queue(), ^{
  176.  
  177. [self updateDatabase];
  178. });
  179.  
  180. } onError:^(NSError *error) {
  181. [subject sendError:error];
  182. }];
  183.  
  184. [engine enqueueOperation:anOp];
  185.  
  186. return subject;
  187. }
  188.  
  189. else if ([elementName isEqualToString:@"errcode"])
  190. {
  191. if ([self.action isEqualToString:@"API_Authenticate"]) {
  192. [subject sendNext:[self.currentParsedCharacterData copy]];
  193. [subject sendCompleted];
  194. }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement