-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
db40851
commit 02d67f6
Showing
17 changed files
with
146 additions
and
114 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
5 changes: 3 additions & 2 deletions
5
data/src/main/java/com/rubylichtenstein/data/breeds/BreedsRemoteApi.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 |
---|---|---|
@@ -1,14 +1,15 @@ | ||
package com.rubylichtenstein.data.breeds | ||
|
||
import com.rubylichtenstein.data.KtorHttpClient | ||
import com.rubylichtenstein.data.images.BASE_URL | ||
import io.ktor.client.request.get | ||
import javax.inject.Inject | ||
|
||
class BreedsRemoteApi @Inject constructor(private val client: KtorHttpClient) { | ||
|
||
suspend fun getAllBreeds(): Result<Map<String, List<String>>> { | ||
suspend fun getAllBreeds(): Map<String, List<String>> { | ||
return client.safeApiCall { | ||
get("${com.rubylichtenstein.data.images.BASE_URL}breeds/list/all") | ||
get("${BASE_URL}breeds/list/all") | ||
} | ||
} | ||
} |
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
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
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
29 changes: 0 additions & 29 deletions
29
ui/src/main/java/com/rubylichtenstein/ui/common/AsyncResult.kt
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
ui/src/main/java/com/rubylichtenstein/ui/common/UiState.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,46 @@ | ||
package com.rubylichtenstein.ui.common | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.catch | ||
import kotlinx.coroutines.flow.map | ||
|
||
fun <T, R> UiState<T>.mapSuccess(transform: (T) -> R): UiState<R> { | ||
return when (this) { | ||
is UiState.Loading -> UiState.Loading | ||
is UiState.Success -> UiState.Success(transform(data)) | ||
is UiState.Error -> UiState.Error(message) | ||
is UiState.Empty -> UiState.Empty(message) | ||
} | ||
} | ||
|
||
sealed interface UiState<out T> { | ||
data class Success<T>(val data: T) : UiState<T> | ||
data class Error(val message: String?) : UiState<Nothing> | ||
data object Loading : UiState<Nothing> | ||
data class Empty(val message: String?) : UiState<Nothing> | ||
} | ||
|
||
fun <T> Flow<T>.asUiState( | ||
errorMessage: String? = null | ||
): Flow<UiState<T>> { | ||
return this | ||
.map<T, UiState<T>> { | ||
UiState.Success(it) | ||
} | ||
.catch { emit(UiState.Error(errorMessage ?: it.message)) } | ||
} | ||
|
||
fun <T> Flow<List<T>>.asUiState( | ||
emptyMessage: String? = null, | ||
errorMessage: String? = null | ||
): Flow<UiState<List<T>>> { | ||
return this | ||
.map { | ||
if (it.isEmpty()) { | ||
UiState.Empty(emptyMessage) | ||
} else { | ||
UiState.Success(it) | ||
} | ||
} | ||
.catch { emit(UiState.Error(errorMessage ?: it.message)) } | ||
} |
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
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
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.