Skip to content

Commit

Permalink
Aggressively set the theme state on every chapter change
Browse files Browse the repository at this point in the history
  • Loading branch information
io7m committed Sep 10, 2024
1 parent 63511b2 commit 9aad052
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ internal class SR2Controller private constructor(

this.logger.debug("Translated {} to {}", chapterURL, resolvedURL)

val future =
val openFuture =
connection.openURL(resolvedURL.toString())
.handle { _, exception ->
if (exception != null) {
Expand Down Expand Up @@ -716,17 +716,28 @@ internal class SR2Controller private constructor(
* If there's a fragment, attempt to scroll to it.
*/

when (val fragment = chapterURL.substringAfter('#', "")) {
"" -> future

else ->
future.thenCompose {
connection.executeJS { js -> js.scrollToId(fragment) }
}.handle { _, exception ->
if (exception != null) {
this.logger.debug("{} Failed to scroll to fragment ID: ", this.name(), exception)
val scrollFuture =
when (val fragment = chapterURL.substringAfter('#', "")) {
"" -> openFuture

else ->
openFuture.thenCompose {
connection.executeJS { js -> js.scrollToId(fragment) }
}.handle { _, exception ->
if (exception != null) {
this.logger.debug("{} Failed to scroll to fragment ID: ", this.name(), exception)
}
}
}
}

/*
* Evidently, web views on more modern platforms will no longer preserve the theme state.
* We therefore need to set the theme information every time the chapter changes for
* any reason.
*/

scrollFuture.thenCompose {
this.executeThemeSet(this.waitForWebViewAvailability(), this.themeNow())
}
} catch (e: Exception) {
this.logger.error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ import androidx.core.view.MenuItemCompat
import androidx.core.view.forEach
import androidx.core.view.isVisible
import io.reactivex.disposables.CompositeDisposable
import org.librarysimplified.r2.api.SR2Bookmark
import org.librarysimplified.r2.api.SR2Bookmark.Type.EXPLICIT
import org.librarysimplified.r2.api.SR2ControllerType
import org.librarysimplified.r2.api.SR2Event
import org.librarysimplified.r2.api.SR2Event.SR2BookmarkEvent.SR2BookmarkCreated
import org.librarysimplified.r2.api.SR2Event.SR2BookmarkEvent.SR2BookmarkDeleted
Expand All @@ -32,7 +29,6 @@ import org.librarysimplified.r2.api.SR2Event.SR2Error.SR2WebViewInaccessible
import org.librarysimplified.r2.api.SR2Event.SR2OnCenterTapped
import org.librarysimplified.r2.api.SR2Event.SR2ReadingPositionChanged
import org.librarysimplified.r2.api.SR2Event.SR2ThemeChanged
import org.librarysimplified.r2.api.SR2Locator
import org.librarysimplified.r2.api.SR2ScrollingMode.SCROLLING_MODE_CONTINUOUS
import org.librarysimplified.r2.api.SR2ScrollingMode.SCROLLING_MODE_PAGINATED
import org.librarysimplified.r2.api.SR2Theme
Expand Down Expand Up @@ -297,21 +293,6 @@ class SR2ReaderFragment : SR2Fragment() {
this.menuBookmarkItem.icon?.colorFilter = currentColorFilter
}

private fun findBookmarkForCurrentPage(
controllerNow: SR2ControllerType,
currentPosition: SR2Locator,
): SR2Bookmark? {
return controllerNow.bookmarksNow()
.find { bookmark -> this.locationMatchesBookmark(bookmark, currentPosition) }
}

private fun locationMatchesBookmark(
bookmark: SR2Bookmark,
location: SR2Locator,
): Boolean {
return bookmark.type == EXPLICIT && location.compareTo(bookmark.locator) == 0
}

private fun onControllerEvent(event: SR2Event) {
SR2UIThread.checkIsUIThread()

Expand Down
2 changes: 1 addition & 1 deletion org.thepalaceproject.android.platform

0 comments on commit 9aad052

Please sign in to comment.