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

Fixes sorting for filters & more logging #43

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
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
15 changes: 11 additions & 4 deletions app/src/main/java/com/github/damontecres/stashapp/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ object Constants {
const val STASH_API_HEADER = "ApiKey"
const val PREF_KEY_STASH_URL = "stashUrl"
const val PREF_KEY_STASH_API_KEY = "stashApi"

const val TAG = "Constants"
}

/**
Expand Down Expand Up @@ -108,7 +110,7 @@ fun createApolloClient(
stashUrl: String?,
apiKey: String?,
): ApolloClient? {
return if (stashUrl!!.isNotBlank()) {
return if (!stashUrl.isNullOrBlank()) {
var cleanedStashUrl = stashUrl.trim()
if (!cleanedStashUrl.startsWith("http://") && !cleanedStashUrl.startsWith("https://")) {
// Assume http
Expand All @@ -119,12 +121,16 @@ fun createApolloClient(
url.buildUpon()
.path("/graphql") // Ensure the URL is the graphql endpoint
.build()
Log.d("Constants", "StashUrl: $stashUrl => $url")
Log.d(Constants.TAG, "StashUrl: $stashUrl => $url")
ApolloClient.Builder()
.serverUrl(url.toString())
.addHttpInterceptor(AuthorizationInterceptor(apiKey))
.build()
} else {
Log.v(
Constants.TAG,
"Cannot create ApolloClient: stashUrl='$stashUrl', apiKey set: ${!apiKey.isNullOrBlank()}",
)
null
}
}
Expand Down Expand Up @@ -168,6 +174,7 @@ suspend fun testStashConnection(
Toast.LENGTH_LONG,
).show()
}
Log.w(Constants.TAG, "Errors in ServerInfoQuery: ${info.errors}")
} else {
if (showToast) {
val version = info.data?.version?.version
Expand All @@ -181,7 +188,7 @@ suspend fun testStashConnection(
return true
}
} catch (ex: ApolloHttpException) {
Log.e("Constants", "ApolloHttpException", ex)
Log.e(Constants.TAG, "ApolloHttpException", ex)
if (ex.statusCode == 401 || ex.statusCode == 403) {
if (showToast) {
Toast.makeText(
Expand All @@ -200,7 +207,7 @@ suspend fun testStashConnection(
}
}
} catch (ex: ApolloException) {
Log.e("Constants", "ApolloException", ex)
Log.e(Constants.TAG, "ApolloException", ex)
if (showToast) {
Toast.makeText(
context,
Expand Down
130 changes: 76 additions & 54 deletions app/src/main/java/com/github/damontecres/stashapp/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ class MainFragment : BrowseSupportFragment() {
addCustomFilterRow(frontPageFilter, adapter, queryEngine)
} else if (filterType == "SavedFilter") {
addSavedFilterRow(frontPageFilter, adapter, queryEngine)
} else {
Log.w(TAG, "Unknown frontPageFilter typename: $filterType")
}
}
}
Expand All @@ -267,68 +269,86 @@ class MainFragment : BrowseSupportFragment() {
CoroutineExceptionHandler { _, ex ->
Log.e(TAG, "Exception in addCustomFilterRow", ex)
}
val msg = frontPageFilter["message"] as Map<String, *>
val objType =
(msg["values"] as Map<String, String>)["objects"] as String
val description =
when (msg["id"] as String) {
"recently_added_objects" -> "Recently Added $objType"
"recently_released_objects" -> "Recently Released $objType"
else -> objType
try {
val msg = frontPageFilter["message"] as Map<String, *>
val objType =
(msg["values"] as Map<String, String>)["objects"] as String
val description =
when (msg["id"].toString()) {
"recently_added_objects" -> "Recently Added $objType"
"recently_released_objects" -> "Recently Released $objType"
else -> objType
}
val sortBy =
(frontPageFilter["sortBy"] as String?)
?: // Some servers may return sortBy in lowercase
(frontPageFilter["sortby"] as String?) ?: when (msg["id"].toString()) {
// Just in case, fall back to a reasonable default
"recently_added_objects" -> "created_at"
"recently_released_objects" -> "date"
else -> null
}
val mode = FilterMode.safeValueOf(frontPageFilter["mode"] as String)
if (mode !in supportedFilterModes) {
Log.w(TAG, "CustomFilter mode is $mode which is not supported yet")
return
}
val mode = FilterMode.valueOf(frontPageFilter["mode"] as String)
if (mode !in supportedFilterModes) {
return
}
rowsAdapter.add(
ListRow(
HeaderItem(description),
adapter,
),
)
viewLifecycleOwner.lifecycleScope.launch(exHandler) {
val direction = frontPageFilter["direction"] as String?
val directionEnum =
if (direction != null) {
val enum = SortDirectionEnum.safeValueOf(direction.uppercase())
if (enum == SortDirectionEnum.UNKNOWN__) {
SortDirectionEnum.ASC
rowsAdapter.add(
ListRow(
HeaderItem(description),
adapter,
),
)
viewLifecycleOwner.lifecycleScope.launch(exHandler) {
val direction = frontPageFilter["direction"] as String?
val directionEnum =
if (direction != null) {
val enum = SortDirectionEnum.safeValueOf(direction.uppercase())
if (enum == SortDirectionEnum.UNKNOWN__) {
SortDirectionEnum.DESC
}
enum
} else {
SortDirectionEnum.DESC
}
enum
} else {
SortDirectionEnum.ASC
}

val sortBy = frontPageFilter["sortBy"] as String?
val filter =
FindFilterType(
direction = Optional.presentIfNotNull(directionEnum),
sort = Optional.presentIfNotNull(sortBy),
per_page = Optional.present(25),
)
val filter =
FindFilterType(
direction = Optional.presentIfNotNull(directionEnum),
sort = Optional.presentIfNotNull(sortBy),
per_page = Optional.present(25),
)

when (mode) {
FilterMode.SCENES -> {
adapter.addAll(0, queryEngine.findScenes(filter))
}
when (mode) {
FilterMode.SCENES -> {
adapter.addAll(0, queryEngine.findScenes(filter))
}

FilterMode.STUDIOS -> {
adapter.addAll(0, queryEngine.findStudios(filter))
}
FilterMode.STUDIOS -> {
adapter.addAll(0, queryEngine.findStudios(filter))
}

FilterMode.PERFORMERS -> {
adapter.addAll(0, queryEngine.findPerformers(filter))
}
FilterMode.PERFORMERS -> {
adapter.addAll(0, queryEngine.findPerformers(filter))
}

FilterMode.MOVIES -> {
adapter.addAll(0, queryEngine.findMovies(filter))
}
FilterMode.MOVIES -> {
adapter.addAll(0, queryEngine.findMovies(filter))
}

else -> {
Log.i(TAG, "Unsupported mode in frontpage: $mode")
else -> {
Log.w(TAG, "Unsupported mode in frontpage: $mode")
}
}
adapter.add(StashCustomFilter(mode, direction, sortBy, description))
}
adapter.add(StashCustomFilter(mode, direction, sortBy, description))
} catch (ex: Exception) {
Log.e(TAG, "Exception during addCustomFilterRow", ex)
Toast.makeText(
requireContext(),
"CustomFilter parse error: ${ex.message}",
Toast.LENGTH_LONG,
).show()
}
}

Expand All @@ -346,11 +366,11 @@ class MainFragment : BrowseSupportFragment() {
Toast.LENGTH_LONG,
).show()
}
val filterId = frontPageFilter["savedFilterId"]
val header = HeaderItem("")
val listRow = ListRow(header, adapter)
rowsAdapter.add(listRow)
viewLifecycleOwner.lifecycleScope.launch(exHandler) {
val filterId = frontPageFilter["savedFilterId"]
val result = queryEngine.getSavedFilter(filterId.toString())

val index = rowsAdapter.indexOf(listRow)
Expand Down Expand Up @@ -411,13 +431,15 @@ class MainFragment : BrowseSupportFragment() {
}

else -> {
Log.i(
Log.w(
TAG,
"Unsupported mode in frontpage: ${result?.mode}",
)
}
}
adapter.add(StashSavedFilter(filterId.toString(), result!!.mode))
} else {
Log.w(TAG, "SavedFilter mode is ${result?.mode} which is not supported yet")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ class QueryEngine(private val context: Context, private val showToasts: Boolean

private suspend fun <D : Operation.Data> executeQuery(query: ApolloCall<D>): ApolloResponse<D> {
val queryName = query.operation.name()
Log.d(TAG, "executeQuery $queryName: ${query.operation}")
try {
val response = query.execute()
if (response.errors.isNullOrEmpty()) {
Log.d(TAG, "executeQuery $queryName successful")
return response
} else {
val errorMsgs = response.errors!!.map { it.message }.joinToString("\n")
Expand All @@ -52,7 +54,7 @@ class QueryEngine(private val context: Context, private val showToasts: Boolean
Toast.LENGTH_LONG,
).show()
}
Log.e(TAG, "Errors in $queryName:\n$errorMsgs")
Log.e(TAG, "Errors in $queryName: ${response.errors}")
throw QueryException("($queryName), ${response.errors!!.size} errors in graphql response")
}
} catch (ex: ApolloNetworkException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class VideoDetailsFragment : DetailsSupportFragment() {
initializeBackground(mSelectedMovie)
onItemViewClickedListener = StashItemViewClickListener(requireActivity())
} else {
Log.w(TAG, "No movie found in intent")
val intent = Intent(requireActivity(), MainActivity::class.java)
startActivity(intent)
}
Expand Down
Loading