TriggerSelectPopupView.m 5.2 KB

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