Skip to content

Commit 7aeb21b

Browse files
committed
Use flow instead of suspend
1 parent 88e04f8 commit 7aeb21b

File tree

5 files changed

+123
-124
lines changed

5 files changed

+123
-124
lines changed

pir/pir-impl/src/main/java/com/duckduckgo/pir/impl/store/PirEventsRepository.kt

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ import kotlinx.coroutines.CoroutineStart
4242
import kotlinx.coroutines.Deferred
4343
import kotlinx.coroutines.async
4444
import kotlinx.coroutines.flow.Flow
45-
import kotlinx.coroutines.flow.emptyFlow
45+
import kotlinx.coroutines.flow.emitAll
46+
import kotlinx.coroutines.flow.flow
4647
import kotlinx.coroutines.flow.flowOf
4748
import kotlinx.coroutines.flow.map
4849
import kotlinx.coroutines.withContext
@@ -51,29 +52,29 @@ import logcat.logcat
5152
import javax.inject.Inject
5253

5354
interface PirEventsRepository {
54-
suspend fun getAllEventLogsFlow(): Flow<List<PirEventLog>>
55+
fun getAllEventLogsFlow(): Flow<List<PirEventLog>>
5556

5657
suspend fun saveEventLog(pirEventLog: PirEventLog)
5758

5859
suspend fun saveBrokerScanLog(pirBrokerScanLog: PirBrokerScanLog)
5960

6061
suspend fun deleteEventLogs()
6162

62-
suspend fun getScannedBrokersFlow(): Flow<List<ScanCompletedBroker>>
63+
fun getScannedBrokersFlow(): Flow<List<ScanCompletedBroker>>
6364

6465
suspend fun getScanErrorResultsCount(): Int
6566

6667
suspend fun getScanSuccessResultsCount(): Int
6768

6869
suspend fun deleteAllScanResults()
6970

70-
suspend fun getTotalScannedBrokersFlow(): Flow<Int>
71+
fun getTotalScannedBrokersFlow(): Flow<Int>
7172

72-
suspend fun getTotalOptOutCompletedFlow(): Flow<Int>
73+
fun getTotalOptOutCompletedFlow(): Flow<Int>
7374

74-
suspend fun getAllOptOutActionLogFlow(): Flow<List<OptOutActionLog>>
75+
fun getAllOptOutActionLogFlow(): Flow<List<OptOutActionLog>>
7576

76-
suspend fun getAllSuccessfullySubmittedOptOutFlow(): Flow<Map<String, String>>
77+
fun getAllSuccessfullySubmittedOptOutFlow(): Flow<Map<String, String>>
7778

7879
suspend fun saveScanCompletedBroker(
7980
brokerName: String,
@@ -108,7 +109,7 @@ interface PirEventsRepository {
108109
detail: String,
109110
)
110111

111-
suspend fun getAllEmailConfirmationLogFlow(): Flow<List<PirEmailConfirmationLog>>
112+
fun getAllEmailConfirmationLogFlow(): Flow<List<PirEmailConfirmationLog>>
112113

113114
suspend fun deleteAllEmailConfirmationsLogs()
114115
}
@@ -145,8 +146,8 @@ class RealPirEventsRepository @Inject constructor(
145146
scanLogDao()?.getAllBrokerScanEvents()?.filter { it.eventType == BROKER_SUCCESS }?.size ?: 0
146147
}
147148

148-
override suspend fun getAllEventLogsFlow(): Flow<List<PirEventLog>> {
149-
return scanLogDao()?.getAllEventLogsFlow() ?: emptyFlow()
149+
override fun getAllEventLogsFlow(): Flow<List<PirEventLog>> = flow {
150+
emitAll(scanLogDao()?.getAllEventLogsFlow() ?: flowOf(listOf()))
150151
}
151152

152153
override suspend fun saveEventLog(pirEventLog: PirEventLog) {
@@ -167,33 +168,35 @@ class RealPirEventsRepository @Inject constructor(
167168
}
168169
}
169170

170-
override suspend fun getScannedBrokersFlow(): Flow<List<ScanCompletedBroker>> {
171-
return scanResultsDao()?.getScanCompletedBrokerFlow() ?: flowOf(emptyList())
171+
override fun getScannedBrokersFlow(): Flow<List<ScanCompletedBroker>> = flow {
172+
emitAll(scanResultsDao()?.getScanCompletedBrokerFlow() ?: flowOf(emptyList()))
172173
}
173174

174-
override suspend fun getTotalScannedBrokersFlow(): Flow<Int> {
175-
return scanResultsDao()?.getScanCompletedBrokerFlow()?.map { it.size } ?: flowOf(0)
175+
override fun getTotalScannedBrokersFlow(): Flow<Int> = flow {
176+
emitAll(scanResultsDao()?.getScanCompletedBrokerFlow()?.map { it.size } ?: flowOf(0))
176177
}
177178

178-
override suspend fun getTotalOptOutCompletedFlow(): Flow<Int> {
179-
return optOutResultsDao()?.getOptOutCompletedBrokerFlow()?.map { it.size } ?: flowOf(0)
179+
override fun getTotalOptOutCompletedFlow(): Flow<Int> = flow {
180+
emitAll(optOutResultsDao()?.getOptOutCompletedBrokerFlow()?.map { it.size } ?: flowOf(0))
180181
}
181182

182-
override suspend fun getAllSuccessfullySubmittedOptOutFlow(): Flow<Map<String, String>> {
183-
return optOutResultsDao()?.getOptOutCompletedBrokerFlow()?.map {
184-
it.filter {
185-
it.isSubmitSuccess
186-
}.map {
187-
(
188-
extractedProfileAdapter.fromJson(it.extractedProfile)?.identifier
189-
?: "Unknown"
190-
) to it.brokerName
191-
}.distinct().toMap()
192-
} ?: flowOf(emptyMap())
183+
override fun getAllSuccessfullySubmittedOptOutFlow(): Flow<Map<String, String>> = flow {
184+
emitAll(
185+
optOutResultsDao()?.getOptOutCompletedBrokerFlow()?.map {
186+
it.filter {
187+
it.isSubmitSuccess
188+
}.map {
189+
(
190+
extractedProfileAdapter.fromJson(it.extractedProfile)?.identifier
191+
?: "Unknown"
192+
) to it.brokerName
193+
}.distinct().toMap()
194+
} ?: flowOf(emptyMap()),
195+
)
193196
}
194197

195-
override suspend fun getAllOptOutActionLogFlow(): Flow<List<OptOutActionLog>> {
196-
return optOutResultsDao()?.getOptOutActionLogFlow() ?: flowOf(emptyList())
198+
override fun getAllOptOutActionLogFlow(): Flow<List<OptOutActionLog>> = flow {
199+
emitAll(optOutResultsDao()?.getOptOutActionLogFlow() ?: flowOf(emptyList()))
197200
}
198201

199202
override suspend fun saveScanCompletedBroker(
@@ -271,8 +274,8 @@ class RealPirEventsRepository @Inject constructor(
271274
)
272275
}
273276

274-
override suspend fun getAllEmailConfirmationLogFlow(): Flow<List<PirEmailConfirmationLog>> {
275-
return emailConfirmationLogDao()?.getAllEmailConfirmationLogsFlow() ?: flowOf(emptyList())
277+
override fun getAllEmailConfirmationLogFlow(): Flow<List<PirEmailConfirmationLog>> = flow {
278+
emitAll(emailConfirmationLogDao()?.getAllEmailConfirmationLogsFlow() ?: flowOf(emptyList()))
276279
}
277280

278281
override suspend fun deleteAllEmailConfirmationsLogs(): Unit = withContext(dispatcherProvider.io()) {

pir/pir-impl/src/main/java/com/duckduckgo/pir/impl/store/PirRepository.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ import kotlinx.coroutines.Deferred
5353
import kotlinx.coroutines.async
5454
import kotlinx.coroutines.awaitAll
5555
import kotlinx.coroutines.flow.Flow
56+
import kotlinx.coroutines.flow.emitAll
57+
import kotlinx.coroutines.flow.flow
5658
import kotlinx.coroutines.flow.flowOf
5759
import kotlinx.coroutines.flow.map
5860
import kotlinx.coroutines.withContext
@@ -136,7 +138,7 @@ interface PirRepository {
136138
extractedProfileId: Long,
137139
): ExtractedProfile?
138140

139-
suspend fun getAllExtractedProfilesFlow(): Flow<List<ExtractedProfile>>
141+
fun getAllExtractedProfilesFlow(): Flow<List<ExtractedProfile>>
140142

141143
suspend fun getAllExtractedProfiles(): List<ExtractedProfile>
142144

@@ -505,12 +507,17 @@ class RealPirRepository(
505507
return@withContext extractedProfileDao()?.getExtractedProfile(extractedProfileId)?.toExtractedProfile()
506508
}
507509

508-
override suspend fun getAllExtractedProfilesFlow(): Flow<List<ExtractedProfile>> =
509-
extractedProfileDao()?.getAllExtractedProfileFlow()?.map { list ->
510-
list.map {
511-
it.toExtractedProfile()
512-
}
513-
} ?: flowOf(emptyList())
510+
override fun getAllExtractedProfilesFlow(): Flow<List<ExtractedProfile>> {
511+
return flow {
512+
emitAll(
513+
extractedProfileDao()?.getAllExtractedProfileFlow()?.map { list ->
514+
list.map {
515+
it.toExtractedProfile()
516+
}
517+
} ?: flowOf(emptyList()),
518+
)
519+
}
520+
}
514521

515522
override suspend fun getAllExtractedProfiles(): List<ExtractedProfile> =
516523
withContext(dispatcherProvider.io()) {

pir/pir-internal/src/main/java/com/duckduckgo/pir/internal/settings/PirDevOptOutActivity.kt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,19 @@ class PirDevOptOutActivity : DuckDuckGoActivity() {
133133
dropDownAdapter.clear()
134134
dropDownAdapter.addAll(brokerOptions)
135135
}
136-
137-
eventsRepository.getAllSuccessfullySubmittedOptOutFlow()
138-
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
139-
.onEach { optOuts ->
140-
optOutAdapter.clear()
141-
optOutAdapter.addAll(
142-
optOuts.map {
143-
"${it.value} - ${it.key}"
144-
},
145-
)
146-
}
147-
.launchIn(lifecycleScope)
148136
}
137+
138+
eventsRepository.getAllSuccessfullySubmittedOptOutFlow()
139+
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
140+
.onEach { optOuts ->
141+
optOutAdapter.clear()
142+
optOutAdapter.addAll(
143+
optOuts.map {
144+
"${it.value} - ${it.key}"
145+
},
146+
)
147+
}
148+
.launchIn(lifecycleScope)
149149
}
150150
}
151151

pir/pir-internal/src/main/java/com/duckduckgo/pir/internal/settings/PirDevScanActivity.kt

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,21 +93,19 @@ class PirDevScanActivity : DuckDuckGoActivity() {
9393
}
9494

9595
private fun bindViews() {
96-
lifecycleScope.launch {
97-
repository.getAllExtractedProfilesFlow()
98-
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
99-
.onEach {
100-
render(it)
101-
}
102-
.launchIn(lifecycleScope)
96+
repository.getAllExtractedProfilesFlow()
97+
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
98+
.onEach {
99+
render(it)
100+
}
101+
.launchIn(lifecycleScope)
103102

104-
eventsRepository.getTotalScannedBrokersFlow()
105-
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
106-
.onEach {
107-
binding.statusSitesScanned.text = getString(R.string.pirStatsStatusScanned, it)
108-
}
109-
.launchIn(lifecycleScope)
110-
}
103+
eventsRepository.getTotalScannedBrokersFlow()
104+
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
105+
.onEach {
106+
binding.statusSitesScanned.text = getString(R.string.pirStatsStatusScanned, it)
107+
}
108+
.launchIn(lifecycleScope)
111109
}
112110

113111
private fun render(extractedProfiles: List<ExtractedProfile>) {

pir/pir-internal/src/main/java/com/duckduckgo/pir/internal/settings/PirResultsActivity.kt

Lines changed: 51 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import com.duckduckgo.pir.internal.settings.PirResultsScreenParams.PirOptOutResu
3838
import com.duckduckgo.pir.internal.settings.PirResultsScreenParams.PirScanResultsScreen
3939
import kotlinx.coroutines.flow.launchIn
4040
import kotlinx.coroutines.flow.onEach
41-
import kotlinx.coroutines.launch
4241
import java.text.SimpleDateFormat
4342
import java.util.Date
4443
import java.util.Locale
@@ -98,78 +97,70 @@ class PirResultsActivity : DuckDuckGoActivity() {
9897
}
9998

10099
private fun showEmailResults() {
101-
lifecycleScope.launch {
102-
eventsRepository.getAllEmailConfirmationLogFlow().flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
103-
.onEach { emailEvents ->
104-
emailEvents.map { result ->
105-
val stringBuilder = StringBuilder()
106-
stringBuilder.append("Time: ${formatter.format(Date(result.eventTimeInMillis))}\n")
107-
stringBuilder.append("EVENT: ${result.eventType}\n")
108-
stringBuilder.append("RESULT: ${result.value}\n")
109-
stringBuilder.toString()
110-
}.also {
111-
render(it)
112-
}
100+
eventsRepository.getAllEmailConfirmationLogFlow().flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
101+
.onEach { emailEvents ->
102+
emailEvents.map { result ->
103+
val stringBuilder = StringBuilder()
104+
stringBuilder.append("Time: ${formatter.format(Date(result.eventTimeInMillis))}\n")
105+
stringBuilder.append("EVENT: ${result.eventType}\n")
106+
stringBuilder.append("RESULT: ${result.value}\n")
107+
stringBuilder.toString()
108+
}.also {
109+
render(it)
113110
}
114-
.launchIn(lifecycleScope)
115-
}
111+
}
112+
.launchIn(lifecycleScope)
116113
}
117114

118115
private fun showOptOutResults() {
119-
lifecycleScope.launch {
120-
eventsRepository.getAllOptOutActionLogFlow()
121-
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
122-
.onEach { optOutEvents ->
123-
optOutEvents.map { result ->
124-
val stringBuilder = StringBuilder()
125-
stringBuilder.append("Time: ${formatter.format(Date(result.completionTimeInMillis))}\n")
126-
stringBuilder.append("BROKER NAME: ${result.brokerName}\n")
127-
stringBuilder.append("EXTRACTED PROFILE: ${result.extractedProfile}\n")
128-
stringBuilder.append("ACTION EXECUTED: ${result.actionType}\n")
129-
stringBuilder.append("IS ERROR: ${result.isError}\n")
130-
stringBuilder.append("RAW RESULT: ${result.result}\n")
131-
stringBuilder.toString()
132-
}.also {
133-
render(it)
134-
}
116+
eventsRepository.getAllOptOutActionLogFlow()
117+
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
118+
.onEach { optOutEvents ->
119+
optOutEvents.map { result ->
120+
val stringBuilder = StringBuilder()
121+
stringBuilder.append("Time: ${formatter.format(Date(result.completionTimeInMillis))}\n")
122+
stringBuilder.append("BROKER NAME: ${result.brokerName}\n")
123+
stringBuilder.append("EXTRACTED PROFILE: ${result.extractedProfile}\n")
124+
stringBuilder.append("ACTION EXECUTED: ${result.actionType}\n")
125+
stringBuilder.append("IS ERROR: ${result.isError}\n")
126+
stringBuilder.append("RAW RESULT: ${result.result}\n")
127+
stringBuilder.toString()
128+
}.also {
129+
render(it)
135130
}
136-
.launchIn(lifecycleScope)
137-
}
131+
}
132+
.launchIn(lifecycleScope)
138133
}
139134

140135
private fun showScanResults() {
141-
lifecycleScope.launch {
142-
eventsRepository.getScannedBrokersFlow()
143-
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
144-
.onEach { scanResults ->
145-
scanResults.map {
146-
val stringBuilder = StringBuilder()
147-
stringBuilder.append("BROKER NAME: ${it.brokerName}\n")
148-
stringBuilder.append("PROFILE ID: ${it.profileQueryId}\n")
149-
stringBuilder.append("COMPLETED WITH NO ERROR: ${it.isSuccess}\n")
150-
stringBuilder.append("DURATION: ${it.endTimeInMillis - it.startTimeInMillis}\n")
151-
stringBuilder.toString()
152-
}.also {
153-
render(it)
154-
}
136+
eventsRepository.getScannedBrokersFlow()
137+
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
138+
.onEach { scanResults ->
139+
scanResults.map {
140+
val stringBuilder = StringBuilder()
141+
stringBuilder.append("BROKER NAME: ${it.brokerName}\n")
142+
stringBuilder.append("PROFILE ID: ${it.profileQueryId}\n")
143+
stringBuilder.append("COMPLETED WITH NO ERROR: ${it.isSuccess}\n")
144+
stringBuilder.append("DURATION: ${it.endTimeInMillis - it.startTimeInMillis}\n")
145+
stringBuilder.toString()
146+
}.also {
147+
render(it)
155148
}
156-
.launchIn(lifecycleScope)
157-
}
149+
}
150+
.launchIn(lifecycleScope)
158151
}
159152

160153
private fun showAllEvents() {
161-
lifecycleScope.launch {
162-
eventsRepository.getAllEventLogsFlow()
163-
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
164-
.onEach { scanEvents ->
165-
scanEvents.map { result ->
166-
"Time: ${formatter.format(Date(result.eventTimeInMillis))}\nEVENT: ${result.eventType}\n"
167-
}.also {
168-
render(it)
169-
}
154+
eventsRepository.getAllEventLogsFlow()
155+
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
156+
.onEach { scanEvents ->
157+
scanEvents.map { result ->
158+
"Time: ${formatter.format(Date(result.eventTimeInMillis))}\nEVENT: ${result.eventType}\n"
159+
}.also {
160+
render(it)
170161
}
171-
.launchIn(lifecycleScope)
172-
}
162+
}
163+
.launchIn(lifecycleScope)
173164
}
174165

175166
private fun render(results: List<String>) {

0 commit comments

Comments
 (0)