Skip to content

Commit

Permalink
refactor: move dispatcher to io for upload avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
yamilmedina committed Nov 4, 2024
1 parent 77e42a2 commit 075e335
Showing 1 changed file with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import com.wire.kalium.logic.data.user.UserRepository
import com.wire.kalium.logic.functional.flatMap
import com.wire.kalium.logic.functional.fold
import com.wire.kalium.logic.functional.map
import com.wire.kalium.util.KaliumDispatcherImpl
import kotlinx.coroutines.withContext
import okio.Path

interface UploadUserAvatarUseCase {
Expand All @@ -40,17 +42,20 @@ interface UploadUserAvatarUseCase {

internal class UploadUserAvatarUseCaseImpl(
private val userDataSource: UserRepository,
private val assetDataSource: AssetRepository
private val assetDataSource: AssetRepository,
private val dispatcher: KaliumDispatcherImpl = KaliumDispatcherImpl
) : UploadUserAvatarUseCase {

override suspend operator fun invoke(imageDataPath: Path, imageDataSize: Long): UploadAvatarResult {
return assetDataSource.uploadAndPersistPublicAsset("image/jpg", imageDataPath, imageDataSize).flatMap { asset ->
userDataSource.updateSelfUser(newAssetId = asset.key).map { asset }
}.fold({
UploadAvatarResult.Failure(it)
}) { updatedAsset ->
UploadAvatarResult.Success(UserAssetId(updatedAsset.key, updatedAsset.domain))
} // TODO(assets): remove old assets, non blocking this response, as will imply deleting locally and remotely
return withContext(dispatcher.io) {
assetDataSource.uploadAndPersistPublicAsset("image/jpg", imageDataPath, imageDataSize).flatMap { asset ->
userDataSource.updateSelfUser(newAssetId = asset.key).map { asset }
}.fold({
UploadAvatarResult.Failure(it)
}) { updatedAsset ->
UploadAvatarResult.Success(UserAssetId(updatedAsset.key, updatedAsset.domain))
} // TODO(assets): remove old assets, non blocking this response, as will imply deleting locally and remotely
}
}
}

Expand Down

0 comments on commit 075e335

Please sign in to comment.