13
13
import com .facebook .react .bridge .WritableNativeMap ;
14
14
import zendesk .chat .Chat ;
15
15
import zendesk .chat .ChatConfiguration ;
16
+ import zendesk .chat .ChatSessionStatus ;
17
+ import zendesk .chat .ChatState ;
18
+ import zendesk .chat .ObservationScope ;
19
+ import zendesk .chat .Observer ;
16
20
import zendesk .chat .ProfileProvider ;
17
21
import zendesk .chat .PreChatFormFieldStatus ;
18
22
import zendesk .chat .ChatEngine ;
@@ -28,6 +32,10 @@ public class RNZendeskChatModule extends ReactContextBaseJavaModule {
28
32
29
33
private ArrayList <String > currentUserTags = new ArrayList ();
30
34
35
+ private boolean visitorSet = false ;
36
+ private ProfileProvider profileProvider ;
37
+ private VisitorInfo visitorInfo ;
38
+
31
39
// private class Converters {
32
40
public static ArrayList <String > getArrayListOfStrings (ReadableMap options , String key , String functionHint ) {
33
41
ArrayList <String > result = new ArrayList ();
@@ -157,14 +165,16 @@ public void setVisitorInfo(ReadableMap options) {
157
165
builder = builder .withPhoneNumber (phone );
158
166
}
159
167
160
- VisitorInfo visitorData = builder .build ();
168
+ visitorInfo = builder .build ();
161
169
162
170
if (Chat .INSTANCE .providers () == null ) {
163
171
Log .e (TAG ,
164
172
"Zendesk Internals are undefined -- did you forget to call RNZendeskModule.init(<account_key>)?" );
165
173
return ;
166
174
}
167
- Chat .INSTANCE .providers ().profileProvider ().setVisitorInfo (visitorData , null );
175
+
176
+ profileProvider = Chat .INSTANCE .providers ().profileProvider ();
177
+ profileProvider .setVisitorInfo (visitorInfo , null );
168
178
}
169
179
170
180
@ ReactMethod
@@ -251,6 +261,7 @@ public void startChat(ReadableMap options) {
251
261
return ;
252
262
}
253
263
setVisitorInfo (options );
264
+ setupObserver ();
254
265
255
266
ReadableMap flagHash = RNZendeskChatModule .getReadableMap (options , "behaviorFlags" , "startChat" );
256
267
boolean showPreChatForm = getBooleanOrDefault (flagHash , "showPreChatForm" , "startChat(behaviorFlags)" , true );
@@ -288,4 +299,27 @@ public void registerPushToken(String token) {
288
299
pushProvider .registerPushToken (token );
289
300
}
290
301
}
302
+
303
+ public void setupObserver (){
304
+ final ObservationScope observationScope = new ObservationScope ();
305
+ Chat .INSTANCE .providers ().chatProvider ().observeChatState (observationScope , new Observer <ChatState >() {
306
+ @ Override
307
+ public void update (ChatState chatState ) {
308
+ ChatSessionStatus chatStatus = chatState .getChatSessionStatus ();
309
+ // Status achieved after the PreChatForm is completed
310
+ if (chatStatus == ChatSessionStatus .STARTED ) {
311
+ // Update the information MID chat here. All info but Department can be updated
312
+ if (!visitorSet ) {
313
+ // Add here the code to set the visitor info - visitorInfo would be a VisitorInfo type variable containing all the information to set
314
+ profileProvider .setVisitorInfo (visitorInfo , null );
315
+ visitorSet = true ;
316
+ }
317
+
318
+ } else {
319
+ // There are few other statuses that you can observe but they are unused in this example
320
+ Log .d (TAG , "[observerSetup] - ChatSessionUpdate -> (unused) status : " + chatStatus .toString ());
321
+ }
322
+ }
323
+ });
324
+ }
291
325
}
0 commit comments