Skip to content

Commit

Permalink
Kitsu fixes #1151
Browse files Browse the repository at this point in the history
  • Loading branch information
Koitharu committed Nov 24, 2024
1 parent c993488 commit 3ebde02
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class ScrobblingSelectorSheet :
lateinit var coil: ImageLoader

private var collapsibleActionViewCallback: CollapseActionViewCallback? = null

private var paginationScrollListener: PaginationScrollListener? = null
private val viewModel by viewModels<ScrobblingSelectorViewModel>()

override fun onCreateViewBinding(inflater: LayoutInflater, container: ViewGroup?): SheetScrobblingSelectorBinding {
Expand All @@ -77,7 +77,11 @@ class ScrobblingSelectorSheet :
adapter = listAdapter
addItemDecoration(decoration)
addItemDecoration(TypedListSpacingDecoration(context, false))
addOnScrollListener(PaginationScrollListener(4, this@ScrobblingSelectorSheet))
addOnScrollListener(
PaginationScrollListener(4, this@ScrobblingSelectorSheet).also {
paginationScrollListener = it
},
)
}
binding.buttonDone.setOnClickListener(this)
initOptionsMenu()
Expand Down Expand Up @@ -112,6 +116,7 @@ class ScrobblingSelectorSheet :
override fun onDestroyView() {
super.onDestroyView()
collapsibleActionViewCallback = null
paginationScrollListener = null
}

override fun onCurrentListChanged(previousList: MutableList<ListModel>, currentList: MutableList<ListModel>) {
Expand All @@ -124,6 +129,7 @@ class ScrobblingSelectorSheet :
currentList.indexOfFirst { it is ScrobblerManga && it.id == selectedId }.coerceAtLeast(0)
}
rv.post(RecyclerViewScrollCallback(rv, target, if (target == 0) 0 else rv.height / 3))
paginationScrollListener?.postInvalidate(rv)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.plus
import org.koitharu.kotatsu.R
Expand Down Expand Up @@ -59,7 +58,7 @@ class ScrobblingSelectorViewModel @Inject constructor(
get() = availableScrobblers[selectedScrobblerIndex.requireValue()]

val content: StateFlow<List<ListModel>> = combine(
scrobblerMangaList.map { it.distinctBy { x -> x.id } },
scrobblerMangaList,
listError,
hasNextPage,
) { list, error, isHasNextPage ->
Expand Down Expand Up @@ -127,14 +126,17 @@ class ScrobblingSelectorViewModel @Inject constructor(
runCatchingCancellable {
currentScrobbler.findManga(checkNotNull(searchQuery.value), offset)
}.onSuccess { list ->
if (!append) {
scrobblerMangaList.value = list
} else if (list.isNotEmpty()) {
scrobblerMangaList.value += list
}
hasNextPage.value = list.isNotEmpty()
val newList = (if (append) {
scrobblerMangaList.value + list
} else {
list
}).distinctBy { x -> x.id }
val changed = newList != scrobblerMangaList.value
scrobblerMangaList.value = newList
hasNextPage.value = changed && newList.isNotEmpty()
}.onFailure { error ->
error.printStackTraceDebug()
hasNextPage.value = false
listError.value = error
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package org.koitharu.kotatsu.scrobbling.kitsu.data

import okhttp3.Interceptor
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.Response
import okhttp3.internal.closeQuietly
import okio.IOException
import org.koitharu.kotatsu.core.network.CommonHeaders
import org.koitharu.kotatsu.parsers.util.mimeType
import org.koitharu.kotatsu.parsers.util.parseHtml
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
import org.koitharu.kotatsu.scrobbling.common.data.ScrobblerStorage
import org.koitharu.kotatsu.scrobbling.common.domain.ScrobblerAuthRequiredException
import org.koitharu.kotatsu.scrobbling.common.domain.model.ScrobblerService
Expand All @@ -23,13 +29,23 @@ class KitsuInterceptor(private val storage: ScrobblerStorage) : Interceptor {
}
val response = chain.proceed(request.build())
if (!isAuthRequest && response.code == HttpURLConnection.HTTP_UNAUTHORIZED) {
response.closeQuietly()
throw ScrobblerAuthRequiredException(ScrobblerService.KITSU)
}
if (response.mimeType?.toMediaTypeOrNull()?.subtype == SUBTYPE_HTML) {
val message = runCatchingCancellable {
response.parseHtml().title().takeUnless { it.isEmpty() }
}.onFailure {
response.closeQuietly()
}.getOrNull() ?: "Invalid response (${response.code})"
throw IOException(message)
}
return response
}

companion object {

const val VND_JSON = "application/vnd.api+json"
const val SUBTYPE_HTML = "html"
}
}
8 changes: 4 additions & 4 deletions app/src/main/res/layout/activity_kitsu_auth.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="emailAddress"
android:hint="@string/email"
android:imeOptions="actionDone"
android:inputType="textEmailAddress"
android:maxLength="512"
android:singleLine="true"
android:textSize="16sp"
tools:hint="Email" />
android:textSize="16sp" />

</com.google.android.material.textfield.TextInputLayout>

Expand All @@ -83,12 +83,12 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="password"
android:hint="@string/password"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:maxLength="512"
android:singleLine="true"
android:textSize="16sp"
tools:hint="Password" />
android:textSize="16sp" />

</com.google.android.material.textfield.TextInputLayout>

Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -767,4 +767,5 @@
<string name="delete_old_backups_summary">Automatically delete old backup files to save storage space</string>
<string name="handle_links">Handle links</string>
<string name="handle_links_summary">Handle manga links from external applications (e.g. web browser). You may also need to enable it manually in the application\'s system settings</string>
<string name="email">Email</string>
</resources>

0 comments on commit 3ebde02

Please sign in to comment.