Skip to content

Commit 784f7fe

Browse files
authored
feat(ios, android, web): Move from deprecated UserSettings API to the new Preferences API (#80)
BREAKING CHANGE: This update moves the library to the newer Preferences API for managing the reader font size, theme, etc. Resolves #79
1 parent 9bc2e4a commit 784f7fe

37 files changed

+295
-605
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ target 'ExampleApp' do
8585
config = use_native_modules!
8686
...
8787
pod 'GCDWebServer', podspec: 'https://raw.githubusercontent.com/readium/GCDWebServer/3.7.5/GCDWebServer.podspec', modular_headers: true
88+
pod 'ReadiumAdapterGCDWebServer', podspec: 'https://raw.githubusercontent.com/readium/swift-toolkit/2.6.0/Support/CocoaPods/ReadiumAdapterGCDWebServer.podspec', modular_headers: true
8889
pod 'R2Navigator', podspec: 'https://raw.githubusercontent.com/readium/swift-toolkit/2.6.0/Support/CocoaPods/ReadiumNavigator.podspec'
8990
pod 'R2Shared', podspec: 'https://raw.githubusercontent.com/readium/swift-toolkit/2.6.0/Support/CocoaPods/ReadiumShared.podspec'
9091
pod 'R2Streamer', podspec: 'https://raw.githubusercontent.com/readium/swift-toolkit/2.6.0/Support/CocoaPods/ReadiumStreamer.podspec'
@@ -160,7 +161,7 @@ DRM is not supported at this time. However, there is a clear path to [support it
160161
|------|------|----------|-------------|
161162
| `file` | [`File`](https://github.com/5-stones/react-native-readium/blob/main/src/interfaces/File.ts) | :x: | A file object containing the path to the eBook file on disk. |
162163
| `location` | [`Locator`](https://github.com/5-stones/react-native-readium/blob/main/src/interfaces/Locator.ts) \| [`Link`](https://github.com/5-stones/react-native-readium/blob/main/src/interfaces/Link.ts) | :white_check_mark: | A locator prop that allows you to externally control the location of the reader (e.g. Chapters or Bookmarks). <br/><br/>:warning: If you want to set the `location` of an ebook on initial load, you should use the `File.initialLocation` property (look at the `file` prop). See more [here](https://github.com/5-stones/react-native-readium/issues/16#issuecomment-1344128937) |
163-
| `settings` | [`Partial<Settings>`](https://github.com/5-stones/react-native-readium/blob/main/src/interfaces/Settings.ts) | :white_check_mark: | An object that allows you to control various aspects of the reader's UI (epub only) |
164+
| `preferences` | [`Partial<Preferences>`](https://github.com/readium/swift-toolkit/blob/main/docs/Guides/Navigator%20Preferences.md#appendix-preference-constraints) | :white_check_mark: | An object that allows you to control various aspects of the reader's UI (epub only) |
164165
| `style` | `ViewStyle` | :white_check_mark: | A traditional style object. |
165166
| `onLocationChange` | `(locator: Locator) => void` | :white_check_mark: | A callback that fires whenever the location is changed (e.g. the user transitions to a new page)|
166167
| `onTableOfContents` | `(toc: Link[] \| null) => void` | :white_check_mark: | A callback that fires once the file is parsed and emits the table of contents embedded in the file. Returns `null` or an empty `[]` if no TOC exists. See the [`Link`](https://github.com/5-stones/react-native-readium/blob/main/src/interfaces/Link.ts) interface for more info. |

android/src/main/java/com/reactnativereadium/ReadiumView.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import com.facebook.react.uimanager.events.RCTEventEmitter
99
import com.reactnativereadium.reader.BaseReaderFragment
1010
import com.reactnativereadium.reader.EpubReaderFragment
1111
import com.reactnativereadium.reader.ReaderViewModel
12+
import com.reactnativereadium.reader.VisualReaderFragment
1213
import com.reactnativereadium.utils.Dimensions
1314
import com.reactnativereadium.utils.File
1415
import com.reactnativereadium.utils.LinkOrLocator
16+
import org.readium.r2.navigator.epub.EpubNavigatorFragment
17+
import org.readium.r2.navigator.epub.EpubPreferences
1518
import org.readium.r2.shared.extensions.toMap
1619

1720
class ReadiumView(
@@ -21,7 +24,7 @@ class ReadiumView(
2124
var file: File? = null
2225
var fragment: BaseReaderFragment? = null
2326
var isViewInitialized: Boolean = false
24-
var lateInitSettings: Map<String, Any>? = null
27+
var lateInitSerializedUserPreferences: String? = null
2528

2629
fun updateLocation(location: LinkOrLocator) : Boolean {
2730
if (fragment == null) {
@@ -31,25 +34,25 @@ class ReadiumView(
3134
}
3235
}
3336

34-
fun updateSettingsFromMap(map: Map<String, Any>?) {
35-
if (map == null) {
37+
fun updatePreferencesFromJsonString(preferences: String?) {
38+
if (preferences == null) {
3639
return
3740
} else if (fragment == null) {
38-
lateInitSettings = map
41+
lateInitSerializedUserPreferences = preferences
3942
return
4043
}
4144

4245
if (fragment is EpubReaderFragment) {
43-
(fragment as EpubReaderFragment).updateSettingsFromMap(map)
46+
(fragment as EpubReaderFragment).updatePreferencesFromJsonString(preferences)
4447
}
4548

46-
lateInitSettings = null
49+
lateInitSerializedUserPreferences = null
4750
}
4851

4952
fun addFragment(frag: BaseReaderFragment) {
5053
fragment = frag
5154
setupLayout()
52-
updateSettingsFromMap(lateInitSettings)
55+
updatePreferencesFromJsonString(lateInitSerializedUserPreferences)
5356
val activity: FragmentActivity? = reactContext.currentActivity as FragmentActivity?
5457
activity!!.supportFragmentManager
5558
.beginTransaction()

android/src/main/java/com/reactnativereadium/ReadiumViewManager.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,9 @@ class ReadiumViewManager(
115115
}
116116
}
117117

118-
@ReactProp(name = "settings")
119-
fun setSettings(view: ReadiumView, settings: ReadableMap) {
120-
val map = mutableMapOf<String, Any>()
121-
settings.toHashMap().forEach { (key, value) -> if (value != null) map[key] = value }
122-
view.updateSettingsFromMap(map)
118+
@ReactProp(name = "preferences")
119+
fun setPreferences(view: ReadiumView, serialisedPreferences: String) {
120+
view.updatePreferencesFromJsonString(serialisedPreferences)
123121
}
124122

125123
@ReactPropGroup(names = ["width", "height"], customType = "Style")

android/src/main/java/com/reactnativereadium/epub/UserSettings.kt

Lines changed: 0 additions & 246 deletions
This file was deleted.

0 commit comments

Comments
 (0)