Skip to content

Commit 139a1f1

Browse files
authored
PIR: Add profile saving validation (#6614)
Task/Issue URL: https://app.asana.com/1/137249556945/project/488551667048375/task/1211075568938200?focus=true ### Description Validates that all profiles are saved on ´saveProfile´ message. ### Steps to test this PR https://app.asana.com/1/137249556945/task/1211099146322592?focus=true ### UI changes No UI changes
1 parent d95f484 commit 139a1f1

File tree

4 files changed

+28
-8
lines changed

4 files changed

+28
-8
lines changed

pir/pir-impl/src/main/java/com/duckduckgo/pir/impl/dashboard/messaging/handlers/PirWebSaveProfileMessageHandler.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,29 @@ class PirWebSaveProfileMessageHandler @Inject constructor(
7373

7474
appCoroutineScope.launch(dispatcherProvider.io()) {
7575
val profiles = pirWebOnboardingStateHolder.toUserProfiles()
76-
repository.saveUserProfiles(profiles)
76+
if (!repository.saveUserProfiles(profiles)) {
77+
logcat { "PIR-WEB: PirWebSaveProfileMessageHandler: failed to save all user profiles" }
78+
jsMessaging.sendResponse(
79+
jsMessage = jsMessage,
80+
response = PirWebMessageResponse.DefaultResponse.ERROR,
81+
)
82+
return@launch
83+
}
7784

78-
// TODO check if all profiles were saved successfully
7985
jsMessaging.sendResponse(
8086
jsMessage,
8187
response = PirWebMessageResponse.DefaultResponse.SUCCESS,
8288
)
8389

84-
context.startForegroundService(Intent(context, PirForegroundScanService::class.java))
85-
scanScheduler.scheduleScans()
90+
// start the initial scan at this point as startScanAndOptOut message is not reliable
91+
startAndScheduleInitialScan()
92+
93+
pirWebOnboardingStateHolder.clear()
8694
}
8795
}
96+
97+
private fun startAndScheduleInitialScan() {
98+
context.startForegroundService(Intent(context, PirForegroundScanService::class.java))
99+
scanScheduler.scheduleScans()
100+
}
88101
}

pir/pir-impl/src/main/java/com/duckduckgo/pir/impl/dashboard/state/PirWebOnboardingStateHolder.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,10 @@ class PirWebOnboardingStateHolder @Inject constructor() {
139139
}
140140
return profiles
141141
}
142+
143+
fun clear() {
144+
names.clear()
145+
addresses.clear()
146+
birthYear = null
147+
}
142148
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ interface PirRepository {
124124

125125
suspend fun replaceUserProfile(userProfile: UserProfile)
126126

127-
suspend fun saveUserProfiles(userProfiles: List<UserProfile>)
127+
suspend fun saveUserProfiles(userProfiles: List<UserProfile>): Boolean
128128

129129
suspend fun getEmailForBroker(dataBroker: String): String
130130

@@ -436,9 +436,10 @@ internal class RealPirRepository(
436436
}
437437
}
438438

439-
override suspend fun saveUserProfiles(userProfiles: List<UserProfile>) =
439+
override suspend fun saveUserProfiles(userProfiles: List<UserProfile>): Boolean =
440440
withContext(dispatcherProvider.io()) {
441-
userProfileDao.insertUserProfiles(userProfiles)
441+
val insertResult = userProfileDao.insertUserProfiles(userProfiles)
442+
insertResult.size == userProfiles.size
442443
}
443444

444445
override suspend fun getEmailForBroker(dataBroker: String): String =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ interface UserProfileDao {
3333
fun insertUserProfile(userProfile: UserProfile)
3434

3535
@Insert(onConflict = OnConflictStrategy.REPLACE)
36-
fun insertUserProfiles(userProfiles: List<UserProfile>)
36+
fun insertUserProfiles(userProfiles: List<UserProfile>): List<Long>
3737

3838
@Query("DELETE from pir_user_profile")
3939
fun deleteAllProfiles()

0 commit comments

Comments
 (0)