Skip to content

Commit 72a3acd

Browse files
committed
chor: stop using suspendCoroutine
1 parent 4362169 commit 72a3acd

File tree

2 files changed

+30
-42
lines changed

2 files changed

+30
-42
lines changed

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,16 @@ internal class ReadiumReaderView(
140140
initialLocations = initialLocator?.locations?.let { if (canScroll(it)) it else null }
141141

142142
if (!reuseFragment || epubReaderFragment == null) {
143+
val vm = EpubReaderViewModel();
144+
vm.identifier = pubIdentifier
145+
vm.pubUrl = pubUrl
146+
vm.publication = publication
147+
vm.locator = initialLocator
148+
vm.preferences = initialPreferences
149+
143150
navigator = EpubReaderFragment()
144-
navigator.vm = EpubReaderViewModel().let()
145-
{
146-
it.identifier = pubIdentifier
147-
it.pubUrl = pubUrl
148-
it.publication = publication
149-
it.locator = initialLocator
150-
it.preferences = initialPreferences
151-
152-
it
153-
}
151+
navigator.vm = vm
152+
154153
layout = LinearLayout(context, attrs)
155154
layout.id = generateViewId()
156155

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

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@ import android.view.ViewGroup
88
import androidx.fragment.app.commitNow
99
import androidx.lifecycle.lifecycleScope
1010
import dk.nota.flutter_readium.R
11-
import dk.nota.flutter_readium.StartLifecycleObserver
1211
import dk.nota.flutter_readium.models.EpubReaderViewModel
1312
import dk.nota.flutter_readium.throttleLatest
1413
import kotlinx.coroutines.CoroutineScope
1514
import kotlinx.coroutines.MainScope
1615
import kotlinx.coroutines.flow.MutableStateFlow
1716
import kotlinx.coroutines.launch
18-
import kotlinx.serialization.encodeToString
1917
import kotlinx.serialization.json.Json
2018
import org.readium.r2.navigator.Decoration
2119
import org.readium.r2.navigator.epub.EpubNavigatorFragment
@@ -24,8 +22,6 @@ import org.readium.r2.navigator.epub.EpubPreferencesEditor
2422
import org.readium.r2.shared.ExperimentalReadiumApi
2523
import org.readium.r2.shared.publication.Locator
2624
import org.readium.r2.shared.util.AbsoluteUrl
27-
import kotlin.coroutines.resume
28-
import kotlin.coroutines.suspendCoroutine
2925
import kotlin.time.Duration
3026

3127

@@ -152,20 +148,20 @@ class EpubReaderFragment : VisualReaderFragment(), EpubNavigatorFragment.Listene
152148
return
153149
}
154150

151+
if (editor == null) {
152+
return
153+
}
154+
155155
try {
156-
suspendCoroutine {
157-
editor?.let {
158-
it.apply {
159-
fontFamily.set(preferences.fontFamily)
160-
fontSize.set(preferences.fontSize)
161-
fontWeight.set(preferences.fontWeight)
162-
scroll.set(preferences.scroll)
163-
backgroundColor.set(preferences.backgroundColor)
164-
textColor.set(preferences.textColor)
165-
}
166-
167-
navigator.submitPreferences(it.preferences)
168-
}
156+
editor?.apply {
157+
fontFamily.set(preferences.fontFamily)
158+
fontSize.set(preferences.fontSize)
159+
fontWeight.set(preferences.fontWeight)
160+
scroll.set(preferences.scroll)
161+
backgroundColor.set(preferences.backgroundColor)
162+
textColor.set(preferences.textColor)
163+
164+
navigator.submitPreferences(preferences)
169165
}
170166
} catch (ex: Exception) {
171167
Log.e(TAG, "Error applying EpubPreferences: $ex")
@@ -181,13 +177,10 @@ class EpubReaderFragment : VisualReaderFragment(), EpubNavigatorFragment.Listene
181177
return
182178
}
183179

184-
suspendCoroutine {
185-
if (navigator.goBackward(animated)) {
186-
Log.d(TAG, "::goLeft: Went back.")
187-
} else {
188-
Log.d(TAG, "::goLeft: Couldn't go back.")
189-
}
190-
it.resume(Unit)
180+
if (navigator.goBackward(animated)) {
181+
Log.d(TAG, "::goLeft: Went back.")
182+
} else {
183+
Log.d(TAG, "::goLeft: Couldn't go back.")
191184
}
192185
}
193186

@@ -200,14 +193,10 @@ class EpubReaderFragment : VisualReaderFragment(), EpubNavigatorFragment.Listene
200193
return
201194
}
202195

203-
suspendCoroutine {
204-
if (navigator.goForward(animated)) {
205-
Log.d(TAG, "::goRight: Went forward.")
206-
} else {
207-
Log.d(TAG, "::goRight: Couldn't go forward.")
208-
}
209-
210-
it.resume(Unit)
196+
if (navigator.goForward(animated)) {
197+
Log.d(TAG, "::goRight: Went forward.")
198+
} else {
199+
Log.d(TAG, "::goRight: Couldn't go forward.")
211200
}
212201
}
213202

0 commit comments

Comments
 (0)