BallotCreateDetailViewController.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "BallotCreateDetailViewController.h"
  21. #import "BallotSelectTableViewController.h"
  22. #import "BallotChoice.h"
  23. @interface BallotCreateDetailViewController ()
  24. @end
  25. static NSData *ballotIdForAcceptedWarning;
  26. @implementation BallotCreateDetailViewController
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. [self updateUIStrings];
  30. [self updateContent];
  31. [_showIntermediateSwitch addTarget:self action:@selector(intermediateUpdated) forControlEvents:UIControlEventValueChanged];
  32. [_multipleChoiceSwitch addTarget:self action:@selector(multipleChoiceUpdated) forControlEvents:UIControlEventValueChanged];
  33. [_cloneButton addTarget:self action:@selector(clonePressed) forControlEvents:UIControlEventTouchUpInside];
  34. [self setupColors];
  35. }
  36. - (void)setupColors {
  37. [_cloneButton setTitleColor:[Colors main] forState:UIControlStateNormal];
  38. _multipleChoiceLabel.textColor = [Colors fontNormal];
  39. _showIntermediateLabel.textColor = [Colors fontNormal];
  40. }
  41. - (void)viewWillAppear:(BOOL)animated {
  42. [self updateContent];
  43. [super viewWillAppear:animated];
  44. }
  45. - (void)updateUIStrings {
  46. [_showIntermediateLabel setText:NSLocalizedStringFromTable(@"ballot_show_intermediate_results", @"Ballot", nil)];
  47. [_multipleChoiceLabel setText:NSLocalizedStringFromTable(@"ballot_multiple_choice", @"Ballot", nil)];
  48. [_cloneButton setTitle:NSLocalizedStringFromTable(@"ballot_clone", @"Ballot", nil) forState:UIControlStateNormal];
  49. [self setTitle:NSLocalizedStringFromTable(@"ballot_options", @"Ballot", nil)];
  50. }
  51. - (void)updateContent {
  52. _showIntermediateSwitch.on = [_ballot isIntermediate];
  53. _multipleChoiceSwitch.on = [_ballot isMultipleChoice];
  54. }
  55. - (void)intermediateUpdated {
  56. [_ballot setIntermediate: _showIntermediateSwitch.on];
  57. }
  58. - (void)multipleChoiceUpdated {
  59. [_ballot setMultipleChoice: _multipleChoiceSwitch.on];
  60. }
  61. - (void)showCloneWarning {
  62. NSString *title = NSLocalizedStringFromTable(@"ballot_clone_warning_title", @"Ballot", nil);
  63. NSString *message = NSLocalizedStringFromTable(@"ballot_clone_warning_message", @"Ballot", nil);
  64. UIAlertController *errAlert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
  65. [errAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"ok", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  66. [self performSegueWithIdentifier:@"ballotCloneSegue" sender:self];
  67. }]];
  68. [errAlert addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
  69. }]];
  70. [self presentViewController:errAlert animated:YES completion:nil];
  71. }
  72. - (BOOL)ballotHasData {
  73. if ([_ballot.title length] > 0) {
  74. return YES;
  75. }
  76. for (BallotChoice *choice in _ballot.choices) {
  77. if ([choice.name length] > 0) {
  78. return YES;
  79. }
  80. }
  81. return NO;
  82. }
  83. #pragma mark - button actions
  84. -(void)clonePressed {
  85. if ([ballotIdForAcceptedWarning isEqualToData:_ballot.id]) {
  86. ;//nop
  87. } else if ([self ballotHasData]) {
  88. [self showCloneWarning];
  89. return;
  90. }
  91. [self performSegueWithIdentifier:@"ballotCloneSegue" sender:self];
  92. }
  93. #pragma mark - Navigation
  94. // In a storyboard-based application, you will often want to do a little preparation before navigation
  95. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  96. if ([segue.identifier isEqualToString:@"ballotCloneSegue"]) {
  97. ballotIdForAcceptedWarning = _ballot.id;
  98. BallotSelectTableViewController *controller = (BallotSelectTableViewController*)segue.destinationViewController;
  99. controller.ballot = _ballot;
  100. }
  101. }
  102. #pragma mark - Table view data source
  103. - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
  104. if (section == 1)
  105. return NSLocalizedStringFromTable(@"ballot_description_intermediate", @"Ballot", nil);
  106. else if (section == 2)
  107. return NSLocalizedStringFromTable(@"ballot_description_multiplechoice", @"Ballot", nil);
  108. return nil;
  109. }
  110. @end