ChatSettingsViewController.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 <QuartzCore/QuartzCore.h>
  21. #import "ChatSettingsViewController.h"
  22. #import "UserSettings.h"
  23. #import "UIImage+Resize.h"
  24. #import "AppDelegate.h"
  25. #import "ModalPresenter.h"
  26. #import "Colors.h"
  27. #import "LicenseStore.h"
  28. #import <Photos/Photos.h>
  29. #import "BundleUtil.h"
  30. #import "UIImage+ColoredImage.h"
  31. @interface ChatSettingsViewController ()
  32. @end
  33. @implementation ChatSettingsViewController
  34. - (id)initWithStyle:(UITableViewStyle)style
  35. {
  36. self = [super initWithStyle:style];
  37. if (self) {
  38. // Custom initialization
  39. }
  40. return self;
  41. }
  42. - (void)viewDidLoad {
  43. [[UserSettings sharedUserSettings] checkWallpaper];
  44. self.wallpaperImageView.layer.shadowColor = [UIColor blackColor].CGColor;
  45. self.wallpaperImageView.layer.shadowOffset = CGSizeMake(1, 1);
  46. self.wallpaperImageView.layer.shadowOpacity = 0.25;
  47. self.wallpaperImageView.layer.shadowRadius = 5.0;
  48. [super viewDidLoad];
  49. }
  50. - (void)viewWillAppear:(BOOL)animated {
  51. [super viewWillAppear:animated];
  52. [self updateView];
  53. }
  54. - (void)updateView {
  55. UIImage *wallpaper = [UserSettings sharedUserSettings].wallpaper;
  56. [self.wallpaperImageView.layer setBorderColor: [[Colors hairline] CGColor]];
  57. [self.wallpaperImageView.layer setBorderWidth: 2.0];
  58. if (wallpaper != nil) {
  59. self.wallpaperImageView.image = wallpaper;
  60. self.wallpaperImageView.backgroundColor = nil;
  61. }
  62. else {
  63. self.wallpaperImageView.image = nil;
  64. [self.wallpaperImageView setBackgroundColor:[Colors backgroundChat]];
  65. if ([LicenseStore requiresLicenseKey] == false) {
  66. UIImage *chatBackground = [BundleUtil imageNamed:@"ChatBackground"];
  67. chatBackground = [chatBackground imageWithTint:[Colors chatBackgroundLines]];
  68. self.wallpaperImageView.backgroundColor = [UIColor colorWithPatternImage:chatBackground];
  69. }
  70. }
  71. if ([UserSettings sharedUserSettings].useDynamicFontSize) {
  72. self.fontSizeLabel.text = NSLocalizedString(@"use_dynamic_font_size", nil);
  73. } else {
  74. self.fontSizeLabel.text = [NSString stringWithFormat:@"%.0f %@", [UserSettings sharedUserSettings].chatFontSize,
  75. NSLocalizedString(@"font_point", nil)];
  76. }
  77. self.biggerEmojiLabel.text = [BundleUtil localizedStringForKey:@"bigger_single_emojis"];
  78. self.showReceivedTimestampSwitch.on = [UserSettings sharedUserSettings].showReceivedTimestamps;
  79. self.returnToSendSwitch.on = [UserSettings sharedUserSettings].returnToSend;
  80. self.biggerEmojiSwitch.on = ![UserSettings sharedUserSettings].disableBigEmojis;
  81. }
  82. #pragma mark - Table view delegate
  83. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  84. if (indexPath.section == 0 && indexPath.row == 0) {
  85. return 178.0;
  86. }
  87. return UITableViewAutomaticDimension;
  88. }
  89. -(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
  90. if (indexPath.section == 0 && indexPath.row == 0) {
  91. return 178.0;
  92. }
  93. return UITableViewAutomaticDimension;
  94. }
  95. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  96. {
  97. __block UIImagePickerController *picker = nil;
  98. if (indexPath.section == 0) {
  99. if (indexPath.row == 0) {
  100. __block CGRect rect;
  101. if (![[NSThread currentThread] isMainThread]) {
  102. dispatch_sync(dispatch_get_main_queue(), ^{
  103. rect = [tableView rectForRowAtIndexPath:indexPath];
  104. });
  105. } else {
  106. rect = [tableView rectForRowAtIndexPath:indexPath];
  107. }
  108. if (@available(iOS 11, *)) {
  109. picker = [[UIImagePickerController alloc] init];
  110. picker.delegate = self;
  111. [ModalPresenter present:picker on:self fromRect:rect inView:self.view];
  112. } else {
  113. [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
  114. switch (status) {
  115. case PHAuthorizationStatusDenied:
  116. case PHAuthorizationStatusRestricted:
  117. dispatch_sync(dispatch_get_main_queue(), ^{
  118. [UIAlertTemplate showAlertWithOwner:[[AppDelegate sharedAppDelegate] currentTopViewController] title:nil message:[BundleUtil localizedStringForKey:@"save_photo_failed_message"] actionOk:nil];
  119. });
  120. break;
  121. case PHAuthorizationStatusNotDetermined:
  122. dispatch_sync(dispatch_get_main_queue(), ^{
  123. [UIAlertTemplate showAlertWithOwner:[[AppDelegate sharedAppDelegate] currentTopViewController] title:nil message:[BundleUtil localizedStringForKey:@"save_photo_failed_message"] actionOk:nil];
  124. });
  125. break;
  126. case PHAuthorizationStatusAuthorized:
  127. dispatch_sync(dispatch_get_main_queue(), ^{
  128. picker = [[UIImagePickerController alloc] init];
  129. picker.delegate = self;
  130. [ModalPresenter present:picker on:self fromRect:rect inView:self.view];
  131. });
  132. break;
  133. }
  134. }];
  135. }
  136. } else if (indexPath.row == 1) {
  137. /* reset */
  138. [UserSettings sharedUserSettings].wallpaper = nil;
  139. [self updateView];
  140. }
  141. }
  142. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  143. }
  144. #pragma mark - image picker delegate
  145. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  146. UIImage *pickedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
  147. /* resize to fit screen (for better performance) */
  148. CGSize screenSize = [[UIScreen mainScreen] bounds].size;
  149. screenSize.height *= [UIScreen mainScreen].scale;
  150. screenSize.width *= [UIScreen mainScreen].scale;
  151. UIImage *scaled = [pickedImage resizedImageWithContentMode:UIViewContentModeScaleAspectFill bounds:screenSize interpolationQuality:kCGInterpolationHigh];
  152. [UserSettings sharedUserSettings].wallpaper = scaled;
  153. [self handlePickerFinished];
  154. [self updateView];
  155. }
  156. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  157. [self handlePickerFinished];
  158. }
  159. - (void)handlePickerFinished {
  160. NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
  161. [self.tableView deselectRowAtIndexPath:indexPath animated:YES];
  162. [ModalPresenter dismissPresentedControllerOn:self animated:YES];
  163. }
  164. - (IBAction)showReceivedTimestampChanged:(id)sender {
  165. [UserSettings sharedUserSettings].showReceivedTimestamps = self.showReceivedTimestampSwitch.on;
  166. }
  167. - (IBAction)returnToSendChanged:(id)sender {
  168. [UserSettings sharedUserSettings].returnToSend = self.returnToSendSwitch.on;
  169. }
  170. - (IBAction)biggerEmojiChanged:(id)sender {
  171. [UserSettings sharedUserSettings].disableBigEmojis = !self.biggerEmojiSwitch.on;
  172. }
  173. @end