Skip to content

Commit

Permalink
fix: fixed invariation violation
Browse files Browse the repository at this point in the history
  • Loading branch information
abhijeetverma committed Jun 10, 2024
1 parent b318166 commit f3a2426
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ This sections helps to attach custom views inside designated sections in the pay
You can follow the below syntax to attach the component.
```ts
HyperSdkReact.notifyAboutRegisterComponent(HyperSdkReact.JuspayHeaderAttached)
AppRegistry.registerComponent(HyperSdkReact.JuspayHeaderAttached, () => CustomComponent);
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.lang.ref.WeakReference;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -77,6 +78,8 @@ public class HyperSdkReactModule extends ReactContextBaseJavaModule implements A

private boolean wasProcessWithActivity = false;

private Set<String> registeredComponents = new HashSet<>();

@NonNull
private WeakReference<Activity> processActivityRef = new WeakReference<>(null);

Expand Down Expand Up @@ -276,16 +279,20 @@ public View getMerchantView(ViewGroup viewGroup, MerchantViewType merchantViewTy
ReactRootView reactRootView = new ReactRootView(activity);
switch (merchantViewType) {
case HEADER:
reactRootView.startReactApplication(reactInstanceManager, MerchantViewConstants.JUSPAY_HEADER);
if (isViewRegistered(MerchantViewConstants.JUSPAY_HEADER))
reactRootView.startReactApplication(reactInstanceManager, MerchantViewConstants.JUSPAY_HEADER);
break;
case FOOTER:
reactRootView.startReactApplication(reactInstanceManager, MerchantViewConstants.JUSPAY_FOOTER);
if (isViewRegistered(MerchantViewConstants.JUSPAY_FOOTER))
reactRootView.startReactApplication(reactInstanceManager, MerchantViewConstants.JUSPAY_FOOTER);
break;
case FOOTER_ATTACHED:
reactRootView.startReactApplication(reactInstanceManager, MerchantViewConstants.JUSPAY_FOOTER_ATTACHED);
if (isViewRegistered(MerchantViewConstants.JUSPAY_FOOTER_ATTACHED))
reactRootView.startReactApplication(reactInstanceManager, MerchantViewConstants.JUSPAY_FOOTER_ATTACHED);
break;
case HEADER_ATTACHED:
reactRootView.startReactApplication(reactInstanceManager, MerchantViewConstants.JUSPAY_HEADER_ATTACHED);
if (isViewRegistered(MerchantViewConstants.JUSPAY_HEADER_ATTACHED))
reactRootView.startReactApplication(reactInstanceManager, MerchantViewConstants.JUSPAY_HEADER_ATTACHED);
break;
}
return reactRootView;
Expand All @@ -298,6 +305,10 @@ public View getMerchantView(ViewGroup viewGroup, MerchantViewType merchantViewTy
}
}

private boolean isViewRegistered(String tag) {
return registeredComponents.contains(tag);
}

private void sendEventToJS(JSONObject data) {
DeviceEventManagerModule.RCTDeviceEventEmitter jsModule = getJSModule();
if (jsModule == null) {
Expand Down Expand Up @@ -470,6 +481,11 @@ public void terminate() {
}
}

@ReactMethod
public void notifyAboutRegisterComponent(String tag) {
registeredComponents.add(tag);
}

@ReactMethod(isBlockingSynchronousMethod = true)
public boolean isNull() {
return hyperServices == null;
Expand Down
2 changes: 2 additions & 0 deletions example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import HyperSdkReact from 'hyper-sdk-react';

AppRegistry.registerComponent(appName, () => App);

HyperSdkReact.notifyAboutRegisterComponent(HyperSdkReact.JuspayHeaderAttached)
AppRegistry.registerComponent(
HyperSdkReact.JuspayHeaderAttached,
() => JuspayTopViewAttached
);
HyperSdkReact.notifyAboutRegisterComponent(HyperSdkReact.JuspayHeader)
AppRegistry.registerComponent(HyperSdkReact.JuspayHeader, () => JuspayTopView);
14 changes: 10 additions & 4 deletions ios/HyperSdkReact.mm
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ -(void)didMoveToSuperview {

@implementation SdkDelegate

NSMutableSet<NSString *> *registeredComponents = [[NSMutableSet alloc] init];

- (id)initWithBridge:(RCTBridge *)bridge {
// Hold references to all merchant views provided to the sdk
self.rootHolder = [[NSMutableDictionary alloc] init];
Expand Down Expand Up @@ -98,13 +100,13 @@ - (UIView * _Nullable)merchantViewForViewType:(NSString * _Nonnull)viewType {
// Create a SDKRootView so that we can attach width constraints once it is attached to it's parent
RCTRootView *rrv = [SDKRootView alloc];
NSString *moduleName = @"JP_003";
if ([viewType isEqual:@"HEADER"]) {
if ([viewType isEqual:@"HEADER"] && [registeredComponents containsObject:@"JuspayHeader"]) {
moduleName = @"JuspayHeader";
} else if ([viewType isEqual:@"HEADER_ATTACHED"]) {
} else if ([viewType isEqual:@"HEADER_ATTACHED"] && [registeredComponents containsObject:@"JuspayHeaderAttached"]) {
moduleName = @"JuspayHeaderAttached";
} else if ([viewType isEqual:@"FOOTER"]) {
} else if ([viewType isEqual:@"FOOTER"] && [registeredComponents containsObject:@"JuspayFooter"]) {
moduleName = @"JuspayFooter";
} else if ([viewType isEqual:@"FOOTER_ATTACHED"]) {
} else if ([viewType isEqual:@"FOOTER_ATTACHED"] && [registeredComponents containsObject:@"JuspayFooterAttached"]) {
moduleName = @"JuspayFooterAttached";
}

Expand Down Expand Up @@ -272,6 +274,10 @@ -(void)stopObserving {
}
}

RCT_EXPORT_METHOD(notifyAboutRegisterComponent:(NSString *)viewType) {
[registeredComponents addObject:viewType];
}

RCT_EXPORT_METHOD(isInitialised:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
if (self.hyperInstance) {
resolve(self.hyperInstance.isInitialised? @true : @false);
Expand Down
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type HyperSdkReactType = {
updateBaseViewController(): void;
openPaymentPage(data: string): void;
updateMerchantViewHeight(tag: string, height: number): void;
notifyAboutRegisterComponent(tag: string): void;
JuspayHeader: string;
JuspayHeaderAttached: string;
JuspayFooter: string;
Expand Down

0 comments on commit f3a2426

Please sign in to comment.