Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.ViewGroupManager;
import com.facebook.react.uimanager.annotations.ReactProp;

import org.json.JSONObject;

Expand All @@ -35,6 +36,13 @@ public class HyperFragmentViewManager extends ViewGroupManager<FrameLayout> {
private static final int COMMAND_PROCESS = 175;

private final ReactApplicationContext reactContext;
// Track props for each view
private String currentNamespace = null;
private String currentPayload = null;
private FrameLayout currentView = null;

// Architecture detection
private final Boolean newArchEnabled = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED;

public HyperFragmentViewManager(ReactApplicationContext reactContext) {
this.reactContext = reactContext;
Expand All @@ -58,6 +66,64 @@ protected FrameLayout createViewInstance(@NonNull ThemedReactContext context) {
public Map<String, Integer> getCommandsMap() {
return MapBuilder.of("process", COMMAND_PROCESS);
}
// Fabric-compatible props
@ReactProp(name = "ns")
public void setNs(FrameLayout view, @Nullable String ns) {
currentNamespace = ns;
currentView = view;
tryProcessProps();
}

@ReactProp(name="height")
public void setHeight(FrameLayout view, @Nullable int height) {

}
@ReactProp(name = "payload")
public void setPayload(FrameLayout view, @Nullable String payload) {
currentPayload = payload;
currentView = view;
tryProcessProps();
}


private void tryProcessProps() {
if (currentNamespace != null && currentPayload != null && currentView != null && newArchEnabled) {
currentView.post(() -> {
processWithProps(currentView, currentNamespace, currentPayload);
});
}
}

private void processWithProps(FrameLayout view, String namespace, String payload) {
try {
setupLayout(view);

JSONObject fragments = new JSONObject();
fragments.put(namespace, view);

JSONObject payloadObj = new JSONObject(payload);
payloadObj.getJSONObject("payload").put("fragmentViewGroups", fragments);

FragmentActivity activity = (FragmentActivity) reactContext.getCurrentActivity();
HyperServices hyperServices = HyperSdkReactModule.getHyperServices();

if (activity == null || hyperServices == null) {
return;
}

hyperServices.process(activity, payloadObj);

} catch (Exception e) {
SdkTracker.trackAndLogBootException(
NAME,
LogConstants.CATEGORY_LIFECYCLE,
LogConstants.SUBCATEGORY_HYPER_SDK,
LogConstants.SDK_TRACKER_LABEL,
"Exception in processWithProps",
e
);
}
}

@Override
public void receiveCommand(@NonNull FrameLayout root, String commandId, @Nullable ReadableArray args) {
Expand Down
87 changes: 83 additions & 4 deletions ios/HyperSdkReact.mm
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,13 @@ + (NSString*)dictionaryToString:(id)dict{
@end

@implementation HyperFragmentViewManagerIOS
RCT_EXPORT_MODULE()


RCT_EXPORT_MODULE(HyperFragmentViewManagerIOS)

NSString *_currentNamespace;
NSString *_currentPayload;
UIView *_currentView;

- (dispatch_queue_t)methodQueue{
return dispatch_get_main_queue();
Expand All @@ -454,7 +460,81 @@ - (UIView *)view
return [[UIView alloc] init];
}

RCT_EXPORT_METHOD(process:(nonnull NSNumber *)viewTag nameSpace:(NSString *)nameSpace payload:(NSString *)payload)

RCT_CUSTOM_VIEW_PROPERTY(ns, NSString, UIView)
{
[self setNs:json forView:view];
}

RCT_CUSTOM_VIEW_PROPERTY(payload, NSString, UIView)
{
[self setPayload:json forView:view];
}


- (void) setHeight:(NSString*)ns forView:(UIView*)view {

}

- (void) setWidth:(NSString*)ns forView:(UIView*)view {

}
- (void)setNs:(NSString *)ns forView:(UIView *)view
{
_currentNamespace = ns;
_currentView = view;
[self tryProcessProps];
}


- (void)setPayload:(NSString *)payload forView:(UIView *)view
{
_currentPayload = payload;
_currentView = view;
[self tryProcessProps];
}

- (void)tryProcessProps
{
if (_currentNamespace && _currentPayload && _currentView) {
dispatch_async(dispatch_get_main_queue(), ^{
[self processWithPropsForView:_currentView ns:_currentNamespace payload:_currentPayload];
});
}
}

- (void)processWithPropsForView:(UIView *)view ns:(NSString *)ns payload:(NSString *)payload
{
HyperServices *hyperServicesInstance = _hyperServicesReference;
if (payload && payload.length > 0) {
@try {
NSDictionary *jsonData = [HyperSdkReact stringToDictionary:payload];
if (jsonData && [jsonData isKindOfClass:[NSDictionary class]] && jsonData.allKeys.count > 0) {
if (hyperServicesInstance.baseViewController == nil || hyperServicesInstance.baseViewController.view.window == nil) {
id baseViewController = RCTPresentedViewController();
if ([baseViewController isMemberOfClass:RCTModalHostViewController.class] && [baseViewController presentingViewController]) {
[hyperServicesInstance setBaseViewController:[baseViewController presentingViewController]];
} else {
[hyperServicesInstance setBaseViewController:baseViewController];
}
}

[self manuallyLayoutChildren:view];

NSMutableDictionary *nestedPayload = [jsonData[@"payload"] mutableCopy];
NSDictionary *fragmentViewGroup = @{ns: view};
nestedPayload[@"fragmentViewGroups"] = fragmentViewGroup;
NSMutableDictionary *updatedJsonData = [jsonData mutableCopy];
updatedJsonData[@"payload"] = nestedPayload;
[hyperServicesInstance process:[updatedJsonData copy]];
}
} @catch (NSException *exception) {
// Handle exception silently
}
}
}

RCT_EXPORT_METHOD(process:(nonnull NSNumber *)viewTag ns:(NSString *)ns payload:(NSString *)payload)
{
HyperServices *hyperServicesInstance = _hyperServicesReference;
if (payload && payload.length>0) {
Expand All @@ -477,7 +557,7 @@ - (UIView *)view
return;
}
NSMutableDictionary *nestedPayload = [jsonData[@"payload"] mutableCopy];
NSDictionary *fragmentViewGroup = @{nameSpace: view};
NSDictionary *fragmentViewGroup = @{ns: view};
nestedPayload[@"fragmentViewGroups"] = fragmentViewGroup;
NSMutableDictionary *updatedJsonData = [jsonData mutableCopy];
updatedJsonData[@"payload"] = nestedPayload;
Expand All @@ -496,4 +576,3 @@ - (void)manuallyLayoutChildren:(UIView *)view {
}

@end

18 changes: 16 additions & 2 deletions src/HyperFragmentView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ if (Platform.OS === 'android') {
);
}

const newArchEnabled = global?.nativeFabricUIManager ? true : false;

const createFragment = (viewId: number, namespace: string, payload: string) => {
if (Platform.OS === 'android') {
if(!newArchEnabled) {
if (Platform.OS === 'android') {
UIManager.dispatchViewManagerCommand(
viewId,
//@ts-ignore
Expand All @@ -49,6 +52,9 @@ const createFragment = (viewId: number, namespace: string, payload: string) => {
]);
}
}

}

};

const HyperFragmentView: React.FC<HyperFragmentViewProps> = ({
Expand All @@ -71,7 +77,15 @@ const HyperFragmentView: React.FC<HyperFragmentViewProps> = ({

return (
<View style={{ height: height, width: width }}>
<HyperFragmentViewManager ref={ref} />
{newArchEnabled?<HyperFragmentViewManager
ref={ref}
style={{ flex: 1 }}
ns={namespace}
payload={payload}
/>:<HyperFragmentViewManager
ref={ref}
style={{ flex: 1 }}
/>}
</View>
);
};
Expand Down