Skip to content

Commit

Permalink
Added close functionality for android and onDismiss callback for open…
Browse files Browse the repository at this point in the history
…Ticket
  • Loading branch information
Sabino Picariello committed Nov 16, 2023
1 parent 62559dc commit b47fca5
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand All @@ -64,6 +69,8 @@ public RNZendeskChat(ReactApplicationContext reactContext) {
customFields = new HashMap<>();
log = new StringBuffer();
tags = new ArrayList<>();

reactContext.addActivityEventListener(this);
}

@ReactMethod
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}


Expand All @@ -347,4 +375,4 @@ public void setNotificationToken(String token) {
pushProvider.registerPushToken(token);
}
}
}
}
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion ios/RNZendeskChat.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,11 @@
#import <React/RCTBridgeModule.h>

@interface RNZendeskChat : NSObject<RCTBridgeModule>
@end


@interface NavigationControllerWithCompletion : UINavigationController
@property (nonatomic, copy) RCTResponseSenderBlock completion;
@end


@end
25 changes: 21 additions & 4 deletions ios/RNZendeskChat.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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];
Expand All @@ -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 {
Expand Down Expand Up @@ -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

0 comments on commit b47fca5

Please sign in to comment.