TriggerSelectPopupView.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // TriggerSelectPopupView.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 11/23/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "CustomLabel.h"
  10. #import "CustomButton.h"
  11. #import "TriggerSelectPopupView.h"
  12. #import "DeviceSelectPopupView.h"
  13. #import "DeviceNodePopupView.h"
  14. #import "TimePickerPopupView.h"
  15. #import "DaylightPopupView.h"
  16. #import "ExternHeatPopupView.h"
  17. @interface TriggerSelectPopupView () {
  18. NSMutableArray<ItemModel> *_checkedItems;
  19. }
  20. @end
  21. @implementation TriggerSelectPopupView
  22. - (id)initFromNib {
  23. for (UIView *view in [CommonUtil nibViews:@"TriggerSelectPopupView"]) {
  24. if ([view isKindOfClass:[TriggerSelectPopupView class]]) {
  25. self = (TriggerSelectPopupView *)view;
  26. //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
  27. self.frame = [UIScreen mainScreen].bounds;
  28. [_lblTimer addTouchEventHandler:^(id label) {
  29. [self btnTimerTouched:nil];
  30. }];
  31. [_lblDaylight addTouchEventHandler:^(id label) {
  32. [self btnDaylightTouched:nil];
  33. }];
  34. [_lblHeat addTouchEventHandler:^(id label) {
  35. [self btnHeatTouched:nil];
  36. }];
  37. [_lblDevice addTouchEventHandler:^(id label) {
  38. [self btnDeviceTouched:nil];
  39. }];
  40. //Localization
  41. [self.btnConfirm setTitle:NSLocalizedString(@"확인", @"확인") forState:UIControlStateNormal];
  42. [self.btnCancel setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  43. }
  44. }
  45. return self;
  46. }
  47. - (void)didMoveToSuperview {
  48. }
  49. #pragma mark - Main Logic
  50. #pragma mark - UI Events
  51. - (IBAction)btnTimerTouched:(id)sender {
  52. ItemModel *trigger = _refTriggers && _refTriggers.count ? _refTriggers[0] : nil;
  53. BOOL isNewTrigger = !trigger.timers || !trigger.timers.count;
  54. ItemModel *timeTrigger = !isNewTrigger ? trigger : [[ItemModel alloc] init];
  55. TimePickerPopupView *tpopup = [[TimePickerPopupView alloc] initFromNib];
  56. tpopup.timeTrigger = timeTrigger;
  57. tpopup.refConditions = _refConditions;
  58. [tpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  59. if (buttonIndex == 0) {//OK
  60. if (isNewTrigger) {
  61. [_refTriggers addObject:timeTrigger];
  62. [self btnConfirmTouched:nil];
  63. }
  64. }
  65. }];
  66. }
  67. - (IBAction)btnDaylightTouched:(id)sender {
  68. ItemModel *trigger = _refTriggers && _refTriggers.count ? _refTriggers[0] : nil;
  69. BOOL isNewTrigger = !trigger.daylights || !trigger.daylights.count;
  70. ItemModel *daylightTrigger = !isNewTrigger ? trigger : [[ItemModel alloc] init];
  71. DaylightPopupView *dpopup = [[DaylightPopupView alloc] initFromNib];
  72. dpopup.daylightTrigger = daylightTrigger;
  73. dpopup.refConditions = _refConditions;
  74. [dpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  75. if (buttonIndex == 0) {//OK
  76. if (isNewTrigger) {
  77. [_refTriggers addObject:daylightTrigger];
  78. [self btnConfirmTouched:nil];
  79. }
  80. }
  81. }];
  82. }
  83. - (IBAction)btnHeatTouched:(id)sender {
  84. ItemModel *trigger = _refTriggers && _refTriggers.count ? _refTriggers[0] : nil;
  85. BOOL isNewTrigger = !trigger.heats || !trigger.heats.count;
  86. ItemModel *heatTrigger = !isNewTrigger ? trigger : [[ItemModel alloc] init];
  87. ExternHeatPopupView *epopup = [[ExternHeatPopupView alloc] initFromNib];
  88. epopup.externHeatTrigger = heatTrigger;
  89. epopup.refConditions = _refConditions;
  90. [epopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  91. if (buttonIndex == 0) {//OK
  92. if (isNewTrigger) {
  93. [_refTriggers addObject:heatTrigger];
  94. [self btnConfirmTouched:nil];
  95. }
  96. }
  97. }];
  98. }
  99. - (IBAction)btnDeviceTouched:(id)sender {
  100. DeviceSelectPopupView *dpopup = [[DeviceSelectPopupView alloc] initFromNib];
  101. dpopup.refDevices = _refDevices;
  102. dpopup.typeCode = ksItemTypeCodeTrigger;
  103. [dpopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  104. if (buttonIndex == 0) {//ok
  105. DeviceNodePopupView *npopup = [[DeviceNodePopupView alloc] initFromNib];
  106. npopup.refDevice = dpopup.selectedDevices[0];
  107. npopup.typeCode = ksItemTypeCodeTrigger;
  108. [npopup showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  109. //action add
  110. if (buttonIndex == 0) {//OK
  111. npopup.refDevice.itemName = @"장치 상태가 바뀔 때";
  112. npopup.refDevice.itemSubTypeCode = ksItemSubTypeCodeDevice; //트리거 타입 설정
  113. [_refTriggers addObject:npopup.refDevice];
  114. [self btnConfirmTouched:nil];
  115. }
  116. }];
  117. //디바이스 트리거인 경우, 요일 반복 제거,
  118. [_refConditions enumerateObjectsUsingBlock:^(ItemModel *pCondition, NSUInteger idx, BOOL * _Nonnull stop) {
  119. if ([pCondition.itemSubTypeCode isEqualToString:ksConditionSubTypeCodeDaysOfWeek]) {
  120. [_refConditions removeObject:pCondition];
  121. *stop = YES;
  122. }
  123. }];
  124. }
  125. }];
  126. }
  127. - (IBAction)btnConfirmTouched:(id)sender {
  128. //validate
  129. [super btnConfirmTouched:sender];
  130. }
  131. @end