Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancing Library Usability with Communication Error Handling in Queries and Mutations #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 32 additions & 14 deletions query/src/main/java/me/pavi2410/useCompose/query/hooks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,65 @@ sealed interface QueryState<out T> {
data class Content<T>(val data: T) : QueryState<T>
}

sealed interface MutationState<T> {
sealed interface MutationState<out T> {
object Idle : MutationState<Nothing>
object Loading : MutationState<Nothing>
data class Error(val message: Throwable) : MutationState<Nothing>
data class Content<T>(val data: T) : MutationState<T>
object Success : MutationState<Nothing>
data class Error(val th: Throwable) : MutationState<Nothing>
// data class Content<T>(val data: T) : MutationState<T> // we unknown callback return type
}

interface Mutation<T> {
interface Mutation<T> {
fun mutate(vararg args: String, callback: (T) -> Unit)
fun cancel()
// val state: MutationState<T>
fun reset()
val state: MutationState<T>
}

@Composable
fun <T> useQuery(query: suspend CoroutineScope.() -> T): State<QueryState<T>> {
return produceState<QueryState<T>>(initialValue = QueryState.Loading) {
value = withContext(Dispatchers.IO) {
val res = query()
QueryState.Content(res)
val res : T
try {
res = query()
QueryState.Content(res)
}catch (e : Exception){
QueryState.Error(e)
}
}
}
}

@SuppressLint("ComposableNaming")
@Composable
fun <T> useMutation(query: suspend CoroutineScope.(args: Array<out String>) -> T): Mutation<T> {
// var mutationState by remember { mutableStateOf(MutationState.Idle) }
var mutationState : MutationState<Nothing> by remember { mutableStateOf(MutationState.Idle) }
val coroutineScope = rememberCoroutineScope()
return object: Mutation<T> {
override fun mutate(vararg args: String, callback: (T) -> Unit) {
coroutineScope.launch {
// mutationState = MutationState.Loading
callback(query(args))
// mutationState = MutationState.Success
mutationState = MutationState.Loading
try {
callback(query(args))
mutationState = MutationState.Success
MutationState.Success
} catch (e : Exception){
mutationState = MutationState.Error(e)
MutationState.Error(e)
}

}
}
override fun cancel() {
coroutineScope.cancel()
}

// override val state: MutationState<T>
// get() = mutationState
override fun reset () {
mutationState = MutationState.Idle
}

override val state: MutationState<Nothing>
get() = mutationState
}
}
}
Loading