Created
February 9, 2021 12:47
-
-
Save RageshAntony/a4fd357973485b5fb8aef0e189ee9e7e to your computer and use it in GitHub Desktop.
How to use Sealed Class Data from Flow in Swift in Kotlin Mutliplatfomr Mobile ?
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(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
import com.apollographql.apollo.api.internal.json.Closeable | |
import kotlinx.coroutines.* | |
import kotlinx.coroutines.channels.* | |
import kotlinx.coroutines.flow.* | |
fun <T> ConflatedBroadcastChannel<T>.wrap(): CFlow<T> = CFlow(asFlow()) | |
fun <T> Flow<T>.wrap(): CFlow<T> = CFlow(this) | |
class CFlow<T>(private val origin: Flow<T>) : Flow<T> by origin { | |
fun watch(block: (T) -> Unit): Closeable { | |
val job = Job(/*ConferenceService.coroutineContext[Job]*/) | |
onEach { | |
block(it) | |
}.launchIn(CoroutineScope(Dispatchers.Default + job)) | |
return object : Closeable { | |
override fun close() { | |
job.cancel() | |
} | |
} | |
} | |
} |
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
func getCats(){ | |
CategoryService().getAllCategories { | |
(flowResult, error) -> Void in | |
flowResult?.watch { (sealedResult) -> Void in | |
switch sealedResult { | |
case is NetworkStatusData<NSArray>: | |
let rs = result as! NetworkStatusData<NSArray> | |
rs.data // How to access the Data | |
break | |
case .none: break | |
//dd | |
case .some(_): break | |
//dd | |
} | |
} | |
} | |
} |
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
GlobalScope.launch(Dispatchers.IO) { | |
CategoryService().getAllCategories().collect { | |
when (it) { | |
is NetworkStatus.Loading -> TODO() | |
is NetworkStatus.CustomSignal -> TODO() | |
is NetworkStatus.CustomSignalDetailed -> TODO() | |
is NetworkStatus.Failure -> { | |
} | |
is NetworkStatus.Data -> { | |
withContext(Dispatchers.Main) { | |
tv.text = it.data[0].name | |
} | |
} | |
} | |
} | |
} |
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
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