BallotListTableViewController.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "BallotListTableViewController.h"
  21. #import "EntityManager.h"
  22. #import "BallotListTableCell.h"
  23. #import "BallotDispatcher.h"
  24. #import "ChatViewController.h"
  25. #import "ChatViewControllerCache.h"
  26. #define BALLOT_LIST_TABLE_CELL_ID @"BallotListTableCellId"
  27. #define MAX_BALLOTS 50
  28. @interface BallotListTableViewController ()
  29. @property NSArray *openBallots;
  30. @property NSArray *closedBallots;
  31. @property EntityManager *entityManager;
  32. @end
  33. @implementation BallotListTableViewController
  34. + (instancetype) ballotListViewControllerForConversation:(Conversation *)conversation {
  35. UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Ballot" bundle:nil];
  36. BallotListTableViewController *viewController = (BallotListTableViewController *) [storyboard instantiateViewControllerWithIdentifier:@"BallotListTableViewController"];
  37. viewController.conversation = conversation;
  38. return viewController;
  39. }
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. _entityManager = [[EntityManager alloc] init];
  43. }
  44. - (void)viewWillAppear:(BOOL)animated {
  45. [self loadData];
  46. [self.tableView reloadData];
  47. [self setTitle:NSLocalizedStringFromTable(@"ballots", @"Ballot", nil)];
  48. [super viewWillAppear:animated];
  49. }
  50. - (void)loadData {
  51. NSFetchRequest *fetchRequest = [_entityManager.entityFetcher fetchRequestForEntity:@"Ballot"];
  52. NSArray *sortDescriptors = @[
  53. [NSSortDescriptor sortDescriptorWithKey:@"modifyDate" ascending:NO],
  54. [NSSortDescriptor sortDescriptorWithKey:@"createDate" ascending:NO]
  55. ];
  56. [fetchRequest setSortDescriptors:sortDescriptors];
  57. fetchRequest.fetchLimit = MAX_BALLOTS;
  58. fetchRequest.predicate = [NSPredicate predicateWithFormat:@"conversation == %@ && state == %d", _conversation, kBallotStateOpen];
  59. _openBallots = [_entityManager.entityFetcher executeFetchRequest:fetchRequest];
  60. fetchRequest.predicate = [NSPredicate predicateWithFormat:@"conversation == %@ && state == %d", _conversation, kBallotStateClosed];
  61. _closedBallots = [_entityManager.entityFetcher executeFetchRequest:fetchRequest];
  62. }
  63. - (Ballot *)ballotForIndexPath:(NSIndexPath *)indexPath {
  64. Ballot *ballot = nil;
  65. if (indexPath.section == 0) {
  66. if (indexPath.row < [_openBallots count]) {
  67. ballot = [_openBallots objectAtIndex: indexPath.row];
  68. }
  69. } else {
  70. if (indexPath.row < [_closedBallots count]) {
  71. ballot = [_closedBallots objectAtIndex: indexPath.row];
  72. }
  73. }
  74. return ballot;
  75. }
  76. #pragma mark - Table view data source
  77. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  78. return 2;
  79. }
  80. -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
  81. if (section == 0) {
  82. return NSLocalizedStringFromTable(@"ballot_open_ballots", @"Ballot", nil);
  83. } else {
  84. return NSLocalizedStringFromTable(@"ballot_closed_ballots", @"Ballot", nil);
  85. }
  86. }
  87. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  88. if (section == 0) {
  89. return [_openBallots count];
  90. } else {
  91. return [_closedBallots count];
  92. }
  93. }
  94. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  95. BallotListTableCell *cell = (BallotListTableCell *)[tableView dequeueReusableCellWithIdentifier:BALLOT_LIST_TABLE_CELL_ID];
  96. Ballot *ballot = [self ballotForIndexPath:indexPath];
  97. [cell.nameLabel setText: ballot.title];
  98. NSString *creatorName = [_entityManager.entityFetcher displayNameForContactId:ballot.creatorId];
  99. [cell.creatorNameLabel setText: creatorName];
  100. NSDate *date;
  101. if (ballot.modifyDate) {
  102. date = ballot.modifyDate;
  103. } else {
  104. date = ballot.createDate;
  105. }
  106. [cell.dateLabel setText: [DateFormatter shortStyleDateTime:date]];
  107. return cell;
  108. }
  109. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  110. Ballot *ballot = [self ballotForIndexPath:indexPath];
  111. if (ballot) {
  112. [BallotDispatcher showViewControllerForBallot:ballot onNavigationController:self.navigationController];
  113. }
  114. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  115. }
  116. #pragma mark - Table view delegae
  117. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  118. {
  119. return YES;
  120. }
  121. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
  122. if (editingStyle == UITableViewCellEditingStyleDelete) {
  123. [_entityManager performSyncBlockAndSafe:^{
  124. Ballot *ballot = [self ballotForIndexPath:indexPath];
  125. for (BaseMessage *message in ballot.message) {
  126. [[_entityManager entityDestroyer] deleteObjectWithObject:message];
  127. }
  128. [[_entityManager entityDestroyer] deleteObjectWithObject:ballot];
  129. ChatViewController *chatViewController = [ChatViewControllerCache controllerForConversation:_conversation];
  130. if (chatViewController != nil) {
  131. [chatViewController updateConversationLastMessage];
  132. [chatViewController updateConversation];
  133. }
  134. }];
  135. [self loadData];
  136. [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
  137. }
  138. }
  139. @end