BallotMessage.m 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "BallotMessage.h"
  21. #import "Ballot.h"
  22. #import "BallotChoice.h"
  23. #define KEY_BALLOT @"ballot"
  24. @implementation BallotMessage
  25. @dynamic ballotState;
  26. @dynamic ballot;
  27. - (NSString*)format {
  28. NSString *prefix = NSLocalizedStringFromTable(@"ballot", @"Ballot", nil);
  29. NSMutableString *string = [NSMutableString stringWithFormat:@"%@: %@\n", prefix, self.ballot.title];
  30. for (BallotChoice *choice in self.ballot.choicesSortedByOrder) {
  31. [string appendFormat:@"- %@\n", choice.name];
  32. }
  33. return string;
  34. }
  35. - (NSString*)logText {
  36. return [self format];
  37. }
  38. - (NSString*)previewText {
  39. return NSLocalizedStringFromTable(@"ballot", @"Ballot", nil);
  40. }
  41. - (NSString *)quotePreviewText {
  42. return [NSString stringWithFormat:@"%@: %@", NSLocalizedStringFromTable(@"ballot", @"Ballot", nil), self.ballot.title];
  43. }
  44. - (BOOL)isClosed {
  45. return self.ballotState.intValue == kBallotMessageStateCloseBallot;
  46. }
  47. - (void)setBallot:(Ballot *)ballot
  48. {
  49. [self willChangeValueForKey:@"ballot"];
  50. [self setPrimitiveValue:ballot forKey:@"ballot"];
  51. // make sure ballot object is fresh
  52. [ballot.managedObjectContext refreshObject:ballot mergeChanges:YES];
  53. [self updateBallotState];
  54. [self didChangeValueForKey:@"ballot"];
  55. }
  56. - (void)updateBallotState {
  57. NSInteger messageState;
  58. switch (self.ballot.state.integerValue) {
  59. case kBallotStateOpen:
  60. messageState = kBallotMessageStateOpenBallot;
  61. break;
  62. case kBallotStateClosed:
  63. messageState = kBallotMessageStateCloseBallot;
  64. break;
  65. default:
  66. // ignore unexpected state
  67. return;
  68. }
  69. self.ballotState = [NSNumber numberWithInteger:messageState];
  70. }
  71. @end