WFCUSwitchTableViewCell.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 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 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] setGlobalSlient: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. default:
  76. break;
  77. }
  78. }
  79. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
  80. [super setSelected:selected animated:animated];
  81. // Configure the view for the selected state
  82. }
  83. - (void)updateView {
  84. BOOL value = false;
  85. switch (_type) {
  86. case SwitchType_Conversation_Top:
  87. value = [[WFCCIMService sharedWFCIMService] getConversationInfo:_conversation].isTop;
  88. break;
  89. case SwitchType_Conversation_Silent:
  90. value = [[WFCCIMService sharedWFCIMService] getConversationInfo:_conversation].isSilent;
  91. break;
  92. case SwitchType_Setting_Global_Silent: {
  93. value = [[WFCCIMService sharedWFCIMService] isGlobalSlient];
  94. break;
  95. }
  96. case SwitchType_Setting_Show_Notification_Detail: {
  97. value = ![[WFCCIMService sharedWFCIMService] isHiddenNotificationDetail];
  98. break;
  99. }
  100. case SwitchType_Conversation_Show_Alias: {
  101. value = ![[WFCCIMService sharedWFCIMService] isHiddenGroupMemberName:_conversation.target];
  102. break;
  103. }
  104. case SwitchType_Conversation_Save_To_Contact:{
  105. value = [[WFCCIMService sharedWFCIMService] isFavGroup:self.conversation.target];
  106. break;
  107. }
  108. default:
  109. break;
  110. }
  111. [self.valueSwitch setOn:value];
  112. }
  113. - (void)setType:(SwitchType)type {
  114. _type = type;
  115. if (_conversation) {
  116. [self updateView];
  117. }
  118. }
  119. @end