15
15
import android .widget .RelativeLayout ;
16
16
import android .widget .TextView ;
17
17
18
+ import com .iterable .iterableapi .InboxSessionManager ;
18
19
import com .iterable .iterableapi .IterableActivityMonitor ;
19
20
import com .iterable .iterableapi .IterableApi ;
20
21
import com .iterable .iterableapi .IterableConstants ;
21
22
import com .iterable .iterableapi .IterableInAppDeleteActionType ;
22
23
import com .iterable .iterableapi .IterableInAppLocation ;
23
24
import com .iterable .iterableapi .IterableInAppManager ;
24
25
import com .iterable .iterableapi .IterableInAppMessage ;
25
- import com .iterable .iterableapi .IterableInboxSession ;
26
26
import com .iterable .iterableapi .IterableLogger ;
27
27
import com .iterable .iterableapi .ui .R ;
28
28
29
29
import java .text .DateFormat ;
30
- import java .util .ArrayList ;
31
- import java .util .Date ;
32
- import java .util .HashMap ;
33
- import java .util .List ;
34
- import java .util .Map ;
35
30
36
31
/**
37
32
* The main class for Inbox UI. Renders the list of Inbox messages and handles touch interaction:
@@ -54,12 +49,12 @@ public class IterableInboxFragment extends Fragment implements IterableInAppMana
54
49
TextView noMessagesBodyTextView ;
55
50
RecyclerView recyclerView ;
56
51
57
- private final SessionManager sessionManager = new SessionManager ();
52
+ private final InboxSessionManager sessionManager = new InboxSessionManager ();
58
53
private IterableInboxAdapterExtension adapterExtension = new DefaultAdapterExtension ();
59
54
private IterableInboxComparator comparator = new DefaultInboxComparator ();
60
55
private IterableInboxFilter filter = new DefaultInboxFilter ();
61
56
private IterableInboxDateMapper dateMapper = new DefaultInboxDateMapper ();
62
- private boolean sessionStarted = false ;
57
+
63
58
64
59
/**
65
60
* Create an Inbox fragment with default parameters
@@ -194,7 +189,8 @@ public void onResume() {
194
189
super .onResume ();
195
190
updateList ();
196
191
IterableApi .getInstance ().getInAppManager ().addListener (this );
197
- startSession ();
192
+
193
+ sessionManager .startSession ();
198
194
}
199
195
200
196
@ Override
@@ -208,7 +204,7 @@ public void onDestroy() {
208
204
super .onDestroy ();
209
205
IterableActivityMonitor .getInstance ().removeCallback (appStateCallback );
210
206
if (this .getActivity () != null && !this .getActivity ().isChangingConfigurations ()) {
211
- stopSession ();
207
+ sessionManager . endSession ();
212
208
}
213
209
}
214
210
@@ -219,24 +215,10 @@ public void onSwitchToForeground() {
219
215
220
216
@ Override
221
217
public void onSwitchToBackground () {
222
- stopSession ();
218
+ sessionManager . endSession ();
223
219
}
224
220
};
225
221
226
- private void startSession () {
227
- if (!sessionStarted ) {
228
- sessionStarted = true ;
229
- sessionManager .onAppDidEnterForeground ();
230
- }
231
- }
232
-
233
- private void stopSession () {
234
- if (sessionStarted ) {
235
- sessionStarted = false ;
236
- sessionManager .onAppDidEnterBackground ();
237
- }
238
- }
239
-
240
222
private void updateList () {
241
223
IterableInboxAdapter adapter = (IterableInboxAdapter ) recyclerView .getAdapter ();
242
224
adapter .setInboxItems (IterableApi .getInstance ().getInAppManager ().getInboxMessages ());
@@ -348,116 +330,4 @@ public CharSequence mapMessageToDateString(@NonNull IterableInAppMessage message
348
330
}
349
331
}
350
332
}
351
-
352
- private static class SessionManager {
353
- IterableInboxSession session = new IterableInboxSession ();
354
- Map <String , ImpressionData > impressions = new HashMap <>();
355
-
356
- private void onAppDidEnterForeground () {
357
- if (session .sessionStartTime != null ) {
358
- IterableLogger .e (TAG , "Inbox session started twice" );
359
- return ;
360
- }
361
- session = new IterableInboxSession (
362
- new Date (),
363
- null ,
364
- IterableApi .getInstance ().getInAppManager ().getInboxMessages ().size (),
365
- IterableApi .getInstance ().getInAppManager ().getUnreadInboxMessagesCount (),
366
- 0 ,
367
- 0 ,
368
- null );
369
- IterableApi .getInstance ().setInboxSessionId (session .sessionId );
370
- }
371
-
372
- private void onAppDidEnterBackground () {
373
- if (session .sessionStartTime == null ) {
374
- IterableLogger .e (TAG , "Inbox Session ended without start" );
375
- return ;
376
- }
377
- endAllImpressions ();
378
- IterableInboxSession sessionToTrack = new IterableInboxSession (
379
- session .sessionStartTime ,
380
- new Date (),
381
- session .startTotalMessageCount ,
382
- session .startUnreadMessageCount ,
383
- IterableApi .getInstance ().getInAppManager ().getInboxMessages ().size (),
384
- IterableApi .getInstance ().getInAppManager ().getUnreadInboxMessagesCount (),
385
- getImpressionList ());
386
- IterableApi .getInstance ().trackInboxSession (sessionToTrack );
387
- IterableApi .getInstance ().clearInboxSessionId ();
388
- session = new IterableInboxSession ();
389
- impressions = new HashMap <>();
390
- }
391
-
392
- private void onMessageImpressionStarted (IterableInAppMessage message ) {
393
- IterableLogger .printInfo ();
394
- String messageId = message .getMessageId ();
395
- ImpressionData impressionData = impressions .get (messageId );
396
- if (impressionData == null ) {
397
- impressionData = new ImpressionData (messageId , message .isSilentInboxMessage ());
398
- impressions .put (messageId , impressionData );
399
- }
400
- impressionData .startImpression ();
401
- }
402
-
403
- private void onMessageImpressionEnded (IterableInAppMessage message ) {
404
- IterableLogger .printInfo ();
405
- String messageId = message .getMessageId ();
406
- ImpressionData impressionData = impressions .get (messageId );
407
- if (impressionData == null ) {
408
- IterableLogger .e (TAG , "onMessageImpressionEnded: impressionData not found" );
409
- return ;
410
- }
411
- if (impressionData .impressionStarted == null ) {
412
- IterableLogger .e (TAG , "onMessageImpressionEnded: impressionStarted is null" );
413
- return ;
414
- }
415
- impressionData .endImpression ();
416
- }
417
-
418
- private void endAllImpressions () {
419
- for (ImpressionData impressionData : impressions .values ()) {
420
- impressionData .endImpression ();
421
- }
422
- }
423
-
424
- private List <IterableInboxSession .Impression > getImpressionList () {
425
- List <IterableInboxSession .Impression > impressionList = new ArrayList <>();
426
- for (ImpressionData impressionData : impressions .values ()) {
427
- impressionList .add (new IterableInboxSession .Impression (
428
- impressionData .messageId ,
429
- impressionData .silentInbox ,
430
- impressionData .displayCount ,
431
- impressionData .duration
432
- ));
433
- }
434
- return impressionList ;
435
- }
436
- }
437
-
438
- private static class ImpressionData {
439
- final String messageId ;
440
- final boolean silentInbox ;
441
- int displayCount = 0 ;
442
- float duration = 0.0f ;
443
-
444
- Date impressionStarted = null ;
445
-
446
- private ImpressionData (String messageId , boolean silentInbox ) {
447
- this .messageId = messageId ;
448
- this .silentInbox = silentInbox ;
449
- }
450
-
451
- private void startImpression () {
452
- this .impressionStarted = new Date ();
453
- }
454
-
455
- private void endImpression () {
456
- if (this .impressionStarted != null ) {
457
- this .displayCount += 1 ;
458
- this .duration += (float ) (new Date ().getTime () - this .impressionStarted .getTime ()) / 1000 ;
459
- this .impressionStarted = null ;
460
- }
461
- }
462
- }
463
333
}
0 commit comments