BallotSelectTableViewController.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "BallotSelectTableViewController.h"
  21. #import "EntityManager.h"
  22. #import "BallotListTableCell.h"
  23. #define BALLOT_LIST_TABLE_CELL_ID @"BallotListTableCellId"
  24. #define MAX_BALLOTS 100
  25. @interface BallotSelectTableViewController ()
  26. @property NSArray *ballots;
  27. @property EntityManager *entityManager;
  28. @end
  29. @implementation BallotSelectTableViewController
  30. - (void)viewDidLoad {
  31. [super viewDidLoad];
  32. _entityManager = [[EntityManager alloc] init];
  33. [self loadData];
  34. _cancelButton.target = self;
  35. _cancelButton.action = @selector(cancelPressed);
  36. [self setTitle:NSLocalizedStringFromTable(@"ballot_choose_title", @"Ballot", nil)];
  37. }
  38. - (void)loadData {
  39. NSFetchRequest *fetchRequest = [_entityManager.entityFetcher fetchRequestForEntity:@"Ballot"];
  40. NSArray *sortDescriptors = @[
  41. [NSSortDescriptor sortDescriptorWithKey:@"modifyDate" ascending:NO],
  42. [NSSortDescriptor sortDescriptorWithKey:@"createDate" ascending:NO]
  43. ];
  44. [fetchRequest setSortDescriptors:sortDescriptors];
  45. fetchRequest.fetchLimit = MAX_BALLOTS;
  46. NSArray *result = [_entityManager.entityFetcher executeFetchRequest:fetchRequest];
  47. if (result) {
  48. _ballots = result;
  49. } else {
  50. _ballots = [NSArray array];
  51. }
  52. }
  53. #pragma mark - button actions
  54. - (void) cancelPressed {
  55. [self.navigationController popViewControllerAnimated:YES];
  56. }
  57. #pragma mark - Table view data source
  58. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  59. return 1;
  60. }
  61. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  62. return [_ballots count];
  63. }
  64. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  65. BallotListTableCell *cell = (BallotListTableCell *)[tableView dequeueReusableCellWithIdentifier:BALLOT_LIST_TABLE_CELL_ID forIndexPath:indexPath];
  66. Ballot *ballot = [_ballots objectAtIndex: indexPath.row];
  67. [cell.nameLabel setText: ballot.title];
  68. NSString *creatorName = [_entityManager.entityFetcher displayNameForContactId:ballot.creatorId];
  69. [cell.creatorNameLabel setText: creatorName];
  70. NSDate *date;
  71. if (ballot.modifyDate) {
  72. date = ballot.modifyDate;
  73. } else {
  74. date = ballot.createDate;
  75. }
  76. [cell.dateLabel setText: [DateFormatter getShortDate:date]];
  77. return cell;
  78. }
  79. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  80. EntityCreator *entityCreator = [[EntityCreator alloc] initWith:_ballot.managedObjectContext];
  81. Ballot *ballot = [_ballots objectAtIndex: indexPath.row];
  82. _ballot.title = [ballot.title copy];
  83. _ballot.assessmentType = [ballot.assessmentType copy];
  84. _ballot.choicesType = [ballot.choicesType copy];
  85. _ballot.type = [ballot.type copy];
  86. [self clearBallotChoices];
  87. for (BallotChoice *choice in ballot.choices) {
  88. BallotChoice *ballotChoice = [entityCreator ballotChoice];
  89. ballotChoice.name = [choice.name copy];
  90. ballotChoice.orderPosition = [choice.orderPosition copy];
  91. ballotChoice.ballot = _ballot;
  92. [_ballot addChoicesObject: ballotChoice];
  93. }
  94. [self.navigationController popViewControllerAnimated:YES];
  95. }
  96. - (void)clearBallotChoices {
  97. for (BallotChoice *choice in _ballot.choices) {
  98. [_ballot.managedObjectContext deleteObject: choice];
  99. }
  100. [_ballot removeChoices:_ballot.choices];
  101. }
  102. @end