WFCUSelectNoDisturbingTimeViewController.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // WFCUSelectNoDisturbingTimeViewController.m
  3. // WFChatUIKit
  4. //
  5. // Created by dali on 2020/10/27.
  6. // Copyright © 2020 Wildfirechat. All rights reserved.
  7. //
  8. #import "WFCUSelectNoDisturbingTimeViewController.h"
  9. #import "WFCUConfigManager.h"
  10. @interface WFCUSelectNoDisturbingTimeViewController () <UITableViewDataSource, UITableViewDelegate>
  11. @property (nonatomic, strong)UITableView *tableView;
  12. @end
  13. @implementation WFCUSelectNoDisturbingTimeViewController
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
  17. self.tableView.backgroundColor = [WFCUConfigManager globalManager].backgroudColor;
  18. self.tableView.delegate = self;
  19. self.tableView.dataSource = self;
  20. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  21. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  22. if (@available(iOS 15, *)) {
  23. self.tableView.sectionHeaderTopPadding = 0;
  24. }
  25. [self.view addSubview:self.tableView];
  26. [self.tableView reloadData];
  27. [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
  28. }
  29. - (void)onTimeChanged:(UIDatePicker *)sender {
  30. NSDate *date = sender.date;
  31. NSCalendar *calendar = [NSCalendar currentCalendar];
  32. NSDateComponents *components = [calendar components:(NSCalendarUnitHour | NSCalendarUnitMinute) fromDate:date];
  33. NSInteger hour = [components hour];
  34. NSInteger minute = [components minute];
  35. NSInteger interval = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:[NSDate date]];
  36. if (sender.tag == 0) {
  37. self.startMins = (hour-interval/3600 + 24)%24*60 + minute;
  38. } else {
  39. self.endMins = (hour-interval/3600 + 24)%24*60 + minute;
  40. }
  41. [self.tableView reloadData];
  42. }
  43. - (void)didMoveToParentViewController:(UIViewController*)parent {
  44. [super didMoveToParentViewController:parent];
  45. if (self.onSelectTime) {
  46. self.onSelectTime(self.startMins, self.endMins);
  47. }
  48. }
  49. #pragma mark - UITableViewDataSource
  50. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  51. return 2;
  52. }
  53. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  54. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  55. if (!cell) {
  56. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
  57. UIDatePicker *datePicker = [[UIDatePicker alloc] initWithFrame:CGRectZero];
  58. datePicker.datePickerMode = UIDatePickerModeTime;
  59. datePicker.frame = CGRectMake(self.view.bounds.size.width - 96, 3, 96, 35);
  60. [datePicker addTarget:self action:@selector(onTimeChanged:) forControlEvents:UIControlEventValueChanged];
  61. datePicker.tag = indexPath.row;
  62. [cell.contentView addSubview:datePicker];
  63. }
  64. UIDatePicker *datePicker = nil;
  65. for (UIView *view in cell.contentView.subviews) {
  66. if([view isKindOfClass:[UIDatePicker class]]) {
  67. datePicker = (UIDatePicker *)view;
  68. break;
  69. }
  70. }
  71. if(indexPath.row == 0) {
  72. datePicker.date = [NSDate dateWithTimeIntervalSince1970:self.startMins*60];
  73. } else {
  74. datePicker.date = [NSDate dateWithTimeIntervalSince1970:self.endMins*60];
  75. }
  76. if (indexPath.row == 0) {
  77. cell.textLabel.text = @"From";
  78. } else {
  79. cell.textLabel.text = @"To";
  80. }
  81. return cell;
  82. }
  83. @end