diff --git a/android/src/main/java/com/saranshmalik/rnzendeskchat/RNZendeskChat.java b/android/src/main/java/com/saranshmalik/rnzendeskchat/RNZendeskChat.java index ce6d074..d7197c9 100644 --- a/android/src/main/java/com/saranshmalik/rnzendeskchat/RNZendeskChat.java +++ b/android/src/main/java/com/saranshmalik/rnzendeskchat/RNZendeskChat.java @@ -4,7 +4,12 @@ import android.app.Activity; import android.content.Context; +import android.content.Intent; + +import com.facebook.react.bridge.ActivityEventListener; import android.util.Log; +import com.facebook.react.bridge.Callback; + import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; @@ -46,7 +51,7 @@ import zendesk.support.request.RequestActivity; import zendesk.support.requestlist.RequestListActivity; -public class RNZendeskChat extends ReactContextBaseJavaModule { +public class RNZendeskChat extends ReactContextBaseJavaModule implements ActivityEventListener { private ReactContext appContext; private static final String TAG = "ZendeskChat"; @@ -64,6 +69,8 @@ public RNZendeskChat(ReactApplicationContext reactContext) { customFields = new HashMap<>(); log = new StringBuffer(); tags = new ArrayList<>(); + + reactContext.addActivityEventListener(this); } @ReactMethod @@ -101,6 +108,7 @@ private String getString(ReadableMap options, String key){ return null; } + private final int INTENT_REQUEST_CODE = 100; @ReactMethod public void setVisitorInfo(ReadableMap options) { @@ -226,21 +234,40 @@ public void appendLog(String log){ this.log.insert(0, "\n"+log); this.log = new StringBuffer(this.log.substring(0, Math.max(0, Math.min(this.log.length()-1, logCapacity)))); } + + Callback onOpenTicketDismiss; @ReactMethod - public void openTicket(){ + public void openTicket(Callback onClose){ Activity activity = getCurrentActivity(); + onOpenTicketDismiss = onClose; + if(this.logId != null) { // Add log custom field customFields.put(this.logId, new CustomField(Long.parseLong(this.logId), this.log.toString())); } // Open a ticket - RequestActivity.builder() + Intent requestActivityIntent = RequestActivity.builder() .withCustomFields(new ArrayList(customFields.values())) .withTags(this.tags) - .show(activity); + .intent(activity); + + activity.startActivityForResult(requestActivityIntent, INTENT_REQUEST_CODE); + } + + @Override + public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) { + + if (requestCode == INTENT_REQUEST_CODE) { + onOpenTicketDismiss.invoke(); + } + } + + @Override + public void onNewIntent(Intent intent) { + } @ReactMethod @@ -336,7 +363,8 @@ public void startChat(ReadableMap options) { @ReactMethod public void dismiss() { - // do nothing see https://pagopa.atlassian.net/browse/IABT-1348?focusedCommentId=31396 + Activity activity = getCurrentActivity(); + activity.finishActivity(INTENT_REQUEST_CODE); } @@ -347,4 +375,4 @@ public void setNotificationToken(String token) { pushProvider.registerPushToken(token); } } -} +} \ No newline at end of file diff --git a/index.d.ts b/index.d.ts index f2c5766..522704f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -37,7 +37,7 @@ declare module 'io-react-native-zendesk' { export function dismiss(): void; // function to open a ticket - export function openTicket(): void; + export function openTicket(onClose: () => void): void; // function to shows all the tickets of the user export function showTickets(): void; diff --git a/ios/RNZendeskChat.h b/ios/RNZendeskChat.h index 7d14912..dcecc7d 100644 --- a/ios/RNZendeskChat.h +++ b/ios/RNZendeskChat.h @@ -3,5 +3,11 @@ #import @interface RNZendeskChat : NSObject +@end + + +@interface NavigationControllerWithCompletion : UINavigationController +@property (nonatomic, copy) RCTResponseSenderBlock completion; +@end + -@end \ No newline at end of file diff --git a/ios/RNZendeskChat.m b/ios/RNZendeskChat.m index be4661c..27ea0e4 100644 --- a/ios/RNZendeskChat.m +++ b/ios/RNZendeskChat.m @@ -65,9 +65,9 @@ - (void)executeOnMainThread:(void (^)(void))block [self startChatFunction:options]; }]; } -RCT_EXPORT_METHOD(openTicket) { +RCT_EXPORT_METHOD(openTicket:(RCTResponseSenderBlock)onClose) { [self executeOnMainThread:^{ - [self openTicketFunction]; + [self openTicketFunction:onClose]; }]; } RCT_EXPORT_METHOD(showTickets) { @@ -260,7 +260,7 @@ - (void) showHelpCenterFunction:(NSDictionary *)options { UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController: controller]; [topController presentViewController:navControl animated:YES completion:nil]; } -- (void) openTicketFunction { +- (void) openTicketFunction:(RCTResponseSenderBlock)onClose { [self initGlobals]; if(logId != nil){ [self addTicketCustomFieldFunction:logId withValue:mutableLog]; @@ -276,7 +276,9 @@ - (void) openTicketFunction { topController = topController.presentedViewController; } currentController = topController; - UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController: openTicketController]; + NavigationControllerWithCompletion *navControl = [[NavigationControllerWithCompletion alloc] initWithRootViewController: openTicketController]; + navControl.completion = onClose; + [topController presentViewController:navControl animated:YES completion:nil]; } - (void) showTicketsFunction { @@ -346,3 +348,18 @@ - (void) registerForNotifications:(NSData *)deviceToken { [ZDKChat registerPushToken:deviceToken]; } @end + +@interface NavigationControllerWithCompletion () + +@end + +@implementation NavigationControllerWithCompletion + +- (void)viewDidDisappear:(BOOL)animated { + [super viewDidDisappear:animated]; + if (self.completion) { + self.completion(@[[NSNull null]]); + } +} + +@end