@@ -10,6 +10,7 @@ import androidx.paging.ExperimentalPagingApi
10
10
import androidx.paging.LoadType
11
11
import androidx.paging.PagingState
12
12
import androidx.paging.RemoteMediator
13
+ import com.flowcrypt.email.R
13
14
import com.flowcrypt.email.api.email.EmailUtil
14
15
import com.flowcrypt.email.api.email.IMAPStoreManager
15
16
import com.flowcrypt.email.api.email.JavaEmailConstants
@@ -52,35 +53,38 @@ class MessagesRemoteMediator(
52
53
private val context : Context ,
53
54
private val roomDatabase : FlowCryptRoomDatabase ,
54
55
private val localFolder : LocalFolder ? = null ,
56
+ private val progressNotifier : (resultCode: Int , progress: Double ) -> Unit
55
57
) : RemoteMediator<Int, MessageEntity>() {
56
58
private var searchNextPageToken: String? = null
57
59
58
60
override suspend fun load (
59
61
loadType : LoadType ,
60
62
state : PagingState <Int , MessageEntity >
61
63
): MediatorResult {
62
- if (loadType == LoadType .PREPEND || localFolder == null || localFolder.isOutbox()) {
63
- return MediatorResult .Success (endOfPaginationReached = true )
64
- }
65
- val activeAccountWithProtectedData = roomDatabase.accountDao().getActiveAccountSuspend()
66
- val accountEntity =
67
- AccountViewModel .getAccountEntityWithDecryptedInfoSuspend(activeAccountWithProtectedData)
68
- ? : return MediatorResult .Success (endOfPaginationReached = true )
69
-
70
- val totalItemsCount = roomDatabase.msgDao().getMsgsCount(
71
- account = accountEntity.email,
72
- label = if (localFolder.searchQuery.isNullOrEmpty()) {
73
- localFolder.fullName
74
- } else {
75
- JavaEmailConstants .FOLDER_SEARCH
64
+ try {
65
+ progressNotifier.invoke(R .id.progress_id_start_of_loading_new_messages, 0.0 )
66
+ if (loadType == LoadType .PREPEND || localFolder == null || localFolder.isOutbox()) {
67
+ return MediatorResult .Success (endOfPaginationReached = true )
76
68
}
77
- )
69
+ val activeAccountWithProtectedData = roomDatabase.accountDao().getActiveAccountSuspend()
70
+ val accountEntity =
71
+ AccountViewModel .getAccountEntityWithDecryptedInfoSuspend(activeAccountWithProtectedData)
72
+ ? : return MediatorResult .Success (endOfPaginationReached = true )
73
+
74
+ val totalItemsCount = roomDatabase.msgDao().getMsgsCount(
75
+ account = accountEntity.email,
76
+ label = if (localFolder.searchQuery.isNullOrEmpty()) {
77
+ localFolder.fullName
78
+ } else {
79
+ JavaEmailConstants .FOLDER_SEARCH
80
+ }
81
+ )
78
82
79
- if (loadType == LoadType .REFRESH && totalItemsCount != 0 ) {
80
- return MediatorResult .Success (endOfPaginationReached = false )
81
- }
83
+ if (loadType == LoadType .REFRESH && totalItemsCount != 0 ) {
84
+ return MediatorResult .Success (endOfPaginationReached = false )
85
+ }
82
86
83
- try {
87
+ progressNotifier.invoke( R .id.progress_id_start_of_loading_new_messages, 10.0 )
84
88
val actionResult = fetchAndCacheMessages(
85
89
accountEntity = accountEntity,
86
90
localFolder = localFolder,
@@ -95,6 +99,8 @@ class MessagesRemoteMediator(
95
99
return MediatorResult .Success (endOfPaginationReached = (actionResult.data ? : 0 ) == 0 )
96
100
} catch (exception: Exception ) {
97
101
return MediatorResult .Error (exception)
102
+ } finally {
103
+ progressNotifier.invoke(R .id.progress_id_done, 100.0 )
98
104
}
99
105
}
100
106
@@ -154,7 +160,9 @@ class MessagesRemoteMediator(
154
160
var countOfFetchedMsgs = 0
155
161
store.getFolder(localFolder.fullName).use { folder ->
156
162
val imapFolder = folder as IMAPFolder
163
+ progressNotifier.invoke(R .id.progress_id_opening_store, 20.0 )
157
164
imapFolder.open(Folder .READ_ONLY )
165
+ progressNotifier.invoke(R .id.progress_id_opening_store, 40.0 )
158
166
159
167
val countOfLoadedMsgs = when {
160
168
countOfAlreadyLoadedMsgs < 0 -> 0
@@ -186,6 +194,7 @@ class MessagesRemoteMediator(
186
194
.getLabelSuspend(accountEntity.email, accountEntity.accountType, folderName)?.let {
187
195
roomDatabase.labelDao().updateSuspend(it.copy(messagesTotal = msgsCount))
188
196
}
197
+ progressNotifier.invoke(R .id.progress_id_getting_list_of_emails, 60.0 )
189
198
if (end < 1 ) {
190
199
handleReceivedMsgs(accountEntity, localFolder, imapFolder, arrayOf())
191
200
} else {
@@ -206,7 +215,7 @@ class MessagesRemoteMediator(
206
215
handleReceivedMsgs(accountEntity, localFolder, folder, msgs)
207
216
}
208
217
}
209
-
218
+ progressNotifier.invoke( R .id.progress_id_getting_list_of_emails, 80.0 )
210
219
return @withContext Result .success(countOfFetchedMsgs)
211
220
}
212
221
@@ -221,21 +230,25 @@ class MessagesRemoteMediator(
221
230
AccountEntity .ACCOUNT_TYPE_GOOGLE -> {
222
231
val labelEntity: LabelEntity ? = roomDatabase.labelDao()
223
232
.getLabelSuspend(accountEntity.email, accountEntity.accountType, localFolder.fullName)
233
+ progressNotifier.invoke(R .id.progress_id_gmail_list, 20.0 )
224
234
val messagesBaseInfo = GmailApiHelper .loadMsgsBaseInfo(
225
235
context = context,
226
236
accountEntity = accountEntity,
227
237
localFolder = localFolder,
228
238
maxResults = pageSize.toLong(),
229
239
nextPageToken = if (totalItemsCount > 0 ) labelEntity?.nextPageToken else null
230
240
)
231
-
241
+ progressNotifier.invoke( R .id.progress_id_gmail_msgs_info, 60.0 )
232
242
if (messagesBaseInfo.messages?.isNotEmpty() == true ) {
233
243
val msgs = GmailApiHelper .loadMsgsInParallel(
234
244
context, accountEntity, messagesBaseInfo.messages
235
245
? : emptyList(), localFolder
236
246
)
237
247
countOfFetchedMsgs = msgs.size
248
+ progressNotifier.invoke(R .id.progress_id_gmail_msgs_info, 90.0 )
238
249
handleReceivedMsgs(accountEntity, localFolder, msgs)
250
+ } else {
251
+ progressNotifier.invoke(R .id.progress_id_gmail_msgs_info, 90.0 )
239
252
}
240
253
241
254
labelEntity?.let {
@@ -392,25 +405,29 @@ class MessagesRemoteMediator(
392
405
var countOfFetchedMsgs = 0
393
406
when (accountEntity.accountType) {
394
407
AccountEntity .ACCOUNT_TYPE_GOOGLE -> {
408
+ progressNotifier.invoke(R .id.progress_id_gmail_list, 20.0 )
395
409
val messagesBaseInfo = GmailApiHelper .loadMsgsBaseInfoUsingSearch(
396
410
context = context,
397
411
accountEntity = accountEntity,
398
412
localFolder = localFolder,
399
413
maxResults = pageSize.toLong(),
400
414
nextPageToken = if (totalItemsCount > 0 ) searchNextPageToken else null
401
415
)
402
-
416
+ progressNotifier.invoke( R .id.progress_id_gmail_msgs_info, 70.0 )
403
417
if (messagesBaseInfo.messages?.isNotEmpty() == true ) {
404
418
val msgs = GmailApiHelper .loadMsgsInParallel(
405
419
context, accountEntity, messagesBaseInfo.messages
406
420
? : emptyList(), localFolder
407
421
)
408
422
countOfFetchedMsgs = msgs.size
423
+ progressNotifier.invoke(R .id.progress_id_gmail_msgs_info, 90.0 )
409
424
handleSearchResults(
410
425
accountEntity,
411
426
localFolder.copy(fullName = JavaEmailConstants .FOLDER_SEARCH ),
412
427
msgs
413
428
)
429
+ } else {
430
+ progressNotifier.invoke(R .id.progress_id_gmail_msgs_info, 90.0 )
414
431
}
415
432
416
433
searchNextPageToken = messagesBaseInfo.nextPageToken
@@ -429,7 +446,9 @@ class MessagesRemoteMediator(
429
446
var countOfFetchedMsgs = 0
430
447
store.getFolder(localFolder.fullName).use { folder ->
431
448
val imapFolder = folder as IMAPFolder
449
+ progressNotifier.invoke(R .id.progress_id_opening_store, 20.0 )
432
450
imapFolder.open(Folder .READ_ONLY )
451
+ progressNotifier.invoke(R .id.progress_id_opening_store, 40.0 )
433
452
434
453
val countOfLoadedMsgs = when {
435
454
countOfAlreadyLoadedMsgs < 0 -> 0
@@ -445,6 +464,7 @@ class MessagesRemoteMediator(
445
464
startCandidate < 1 -> 1
446
465
else -> startCandidate
447
466
}
467
+ progressNotifier.invoke(R .id.progress_id_getting_list_of_emails, 60.0 )
448
468
449
469
if (end < 1 ) {
450
470
handleSearchResults(accountEntity, localFolder, imapFolder, arrayOf())
@@ -461,6 +481,8 @@ class MessagesRemoteMediator(
461
481
countOfFetchedMsgs = bufferedMsgs.size
462
482
handleSearchResults(accountEntity, localFolder, imapFolder, bufferedMsgs)
463
483
}
484
+
485
+ progressNotifier.invoke(R .id.progress_id_getting_list_of_emails, 80.0 )
464
486
}
465
487
466
488
return @withContext Result .success(countOfFetchedMsgs)
0 commit comments