Skip to content

Instantly share code, notes, and snippets.

@johnkil
Created October 20, 2023 15:02
Show Gist options
  • Save johnkil/f31c6d92837c83ea9ede27314cdc932a to your computer and use it in GitHub Desktop.
Save johnkil/f31c6d92837c83ea9ede27314cdc932a to your computer and use it in GitHub Desktop.
class SyncChallengesProgressUseCase @Inject constructor(
private val challengesRepository: ChallengesRepository,
private val getStepsFromFitnessApp: GetStepsFromFitnessAppUseCase,
getPhoneManufactureData: GetPhoneManufactureDataUseCase
) {
private val deviceId = getPhoneManufactureData().deviceId
suspend operator fun invoke(startDate: Instant): SyncChallengesProgressResult {
val now = Clock.System.now()
if (startDate.daysUntil(now, TimeZone.currentSystemDefault()) == 0) {
return SyncChallengesProgressResult.Success
} else {
val steps = getStepsFromFitnessApp(startDate, now).getOrElse {
return SyncChallengesProgressResult.Failure
}
val syncResponse = challengesRepository.syncChallengesProgress(
SyncChallengesProgressRequest(
deviceId = deviceId,
steps = ChallengeStepsModelMapper.ChallengeStepsModel(steps)
)
)
return if (syncResponse is ApiResponse.Success && syncResponse.data.success) {
SyncChallengesProgressResult.Success
} else {
SyncChallengesProgressResult.Failure
}
}
}
}
sealed interface SyncChallengesProgressResult {
data object Success : SyncChallengesProgressResult
data object Failure : SyncChallengesProgressResult
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment