1
1
package com.parsely.parselyandroid
2
2
3
3
import com.parsely.parselyandroid.JsonSerializer.toParselyEventsPayload
4
+ import com.parsely.parselyandroid.Logging.log
4
5
import kotlinx.coroutines.CoroutineScope
5
6
import kotlinx.coroutines.launch
6
7
import kotlinx.coroutines.sync.Mutex
@@ -10,12 +11,17 @@ internal class FlushQueue(
10
11
private val flushManager : FlushManager ,
11
12
private val repository : QueueRepository ,
12
13
private val restClient : RestClient ,
13
- private val scope : CoroutineScope
14
+ private val scope : CoroutineScope ,
15
+ private val connectivityStatusProvider : ConnectivityStatusProvider
14
16
) {
15
17
16
18
private val mutex = Mutex ()
17
19
18
20
operator fun invoke (skipSendingEvents : Boolean ) {
21
+ if (! connectivityStatusProvider.isReachable()) {
22
+ log(" Network unreachable. Not flushing." )
23
+ return
24
+ }
19
25
scope.launch {
20
26
mutex.withLock {
21
27
val eventsToSend = repository.getStoredQueue()
@@ -26,23 +32,23 @@ internal class FlushQueue(
26
32
}
27
33
28
34
if (skipSendingEvents) {
29
- ParselyTracker . PLog (" Debug mode on. Not sending to Parse.ly. Otherwise, would sent ${eventsToSend.size} events" )
35
+ log (" Debug mode on. Not sending to Parse.ly. Otherwise, would sent ${eventsToSend.size} events" )
30
36
repository.remove(eventsToSend)
31
37
return @launch
32
38
}
33
- ParselyTracker . PLog (" Sending request with %d events" , eventsToSend.size)
39
+ log (" Sending request with %d events" , eventsToSend.size)
34
40
val jsonPayload = toParselyEventsPayload(eventsToSend)
35
- ParselyTracker . PLog (" POST Data %s" , jsonPayload)
36
- ParselyTracker . PLog (" Requested %s" , ParselyTracker .ROOT_URL )
41
+ log (" POST Data %s" , jsonPayload)
42
+ log (" Requested %s" , ParselyTracker .ROOT_URL )
37
43
restClient.send(jsonPayload)
38
44
.fold(
39
45
onSuccess = {
40
- ParselyTracker . PLog (" Pixel request success" )
46
+ log (" Pixel request success" )
41
47
repository.remove(eventsToSend)
42
48
},
43
49
onFailure = {
44
- ParselyTracker . PLog (" Pixel request exception" )
45
- ParselyTracker . PLog (it.toString())
50
+ log (" Pixel request exception" )
51
+ log (it.toString())
46
52
}
47
53
)
48
54
}
0 commit comments