1/**
2 * Copyright (c) Facebook, Inc. and its affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#import "RNDateTimePicker.h"
9
10#import <React/RCTUtils.h>
11#import <React/UIView+React.h>
12
13@interface RNDateTimePicker ()
14
15@property (nonatomic, copy) RCTBubblingEventBlock onChange;
16@property (nonatomic, copy) RCTBubblingEventBlock onPickerDismiss;
17@property (nonatomic, assign) NSInteger reactMinuteInterval;
18
19@end
20
21@implementation RNDateTimePicker
22
23- (instancetype)initWithFrame:(CGRect)frame
24{
25  if ((self = [super initWithFrame:frame])) {
26    #ifndef RCT_NEW_ARCH_ENABLED
27      // somehow, with Fabric, the callbacks are executed here as well as in RNDateTimePickerComponentView
28      // so do not register it with Fabric, to avoid potential problems
29      [self addTarget:self action:@selector(didChange)
30               forControlEvents:UIControlEventValueChanged];
31      [self addTarget:self action:@selector(onDismiss:) forControlEvents:UIControlEventEditingDidEnd];
32    #endif
33
34    _reactMinuteInterval = 1;
35  }
36  return self;
37}
38
39RCT_NOT_IMPLEMENTED(- (instancetype)initWithCoder:(NSCoder *)aDecoder)
40
41- (void)didChange
42{
43  if (_onChange) {
44    _onChange(@{ @"timestamp": @(self.date.timeIntervalSince1970 * 1000.0) });
45  }
46}
47
48- (void)onDismiss:(RNDateTimePicker *)sender
49{
50  if (_onPickerDismiss) {
51    _onPickerDismiss(@{});
52  }
53}
54
55- (void)setDatePickerMode:(UIDatePickerMode)datePickerMode
56{
57  [super setDatePickerMode:datePickerMode];
58  // We need to set minuteInterval after setting datePickerMode, otherwise minuteInterval is invalid in time mode.
59  self.minuteInterval = _reactMinuteInterval;
60}
61
62- (void)setMinuteInterval:(NSInteger)minuteInterval
63{
64  [super setMinuteInterval:minuteInterval];
65  _reactMinuteInterval = minuteInterval;
66}
67
68- (void)setDate:(NSDate *)date {
69    // Need to avoid the case where values coming back through the bridge trigger a new valueChanged event
70    if (![self.date isEqualToDate:date]) {
71        [super setDate:date animated:NO];
72    }
73}
74
75@end
76