ProgressViewController.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-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 "ProgressViewController.h"
  21. #import "RectUtil.h"
  22. #import "BundleUtil.h"
  23. @interface ProgressViewController ()
  24. @property (nonatomic) CGFloat progress;
  25. @property NSMutableDictionary *itemsToSend;
  26. @property UIVisualEffectView *blurView;
  27. @end
  28. @implementation ProgressViewController
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. _itemsToSend = [NSMutableDictionary dictionary];
  32. [_cancelButton setTitle: [BundleUtil localizedStringForKey:@"cancel"] forState:UIControlStateNormal];
  33. }
  34. - (void)setupColors {
  35. switch ([Colors getTheme]) {
  36. case ColorThemeDark:
  37. case ColorThemeDarkWork:
  38. self.view.tintColor = [Colors main];
  39. _contentView.backgroundColor = [Colors background];
  40. _label.textColor = [Colors fontNormal];
  41. [self darkenVisualEffectsView];
  42. break;
  43. case ColorThemeLight:
  44. case ColorThemeLightWork:
  45. case ColorThemeUndefined:
  46. break;
  47. }
  48. _contentView.layer.cornerRadius = 15.0;
  49. }
  50. - (void)darkenVisualEffectsView {
  51. CGRect rect = self.view.bounds;
  52. UIBlurEffectStyle blurStyle = UIBlurEffectStyleExtraLight;
  53. blurStyle = UIBlurEffectStyleDark;
  54. UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:blurStyle];
  55. _blurView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
  56. _blurView.frame = rect;
  57. _blurView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  58. [_visualEffectsView removeFromSuperview];
  59. _visualEffectsView = nil;
  60. [_blurView.contentView addSubview:_contentView];
  61. [self.view addSubview:_blurView];
  62. NSLayoutConstraint *horizontal = [NSLayoutConstraint constraintWithItem:_contentView
  63. attribute:NSLayoutAttributeCenterX
  64. relatedBy:NSLayoutRelationEqual
  65. toItem:_blurView
  66. attribute:NSLayoutAttributeCenterX
  67. multiplier:1.f constant:0.f];
  68. NSLayoutConstraint *vertical = [NSLayoutConstraint constraintWithItem:_contentView
  69. attribute:NSLayoutAttributeCenterY
  70. relatedBy:NSLayoutRelationEqual
  71. toItem:_blurView
  72. attribute:NSLayoutAttributeCenterY
  73. multiplier:1.f constant:0.f];
  74. [_blurView addConstraints:@[horizontal, vertical]];
  75. }
  76. - (void)viewWillAppear:(BOOL)animated {
  77. [super viewWillAppear:animated];
  78. self.navigationController.navigationBarHidden = YES;
  79. [self updateProgressLabel];
  80. _progressView.progress = 0.0;
  81. [self setupColors];
  82. }
  83. - (void)updateProgressLabel {
  84. NSInteger inProgressOrSentCount = 0;
  85. for (NSString *key in _itemsToSend.keyEnumerator) {
  86. NSNumber *progress = [_itemsToSend objectForKey:key];
  87. if (progress.floatValue > 0.0) {
  88. inProgressOrSentCount++;
  89. };
  90. }
  91. NSString *sendingText = NSLocalizedString(@"sending_count", nil);
  92. NSInteger currentItemCount = inProgressOrSentCount <= _totalCount ? inProgressOrSentCount : _totalCount;
  93. NSString *text = [NSString stringWithFormat:sendingText, currentItemCount, (long)_totalCount];
  94. [_label setText: text];
  95. }
  96. - (void)setProgress:(NSNumber *)progress forItem:(id)item {
  97. if (item) {
  98. [_itemsToSend setObject:progress forKey:item];
  99. }
  100. dispatch_async(dispatch_get_main_queue(), ^{
  101. [self updateProgressLabel];
  102. [self updateProgressView];
  103. });
  104. }
  105. - (void)finishedItem:(id)item {
  106. [self setProgress:[NSNumber numberWithFloat:1.0] forItem:item];
  107. }
  108. - (void)updateProgressView {
  109. CGFloat progress = 0.0;
  110. for (NSString *key in _itemsToSend.keyEnumerator) {
  111. NSNumber *itemProgress = [_itemsToSend objectForKey:key];
  112. progress += itemProgress.floatValue;
  113. }
  114. _progressView.progress = progress / _totalCount;
  115. }
  116. - (IBAction)didCancel:(id)sender {
  117. [_delegate progressViewDidCancel];
  118. }
  119. @end