Created
February 1, 2021 16:35
-
-
Save RageshAntony/b8003be28833ec5bc8b50a27ce043e51 to your computer and use it in GitHub Desktop.
Network Status Sealed Class in Kotlin without Erased Type problem
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
@ApolloExperimental | |
override suspend fun getAllCategories(): CFlow<NetworkStatus<List<GetAllCategoriesQuery.Category>>> { | |
return flow<NetworkStatus<List<GetAllCategoriesQuery.Category>>> { | |
apolloClient.query(GetAllCategoriesQuery()).execute().collect { | |
if (it.hasErrors()) { | |
emit( NetworkStatus.customStatusDetailed(ErrorCaseData(404, "Apollo Error", "APP"))) | |
} | |
else { | |
emit(NetworkStatus.data<List<GetAllCategoriesQuery.Category>>(it.data?.category as List<GetAllCategoriesQuery.Category> ) ) | |
} | |
} | |
}.wrap() | |
} |
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
package india.techrova.kmmapollotest.shared.data.api | |
sealed class NetworkStatus<T> { | |
data class Loading<T>(var loading: Boolean) : NetworkStatus<T> () | |
data class CustomSignal<T>(var signal: String) : NetworkStatus<T>() | |
data class CustomSignalDetailed<T>(var signal: ErrorCaseData) : NetworkStatus<T>() | |
data class Failure<T>(val e: Throwable) : NetworkStatus<T>() | |
data class Data<T> (val data: T ) : NetworkStatus<T>() | |
companion object { | |
fun <T>loading(isLoading: Boolean): NetworkStatus<T> = Loading(isLoading) | |
fun <T> customstatus(signal: String): NetworkStatus<T> = CustomSignal(signal) | |
fun <T> customStatusDetailed(signals: ErrorCaseData): NetworkStatus<T> = CustomSignalDetailed(signals) | |
fun <T> failure(e: Throwable): NetworkStatus<T> = Failure(e) | |
fun <T> data(data: T): NetworkStatus<T> = Data<T>(data) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment