BallotResultViewController.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2014-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 "BallotResultViewController.h"
  21. #import "EntityManager.h"
  22. #import "BallotResultMatrixView.h"
  23. #import "RectUtil.h"
  24. #import "BallotHeaderView.h"
  25. #import "NibUtil.h"
  26. #define IPAD_PADDING_FACTOR (3.0/4.0)
  27. @interface BallotResultViewController ()
  28. @property BallotHeaderView *headerView;
  29. @property BallotResultMatrixView *matrixView;
  30. @end
  31. @implementation BallotResultViewController
  32. + (instancetype) ballotResultViewControllerForBallot:(Ballot *)ballot {
  33. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Ballot" bundle:nil];
  34. BallotResultViewController *viewController = (BallotResultViewController *) [storyboard instantiateViewControllerWithIdentifier:@"BallotResultViewController"];
  35. viewController.ballot = ballot;
  36. return viewController;
  37. }
  38. -(void)viewDidLayoutSubviews {
  39. CGFloat top = self.topLayoutGuide.length;
  40. _headerPlaceholderView.frame = [RectUtil setYPositionOf:_headerPlaceholderView.frame y:top];
  41. _resultView.frame = [RectUtil setYPositionOf:_headerPlaceholderView.frame y:CGRectGetMaxY(_headerPlaceholderView.frame)];
  42. CGFloat height = self.view.bounds.size.height - CGRectGetMaxY(_headerPlaceholderView.frame);
  43. _resultView.frame = [RectUtil setHeightOf:_resultView.frame height:height];
  44. [_matrixView adaptToInterfaceRotation];
  45. }
  46. - (void)viewDidLoad {
  47. [super viewDidLoad];
  48. _headerView = (BallotHeaderView *)[NibUtil loadViewFromNibWithName:@"BallotHeaderView"];
  49. _headerView.ballot = _ballot;
  50. _headerView.frame = _headerPlaceholderView.bounds;
  51. [_headerPlaceholderView addSubview: _headerView];
  52. if ([_ballot isClosed]) {
  53. [self setTitle:NSLocalizedStringFromTable(@"ballot_results", @"Ballot", nil)];
  54. } else {
  55. [self setTitle:NSLocalizedStringFromTable(@"ballot_intermediate_results", @"Ballot", nil)];
  56. }
  57. [self updateContent];
  58. [self setupColors];
  59. }
  60. - (void)setupColors {
  61. self.view.backgroundColor = [Colors background];
  62. }
  63. - (CGSize)preferredContentSize {
  64. if (SYSTEM_IS_IPAD) {
  65. CGFloat maxWidth, maxHeight;
  66. if (self.view.window == nil) {
  67. // window is not set until view is on screen
  68. CGSize size = self.view.bounds.size;
  69. maxWidth = size.width;
  70. maxHeight = size.height;
  71. } else {
  72. CGSize size = self.view.window.bounds.size;
  73. maxWidth = size.height;
  74. maxHeight = size.width;
  75. }
  76. return CGSizeMake(maxWidth * IPAD_PADDING_FACTOR, maxHeight * IPAD_PADDING_FACTOR);
  77. } else {
  78. return [super preferredContentSize];
  79. }
  80. }
  81. - (void)updateContent {
  82. CGRect rect = _resultView.bounds;
  83. _matrixView = [[BallotResultMatrixView alloc] initWithFrame: rect];
  84. _matrixView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
  85. _matrixView.ballot = _ballot;
  86. [_resultView addSubview: _matrixView];
  87. }
  88. @end