WFCUSwitchTableViewCell.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. //
  2. // SwitchTableViewCell.m
  3. // WildFireChat
  4. //
  5. // Created by heavyrain lee on 27/12/2017.
  6. // Copyright © 2017 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUSwitchTableViewCell.h"
  9. #import "MBProgressHUD.h"
  10. @interface WFCUSwitchTableViewCell()
  11. @property(nonatomic, strong)WFCCConversation *conversation;
  12. @property(nonatomic, strong)UISwitch *valueSwitch;
  13. @end
  14. @implementation WFCUSwitchTableViewCell
  15. - (void)awakeFromNib {
  16. [super awakeFromNib];
  17. // Initialization code
  18. }
  19. - (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier conversation:(WFCCConversation*)conversation {
  20. self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
  21. if(self) {
  22. self.valueSwitch = [[UISwitch alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 56, 8, 40, 40)];
  23. [self.contentView addSubview:self.valueSwitch];
  24. [self.valueSwitch addTarget:self action:@selector(onSwitch:) forControlEvents:UIControlEventValueChanged];
  25. self.type = SwitchType_Conversation_None;
  26. self.conversation = conversation;
  27. }
  28. return self;
  29. }
  30. - (void)onSwitch:(id)sender {
  31. BOOL value = _valueSwitch.on;
  32. __weak typeof(self)ws = self;
  33. switch (_type) {
  34. case SwitchType_Conversation_Top:
  35. {
  36. [[WFCCIMService sharedWFCIMService] setConversation:_conversation top:value?1:0 success:nil error:^(int error_code) {
  37. [ws.valueSwitch setOn:!value];
  38. }];
  39. break;
  40. }
  41. case SwitchType_Conversation_Silent:
  42. {
  43. [[WFCCIMService sharedWFCIMService] setConversation:_conversation silent:value success:nil error:^(int error_code) {
  44. [ws.valueSwitch setOn:!value];
  45. }];
  46. break;
  47. }
  48. case SwitchType_Setting_Global_Silent:
  49. {
  50. [[WFCCIMService sharedWFCIMService] setGlobalSilent:!value success:^{
  51. } error:^(int error_code) {
  52. }];
  53. break;
  54. }
  55. case SwitchType_Setting_Show_Notification_Detail: {
  56. [[WFCCIMService sharedWFCIMService] setHiddenNotificationDetail:!value success:^{
  57. } error:^(int error_code) {
  58. dispatch_async(dispatch_get_main_queue(), ^{
  59. ws.valueSwitch.on = !ws.valueSwitch.on;
  60. });
  61. }];
  62. break;
  63. }
  64. case SwitchType_Conversation_Show_Alias: {
  65. [[WFCCIMService sharedWFCIMService] setHiddenGroupMemberName:!value group:self.conversation.target success:^{
  66. } error:^(int error_code) {
  67. }];
  68. }
  69. break;
  70. case SwitchType_Conversation_Save_To_Contact:
  71. [[WFCCIMService sharedWFCIMService] setFavGroup:self.conversation.target fav:value success:^{
  72. } error:^(int error_code) {
  73. }];
  74. break;
  75. case SwitchType_Setting_Sync_Draft:
  76. [[WFCCIMService sharedWFCIMService] setEnableSyncDraft:value success:^{
  77. } error:^(int error_code) {
  78. }];
  79. break;
  80. break;
  81. case SwitchType_Setting_Voip_Silent:
  82. [[WFCCIMService sharedWFCIMService] setVoipNotificationSilent:!value success:^{
  83. } error:^(int error_code) {
  84. }];
  85. default:
  86. break;
  87. }
  88. }
  89. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  90. [super setSelected:selected animated:animated];
  91. // Configure the view for the selected state
  92. }
  93. - (void)updateView {
  94. BOOL value = false;
  95. switch (_type) {
  96. case SwitchType_Conversation_Top:
  97. value = [[WFCCIMService sharedWFCIMService] getConversationInfo:_conversation].isTop>0;
  98. break;
  99. case SwitchType_Conversation_Silent:
  100. value = [[WFCCIMService sharedWFCIMService] getConversationInfo:_conversation].isSilent;
  101. break;
  102. case SwitchType_Setting_Global_Silent: {
  103. value = ![[WFCCIMService sharedWFCIMService] isGlobalSilent];
  104. break;
  105. }
  106. case SwitchType_Setting_Show_Notification_Detail: {
  107. value = ![[WFCCIMService sharedWFCIMService] isHiddenNotificationDetail];
  108. break;
  109. }
  110. case SwitchType_Setting_Sync_Draft: {
  111. value = [[WFCCIMService sharedWFCIMService] isEnableSyncDraft];
  112. break;
  113. }
  114. case SwitchType_Conversation_Show_Alias: {
  115. value = ![[WFCCIMService sharedWFCIMService] isHiddenGroupMemberName:_conversation.target];
  116. break;
  117. }
  118. case SwitchType_Conversation_Save_To_Contact:{
  119. value = [[WFCCIMService sharedWFCIMService] isFavGroup:self.conversation.target];
  120. break;
  121. }
  122. case SwitchType_Setting_Voip_Silent: {
  123. value = ![[WFCCIMService sharedWFCIMService] isVoipNotificationSilent];
  124. break;
  125. }
  126. default:
  127. break;
  128. }
  129. [self.valueSwitch setOn:value];
  130. }
  131. - (void)setType:(SwitchType)type {
  132. _type = type;
  133. if (_conversation || type == SwitchType_Setting_Global_Silent || type == SwitchType_Setting_Show_Notification_Detail || type == SwitchType_Setting_Sync_Draft || type == SwitchType_Setting_Voip_Silent) {
  134. [self updateView];
  135. }
  136. }
  137. @end