Skip to content

Commit

Permalink
refactor(iOS): cleanup conversion code (#2499)
Browse files Browse the repository at this point in the history
## Description

I've recently watched a [talk by Jonathan
Müller](https://youtu.be/zGWj7Qo_POY?si=XY-llWKz3esbcGOl) from
`CppOnSea`
and it was brought to my attention that there is `using enum` syntax in
C++ (since C++20), which we can use
to make our conversion code much more readable.

## Changes

Refactored RNSConvert.mm

## Test code and steps to reproduce

Everything should work as before.

## Checklist

- [x] Ensured that CI passes
  • Loading branch information
kkafar authored Nov 14, 2024
1 parent a1dbae1 commit f489762
Showing 1 changed file with 82 additions and 68 deletions.
150 changes: 82 additions & 68 deletions ios/RNSConvert.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ + (UISemanticContentAttribute)UISemanticContentAttributeFromCppEquivalent:
(react::RNSScreenStackHeaderConfigDirection)direction
{
switch (direction) {
case react::RNSScreenStackHeaderConfigDirection::Rtl:
using enum react::RNSScreenStackHeaderConfigDirection;

case Rtl:
return UISemanticContentAttributeForceRightToLeft;
case react::RNSScreenStackHeaderConfigDirection::Ltr:
case Ltr:
return UISemanticContentAttributeForceLeftToRight;
}
}
Expand All @@ -22,11 +24,13 @@ + (UINavigationItemBackButtonDisplayMode)UINavigationItemBackButtonDisplayModeFr
(react::RNSScreenStackHeaderConfigBackButtonDisplayMode)backButtonDisplayMode
{
switch (backButtonDisplayMode) {
case react::RNSScreenStackHeaderConfigBackButtonDisplayMode::Default:
using enum react::RNSScreenStackHeaderConfigBackButtonDisplayMode;

case Default:
return UINavigationItemBackButtonDisplayModeDefault;
case react::RNSScreenStackHeaderConfigBackButtonDisplayMode::Generic:
case Generic:
return UINavigationItemBackButtonDisplayModeGeneric;
case react::RNSScreenStackHeaderConfigBackButtonDisplayMode::Minimal:
case Minimal:
return UINavigationItemBackButtonDisplayModeMinimal;
}
}
Expand All @@ -35,46 +39,49 @@ + (RNSScreenStackPresentation)RNSScreenStackPresentationFromCppEquivalent:
(react::RNSScreenStackPresentation)stackPresentation
{
switch (stackPresentation) {
case react::RNSScreenStackPresentation::Push:
using enum react::RNSScreenStackPresentation;

case Push:
return RNSScreenStackPresentationPush;
case react::RNSScreenStackPresentation::Modal:
case Modal:
return RNSScreenStackPresentationModal;
case react::RNSScreenStackPresentation::FullScreenModal:
case FullScreenModal:
return RNSScreenStackPresentationFullScreenModal;
case react::RNSScreenStackPresentation::FormSheet:
case FormSheet:
return RNSScreenStackPresentationFormSheet;
case react::RNSScreenStackPresentation::ContainedModal:
case ContainedModal:
return RNSScreenStackPresentationContainedModal;
case react::RNSScreenStackPresentation::TransparentModal:
case TransparentModal:
return RNSScreenStackPresentationTransparentModal;
case react::RNSScreenStackPresentation::ContainedTransparentModal:
case ContainedTransparentModal:
return RNSScreenStackPresentationContainedTransparentModal;
}
}

+ (RNSScreenStackAnimation)RNSScreenStackAnimationFromCppEquivalent:(react::RNSScreenStackAnimation)stackAnimation
{
switch (stackAnimation) {
// these four are intentionally grouped
case react::RNSScreenStackAnimation::Slide_from_right:
case react::RNSScreenStackAnimation::Ios_from_right:
case react::RNSScreenStackAnimation::Default:
using enum react::RNSScreenStackAnimation;
// these three are intentionally grouped
case Slide_from_right:
case Ios_from_right:
case Default:
return RNSScreenStackAnimationDefault;
// these two are intentionally grouped
case react::RNSScreenStackAnimation::Slide_from_left:
case react::RNSScreenStackAnimation::Ios_from_left:
case Slide_from_left:
case Ios_from_left:
return RNSScreenStackAnimationSlideFromLeft;
case react::RNSScreenStackAnimation::Flip:
case Flip:
return RNSScreenStackAnimationFlip;
case react::RNSScreenStackAnimation::Simple_push:
case Simple_push:
return RNSScreenStackAnimationSimplePush;
case react::RNSScreenStackAnimation::None:
case None:
return RNSScreenStackAnimationNone;
case react::RNSScreenStackAnimation::Fade:
case Fade:
return RNSScreenStackAnimationFade;
case react::RNSScreenStackAnimation::Slide_from_bottom:
case Slide_from_bottom:
return RNSScreenStackAnimationSlideFromBottom;
case react::RNSScreenStackAnimation::Fade_from_bottom:
case Fade_from_bottom:
return RNSScreenStackAnimationFadeFromBottom;
}
}
Expand All @@ -83,17 +90,19 @@ + (RNSScreenStackHeaderSubviewType)RNSScreenStackHeaderSubviewTypeFromCppEquival
(react::RNSScreenStackHeaderSubviewType)subviewType
{
switch (subviewType) {
case react::RNSScreenStackHeaderSubviewType::Left:
using enum react::RNSScreenStackHeaderSubviewType;

case Left:
return RNSScreenStackHeaderSubviewTypeLeft;
case react::RNSScreenStackHeaderSubviewType::Right:
case Right:
return RNSScreenStackHeaderSubviewTypeRight;
case react::RNSScreenStackHeaderSubviewType::Title:
case Title:
return RNSScreenStackHeaderSubviewTypeTitle;
case react::RNSScreenStackHeaderSubviewType::Center:
case Center:
return RNSScreenStackHeaderSubviewTypeCenter;
case react::RNSScreenStackHeaderSubviewType::SearchBar:
case SearchBar:
return RNSScreenStackHeaderSubviewTypeSearchBar;
case react::RNSScreenStackHeaderSubviewType::Back:
case Back:
return RNSScreenStackHeaderSubviewTypeBackButton;
}
}
Expand All @@ -102,19 +111,21 @@ + (RNSScreenReplaceAnimation)RNSScreenReplaceAnimationFromCppEquivalent:
(react::RNSScreenReplaceAnimation)replaceAnimation
{
switch (replaceAnimation) {
case react::RNSScreenReplaceAnimation::Pop:
using enum react::RNSScreenReplaceAnimation;
case Pop:
return RNSScreenReplaceAnimationPop;
case react::RNSScreenReplaceAnimation::Push:
case Push:
return RNSScreenReplaceAnimationPush;
}
}

+ (RNSScreenSwipeDirection)RNSScreenSwipeDirectionFromCppEquivalent:(react::RNSScreenSwipeDirection)swipeDirection
{
switch (swipeDirection) {
case react::RNSScreenSwipeDirection::Horizontal:
using enum react::RNSScreenSwipeDirection;
case Horizontal:
return RNSScreenSwipeDirectionHorizontal;
case react::RNSScreenSwipeDirection::Vertical:
case Vertical:
return RNSScreenSwipeDirectionVertical;
}
}
Expand Down Expand Up @@ -144,13 +155,14 @@ + (UITextAutocapitalizationType)UITextAutocapitalizationTypeFromCppEquivalent:
(react::RNSSearchBarAutoCapitalize)autoCapitalize
{
switch (autoCapitalize) {
case react::RNSSearchBarAutoCapitalize::Words:
using enum react::RNSSearchBarAutoCapitalize;
case Words:
return UITextAutocapitalizationTypeWords;
case react::RNSSearchBarAutoCapitalize::Sentences:
case Sentences:
return UITextAutocapitalizationTypeSentences;
case react::RNSSearchBarAutoCapitalize::Characters:
case Characters:
return UITextAutocapitalizationTypeAllCharacters;
case react::RNSSearchBarAutoCapitalize::None:
case None:
return UITextAutocapitalizationTypeNone;
}
}
Expand All @@ -159,11 +171,12 @@ + (UITextAutocapitalizationType)UITextAutocapitalizationTypeFromCppEquivalent:
+ (RNSSearchBarPlacement)RNSScreenSearchBarPlacementFromCppEquivalent:(react::RNSSearchBarPlacement)placement
{
switch (placement) {
case react::RNSSearchBarPlacement::Stacked:
using enum react::RNSSearchBarPlacement;
case Stacked:
return RNSSearchBarPlacementStacked;
case react::RNSSearchBarPlacement::Automatic:
case Automatic:
return RNSSearchBarPlacementAutomatic;
case react::RNSSearchBarPlacement::Inline:
case Inline:
return RNSSearchBarPlacementInline;
}
}
Expand All @@ -179,68 +192,69 @@ + (RNSSearchBarPlacement)RNSScreenSearchBarPlacementFromCppEquivalent:(react::RN

+ (RNSBlurEffectStyle)RNSBlurEffectStyleFromCppEquivalent:(react::RNSScreenStackHeaderConfigBlurEffect)blurEffect
{
using enum react::RNSScreenStackHeaderConfigBlurEffect;
#if !TARGET_OS_TV && defined(__IPHONE_OS_VERSION_MAX_ALLOWED) && defined(__IPHONE_13_0) && \
__IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
if (@available(iOS 13.0, *)) {
switch (blurEffect) {
case react::RNSScreenStackHeaderConfigBlurEffect::None:
case None:
return RNSBlurEffectStyleNone;
case react::RNSScreenStackHeaderConfigBlurEffect::ExtraLight:
case ExtraLight:
return RNSBlurEffectStyleExtraLight;
case react::RNSScreenStackHeaderConfigBlurEffect::Light:
case Light:
return RNSBlurEffectStyleLight;
case react::RNSScreenStackHeaderConfigBlurEffect::Dark:
case Dark:
return RNSBlurEffectStyleDark;
case react::RNSScreenStackHeaderConfigBlurEffect::Regular:
case Regular:
return RNSBlurEffectStyleRegular;
case react::RNSScreenStackHeaderConfigBlurEffect::Prominent:
case Prominent:
return RNSBlurEffectStyleProminent;
case react::RNSScreenStackHeaderConfigBlurEffect::SystemUltraThinMaterial:
case SystemUltraThinMaterial:
return RNSBlurEffectStyleSystemUltraThinMaterial;
case react::RNSScreenStackHeaderConfigBlurEffect::SystemThinMaterial:
case SystemThinMaterial:
return RNSBlurEffectStyleSystemThinMaterial;
case react::RNSScreenStackHeaderConfigBlurEffect::SystemMaterial:
case SystemMaterial:
return RNSBlurEffectStyleSystemMaterial;
case react::RNSScreenStackHeaderConfigBlurEffect::SystemThickMaterial:
case SystemThickMaterial:
return RNSBlurEffectStyleSystemThickMaterial;
case react::RNSScreenStackHeaderConfigBlurEffect::SystemChromeMaterial:
case SystemChromeMaterial:
return RNSBlurEffectStyleSystemChromeMaterial;
case react::RNSScreenStackHeaderConfigBlurEffect::SystemUltraThinMaterialLight:
case SystemUltraThinMaterialLight:
return RNSBlurEffectStyleSystemUltraThinMaterialLight;
case react::RNSScreenStackHeaderConfigBlurEffect::SystemThinMaterialLight:
case SystemThinMaterialLight:
return RNSBlurEffectStyleSystemThinMaterialLight;
case react::RNSScreenStackHeaderConfigBlurEffect::SystemMaterialLight:
case SystemMaterialLight:
return RNSBlurEffectStyleSystemMaterialLight;
case react::RNSScreenStackHeaderConfigBlurEffect::SystemThickMaterialLight:
case SystemThickMaterialLight:
return RNSBlurEffectStyleSystemThickMaterialLight;
case react::RNSScreenStackHeaderConfigBlurEffect::SystemChromeMaterialLight:
case SystemChromeMaterialLight:
return RNSBlurEffectStyleSystemChromeMaterialLight;
case react::RNSScreenStackHeaderConfigBlurEffect::SystemUltraThinMaterialDark:
case SystemUltraThinMaterialDark:
return RNSBlurEffectStyleSystemUltraThinMaterialDark;
case react::RNSScreenStackHeaderConfigBlurEffect::SystemThinMaterialDark:
case SystemThinMaterialDark:
return RNSBlurEffectStyleSystemThinMaterialDark;
case react::RNSScreenStackHeaderConfigBlurEffect::SystemMaterialDark:
case SystemMaterialDark:
return RNSBlurEffectStyleSystemMaterialDark;
case react::RNSScreenStackHeaderConfigBlurEffect::SystemThickMaterialDark:
case SystemThickMaterialDark:
return RNSBlurEffectStyleSystemThickMaterialDark;
case react::RNSScreenStackHeaderConfigBlurEffect::SystemChromeMaterialDark:
case SystemChromeMaterialDark:
return RNSBlurEffectStyleSystemChromeMaterialDark;
}
}
#endif

switch (blurEffect) {
case react::RNSScreenStackHeaderConfigBlurEffect::None:
case None:
return RNSBlurEffectStyleNone;
case react::RNSScreenStackHeaderConfigBlurEffect::Light:
case Light:
return RNSBlurEffectStyleLight;
case react::RNSScreenStackHeaderConfigBlurEffect::Dark:
case Dark:
return RNSBlurEffectStyleDark;
case react::RNSScreenStackHeaderConfigBlurEffect::Regular:
case Regular:
return RNSBlurEffectStyleRegular;
case react::RNSScreenStackHeaderConfigBlurEffect::Prominent:
case Prominent:
return RNSBlurEffectStyleProminent;
case react::RNSScreenStackHeaderConfigBlurEffect::ExtraLight:
case ExtraLight:
default:
return RNSBlurEffectStyleNone;
}
Expand Down

0 comments on commit f489762

Please sign in to comment.