Skip to content

Conversation

@henninghall
Copy link
Owner

@henninghall henninghall commented Dec 12, 2024

Fixes a runtime android error visible on recent version of react native and expo with new architecture enabled.
Fixes #840

@johnf
Copy link

johnf commented Dec 24, 2024

I have made some changes to get this working. Here is a diff, let me know if you want a PR

Main change is that the argument is a std::string not an NSString

NOTE: I don't code in ObjC, got Chat GPT to help me make it compile :) So no doubt there is probably a more correct way to do this. But tested and works for me

# https://github.com/henninghall/react-native-date-picker/pull/873
diff --git a/src/fabric/RNDatePickerNativeComponent.ts b/src/fabric/RNDatePickerNativeComponent.ts
index 8f9d67e5fdcad8e83c75cdaa861bed777d676951..456439e573260a7c795cba7bcb6a2e4c4b9b4ad0 100644
--- a/src/fabric/RNDatePickerNativeComponent.ts
+++ b/src/fabric/RNDatePickerNativeComponent.ts
@@ -13,9 +13,9 @@ type DateEvent = {
 
 export interface NativeProps extends ViewProps {
   locale?: string
-  date: Double
-  maximumDate?: Double
-  minimumDate?: Double
+  date: string
+  maximumDate?: string
+  minimumDate?: string
   minuteInterval?: Int32
   mode?: WithDefault<'date' | 'time' | 'datetime', 'datetime'>
   onChange: BubblingEventHandler<DateEvent>
diff --git a/ios/RNDatePicker.mm b/ios/RNDatePicker.mm
--- a/ios/RNDatePicker.mm
+++ b/ios/RNDatePicker.mm
@@ -48,9 +48,11 @@
 }
 #endif
 
-NSDate* unixMillisToNSDate (double unixMillis) {
-    double time = unixMillis/1000.0;
-    return [NSDate dateWithTimeIntervalSince1970: time];
+NSDate *iso8601StringToNSDate(const std::string &iso8601String) {
+    NSString *nsString = [NSString stringWithUTF8String:iso8601String.c_str()];
+    NSISO8601DateFormatter *isoFormatter = [[NSISO8601DateFormatter alloc] init];
+    NSDate *date = [isoFormatter dateFromString:nsString];
+    return date;
 }
 
 #ifdef RCT_NEW_ARCH_ENABLED
@@ -101,7 +103,7 @@
     
     //  date
     if(oldViewProps.date != newViewProps.date) {
-        [_picker setDate: unixMillisToNSDate(newViewProps.date)];
+        [_picker setDate: iso8601StringToNSDate(newViewProps.date)];
     }
     
     //  locale
@@ -113,12 +115,12 @@
     
     // maximumDate
     if(oldViewProps.maximumDate != newViewProps.maximumDate) {
-        [_picker setMaximumDate: unixMillisToNSDate(newViewProps.maximumDate)];
+        [_picker setMaximumDate: iso8601StringToNSDate(newViewProps.maximumDate)];
     }
     
     //  minimumDate
     if(oldViewProps.minimumDate != newViewProps.minimumDate) {
-        [_picker setMinimumDate: unixMillisToNSDate(newViewProps.minimumDate)];
+        [_picker setMinimumDate: iso8601StringToNSDate(newViewProps.minimumDate)];
     }
     
     //  setMinuteInterval
diff --git a/ios/RNDatePickerManager.mm b/ios/RNDatePickerManager.mm
--- a/ios/RNDatePickerManager.mm
+++ b/ios/RNDatePickerManager.mm
@@ -36,10 +36,10 @@
 }
 
 RCT_EXPORT_VIEW_PROPERTY(text, NSString)
-RCT_EXPORT_VIEW_PROPERTY(date, NSDate)
+RCT_EXPORT_VIEW_PROPERTY(date, NSString)
 RCT_EXPORT_VIEW_PROPERTY(locale, NSLocale)
-RCT_EXPORT_VIEW_PROPERTY(minimumDate, NSDate)
-RCT_EXPORT_VIEW_PROPERTY(maximumDate, NSDate)
+RCT_EXPORT_VIEW_PROPERTY(minimumDate, NSString)
+RCT_EXPORT_VIEW_PROPERTY(maximumDate, NSString)
 RCT_EXPORT_VIEW_PROPERTY(minuteInterval, NSInteger)
 RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
 RCT_REMAP_VIEW_PROPERTY(mode, datePickerMode, UIDatePickerMode)

@kupilikula
Copy link

I am waiting for this PR fix. Would greatly appreciate if someone got this completed and merged!

@henninghall henninghall marked this pull request as ready for review January 26, 2025 15:05
@henninghall henninghall merged commit 316838b into master Jan 26, 2025
6 of 8 checks passed
@henninghall henninghall deleted the whitespace-error branch January 26, 2025 16:36
@henninghall
Copy link
Owner Author

thanks @johnf, this fix is released in v5.0.9

@efstathiosntonas
Copy link

efstathiosntonas commented Jan 28, 2025

hey folks, after updating to 5.0.9 (on Paper) i got plenty of issues with the stacktrace below. The app won't crash, it will just stuck and will never display the picker. Downgrading to 5.0.8 fixes the issue.

The PR name is very misleading, the changes are on iOS side!!

NSInvalidArgumentException: -[__NSCFNumber timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0xba5b476fe86c3bb6

0  CoreFoundation +0x83efc    <redacted>
1  libobjc.A.dylib +0x172b4   _objc_exception_throw
2  CoreFoundation +0x18d478   <redacted>
3  CoreFoundation +0x20f90    <redacted>
4  CoreFoundation +0x208ac    _CF_forwarding_prep_0
5  CoreFoundation +0x62130    <redacted>
6  UIKitCore +0xe8cc60        -[_UIDatePickerMode_DateAndTime _resetBaseDateComponentsIfNecessaryFromDate:]
7  UIKitCore +0xe8cb3c        -[_UIDatePickerMode_DateAndTime loadDate:animated:]
8  UIKitCore +0xe92d6c        -[_UIDatePickerView _loadDateAnimated:]
9  UIKitCore +0xe94464        -[_UIDatePickerView layoutSubviews]
10 UIKitCore +0xe9f544        -[UIPickerView viewForRow:forComponent:]
11 UIKitCore +0xe93ea4        -[_UIDatePickerView _updateEnabledCellsIncludingWMDCells:]
12 UIKitCore +0xe93020        -[_UIDatePickerView _updatedLastSelectedComponentsByValidatingSelectedDateWithLastManipulatedComponent:]
13 CoreFoundation +0x51768    <redacted>
14 CoreFoundation +0x51684    <redacted>
15 CoreFoundation +0x515cc    <redacted>
16 CoreFoundation +0x4fb94    _CFXNotificationPost
17 Foundation +0x31250        <redacted>
18 UIKitCore +0xec14a0        __significantTimeChange
19 CoreFoundation +0x517a4    <redacted>
20 CoreFoundation +0x51684    <redacted>
21 libdispatch.dylib +0x2138  __dispatch_call_block_and_release
22 libdispatch.dylib +0x3dd0  __dispatch_client_callout
23 libdispatch.dylib +0x125a0 __dispatch_main_queue_drain
24 libdispatch.dylib +0x121b4 __dispatch_main_queue_callback_4CF
25 CoreFoundation +0x566ec    <redacted>
26 CoreFoundation +0x538f0    <redacted>
27 CoreFoundation +0x52cb4    CFRunLoopRunSpecific
28 GraphicsServices +0x11a4   _GSEventRunModal
29 UIKitCore +0x40aae4        -[UIApplication _run]
30 UIKitCore +0x4bed94        _UIApplicationMain
31 XXXXX +0x8798               main (main.m:8:12)
32 dyld +0x3d150              <redacted>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error: Exception in HostFunction: Non-whitespace character found after end of conversion: "-07-10T12:51:52.881Z"

5 participants