BallotChoice.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "BallotChoice.h"
  21. #import "Ballot.h"
  22. #import "BallotResult.h"
  23. #import "MyIdentityStore.h"
  24. #import "Conversation.h"
  25. #import "Contact.h"
  26. @implementation BallotChoice
  27. @dynamic createDate;
  28. @dynamic id;
  29. @dynamic modifyDate;
  30. @dynamic name;
  31. @dynamic orderPosition;
  32. @dynamic ballot;
  33. @dynamic result;
  34. - (BallotResult *)getOwnResult {
  35. NSString *myId = [MyIdentityStore sharedMyIdentityStore].identity;
  36. return [self getResultForId: myId];
  37. }
  38. - (BallotResult *)getResultForId:(NSString *)contactId {
  39. for (BallotResult *result in self.result) {
  40. if ([result.participantId isEqualToString: contactId]) {
  41. return result;
  42. }
  43. }
  44. return nil;
  45. }
  46. - (void)removeResultForContact:(NSString *)contactId {
  47. NSMutableSet *matchingResults = [NSMutableSet set];
  48. for (BallotResult *result in self.result) {
  49. if ([result.participantId isEqualToString: contactId]) {
  50. [matchingResults addObject: result];
  51. }
  52. }
  53. for (BallotResult *result in matchingResults) {
  54. [self removeResultObject:result];
  55. [self.managedObjectContext deleteObject:result];
  56. }
  57. }
  58. - (NSInteger)totalCountOfResultsTrue {
  59. NSInteger count = 0;
  60. for (BallotResult *result in self.result) {
  61. if (result.boolValue && [self isParticipantGroupMember:result.participantId]) {
  62. count++;
  63. }
  64. }
  65. return count;
  66. }
  67. - (NSSet*)participantIdsForResultsTrue {
  68. NSMutableSet *set = [NSMutableSet set];
  69. for (BallotResult *result in self.result) {
  70. if (result.boolValue && [self isParticipantGroupMember:result.participantId]) {
  71. [set addObject:result.participantId];
  72. }
  73. }
  74. return set;
  75. }
  76. - (NSSet *)getAllParticipantIds {
  77. NSMutableSet *set = [NSMutableSet set];
  78. for (BallotResult *result in self.result) {
  79. if ([self isParticipantGroupMember:result.participantId]) {
  80. [set addObject:result.participantId];
  81. }
  82. }
  83. return set;
  84. }
  85. - (BOOL)isParticipantGroupMember:(NSString *)participantId {
  86. NSString *myId = [MyIdentityStore sharedMyIdentityStore].identity;
  87. if ([myId isEqualToString:participantId]) {
  88. return YES;
  89. }
  90. for (Contact *contact in self.ballot.conversation.participants) {
  91. if ([contact.identity isEqualToString:participantId]) {
  92. return YES;
  93. }
  94. }
  95. return NO;
  96. }
  97. @end