AppManager.m 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "AppManager.h"
  17. #import <FirebaseCore/FIRApp.h>
  18. #import "FIRPhoneAuthProvider.h"
  19. #import "FirebaseAuth.h"
  20. NS_ASSUME_NONNULL_BEGIN
  21. @implementation AppManager {
  22. /** @var _createdAppNames
  23. @brief The set of names of live (created but not deleted) app, to avoid iCore warnings.
  24. */
  25. NSMutableSet<NSString *> *_liveAppNames;
  26. }
  27. - (instancetype)init {
  28. self = [super init];
  29. if (self) {
  30. _count = 2;
  31. _liveAppNames = [[NSMutableSet<NSString *> alloc] initWithCapacity:_count - 1];
  32. }
  33. return self;
  34. }
  35. - (nullable FIRApp *)appAtIndex:(int)index {
  36. if (index == 0) {
  37. return [FIRApp defaultApp];
  38. }
  39. NSString *name = [self appNameWithIndex:index];
  40. if ([_liveAppNames containsObject:name]) {
  41. return [FIRApp appNamed:[self appNameWithIndex:index]];
  42. }
  43. return nil;
  44. }
  45. - (void)recreateAppAtIndex:(int)index
  46. withOptions:(nullable FIROptions *)options
  47. completion:(void (^)(void))completion {
  48. [self deleteAppAtIndex:index completion:^() {
  49. if (index == 0) {
  50. if (options) {
  51. [FIRApp configureWithOptions:options];
  52. }
  53. } else {
  54. NSString *name = [self appNameWithIndex:index];
  55. if (options) {
  56. [FIRApp configureWithName:name options:options];
  57. [_liveAppNames addObject:name];
  58. } else {
  59. [_liveAppNames removeObject:name];
  60. }
  61. }
  62. completion();
  63. }];
  64. }
  65. + (instancetype)sharedInstance {
  66. static dispatch_once_t onceToken;
  67. static AppManager *sharedInstance;
  68. dispatch_once(&onceToken, ^{
  69. sharedInstance = [[self alloc] init];
  70. });
  71. return sharedInstance;
  72. }
  73. + (FIRApp *)app {
  74. AppManager *manager = [self sharedInstance];
  75. return [manager appAtIndex:manager.active];
  76. }
  77. + (FIRAuth *)auth {
  78. return [FIRAuth authWithApp:[self app]];
  79. }
  80. + (FIRPhoneAuthProvider *)phoneAuthProvider {
  81. return [FIRPhoneAuthProvider providerWithAuth:[self auth]];
  82. }
  83. #pragma mark - Helpers
  84. /** @fn appNameWithIndex:
  85. @brief Gets the app name for the given index.
  86. @param index The index of the app managed by this instance.
  87. @returns The app name for the FIRApp instance.
  88. */
  89. - (NSString *)appNameWithIndex:(int)index {
  90. return [NSString stringWithFormat:@"APP_%02d", index];
  91. }
  92. /** @fn deleteAppAtIndex:withOptions:completion:
  93. @brief Deletes the app at the given index.
  94. @param index The index of the app to be deleted, 0 being the default app.
  95. @param completion The block to call when completes.
  96. */
  97. - (void)deleteAppAtIndex:(int)index
  98. completion:(void (^)(void))completion {
  99. FIRApp *app = [self appAtIndex:index];
  100. if (app) {
  101. [app deleteApp:^(BOOL success) {
  102. if (success) {
  103. completion();
  104. } else {
  105. NSLog(@"Failed to delete app '%@'.", app.name);
  106. }
  107. }];
  108. } else {
  109. completion();
  110. }
  111. }
  112. @end
  113. NS_ASSUME_NONNULL_END