QuizSetViewController.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // QuizSetViewController.m
  3. // OneCable
  4. //
  5. // Created by nComz on 2017. 5. 19..
  6. // Copyright © 2017년 ntels. All rights reserved.
  7. //
  8. #import "QuizSetViewController.h"
  9. #import "RequestHandler.h"
  10. #import "CustomButton.h"
  11. #import "CustomTextField.h"
  12. #import "CustomDatePopupView.h"
  13. #import "ChangeEmailPopupView.h"
  14. #import "CommonUtil.h"
  15. #import "ImageUtil.h"
  16. @interface QuizSetViewController () <CustomTextFieldDelegate>{
  17. DataSelectListModel *_quizList;
  18. DataSelectModel *_selectedQuiz;
  19. CustomDatePopupView *_popDate;
  20. NSString *answer;
  21. }
  22. @end
  23. @implementation QuizSetViewController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. [self initUI];
  27. }
  28. - (void)viewWillAppear:(BOOL)animated {
  29. [super viewWillAppear:animated];
  30. [self requestInviteQuizList];
  31. }
  32. - (void)initUI{
  33. answer = @"";
  34. _btnConfirm.enabled = NO;
  35. [self showDirectInputArea:NO];
  36. _txtQuizSelect.delegate = self;
  37. _txtSelfQuiz.delegate = self;
  38. _txtQuizSelect.keyboardType = UIKeyboardTypeDefault;
  39. _txtQuizSelect.returnKeyType = UIReturnKeyDone;
  40. _txtQuizSelect.placeholder = @"퀴즈 선택";
  41. // _txtQuizSelect.customTextFieldSuperview = CustomTextFieldSuperviewIsContentView;
  42. }
  43. -(void)showDirectInputArea:(BOOL)isShow
  44. {
  45. _viewSelfQuiz.hidden = !isShow;
  46. if(_viewSelfQuiz.isHidden){
  47. _viewHeight.constant = 0;
  48. } else{
  49. _viewHeight.constant = 224.0f;
  50. }
  51. }
  52. - (void)updateUI {
  53. _btnConfirm.enabled = [self isValidRequestInfo];
  54. if (_selectedQuiz != nil) {
  55. QuizModel *info = (QuizModel *)_selectedQuiz.value;
  56. [self showDirectInputArea:[info isUserEditQuiz]];
  57. _txtQuizSelect.text = _selectedQuiz.title;
  58. } else {
  59. [self showDirectInputArea:NO];
  60. }
  61. }
  62. -(void)showQuizList{
  63. [[JDFacade facade] selectDatas:self listInfo:_quizList completion:^(DataSelectModel *seleced) {
  64. _selectedQuiz = seleced;
  65. _txtQuizSelect.text = _selectedQuiz.title;
  66. [self updateUI];
  67. }];
  68. }
  69. - (void)requestQuizUpdate{
  70. QuizModel *quizInfo = (QuizModel *)_selectedQuiz.value;
  71. NSLog(@"Quiz Info : %@", quizInfo);
  72. NSString *quiz = quizInfo.quiz;
  73. if (quizInfo.isUserEditQuiz) {
  74. quiz = [_txtSelfQuiz.text trim];
  75. }
  76. //parameters
  77. NSDictionary *parameter = @{@"type": @"quiz" ,
  78. @"quiz": quiz,
  79. @"quiz_id":quizInfo.quizId,
  80. @"status_code":quizInfo.statusCode,
  81. @"answer":answer};
  82. NSString *path = [[JDFacade facade] getUrlWithCustGroupIDAndMemberID:API_PUT_MEMBER_UPDATE];
  83. [[RequestHandler handler] sendAsyncPutRequestAPIPath:path parameters:parameter modelClass:[JDJSONModel class] completion:^(id responseObject) {
  84. [JDFacade facade].loginUser.quiz = quiz;
  85. [self dismissViewControllerAnimated:YES completion:nil];
  86. [self.delegate dismissQuizView];
  87. } failure:^(id errorObject) {
  88. JDErrorModel *error = (JDErrorModel *)errorObject;
  89. [[JDFacade facade] alert:error.errorMessage];
  90. }];
  91. }
  92. - (void)requestInviteQuizList {
  93. if (![JDFacade facade].loginUser.hasHomeHub) {
  94. return;
  95. }
  96. NSString *path = [NSString stringWithFormat:API_POST_HOMEGROUP_INVITE_QUIZ];
  97. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:nil modelClass:[QuizListModel class] completion:^(id responseObject) {
  98. QuizListModel *quizList = (QuizListModel *)responseObject;
  99. //NSLog(@"Response : %@", responseObject);
  100. _quizList = [[DataSelectListModel alloc] init];
  101. _quizList.title = @"퀴즈 선택";
  102. for (QuizModel *info in quizList.quizList) {
  103. DataSelectModel *data = [[DataSelectModel alloc] init];
  104. data.title = info.quiz;
  105. data.value = info;
  106. [_quizList.list addObject:data];
  107. }
  108. } failure:^(id errorObject) {
  109. JDErrorModel *error = (JDErrorModel *)errorObject;
  110. [[JDFacade facade] alert:error.errorMessage];
  111. }];
  112. }
  113. -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  114. if ([textField isEqual:_txtQuizSelect]) {
  115. [self showQuizList];
  116. return NO;
  117. }
  118. return YES;
  119. }
  120. - (void)textFieldDidEndEditing:(UITextField *)textField{
  121. if (_txtSelfQuiz.text.length > 0) {
  122. UIImage *active = [ImageUtil stretchedImage:[UIImage imageNamed:@"img_input_round_bg_active"] expectSize:CGSizeMake(_txtSelfQuiz.frame.size.width, _txtSelfQuiz.frame.size.height)];
  123. [_txtSelfQuiz setBackground:active];
  124. _btnConfirm.enabled = YES;
  125. } else {
  126. _btnConfirm.enabled = NO;
  127. }
  128. }
  129. - (BOOL)isValidRequestInfo {
  130. BOOL result = YES;
  131. QuizModel *quizInfo = (_selectedQuiz != nil) ? (QuizModel *)_selectedQuiz.value : nil;
  132. if (result && quizInfo == nil)
  133. {
  134. result = NO;
  135. }
  136. if (result && quizInfo.isUserEditQuiz)
  137. {
  138. if ([[_txtSelfQuiz.text trim] isEmptyString])
  139. {
  140. result = NO;
  141. }
  142. }
  143. return result;
  144. }
  145. - (IBAction)btnInputQuizTouched:(id)sender {
  146. if (_popDate == nil) {
  147. _popDate = [[CustomDatePopupView alloc] initWithTitle:@"정답 날짜 입력"];
  148. }
  149. [_popDate showWithCompletion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  150. if (buttonIndex == 0) {//OK
  151. NSLog(@"Input Date : %@%@%@", _popDate.txtYear.text, _popDate.txtMonth.text, _popDate.txtDay.text);
  152. NSString *date = [NSString stringWithFormat:@"%@%@%@",_popDate.txtYear.text,_popDate.txtMonth.text,_popDate.txtDay.text];
  153. answer = date;
  154. if ([date isEqualToString:@""]) {
  155. [_btnInputQuizAnswer setTitle:[CommonUtil currentDate2] forState:UIControlStateNormal];
  156. answer = [CommonUtil currentDate3];
  157. } else {
  158. NSString *date = [NSString stringWithFormat:@"%@ / %@ / %@",_popDate.txtYear.text,_popDate.txtMonth.text,_popDate.txtDay.text];
  159. [_btnInputQuizAnswer setTitle:date forState:UIControlStateNormal];
  160. }
  161. [_btnInputQuizAnswer setBackgroundImage:[UIImage imageNamed:@"img_input_round_bg_active"] forState:UIControlStateNormal];
  162. [_btnInputQuizAnswer setTitleColor:kUITextColor01 forState:UIControlStateNormal];
  163. }
  164. }];
  165. }
  166. - (IBAction)btnCancelTouched:(id)sender {
  167. [self dismissViewControllerAnimated:YES completion:nil];
  168. }
  169. - (IBAction)btnConfirmTouched:(id)sender {
  170. [self requestQuizUpdate];
  171. }
  172. - (void)didReceiveMemoryWarning {
  173. [super didReceiveMemoryWarning];
  174. }
  175. @end