Skip to content

Commit e0824a4

Browse files
CopilotSaadnajmi
andcommitted
Refactor to share bundler configuration logic between iOS and macOS
Co-authored-by: Saadnajmi <[email protected]>
1 parent 507e6f2 commit e0824a4

File tree

1 file changed

+39
-53
lines changed

1 file changed

+39
-53
lines changed

packages/react-native/React/CoreModules/RCTDevMenu.mm

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ + (void)initialize
127127
{
128128
#if !TARGET_OS_OSX // [macOS]
129129
// We're swizzling here because it's poor form to override methods in a category,
130-
RCTOriginalUIWindowMotionEndedWithEventImp = (MotionEndedWithEventImpType) RCTSwapInstanceMethods([UIWindow class], @selector(motionEnded:withEvent:), @selector(RCT_motionEnded:withEvent:)); // [macOS]
130+
RCTOriginalUIWindowMotionEndedWithEventImp = (MotionEndedWithEventImpType)RCTSwapInstanceMethods(
131+
[UIWindow class], @selector(motionEnded:withEvent:), @selector(RCT_motionEnded:withEvent:)); // [macOS]
131132
#endif // [macOS]
132133
}
133134

@@ -341,6 +342,37 @@ - (void)setDefaultJSBundle
341342
return @"Configure Bundler";
342343
}
343344
handler:^{
345+
// Shared callback for applying bundler configuration
346+
void (^applyBundlerConfig)(NSString *, NSString *, NSString *) =
347+
^(NSString *ipAddress, NSString *port, NSString *bundleRoot) {
348+
// If both IP and port are empty, reset to default
349+
if (ipAddress.length == 0 && port.length == 0) {
350+
[weakSelf setDefaultJSBundle];
351+
return;
352+
}
353+
354+
// Parse and validate port number
355+
NSNumberFormatter *formatter = [NSNumberFormatter new];
356+
formatter.numberStyle = NSNumberFormatterDecimalStyle;
357+
NSNumber *portNumber = [formatter numberFromString:port];
358+
if (portNumber == nil) {
359+
portNumber = [NSNumber numberWithInt:RCT_METRO_PORT];
360+
}
361+
362+
// Set the bundler location
363+
[RCTBundleURLProvider sharedSettings].jsLocation =
364+
[NSString stringWithFormat:@"%@:%d", ipAddress, portNumber.intValue];
365+
366+
if (bundleRoot.length == 0) {
367+
[bundleManager resetBundleURL];
368+
} else {
369+
bundleManager.bundleURL =
370+
[[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:bundleRoot];
371+
}
372+
373+
RCTTriggerReloadCommandListeners(@"Dev menu - apply changes");
374+
};
375+
344376
#if !TARGET_OS_OSX // [macOS]
345377
UIAlertController *alertController = [UIAlertController
346378
alertControllerWithTitle:@"Configure Bundler"
@@ -364,28 +396,8 @@ - (void)setDefaultJSBundle
364396
UITextField *ipTextField = textfields[0];
365397
UITextField *portTextField = textfields[1];
366398
UITextField *bundleRootTextField = textfields[2];
367-
NSString *bundleRoot = bundleRootTextField.text;
368-
if (ipTextField.text.length == 0 && portTextField.text.length == 0) {
369-
[weakSelf setDefaultJSBundle];
370-
return;
371-
}
372-
NSNumberFormatter *formatter = [NSNumberFormatter new];
373-
formatter.numberStyle = NSNumberFormatterDecimalStyle;
374-
NSNumber *portNumber =
375-
[formatter numberFromString:portTextField.text];
376-
if (portNumber == nil) {
377-
portNumber = [NSNumber numberWithInt:RCT_METRO_PORT];
378-
}
379-
[RCTBundleURLProvider sharedSettings].jsLocation = [NSString
380-
stringWithFormat:@"%@:%d", ipTextField.text, portNumber.intValue];
381-
if (bundleRoot.length == 0) {
382-
[bundleManager resetBundleURL];
383-
} else {
384-
bundleManager.bundleURL = [[RCTBundleURLProvider sharedSettings]
385-
jsBundleURLForBundleRoot:bundleRoot];
386-
}
387-
388-
RCTTriggerReloadCommandListeners(@"Dev menu - apply changes");
399+
applyBundlerConfig(
400+
ipTextField.text, portTextField.text, bundleRootTextField.text);
389401
}]];
390402
[alertController addAction:[UIAlertAction actionWithTitle:@"Reset to Default"
391403
style:UIAlertActionStyleDefault
@@ -437,36 +449,10 @@ - (void)setDefaultJSBundle
437449
completionHandler:^(NSModalResponse response) {
438450
if (response == NSAlertFirstButtonReturn) {
439451
// Apply Changes
440-
NSString *ipAddress = ipTextField.stringValue;
441-
NSString *port = portTextField.stringValue;
442-
NSString *bundleRoot = entrypointTextField.stringValue;
443-
444-
// If both IP and port are empty, reset to default
445-
if (ipAddress.length == 0 && port.length == 0) {
446-
[weakSelf setDefaultJSBundle];
447-
return;
448-
}
449-
450-
// Parse and validate port number
451-
NSNumberFormatter *formatter = [NSNumberFormatter new];
452-
formatter.numberStyle = NSNumberFormatterDecimalStyle;
453-
NSNumber *portNumber = [formatter numberFromString:port];
454-
if (portNumber == nil) {
455-
portNumber = [NSNumber numberWithInt:RCT_METRO_PORT];
456-
}
457-
458-
// Set the bundler location - matches iOS behavior
459-
[RCTBundleURLProvider sharedSettings].jsLocation =
460-
[NSString stringWithFormat:@"%@:%d", ipAddress, portNumber.intValue];
461-
462-
if (bundleRoot.length == 0) {
463-
[bundleManager resetBundleURL];
464-
} else {
465-
bundleManager.bundleURL = [[RCTBundleURLProvider sharedSettings]
466-
jsBundleURLForBundleRoot:bundleRoot];
467-
}
468-
469-
RCTTriggerReloadCommandListeners(@"Dev menu - apply changes");
452+
applyBundlerConfig(
453+
ipTextField.stringValue,
454+
portTextField.stringValue,
455+
entrypointTextField.stringValue);
470456
} else if (response == NSAlertSecondButtonReturn) {
471457
// Reset to Default
472458
[weakSelf setDefaultJSBundle];

0 commit comments

Comments
 (0)