SBExampleViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // SBExampleViewController.m
  3. // JDStatusBarNotificationExample
  4. //
  5. // Created by Markus on 29.10.13.
  6. // Copyright (c) 2013 Markus. All rights reserved.
  7. //
  8. #import "JDStatusBarNotification.h"
  9. #import "SBCustomStyleViewController.h"
  10. #import "SBExampleViewController.h"
  11. static NSString *const JDButtonName = @"JDButtonName";
  12. static NSString *const JDButtonInfo = @"JDButtonInfo";
  13. static NSString *const JDNotificationText = @"JDNotificationText";
  14. static NSString *const SBStyle1 = @"SBStyle1";
  15. static NSString *const SBStyle2 = @"SBStyle2";
  16. @interface SBExampleViewController ()
  17. @property (nonatomic, strong) NSArray *data;
  18. @property (nonatomic, assign) UIActivityIndicatorViewStyle indicatorStyle;
  19. @property (nonatomic, assign) CGFloat progress;
  20. @property (nonatomic, weak) NSTimer *timer;
  21. @end
  22. @implementation SBExampleViewController
  23. - (id)initWithStyle:(UITableViewStyle)style
  24. {
  25. self = [super initWithStyle:style];
  26. if (self) {
  27. self.title = @"JDStatusBarNotification";
  28. [JDStatusBarNotification addStyleNamed:SBStyle1
  29. prepare:^JDStatusBarStyle *(JDStatusBarStyle *style) {
  30. style.barColor = [UIColor colorWithRed:0.797 green:0.000 blue:0.662 alpha:1.000];
  31. style.textColor = [UIColor whiteColor];
  32. style.animationType = JDStatusBarAnimationTypeFade;
  33. style.font = [UIFont fontWithName:@"SnellRoundhand-Bold" size:17.0];
  34. style.progressBarColor = [UIColor colorWithRed:0.986 green:0.062 blue:0.598 alpha:1.000];
  35. style.progressBarHeight = 20.0;
  36. return style;
  37. }];
  38. [JDStatusBarNotification addStyleNamed:SBStyle2
  39. prepare:^JDStatusBarStyle *(JDStatusBarStyle *style) {
  40. style.barColor = [UIColor cyanColor];
  41. style.textColor = [UIColor colorWithRed:0.056 green:0.478 blue:0.998 alpha:1.000];
  42. style.animationType = JDStatusBarAnimationTypeBounce;
  43. style.progressBarColor = style.textColor;
  44. style.progressBarHeight = 5.0;
  45. style.progressBarPosition = JDStatusBarProgressBarPositionTop;
  46. if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
  47. style.font = [UIFont fontWithName:@"DINCondensed-Bold" size:17.0];
  48. style.textVerticalPositionAdjustment = 2.0;
  49. } else {
  50. style.font = [UIFont fontWithName:@"HelveticaNeue-CondensedBold" size:17.0];
  51. }
  52. return style;
  53. }];
  54. self.data = @[@[@{JDButtonName:@"Show Notification", JDButtonInfo:@"JDStatusBarStyleDefault", JDNotificationText:@"Better call Saul!"},
  55. @{JDButtonName:@"Show Progress", JDButtonInfo:@"0-100% in 1s", JDNotificationText:@"Some Progress…"},
  56. @{JDButtonName:@"Show Activity Indicator", JDButtonInfo:@"UIActivityIndicatorViewStyleGray", JDNotificationText:@"Some Activity…"},
  57. @{JDButtonName:@"Update text", JDButtonInfo:@"", JDNotificationText:@"Some Activity…"},
  58. @{JDButtonName:@"Dismiss Notification", JDButtonInfo:@"Animated", JDNotificationText:@""}],
  59. @[@{JDButtonName:@"Show JDStatusBarStyleError", JDButtonInfo:@"Duration: 2s", JDNotificationText:@"No, I don't have the money.."},
  60. @{JDButtonName:@"Show JDStatusBarStyleWarning", JDButtonInfo:@"Duration: 2s", JDNotificationText:@"You know who I am!"},
  61. @{JDButtonName:@"Show JDStatusBarStyleSuccess", JDButtonInfo:@"Duration: 2s", JDNotificationText:@"That's how we roll!"},
  62. @{JDButtonName:@"Show JDStatusBarStyleDark", JDButtonInfo:@"Duration: 2s", JDNotificationText:@"Don't mess with me!"},
  63. @{JDButtonName:@"Show JDStatusBarStyleMatrix", JDButtonInfo:@"Duration: 2s", JDNotificationText:@"Wake up Neo…"}],
  64. @[@{JDButtonName:@"Show custom style 1", JDButtonInfo:@"Duration: 4s, JDStatusBarAnimationTypeFade", JDNotificationText:@"Oh, I love it!"},
  65. @{JDButtonName:@"Show custom style 2", JDButtonInfo:@"Duration: 4s, JDStatusBarAnimationTypeBounce", JDNotificationText:@"Level up!"}],
  66. @[@{JDButtonName:@"Create your own style", JDButtonInfo:@"Test all possibilities", JDNotificationText:@""}]];
  67. }
  68. return self;
  69. }
  70. - (void)viewDidLoad;
  71. {
  72. [super viewDidLoad];
  73. self.tableView.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.95 alpha:1.0];
  74. self.tableView.backgroundView = nil;
  75. // presenting a notification, before a keyWindow is set
  76. self.indicatorStyle = UIActivityIndicatorViewStyleWhite;
  77. [JDStatusBarNotification showWithStatus:@"Hello World!" dismissAfter:2.0
  78. styleName:JDStatusBarStyleMatrix];
  79. }
  80. - (BOOL)prefersStatusBarHidden
  81. {
  82. return NO;
  83. }
  84. #pragma mark UITableViewDataSource
  85. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
  86. {
  87. return self.data.count;
  88. }
  89. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
  90. {
  91. return [self.data[section] count];
  92. }
  93. - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
  94. {
  95. // create / dequeue cell
  96. static NSString* identifier = @"identifier";
  97. UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier];
  98. if (cell == nil) {
  99. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
  100. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  101. cell.textLabel.font = [UIFont systemFontOfSize:15.0];
  102. cell.detailTextLabel.font = [UIFont systemFontOfSize:11.0];
  103. cell.detailTextLabel.textColor = [UIColor colorWithWhite:0.5 alpha:1.0];
  104. if ([[[UIDevice currentDevice] systemVersion] floatValue] < 7.0) {
  105. cell.textLabel.backgroundColor = [UIColor clearColor];
  106. cell.detailTextLabel.backgroundColor = [UIColor clearColor];
  107. cell.backgroundView = [[UIView alloc] init];
  108. cell.backgroundView.backgroundColor = [UIColor whiteColor];
  109. cell.selectedBackgroundView = [[UIView alloc] init];
  110. cell.selectedBackgroundView.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
  111. }
  112. }
  113. NSDictionary *data = self.data[indexPath.section][indexPath.row];
  114. cell.textLabel.text = data[JDButtonName];
  115. cell.detailTextLabel.text = data[JDButtonInfo];
  116. return cell;
  117. }
  118. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;
  119. {
  120. NSInteger section = indexPath.section;
  121. NSInteger row = indexPath.row;
  122. NSDictionary *data = self.data[indexPath.section][indexPath.row];
  123. NSString *status = data[JDNotificationText];
  124. // reset progress timer
  125. self.progress = 0.0;
  126. [self.timer invalidate];
  127. self.timer = nil;
  128. // show notification
  129. if (section == 0) {
  130. if (row == 0) {
  131. self.indicatorStyle = UIActivityIndicatorViewStyleGray;
  132. [JDStatusBarNotification showWithStatus:status];
  133. } else if (row == 1) {
  134. if(![JDStatusBarNotification isVisible]) {
  135. self.indicatorStyle = UIActivityIndicatorViewStyleGray;
  136. [JDStatusBarNotification showWithStatus:status dismissAfter:1.4];
  137. }
  138. [self startTimer];
  139. } else if (row == 2) {
  140. if(![JDStatusBarNotification isVisible]) {
  141. self.indicatorStyle = UIActivityIndicatorViewStyleGray;
  142. [JDStatusBarNotification showWithStatus:status dismissAfter:2.0];
  143. }
  144. [JDStatusBarNotification showActivityIndicator:YES
  145. indicatorStyle:self.indicatorStyle];
  146. } else if (row == 3) {
  147. [JDStatusBarNotification updateStatus:@"Replaced Text.."];
  148. } else if (row == 4) {
  149. [JDStatusBarNotification dismiss];
  150. }
  151. } else if (section == 1) {
  152. self.indicatorStyle = UIActivityIndicatorViewStyleWhite;
  153. NSString *style = JDStatusBarStyleError;
  154. if (row == 1) {
  155. style = JDStatusBarStyleWarning;
  156. self.indicatorStyle = UIActivityIndicatorViewStyleGray;
  157. } else if(row == 2) {
  158. style = JDStatusBarStyleSuccess;
  159. } else if(row == 3) {
  160. style = JDStatusBarStyleDark;
  161. } else if(row == 4) {
  162. style = JDStatusBarStyleMatrix;
  163. }
  164. [JDStatusBarNotification showWithStatus:status
  165. dismissAfter:2.0
  166. styleName:style];
  167. } else if (section == 2) {
  168. self.indicatorStyle = (row==0) ? UIActivityIndicatorViewStyleWhite : UIActivityIndicatorViewStyleGray;
  169. NSString *style = (row==0) ? SBStyle1 : SBStyle2;
  170. [JDStatusBarNotification showWithStatus:status
  171. dismissAfter:4.0
  172. styleName:style];
  173. } else if (section == 3) {
  174. SBCustomStyleViewController* viewController = [[SBCustomStyleViewController alloc] init];
  175. viewController.title = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
  176. [self.navigationController pushViewController:viewController animated:YES];
  177. }
  178. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  179. }
  180. - (void)startTimer;
  181. {
  182. [JDStatusBarNotification showProgress:self.progress];
  183. [self.timer invalidate];
  184. self.timer = nil;
  185. if (self.progress < 1.0) {
  186. CGFloat step = 0.02;
  187. self.timer = [NSTimer scheduledTimerWithTimeInterval:step target:self
  188. selector:@selector(startTimer)
  189. userInfo:nil repeats:NO];
  190. self.progress += step;
  191. } else {
  192. [self performSelector:@selector(hideProgress)
  193. withObject:nil afterDelay:0.5];
  194. }
  195. }
  196. - (void)hideProgress;
  197. {
  198. [JDStatusBarNotification showProgress:0.0];
  199. }
  200. @end