Skip to content

Commit

Permalink
Merge pull request #26 from pagopa/IOPID-1127-onClose-event
Browse files Browse the repository at this point in the history
[IOPID-1127] Close functionality for android + onDismiss callback for openTicket and showTickets
  • Loading branch information
sabontech authored Nov 20, 2023
2 parents 62559dc + 488324c commit 6649202
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# see https://help.github.com/en/articles/about-code-owners#example-of-a-codeowners-file

* @pagopa/io-app
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
import android.app.Activity;
import android.content.Context;

import android.content.Intent;

import com.facebook.react.bridge.ActivityEventListener;
import android.util.Log;

import androidx.annotation.Nullable;

import com.facebook.react.bridge.Callback;


import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
Expand Down Expand Up @@ -46,7 +54,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 @@ -58,12 +66,18 @@ public class RNZendeskChat extends ReactContextBaseJavaModule {
private String logId;
private RequestProvider requestProvider;

@Nullable
private Callback onOpenTicketDismiss;

public RNZendeskChat(ReactApplicationContext reactContext) {
super(reactContext);
appContext = reactContext;
customFields = new HashMap<>();
log = new StringBuffer();
tags = new ArrayList<>();

onOpenTicketDismiss = null;
reactContext.addActivityEventListener(this);
}

@ReactMethod
Expand Down Expand Up @@ -101,6 +115,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 @@ -228,19 +243,23 @@ public void appendLog(String log){
}

@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);
}

@ReactMethod
Expand Down Expand Up @@ -278,13 +297,16 @@ public void onError(ErrorResponse errorResponse) {
}

@ReactMethod
public void showTickets(){
public void showTickets(Callback onClose){
Activity activity = getCurrentActivity();
onOpenTicketDismiss = onClose;

// Show the user's tickets
RequestListActivity.builder()
Intent requestActivityIntent = RequestListActivity.builder()
.withContactUsButtonVisible(false)
.show(activity);
.intent(activity);

activity.startActivityForResult(requestActivityIntent, INTENT_REQUEST_CODE);
}

@ReactMethod
Expand Down Expand Up @@ -336,7 +358,10 @@ public void startChat(ReadableMap options) {

@ReactMethod
public void dismiss() {
// do nothing see https://pagopa.atlassian.net/browse/IABT-1348?focusedCommentId=31396
Activity activity = getCurrentActivity();
if (activity != null) {
activity.finishActivity(INTENT_REQUEST_CODE);
}
}


Expand All @@ -347,4 +372,17 @@ public void setNotificationToken(String token) {
pushProvider.registerPushToken(token);
}
}
}

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if (requestCode == INTENT_REQUEST_CODE && onOpenTicketDismiss != null) {
onOpenTicketDismiss.invoke();
onOpenTicketDismiss = null;
}
}

@Override
public void onNewIntent(Intent intent) {
Log.d(TAG, "onNewIntent");
}
}
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ 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;
export function showTickets(onClose: () => void): void;

// function that return the number of tickets created by the user
export function hasOpenedTickets(): Promise<number>;
Expand Down
1 change: 0 additions & 1 deletion ios/RNZendeskChat.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
#import <React/RCTBridgeModule.h>

@interface RNZendeskChat : NSObject<RCTBridgeModule>

@end
37 changes: 29 additions & 8 deletions ios/RNZendeskChat.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
#import <SupportSDK/SupportSDK.h>
#import <SupportProvidersSDK/SupportProvidersSDK.h>
#import <ZendeskCoreSDK/ZendeskCoreSDK.h>

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

@implementation RNZendeskChat
RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(setVisitorInfo:(NSDictionary *)options) {
Expand Down Expand Up @@ -65,14 +70,14 @@ - (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) {
RCT_EXPORT_METHOD(showTickets:(RCTResponseSenderBlock)onClose) {
[self executeOnMainThread:^{
[self showTicketsFunction];
[self showTicketsFunction:onClose];
}];
}
RCT_EXPORT_METHOD(showHelpCenter:(NSDictionary *)options) {
Expand Down Expand Up @@ -260,7 +265,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,10 +281,12 @@ - (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 {
- (void) showTicketsFunction:(RCTResponseSenderBlock)onClose {
ZDKRequestListUiConfiguration * config = [ZDKRequestListUiConfiguration new];
config.allowRequestCreation = false;
UIViewController *showTicketsController = [ZDKRequestUi buildRequestListWith:@[config]];
Expand All @@ -288,7 +295,9 @@ - (void) showTicketsFunction {
topController = topController.presentedViewController;
}
currentController = topController;
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController: showTicketsController];
NavigationControllerWithCompletion *navControl = [[NavigationControllerWithCompletion alloc] initWithRootViewController: showTicketsController];
navControl.completion = onClose;

[topController presentViewController:navControl animated:YES completion:nil];
}
- (void) startChatFunction:(NSDictionary *)options {
Expand Down Expand Up @@ -346,3 +355,15 @@ - (void) registerForNotifications:(NSData *)deviceToken {
[ZDKChat registerPushToken:deviceToken];
}
@end

@implementation NavigationControllerWithCompletion

- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
if (self.completion) {
self.completion(@[[NSNull null]]);
self.completion = nil;
}
}

@end

0 comments on commit 6649202

Please sign in to comment.