Skip to content

Commit 9c0fd09

Browse files
authored
Merge pull request #27 from Notalib/fix/scroll-to-location
fix: when loading a chapter in a new file, scrollToLocation didn't work right
2 parents 59d8096 + 7e2df24 commit 9c0fd09

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

flutter_readium/android/src/main/kotlin/dk/nota/flutter_readium/ReadiumReader.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ object ReadiumReader : TimebasedNavigator.TimebasedListener, EpubNavigator.Visua
781781
return epubNavigator?.isReaderReady() ?: false
782782
}
783783

784-
fun epubGo(locator: Locator, animated: Boolean) {
784+
suspend fun epubGo(locator: Locator, animated: Boolean) {
785785
epubNavigator?.go(locator, animated)
786786
}
787787

flutter_readium/android/src/main/kotlin/dk/nota/flutter_readium/ReadiumReaderWidget.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ class ReadiumReaderWidget(
175175
}
176176

177177
override fun onVisualCurrentLocationChanged(locator: Locator) {
178-
Log.d(TAG, "::onVisualCurrentLocationChanged $Locator")
178+
Log.d(TAG, "::onVisualCurrentLocationChanged $locator")
179179
}
180180

181181
override fun onVisualReaderIsReady() {

flutter_readium/android/src/main/kotlin/dk/nota/flutter_readium/Utils.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.app.Application
55
import android.content.Context
66
import android.content.ContextWrapper
77
import kotlinx.coroutines.CoroutineScope
8+
import kotlinx.coroutines.flow.MutableStateFlow
89
import kotlinx.coroutines.withContext
910
import org.json.JSONArray
1011
import org.json.JSONObject
@@ -79,3 +80,7 @@ fun canScroll(locations: Locator.Locations) =
7980
): T {
8081
return withContext(scope.coroutineContext, block)
8182
}
83+
84+
fun <T> MutableStateFlow<T>.update(new: T) {
85+
if (this.value != new) this.value = new
86+
}

flutter_readium/android/src/main/kotlin/dk/nota/flutter_readium/fragments/EpubReaderFragment.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class EpubReaderFragment : VisualReaderFragment(), EpubNavigatorFragment.Listene
130130
Log.d(TAG, "::goRight")
131131
val navigator = epubNavigator
132132
if (navigator == null) {
133-
Log.d(TAG, "::goLeft. Navigator not ready.")
133+
Log.d(TAG, "::goRight. Navigator not ready.")
134134
return
135135
}
136136

@@ -289,15 +289,16 @@ class EpubReaderFragment : VisualReaderFragment(), EpubNavigatorFragment.Listene
289289
return
290290
}
291291

292-
// DFG: This will be relative to your app's src/main/assets/ folder.
293-
// To reference assets from other flutter packages use 'flutter_assets/packages/<package>/assets/.*'
294-
// Readium uses WebViewAssetLoader.AssetsPathHandler under the surface.
295292
val preferences = model.preferences ?: EpubPreferences()
296293
model.preferences = preferences
297294
val navigatorFactory = model.navigatorFactory!!
298295
val fragmentFactory = navigatorFactory.createFragmentFactory(
299296
configuration = EpubNavigatorFragment.Configuration(
300297
shouldApplyInsetsPadding = false,
298+
299+
// DFG: This will be relative to your app's src/main/assets/ folder.
300+
// To reference assets from other flutter packages use 'flutter_assets/packages/<package>/assets/.*'
301+
// Readium uses WebViewAssetLoader.AssetsPathHandler under the surface.
301302
servedAssets = listOf(
302303
"flutter_assets/packages/flutter_readium/assets/.*",
303304
)

flutter_readium/android/src/main/kotlin/dk/nota/flutter_readium/navigators/EpubNavigator.kt

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ class EpubNavigator : BaseNavigator, EpubReaderFragment.Listener {
8282
var editor: EpubPreferencesEditor? = null
8383

8484
/*
85-
* The initial locations to scroll to when the navigator is ready.
85+
* Pending scroll target to be applied when the page is loaded.
8686
*/
87-
var initialLocations: Locator.Locations? = null
87+
var pendingScrollToLocations: Locator.Locations? = null
8888

8989
val preferences: EpubPreferences?
9090
get() = editor?.preferences
@@ -105,7 +105,7 @@ class EpubNavigator : BaseNavigator, EpubReaderFragment.Listener {
105105
}
106106

107107
override suspend fun initNavigator() {
108-
initialLocations =
108+
pendingScrollToLocations =
109109
initialLocator?.locations?.let { locations ->
110110
if (canScroll(locations)) locations else null
111111
}
@@ -132,20 +132,23 @@ class EpubNavigator : BaseNavigator, EpubReaderFragment.Listener {
132132
}
133133
}
134134

135-
fun go(locator: Locator, animated: Boolean) {
135+
suspend fun go(locator: Locator, animated: Boolean): Boolean {
136136
val navigator = epubNavigator
137137
if (navigator == null) {
138138
Log.d(TAG, "::go - epubNavigator is null!")
139-
return
139+
return false
140140
}
141141

142-
mainScope.launch {
142+
return withScope(mainScope) {
143143
afterFragmentStarted()
144-
if (navigator.go(locator, animated)) {
145-
Log.d(TAG, "GO returned.")
146-
} else {
147-
Log.w(TAG, "GO FAILED!")
144+
if (!navigator.go(locator, animated)) {
145+
Log.w(TAG, "::go - FAILED!")
146+
return@withScope false
148147
}
148+
149+
Log.d(TAG, "::go - returned true")
150+
151+
return@withScope true
149152
}
150153
}
151154

@@ -217,13 +220,15 @@ class EpubNavigator : BaseNavigator, EpubReaderFragment.Listener {
217220
Log.d(TAG, "::onPageLoaded")
218221
visualListener.onPageLoaded()
219222

220-
val locations = initialLocations
221-
if (locations != null) {
222-
initialLocations = null
223+
pendingScrollToLocations?.let { locations ->
224+
Log.d(TAG, "::onPageLoaded - pendingScrollToLocations: $locations")
223225

224-
mainScope.launch {
226+
mainScope.async {
225227
scrollToLocations(locations, toStart = true)
226228
}
229+
230+
pendingScrollToLocations = null
231+
227232
}
228233

229234
notifyIsReady()
@@ -385,14 +390,14 @@ class EpubNavigator : BaseNavigator, EpubReaderFragment.Listener {
385390

386391
if (shouldGo) {
387392
Log.d(TAG, "::goToLocator: Go to $locatorHref from $currentHref")
393+
pendingScrollToLocations = locations
388394
go(locator, animated)
389395
} else if (!shouldScroll) {
390396
Log.w(TAG, "::goToLocator: Already at $locatorHref, no scroll target, go to start")
391397
scrollToLocations(Locator.Locations(progression = 0.0), true)
392398
} else {
393-
Log.d(TAG, "::goToLocator: Don't go to $locatorHref, already there")
394-
}
395-
if (shouldScroll) {
399+
Log.d(TAG, "::goToLocator: Already at $locatorHref, scroll to position")
400+
396401
scrollToLocations(locations, false)
397402
}
398403
}.await()

0 commit comments

Comments
 (0)