BrandingUtils.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2016-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 <SDWebImage/UIImageView+WebCache.h>
  21. #import "BrandingUtils.h"
  22. #import "LicenseStore.h"
  23. #import "MyIdentityStore.h"
  24. #import "Colors.h"
  25. #import "UIImage+ColoredImage.h"
  26. #ifdef DEBUG
  27. static const DDLogLevel ddLogLevel = DDLogLevelVerbose;
  28. #else
  29. static const DDLogLevel ddLogLevel = DDLogLevelWarning;
  30. #endif
  31. @implementation BrandingUtils
  32. + (void)updateTitleLogoOfNavigationItem:(UINavigationItem*)navigationItem navigationController:(UINavigationController *)navigationController {
  33. if ([LicenseStore requiresLicenseKey]) {
  34. // If the license key includes a logo URL, use it
  35. NSString *logoUrl = nil;
  36. switch ([Colors getTheme]) {
  37. case ColorThemeDark:
  38. case ColorThemeDarkWork:
  39. logoUrl = [MyIdentityStore sharedMyIdentityStore].licenseLogoDarkUrl;
  40. break;
  41. case ColorThemeLight:
  42. case ColorThemeLightWork:
  43. case ColorThemeUndefined:
  44. logoUrl = [MyIdentityStore sharedMyIdentityStore].licenseLogoLightUrl;
  45. break;
  46. }
  47. if (logoUrl != nil) {
  48. UIImageView *logoView = [[UIImageView alloc] init];
  49. [logoView sd_setImageWithURL:[[NSURL alloc] initWithString:logoUrl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  50. if (error == nil) {
  51. [self setupLogoForThreema:false logo:image navigationItem:navigationItem navigationController:navigationController];
  52. } else {
  53. DDLogError(@"Loading logo failed: %@", error);
  54. }
  55. }];
  56. } else {
  57. // Default work logo
  58. UIImage *logo;
  59. switch ([Colors getTheme]) {
  60. case ColorThemeDark:
  61. case ColorThemeDarkWork:
  62. logo = [UIImage imageNamed:@"ThreemaWorkWhite"];
  63. break;
  64. case ColorThemeLight:
  65. case ColorThemeLightWork:
  66. case ColorThemeUndefined:
  67. logo = [UIImage imageNamed:@"ThreemaWorkBlack"];
  68. break;
  69. }
  70. [self setupLogoForThreema:true logo:logo navigationItem:navigationItem navigationController:navigationController];
  71. }
  72. } else {
  73. // Default threema logo
  74. UIImage *logo;
  75. switch ([Colors getTheme]) {
  76. case ColorThemeDark:
  77. case ColorThemeDarkWork:
  78. logo = [UIImage imageNamed:@"ThreemaWhite"];
  79. break;
  80. case ColorThemeLight:
  81. case ColorThemeLightWork:
  82. case ColorThemeUndefined:
  83. logo = [UIImage imageNamed:@"ThreemaBlack"];
  84. break;
  85. }
  86. [self setupLogoForThreema:true logo:logo navigationItem:navigationItem navigationController:navigationController];
  87. }
  88. }
  89. + (void)setupLogoForThreema:(BOOL)threema logo:(UIImage *)logo navigationItem:(UINavigationItem*)navigationItem navigationController:(UINavigationController *)navigationController {
  90. if (navigationController == nil) {
  91. return;
  92. }
  93. UIImageView *logoView = [[UIImageView alloc] initWithImage:logo];
  94. logoView.contentMode = UIViewContentModeScaleAspectFit;
  95. CGFloat height = threema == true ? 15.0 : 28.0;
  96. int y = 0;
  97. CGFloat navigationBarWidth = navigationController.navigationBar.frame.size.width;
  98. BOOL correctSize = NO;
  99. while (correctSize == NO && height > 0) {
  100. CGFloat totalFreeWidth = navigationBarWidth;
  101. CGFloat width = height * logo.size.width / logo.size.height;
  102. for (UIBarButtonItem *item in navigationItem.leftBarButtonItems) {
  103. UIView *view = [item valueForKey:@"view"];
  104. totalFreeWidth -= view.frame.size.width;
  105. }
  106. for (UIBarButtonItem *item in navigationItem.rightBarButtonItems) {
  107. UIView *view = [item valueForKey:@"view"];
  108. totalFreeWidth -= view.frame.size.width;
  109. }
  110. logoView.frame = CGRectMake(0, y, width, height);
  111. if (totalFreeWidth - 30.0 > logoView.frame.size.width) {
  112. correctSize = YES;
  113. } else {
  114. height = height - 2;
  115. y = y + 1;
  116. }
  117. }
  118. // Wrap in UIView to keep iOS from messing with the size
  119. UIView *wrapView = [[UIView alloc] initWithFrame:logoView.frame];
  120. [wrapView addSubview:logoView];
  121. wrapView.accessibilityElementsHidden = true;
  122. // If we're running on the main thread (common if the image is already cached, e.g. when switching tabs),
  123. // then set the title view immediately; if we dispatch, the user might see the normal title for a brief
  124. // instance.
  125. if ([[NSOperationQueue currentQueue] underlyingQueue] != dispatch_get_main_queue()) {
  126. dispatch_async(dispatch_get_main_queue(), ^{
  127. navigationItem.titleView = wrapView;
  128. });
  129. } else {
  130. navigationItem.titleView = wrapView;
  131. }
  132. }
  133. @end