123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- // _____ _
- // |_ _| |_ _ _ ___ ___ _ __ __ _
- // | | | ' \| '_/ -_) -_) ' \/ _` |_
- // |_| |_||_|_| \___\___|_|_|_\__,_(_)
- //
- // Threema iOS Client
- // Copyright (c) 2014-2020 Threema GmbH
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU Affero General Public License, version 3,
- // as published by the Free Software Foundation.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU Affero General Public License for more details.
- //
- // You should have received a copy of the GNU Affero General Public License
- // along with this program. If not, see <https://www.gnu.org/licenses/>.
- #import "BallotSelectTableViewController.h"
- #import "EntityManager.h"
- #import "BallotListTableCell.h"
- #define BALLOT_LIST_TABLE_CELL_ID @"BallotListTableCellId"
- #define MAX_BALLOTS 100
- @interface BallotSelectTableViewController ()
- @property NSArray *ballots;
- @property EntityManager *entityManager;
- @end
- @implementation BallotSelectTableViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- _entityManager = [[EntityManager alloc] init];
- [self loadData];
-
- _cancelButton.target = self;
- _cancelButton.action = @selector(cancelPressed);
-
- [self setTitle:NSLocalizedStringFromTable(@"ballot_choose_title", @"Ballot", nil)];
- }
- - (void)loadData {
- NSFetchRequest *fetchRequest = [_entityManager.entityFetcher fetchRequestForEntity:@"Ballot"];
-
- NSArray *sortDescriptors = @[
- [NSSortDescriptor sortDescriptorWithKey:@"modifyDate" ascending:NO],
- [NSSortDescriptor sortDescriptorWithKey:@"createDate" ascending:NO]
- ];
-
- [fetchRequest setSortDescriptors:sortDescriptors];
- fetchRequest.fetchLimit = MAX_BALLOTS;
-
- NSArray *result = [_entityManager.entityFetcher executeFetchRequest:fetchRequest];
- if (result) {
- _ballots = result;
- } else {
- _ballots = [NSArray array];
- }
- }
- #pragma mark - button actions
- - (void) cancelPressed {
- [self.navigationController popViewControllerAnimated:YES];
- }
- #pragma mark - Table view data source
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return [_ballots count];
- }
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- BallotListTableCell *cell = (BallotListTableCell *)[tableView dequeueReusableCellWithIdentifier:BALLOT_LIST_TABLE_CELL_ID forIndexPath:indexPath];
-
- Ballot *ballot = [_ballots objectAtIndex: indexPath.row];
- [cell.nameLabel setText: ballot.title];
-
- NSString *creatorName = [_entityManager.entityFetcher displayNameForContactId:ballot.creatorId];
- [cell.creatorNameLabel setText: creatorName];
-
- NSDate *date;
- if (ballot.modifyDate) {
- date = ballot.modifyDate;
- } else {
- date = ballot.createDate;
- }
-
- [cell.dateLabel setText: [DateFormatter getShortDate:date]];
-
- return cell;
- }
- -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- EntityCreator *entityCreator = [[EntityCreator alloc] initWith:_ballot.managedObjectContext];
-
- Ballot *ballot = [_ballots objectAtIndex: indexPath.row];
-
- _ballot.title = [ballot.title copy];
- _ballot.assessmentType = [ballot.assessmentType copy];
- _ballot.choicesType = [ballot.choicesType copy];
- _ballot.type = [ballot.type copy];
-
- [self clearBallotChoices];
-
- for (BallotChoice *choice in ballot.choices) {
- BallotChoice *ballotChoice = [entityCreator ballotChoice];
- ballotChoice.name = [choice.name copy];
- ballotChoice.orderPosition = [choice.orderPosition copy];
- ballotChoice.ballot = _ballot;
-
- [_ballot addChoicesObject: ballotChoice];
- }
- [self.navigationController popViewControllerAnimated:YES];
- }
- - (void)clearBallotChoices {
- for (BallotChoice *choice in _ballot.choices) {
- [_ballot.managedObjectContext deleteObject: choice];
- }
-
- [_ballot removeChoices:_ballot.choices];
- }
- @end
|