StatusNavigationBar.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2012-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import "StatusNavigationBar.h"
  21. #import <UIKit/UIKit.h>
  22. #import "ServerConnector.h"
  23. #import "VoIPHelper.h"
  24. #import "Threema-Swift.h"
  25. #import <QuartzCore/QuartzCore.h>
  26. @implementation StatusNavigationBar {
  27. UIImageView *statusView;
  28. UITapGestureRecognizer *tap;
  29. UIView *navBarTapView;
  30. }
  31. - (id)initWithFrame:(CGRect)frame {
  32. self = [super initWithFrame:(CGRect)frame];
  33. if (self) {
  34. [self setup];
  35. }
  36. return self;
  37. }
  38. - (id)initWithCoder:(NSCoder *)aDecoder {
  39. self = [super initWithCoder:aDecoder];
  40. if (self) {
  41. [self setup];
  42. }
  43. return self;
  44. }
  45. - (void)setup {
  46. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callInBackground:) name:kNotificationCallInBackground object:nil];
  47. [self addStatusView];
  48. [[ServerConnector sharedServerConnector] addObserver:self forKeyPath:@"connectionState" options:0 context:nil];
  49. [self updateBarBgColor];
  50. _ignoreSetItems = NO;
  51. }
  52. - (void)addStatusView {
  53. statusView = [[UIImageView alloc] init];
  54. [self updateStatusFrame];
  55. [self addSubview:statusView];
  56. }
  57. - (void)dealloc {
  58. [[NSNotificationCenter defaultCenter] removeObserver:self];
  59. [[ServerConnector sharedServerConnector] removeObserver:self forKeyPath:@"connectionState"];
  60. }
  61. - (void)updateStatusFrame {
  62. statusView.frame = CGRectMake(0, self.frame.size.height - 2, self.frame.size.width, 2);
  63. }
  64. - (void)layoutSubviews {
  65. [super layoutSubviews];
  66. [self updateStatusFrame];
  67. if ([[VoIPHelper shared] isCallActiveInBackground]) {
  68. if (!tap)
  69. tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showCallScreen:)];
  70. if (@available(iOS 11.0, *)) {
  71. [self addGestureRecognizer:tap];
  72. } else {
  73. CGRect frame = CGRectMake(0, 0, self.bounds.size.width, 32.0);
  74. navBarTapView = [[UIView alloc] initWithFrame:frame];
  75. [self addSubview:navBarTapView];
  76. navBarTapView.backgroundColor = [UIColor clearColor];
  77. [navBarTapView setUserInteractionEnabled:YES];
  78. [navBarTapView addGestureRecognizer:tap];
  79. }
  80. } else {
  81. dispatch_async(dispatch_get_main_queue(),^{
  82. if (@available(iOS 11.0, *)) {
  83. [self removeGestureRecognizer:tap];
  84. } else {
  85. [navBarTapView removeGestureRecognizer:tap];
  86. [navBarTapView removeFromSuperview];
  87. }
  88. });
  89. }
  90. }
  91. - (void)updateBarBgColor {
  92. NSString *barBgColor;
  93. switch ([ServerConnector sharedServerConnector].connectionState) {
  94. case ConnectionStateConnected:
  95. barBgColor = @"orange";
  96. break;
  97. case ConnectionStateLoggedIn:
  98. barBgColor = @"green";
  99. break;
  100. case ConnectionStateConnecting:
  101. case ConnectionStateDisconnecting:
  102. case ConnectionStateDisconnected:
  103. if ([[NSUserDefaults standardUserDefaults] boolForKey:@"FASTLANE_SNAPSHOT"]) {
  104. barBgColor = @"green";
  105. } else {
  106. barBgColor = @"red";
  107. }
  108. break;
  109. default:
  110. break;
  111. }
  112. [self showOrHideStatusView];
  113. UIImage *colorBar = [[UIImage imageNamed:[NSString stringWithFormat:@"StatusBar_%@", barBgColor]] resizableImageWithCapInsets:UIEdgeInsetsZero];
  114. statusView.image = colorBar;
  115. }
  116. - (void)hideStatusView {
  117. statusView.hidden = true;
  118. }
  119. - (void)showOrHideStatusView {
  120. switch ([ServerConnector sharedServerConnector].connectionState) {
  121. case ConnectionStateConnected:
  122. statusView.hidden = false;
  123. break;
  124. case ConnectionStateLoggedIn:
  125. statusView.hidden = true;
  126. break;
  127. case ConnectionStateConnecting:
  128. case ConnectionStateDisconnecting:
  129. case ConnectionStateDisconnected:
  130. if ([[NSUserDefaults standardUserDefaults] boolForKey:@"FASTLANE_SNAPSHOT"]) {
  131. statusView.hidden = true;
  132. } else {
  133. statusView.hidden = false;
  134. }
  135. break;
  136. default:
  137. break;
  138. }
  139. }
  140. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  141. if (object == [ServerConnector sharedServerConnector] && [keyPath isEqualToString:@"connectionState"]) {
  142. dispatch_async(dispatch_get_main_queue(), ^{
  143. [self updateBarBgColor];
  144. });
  145. }
  146. }
  147. - (void)callInBackground:(NSNotification*)notification {
  148. if ([[VoIPHelper shared] isCallActiveInBackground]) {
  149. if (!tap)
  150. tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showCallScreen:)];
  151. if (@available(iOS 11.0, *)) {
  152. [self addGestureRecognizer:tap];
  153. } else {
  154. CGRect frame = CGRectMake(0, 0, self.bounds.size.width, 32.0);
  155. navBarTapView = [[UIView alloc] initWithFrame:frame];
  156. [self addSubview:navBarTapView];
  157. navBarTapView.backgroundColor = [UIColor clearColor];
  158. [navBarTapView setUserInteractionEnabled:YES];
  159. [navBarTapView addGestureRecognizer:tap];
  160. }
  161. } else {
  162. dispatch_async(dispatch_get_main_queue(), ^{
  163. if (@available(iOS 11.0, *)) {
  164. [self removeGestureRecognizer:tap];
  165. } else {
  166. [navBarTapView removeGestureRecognizer:tap];
  167. [navBarTapView removeFromSuperview];
  168. }
  169. });
  170. }
  171. [Colors updateNavigationBar:self];
  172. }
  173. - (void)showCallScreen:(UITapGestureRecognizer *)tapRecognizer {
  174. if ([[VoIPHelper shared] isCallActiveInBackground]) {
  175. [[VoIPCallStateManager shared] presentCallViewController];
  176. }
  177. }
  178. @end