-
Notifications
You must be signed in to change notification settings - Fork 934
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional "default browser" prompts: fixed an issue with active days…
… conversion window (#5599) Task/Issue URL: https://app.asana.com/0/1208671518894266/1209329916239779
- Loading branch information
1 parent
2bcb0e8
commit 748ae69
Showing
9 changed files
with
305 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
.../com/duckduckgo/app/browser/defaultbrowsing/prompts/store/ExperimentAppUsageRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright (c) 2025 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.app.browser.defaultbrowsing.prompts.store | ||
|
||
import com.duckduckgo.feature.toggles.api.Toggle | ||
import com.duckduckgo.feature.toggles.api.Toggle.State.Cohort | ||
import java.time.format.DateTimeParseException | ||
|
||
/** | ||
* Tracks the days app is used to leverage for purposes related to experiments run with the [NA Experiment Framework](https://app.asana.com/0/1208889145294658/1208889101183474). | ||
* | ||
* See [Cohort.enrollmentDateET] for how the framework stores enrollment dates. | ||
*/ | ||
interface ExperimentAppUsageRepository { | ||
|
||
suspend fun recordAppUsedNow() | ||
|
||
/** | ||
* Returns the number of active days the app has been used since enrollment. | ||
* | ||
* Crossing a dateline in local time will not increment the returned count. | ||
* Only if a given instant crossed dateline in ET timezone, the value will be incremented. | ||
* | ||
* @return Count if successful. | ||
* [UserNotEnrolledException] if the given feature is disable or user is not assigned to a cohort. | ||
* [DateTimeParseException] if the enrollment date is malformed. | ||
*/ | ||
suspend fun getActiveDaysUsedSinceEnrollment(toggle: Toggle): Result<Long> | ||
|
||
class UserNotEnrolledException : Exception() | ||
} |
81 changes: 81 additions & 0 deletions
81
.../duckduckgo/app/browser/defaultbrowsing/prompts/store/ExperimentAppUsageRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright (c) 2025 DuckDuckGo | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.duckduckgo.app.browser.defaultbrowsing.prompts.store | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Entity | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.PrimaryKey | ||
import androidx.room.Query | ||
import com.duckduckgo.app.browser.defaultbrowsing.prompts.store.ExperimentAppUsageRepository.UserNotEnrolledException | ||
import com.duckduckgo.common.utils.DispatcherProvider | ||
import com.duckduckgo.di.scopes.AppScope | ||
import com.duckduckgo.feature.toggles.api.Toggle | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import dagger.SingleInstanceIn | ||
import java.time.ZoneId | ||
import java.time.ZonedDateTime | ||
import java.time.format.DateTimeFormatter | ||
import java.time.format.DateTimeParseException | ||
import java.time.temporal.ChronoUnit | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.withContext | ||
|
||
@ContributesBinding(scope = AppScope::class) | ||
@SingleInstanceIn(scope = AppScope::class) | ||
class ExperimentAppUsageRepositoryImpl @Inject constructor( | ||
private val dispatchers: DispatcherProvider, | ||
private val experimentAppUsageDao: ExperimentAppUsageDao, | ||
) : ExperimentAppUsageRepository { | ||
|
||
override suspend fun recordAppUsedNow() = withContext(dispatchers.io()) { | ||
val isoDateET = ZonedDateTime.now(ZoneId.of("America/New_York")) | ||
.truncatedTo(ChronoUnit.DAYS) | ||
.format(DateTimeFormatter.ISO_LOCAL_DATE) | ||
|
||
experimentAppUsageDao.insert(ExperimentAppUsageEntity(isoDateET)) | ||
} | ||
|
||
override suspend fun getActiveDaysUsedSinceEnrollment(toggle: Toggle): Result<Long> = withContext(dispatchers.io()) { | ||
toggle.getCohort()?.enrollmentDateET?.let { enrollmentZonedDateTimeETString -> | ||
try { | ||
val isoDateET = ZonedDateTime.parse(enrollmentZonedDateTimeETString) | ||
.truncatedTo(ChronoUnit.DAYS) | ||
.format(DateTimeFormatter.ISO_LOCAL_DATE) | ||
|
||
val daysUsed = experimentAppUsageDao.getNumberOfDaysAppUsedSinceDateET(isoDateET) | ||
Result.success(daysUsed) | ||
} catch (ex: DateTimeParseException) { | ||
Result.failure(ex) | ||
} | ||
} ?: Result.failure(UserNotEnrolledException()) | ||
} | ||
} | ||
|
||
@Dao | ||
abstract class ExperimentAppUsageDao { | ||
|
||
@Insert(onConflict = OnConflictStrategy.IGNORE) | ||
abstract fun insert(experimentAppUsageEntity: ExperimentAppUsageEntity) | ||
|
||
@Query("SELECT COUNT(*) from experiment_app_usage_entity WHERE isoDateET > :isoDateET") | ||
abstract fun getNumberOfDaysAppUsedSinceDateET(isoDateET: String): Long | ||
} | ||
|
||
@Entity(tableName = "experiment_app_usage_entity") | ||
data class ExperimentAppUsageEntity(@PrimaryKey val isoDateET: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.