Skip to content

Commit

Permalink
Fix trying to update UI on IO thread (#138)
Browse files Browse the repository at this point in the history
Closes #137

Reduces the work done on the IO thread to only the actual network calls.
  • Loading branch information
damontecres authored Feb 23, 2024
1 parent a1e878e commit ee2c621
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 142 deletions.
109 changes: 55 additions & 54 deletions app/src/main/java/com/github/damontecres/stashapp/util/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -244,61 +244,53 @@ suspend fun testStashConnection(
context: Context,
showToast: Boolean,
): ServerInfoQuery.Data? {
return withContext(Dispatchers.IO) {
val client = createApolloClient(context)
if (client == null) {
if (showToast) {
Toast.makeText(
context,
"Stash server URL is not set.",
Toast.LENGTH_LONG,
).show()
}
} else {
try {
val info = client.query(ServerInfoQuery()).execute()
if (info.hasErrors()) {
if (showToast) {
Toast.makeText(
context,
"Failed to connect to Stash. Check URL or API Key.",
Toast.LENGTH_LONG,
).show()
}
Log.w(Constants.TAG, "Errors in ServerInfoQuery: ${info.errors}")
} else {
if (showToast) {
val version = info.data?.version?.version
val sceneCount = info.data?.stats?.scene_count
Toast.makeText(
context,
"Connected to Stash ($version) with $sceneCount scenes!",
Toast.LENGTH_SHORT,
).show()
}
return@withContext info.data
val client = createApolloClient(context)
if (client == null) {
if (showToast) {
Toast.makeText(
context,
"Stash server URL is not set.",
Toast.LENGTH_LONG,
).show()
}
} else {
try {
val info =
withContext(Dispatchers.IO) {
client.query(ServerInfoQuery()).execute()
}
if (info.hasErrors()) {
if (showToast) {
Toast.makeText(
context,
"Failed to connect to Stash. Check URL or API Key.",
Toast.LENGTH_LONG,
).show()
}
Log.w(Constants.TAG, "Errors in ServerInfoQuery: ${info.errors}")
} else {
if (showToast) {
val version = info.data?.version?.version
val sceneCount = info.data?.stats?.scene_count
Toast.makeText(
context,
"Connected to Stash ($version) with $sceneCount scenes!",
Toast.LENGTH_SHORT,
).show()
}
} catch (ex: ApolloHttpException) {
Log.e(Constants.TAG, "ApolloHttpException", ex)
if (ex.statusCode == 401 || ex.statusCode == 403) {
if (showToast) {
Toast.makeText(
context,
"Failed to connect to Stash. API Key was not valid.",
Toast.LENGTH_LONG,
).show()
}
} else {
if (showToast) {
Toast.makeText(
context,
"Failed to connect to Stash. Error was '${ex.message}'",
Toast.LENGTH_LONG,
).show()
}
return info.data
}
} catch (ex: ApolloHttpException) {
Log.e(Constants.TAG, "ApolloHttpException", ex)
if (ex.statusCode == 401 || ex.statusCode == 403) {
if (showToast) {
Toast.makeText(
context,
"Failed to connect to Stash. API Key was not valid.",
Toast.LENGTH_LONG,
).show()
}
} catch (ex: ApolloException) {
Log.e(Constants.TAG, "ApolloException", ex)
} else {
if (showToast) {
Toast.makeText(
context,
Expand All @@ -307,9 +299,18 @@ suspend fun testStashConnection(
).show()
}
}
} catch (ex: ApolloException) {
Log.e(Constants.TAG, "ApolloException", ex)
if (showToast) {
Toast.makeText(
context,
"Failed to connect to Stash. Error was '${ex.message}'",
Toast.LENGTH_LONG,
).show()
}
}
return@withContext null
}
return null
}

fun convertFilter(filter: SavedFilterData.Find_filter?): FindFilterType? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,56 +38,57 @@ class MutationEngine(private val context: Context, private val showToasts: Boole
private val serverPreferences = ServerPreferences(context)

private suspend fun <D : Mutation.Data> executeMutation(mutation: Mutation<D>): ApolloResponse<D> {
return withContext(Dispatchers.IO) {
val mutationName = mutation.name()
try {
val response = client.mutation(mutation).execute()
if (response.errors.isNullOrEmpty()) {
Log.d(TAG, "executeMutation $mutationName successful")
return@withContext response
} else {
val errorMsgs = response.errors!!.joinToString("\n") { it.message }
if (showToasts) {
Toast.makeText(
context,
"${response.errors!!.size} errors in response ($mutationName)\n$errorMsgs",
Toast.LENGTH_LONG,
).show()
}
Log.e(TAG, "Errors in $mutationName: ${response.errors}")
throw MutationException("($mutationName), ${response.errors!!.size} errors in graphql response")
val mutationName = mutation.name()
try {
val response =
withContext(Dispatchers.IO) {
client.mutation(mutation).execute()
}
} catch (ex: ApolloNetworkException) {
if (response.errors.isNullOrEmpty()) {
Log.d(TAG, "executeMutation $mutationName successful")
return response
} else {
val errorMsgs = response.errors!!.joinToString("\n") { it.message }
if (showToasts) {
Toast.makeText(
context,
"Network error ($mutationName). Message: ${ex.message}, ${ex.cause?.message}",
"${response.errors!!.size} errors in response ($mutationName)\n$errorMsgs",
Toast.LENGTH_LONG,
).show()
}
Log.e(TAG, "Network error in $mutationName", ex)
throw MutationException("Network error ($mutationName)", ex)
} catch (ex: ApolloHttpException) {
if (showToasts) {
Toast.makeText(
context,
"HTTP error ($mutationName). Status=${ex.statusCode}, Msg=${ex.message}",
Toast.LENGTH_LONG,
).show()
}
Log.e(TAG, "HTTP ${ex.statusCode} error in $mutationName", ex)
throw MutationException("HTTP ${ex.statusCode} ($mutationName)", ex)
} catch (ex: ApolloException) {
if (showToasts) {
Toast.makeText(
context,
"Server query error ($mutationName). Msg=${ex.message}, ${ex.cause?.message}",
Toast.LENGTH_LONG,
).show()
}
Log.e(TAG, "ApolloException in $mutationName", ex)
throw MutationException("Apollo exception ($mutationName)", ex)
Log.e(TAG, "Errors in $mutationName: ${response.errors}")
throw MutationException("($mutationName), ${response.errors!!.size} errors in graphql response")
}
} catch (ex: ApolloNetworkException) {
if (showToasts) {
Toast.makeText(
context,
"Network error ($mutationName). Message: ${ex.message}, ${ex.cause?.message}",
Toast.LENGTH_LONG,
).show()
}
Log.e(TAG, "Network error in $mutationName", ex)
throw MutationException("Network error ($mutationName)", ex)
} catch (ex: ApolloHttpException) {
if (showToasts) {
Toast.makeText(
context,
"HTTP error ($mutationName). Status=${ex.statusCode}, Msg=${ex.message}",
Toast.LENGTH_LONG,
).show()
}
Log.e(TAG, "HTTP ${ex.statusCode} error in $mutationName", ex)
throw MutationException("HTTP ${ex.statusCode} ($mutationName)", ex)
} catch (ex: ApolloException) {
if (showToasts) {
Toast.makeText(
context,
"Server query error ($mutationName). Msg=${ex.message}, ${ex.cause?.message}",
Toast.LENGTH_LONG,
).show()
}
Log.e(TAG, "ApolloException in $mutationName", ex)
throw MutationException("Apollo exception ($mutationName)", ex)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,60 +46,61 @@ class QueryEngine(private val context: Context, private val showToasts: Boolean
private val client = createApolloClient(context) ?: throw StashNotConfiguredException()

private suspend fun <D : Operation.Data> executeQuery(query: ApolloCall<D>): ApolloResponse<D> {
return withContext(Dispatchers.IO) {
val queryName = query.operation.name()
Log.v(
TAG,
"executeQuery $queryName",
)
try {
val response = query.execute()
if (response.errors.isNullOrEmpty()) {
Log.v(TAG, "executeQuery $queryName successful")
return@withContext response
} else {
val errorMsgs = response.errors!!.joinToString("\n") { it.message }
if (showToasts) {
Toast.makeText(
context,
"${response.errors!!.size} errors in response ($queryName)\n$errorMsgs",
Toast.LENGTH_LONG,
).show()
}
Log.e(TAG, "Errors in $queryName: ${response.errors}")
throw QueryException("($queryName), ${response.errors!!.size} errors in graphql response")
val queryName = query.operation.name()
Log.v(
TAG,
"executeQuery $queryName",
)
try {
val response =
withContext(Dispatchers.IO) {
query.execute()
}
} catch (ex: ApolloNetworkException) {
if (showToasts) {
Toast.makeText(
context,
"Network error ($queryName). Message: ${ex.message}, ${ex.cause?.message}",
Toast.LENGTH_LONG,
).show()
}
Log.e(TAG, "Network error in $queryName", ex)
throw QueryException("Network error ($queryName)", ex)
} catch (ex: ApolloHttpException) {
if (showToasts) {
Toast.makeText(
context,
"HTTP error ($queryName). Status=${ex.statusCode}, Msg=${ex.message}",
Toast.LENGTH_LONG,
).show()
}
Log.e(TAG, "HTTP ${ex.statusCode} error in $queryName", ex)
throw QueryException("HTTP ${ex.statusCode} ($queryName)", ex)
} catch (ex: ApolloException) {
if (response.errors.isNullOrEmpty()) {
Log.v(TAG, "executeQuery $queryName successful")
return response
} else {
val errorMsgs = response.errors!!.joinToString("\n") { it.message }
if (showToasts) {
Toast.makeText(
context,
"Server query error ($queryName). Msg=${ex.message}, ${ex.cause?.message}",
"${response.errors!!.size} errors in response ($queryName)\n$errorMsgs",
Toast.LENGTH_LONG,
).show()
}
Log.e(TAG, "ApolloException in $queryName", ex)
throw QueryException("Apollo exception ($queryName)", ex)
Log.e(TAG, "Errors in $queryName: ${response.errors}")
throw QueryException("($queryName), ${response.errors!!.size} errors in graphql response")
}
} catch (ex: ApolloNetworkException) {
if (showToasts) {
Toast.makeText(
context,
"Network error ($queryName). Message: ${ex.message}, ${ex.cause?.message}",
Toast.LENGTH_LONG,
).show()
}
Log.e(TAG, "Network error in $queryName", ex)
throw QueryException("Network error ($queryName)", ex)
} catch (ex: ApolloHttpException) {
if (showToasts) {
Toast.makeText(
context,
"HTTP error ($queryName). Status=${ex.statusCode}, Msg=${ex.message}",
Toast.LENGTH_LONG,
).show()
}
Log.e(TAG, "HTTP ${ex.statusCode} error in $queryName", ex)
throw QueryException("HTTP ${ex.statusCode} ($queryName)", ex)
} catch (ex: ApolloException) {
if (showToasts) {
Toast.makeText(
context,
"Server query error ($queryName). Msg=${ex.message}, ${ex.cause?.message}",
Toast.LENGTH_LONG,
).show()
}
Log.e(TAG, "ApolloException in $queryName", ex)
throw QueryException("Apollo exception ($queryName)", ex)
}
}

Expand Down

0 comments on commit ee2c621

Please sign in to comment.