NCConnectionController.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. #import "NCConnectionController.h"
  6. #import "AFNetworking.h"
  7. #import "NCAPIController.h"
  8. #import "NCDatabaseManager.h"
  9. #import "NCSettingsController.h"
  10. #import "NCUserInterfaceController.h"
  11. #import "NextcloudTalk-Swift.h"
  12. NSString * const NCAppStateHasChangedNotification = @"NCAppStateHasChangedNotification";
  13. NSString * const NCConnectionStateHasChangedNotification = @"NCConnectionStateHasChangedNotification";
  14. @implementation NCConnectionController
  15. @synthesize appState = _appState;
  16. @synthesize connectionState = _connectionState;
  17. + (NCConnectionController *)sharedInstance
  18. {
  19. static dispatch_once_t once;
  20. static NCConnectionController *sharedInstance;
  21. dispatch_once(&once, ^{
  22. sharedInstance = [[self alloc] init];
  23. });
  24. return sharedInstance;
  25. }
  26. - (id)init
  27. {
  28. self = [super init];
  29. if (self) {
  30. self.appState = kAppStateUnknown;
  31. self.connectionState = kConnectionStateUnknown;
  32. [self checkAppState];
  33. [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
  34. NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
  35. [self checkConnectionState];
  36. }];
  37. }
  38. return self;
  39. }
  40. - (void)notifyAppState
  41. {
  42. NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@(self.appState) forKey:@"appState"];
  43. [[NSNotificationCenter defaultCenter] postNotificationName:NCAppStateHasChangedNotification
  44. object:self
  45. userInfo:userInfo];
  46. }
  47. - (void)notifyConnectionState
  48. {
  49. NSDictionary *userInfo = [NSDictionary dictionaryWithObject:@(self.connectionState) forKey:@"connectionState"];
  50. [[NSNotificationCenter defaultCenter] postNotificationName:NCConnectionStateHasChangedNotification
  51. object:self
  52. userInfo:userInfo];
  53. }
  54. - (BOOL)isNetworkAvailable {
  55. return [AFNetworkReachabilityManager sharedManager].reachable;
  56. }
  57. - (void)checkConnectionState
  58. {
  59. if (![self isNetworkAvailable]) {
  60. [self setConnectionState:kConnectionStateDisconnected];
  61. [self notifyConnectionState];
  62. } else {
  63. ConnectionState previousState = self.connectionState;
  64. [self setConnectionState:kConnectionStateConnected];
  65. [self checkAppState];
  66. if (previousState == kConnectionStateDisconnected) {
  67. [self notifyConnectionState];
  68. }
  69. }
  70. }
  71. - (void)checkAppState
  72. {
  73. TalkAccount *activeAccount = [[NCDatabaseManager sharedInstance] activeAccount];
  74. SignalingSettings *activeAccountSignalingConfig = [[[NCSettingsController sharedInstance] signalingConfigurations] objectForKey:activeAccount.accountId];
  75. if (!activeAccount.server || !activeAccount.user) {
  76. [self setAppState:kAppStateNotServerProvided];
  77. [[NCUserInterfaceController sharedInstance] presentLoginViewController];
  78. } else if (!activeAccount.userId || !activeAccount.userDisplayName) {
  79. [self setAppState:kAppStateMissingUserProfile];
  80. [[NCSettingsController sharedInstance] getUserProfileForAccountId:activeAccount.accountId withCompletionBlock:^(NSError *error) {
  81. if (error) {
  82. [self notifyAppState];
  83. } else {
  84. [self checkAppState];
  85. }
  86. }];
  87. } else if (!activeAccountSignalingConfig) {
  88. [self setAppState:kAppStateMissingServerCapabilities];
  89. [[NCSettingsController sharedInstance] getCapabilitiesForAccountId:activeAccount.accountId withCompletionBlock:^(NSError *error) {
  90. if (error) {
  91. [self notifyAppState];
  92. return;
  93. }
  94. [self setAppState:kAppStateMissingSignalingConfiguration];
  95. [[NCSettingsController sharedInstance] updateSignalingConfigurationForAccountId:activeAccount.accountId withCompletionBlock:^(NCExternalSignalingController * _Nullable signalingServer, NSError *error) {
  96. if (error) {
  97. [self notifyAppState];
  98. return;
  99. }
  100. [self checkAppState];
  101. }];
  102. }];
  103. } else {
  104. [self setAppState:kAppStateReady];
  105. }
  106. [self notifyAppState];
  107. }
  108. @end