SDNetworkActivityIndicator.m 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * This file is part of the SDNetworkActivityIndicator package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import "SDNetworkActivityIndicator.h"
  9. static SDNetworkActivityIndicator *instance;
  10. @implementation SDNetworkActivityIndicator
  11. + (id)sharedActivityIndicator
  12. {
  13. if (instance == nil)
  14. {
  15. instance = [[SDNetworkActivityIndicator alloc] init];
  16. }
  17. return instance;
  18. }
  19. - (id)init
  20. {
  21. if ((self = [super init]))
  22. {
  23. counter = 0;
  24. }
  25. return self;
  26. }
  27. - (void)startActivity
  28. {
  29. @synchronized(self)
  30. {
  31. if (counter == 0)
  32. {
  33. dispatch_async(dispatch_get_main_queue(), ^{
  34. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  35. });
  36. }
  37. counter++;
  38. }
  39. }
  40. - (void)stopActivity
  41. {
  42. @synchronized(self)
  43. {
  44. if (counter > 0 && --counter == 0)
  45. {
  46. dispatch_async(dispatch_get_main_queue(), ^{
  47. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  48. });
  49. }
  50. }
  51. }
  52. - (void)stopAllActivity
  53. {
  54. @synchronized(self)
  55. {
  56. counter = 0;
  57. dispatch_async(dispatch_get_main_queue(), ^{
  58. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  59. });
  60. }
  61. }
  62. @end