From 5e6e451e6c4c75dcf35a11a1b348f8911660247f Mon Sep 17 00:00:00 2001 From: Jacob Spizziri Date: Tue, 22 Mar 2022 11:22:50 -0500 Subject: [PATCH] fix(ios): fix view sizing on iOS --- .../ReadiumExample.xcodeproj/project.pbxproj | 28 +++++++++---------- example/package.json | 5 ++-- example/yarn.lock | 5 ++++ ios/App/AppModule.swift | 2 -- ios/Reader/ReaderModule.swift | 7 ++--- ios/ReadiumView.swift | 15 +++++++--- ios/ReadiumViewManager.swift | 3 +- src/index.tsx | 10 ++++--- 8 files changed, 43 insertions(+), 32 deletions(-) diff --git a/example/ios/ReadiumExample.xcodeproj/project.pbxproj b/example/ios/ReadiumExample.xcodeproj/project.pbxproj index ccdd620..249547b 100644 --- a/example/ios/ReadiumExample.xcodeproj/project.pbxproj +++ b/example/ios/ReadiumExample.xcodeproj/project.pbxproj @@ -11,11 +11,11 @@ 13B07FBC1A68108700A75B9A /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB01A68108700A75B9A /* AppDelegate.m */; }; 13B07FBF1A68108700A75B9A /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; }; 13B07FC11A68108700A75B9A /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; }; + 1E3BEE17F3AADB11F9BCBDF6 /* libPods-ReadiumExample.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3A8823A8FA18976EF67D387D /* libPods-ReadiumExample.a */; }; 2D02E4BC1E0B4A80006451C7 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB01A68108700A75B9A /* AppDelegate.m */; }; 2D02E4BD1E0B4A84006451C7 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 13B07FB51A68108700A75B9A /* Images.xcassets */; }; 2D02E4BF1E0B4AB3006451C7 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 13B07FB71A68108700A75B9A /* main.m */; }; 2DCD954D1E0B4F2C00145EB5 /* ReadiumExampleTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 00E356F21AD99517003FC87E /* ReadiumExampleTests.m */; }; - 4C39C56BAD484C67AA576FFA /* libPods-ReadiumExample.a in Frameworks */ = {isa = PBXBuildFile; fileRef = CA3E69C5B9553B26FBA2DF04 /* libPods-ReadiumExample.a */; }; 81AB9BB82411601600AC10FF /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */; }; /* End PBXBuildFile section */ @@ -49,10 +49,10 @@ 13B07FB71A68108700A75B9A /* main.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = main.m; path = ReadiumExample/main.m; sourceTree = ""; }; 2D02E47B1E0B4A5D006451C7 /* ReadiumExample-tvOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "ReadiumExample-tvOS.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 2D02E4901E0B4A5D006451C7 /* ReadiumExample-tvOSTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "ReadiumExample-tvOSTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; - 47F7ED3B7971BE374F7B8635 /* Pods-ReadiumExample.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ReadiumExample.debug.xcconfig"; path = "Target Support Files/Pods-ReadiumExample/Pods-ReadiumExample.debug.xcconfig"; sourceTree = ""; }; + 3A8823A8FA18976EF67D387D /* libPods-ReadiumExample.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-ReadiumExample.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 81AB9BB72411601600AC10FF /* LaunchScreen.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = LaunchScreen.storyboard; path = ReadiumExample/LaunchScreen.storyboard; sourceTree = ""; }; - CA3E69C5B9553B26FBA2DF04 /* libPods-ReadiumExample.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-ReadiumExample.a"; sourceTree = BUILT_PRODUCTS_DIR; }; - E00ACF0FDA8BF921659E2F9A /* Pods-ReadiumExample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ReadiumExample.release.xcconfig"; path = "Target Support Files/Pods-ReadiumExample/Pods-ReadiumExample.release.xcconfig"; sourceTree = ""; }; + B5F4D734618153BAFE0DA839 /* Pods-ReadiumExample.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ReadiumExample.debug.xcconfig"; path = "Target Support Files/Pods-ReadiumExample/Pods-ReadiumExample.debug.xcconfig"; sourceTree = ""; }; + C09FB98F711B70679162A129 /* Pods-ReadiumExample.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ReadiumExample.release.xcconfig"; path = "Target Support Files/Pods-ReadiumExample/Pods-ReadiumExample.release.xcconfig"; sourceTree = ""; }; ED297162215061F000B7C4FE /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = System/Library/Frameworks/JavaScriptCore.framework; sourceTree = SDKROOT; }; ED2971642150620600B7C4FE /* JavaScriptCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = JavaScriptCore.framework; path = Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS12.0.sdk/System/Library/Frameworks/JavaScriptCore.framework; sourceTree = DEVELOPER_DIR; }; /* End PBXFileReference section */ @@ -69,7 +69,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 4C39C56BAD484C67AA576FFA /* libPods-ReadiumExample.a in Frameworks */, + 1E3BEE17F3AADB11F9BCBDF6 /* libPods-ReadiumExample.a in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -126,7 +126,7 @@ children = ( ED297162215061F000B7C4FE /* JavaScriptCore.framework */, ED2971642150620600B7C4FE /* JavaScriptCore.framework */, - CA3E69C5B9553B26FBA2DF04 /* libPods-ReadiumExample.a */, + 3A8823A8FA18976EF67D387D /* libPods-ReadiumExample.a */, ); name = Frameworks; sourceTree = ""; @@ -134,8 +134,8 @@ 6B9684456A2045ADE5A6E47E /* Pods */ = { isa = PBXGroup; children = ( - 47F7ED3B7971BE374F7B8635 /* Pods-ReadiumExample.debug.xcconfig */, - E00ACF0FDA8BF921659E2F9A /* Pods-ReadiumExample.release.xcconfig */, + B5F4D734618153BAFE0DA839 /* Pods-ReadiumExample.debug.xcconfig */, + C09FB98F711B70679162A129 /* Pods-ReadiumExample.release.xcconfig */, ); path = Pods; sourceTree = ""; @@ -207,13 +207,13 @@ isa = PBXNativeTarget; buildConfigurationList = 13B07F931A680F5B00A75B9A /* Build configuration list for PBXNativeTarget "ReadiumExample" */; buildPhases = ( - 4F0A6FC082772762E3E4C96C /* [CP] Check Pods Manifest.lock */, + 33EE075F7855AB1C624A0943 /* [CP] Check Pods Manifest.lock */, FD10A7F022414F080027D42C /* Start Packager */, 13B07F871A680F5B00A75B9A /* Sources */, 13B07F8C1A680F5B00A75B9A /* Frameworks */, 13B07F8E1A680F5B00A75B9A /* Resources */, 00DD1BFF1BD5951E006B06BC /* Bundle React Native code and images */, - C1D60D28B925C94BD88E79D7 /* [CP] Copy Pods Resources */, + EA21C5771E4FF2B1C3C3D90B /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -371,7 +371,7 @@ shellPath = /bin/sh; shellScript = "export NODE_BINARY=node\n../node_modules/react-native/scripts/react-native-xcode.sh"; }; - 4F0A6FC082772762E3E4C96C /* [CP] Check Pods Manifest.lock */ = { + 33EE075F7855AB1C624A0943 /* [CP] Check Pods Manifest.lock */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( @@ -393,7 +393,7 @@ shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n"; showEnvVarsInLog = 0; }; - C1D60D28B925C94BD88E79D7 /* [CP] Copy Pods Resources */ = { + EA21C5771E4FF2B1C3C3D90B /* [CP] Copy Pods Resources */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; files = ( @@ -583,7 +583,7 @@ }; 13B07F941A680F5B00A75B9A /* Debug */ = { isa = XCBuildConfiguration; - baseConfigurationReference = 47F7ED3B7971BE374F7B8635 /* Pods-ReadiumExample.debug.xcconfig */; + baseConfigurationReference = B5F4D734618153BAFE0DA839 /* Pods-ReadiumExample.debug.xcconfig */; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; @@ -607,7 +607,7 @@ }; 13B07F951A680F5B00A75B9A /* Release */ = { isa = XCBuildConfiguration; - baseConfigurationReference = E00ACF0FDA8BF921659E2F9A /* Pods-ReadiumExample.release.xcconfig */; + baseConfigurationReference = C09FB98F711B70679162A129 /* Pods-ReadiumExample.release.xcconfig */; buildSettings = { ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; diff --git a/example/package.json b/example/package.json index 1cc283e..752a148 100644 --- a/example/package.json +++ b/example/package.json @@ -24,10 +24,11 @@ "@babel/runtime": "^7.12.5", "@react-native-community/eslint-config": "^2.0.0", "babel-jest": "^26.6.3", + "babel-plugin-module-resolver": "^4.0.0", "eslint": "7.14.0", "jest": "^26.6.3", "metro-react-native-babel-preset": "^0.66.2", - "react-test-renderer": "17.0.2", - "babel-plugin-module-resolver": "^4.0.0" + "react-native-clean-project": "^4.0.1", + "react-test-renderer": "17.0.2" } } diff --git a/example/yarn.lock b/example/yarn.lock index 821a0f8..8918966 100644 --- a/example/yarn.lock +++ b/example/yarn.lock @@ -5367,6 +5367,11 @@ react-is@^16.13.1, react-is@^16.7.0: resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.13.1.tgz#789729a4dc36de2999dc156dd6c1d9c18cea56a4" integrity sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ== +react-native-clean-project@^4.0.1: + version "4.0.1" + resolved "https://registry.yarnpkg.com/react-native-clean-project/-/react-native-clean-project-4.0.1.tgz#c0e2b17eebf32a683aa67da45fe7932857166da7" + integrity sha512-B7rXdFC4bfA+Vv7lZ9bKS6cUDgqg04OR5D69sNnhheFBJQ1V04cIfJ1Fu0sbqRsIehqlOcySQRpp8tm7r7PvLQ== + react-native-codegen@^0.0.8: version "0.0.8" resolved "https://registry.yarnpkg.com/react-native-codegen/-/react-native-codegen-0.0.8.tgz#b7796a54074139d956fff2862cf1285db43c891b" diff --git a/ios/App/AppModule.swift b/ios/App/AppModule.swift index f40f901..1f9d934 100644 --- a/ios/App/AppModule.swift +++ b/ios/App/AppModule.swift @@ -28,8 +28,6 @@ final class AppModule { fatalError("Can't start publication server") } - let httpClient = DefaultHTTPClient() - reader = ReaderModule(delegate: self, resourcesServer: server) // opds = OPDSModule(delegate: self) diff --git a/ios/Reader/ReaderModule.swift b/ios/Reader/ReaderModule.swift index b1cdc17..b932f74 100644 --- a/ios/Reader/ReaderModule.swift +++ b/ios/Reader/ReaderModule.swift @@ -51,9 +51,7 @@ final class ReaderModule: ReaderModuleAPI { guard let module = self.formatModules.first( where:{ $0.publicationFormats.contains(publication.format) } ) else { - // delegate.presentError(ReaderError.formatNotSupported, from: rootViewController) - // completion() - print(">>>>>>>> woof 1") + print("Unable to display the publication due to an unsupported format.") return nil } @@ -65,10 +63,9 @@ final class ReaderModule: ReaderModuleAPI { resourcesServer: resourcesServer ) } catch { - print(">>>>>>>> woof 2") + print("An unexpected error occurred when attempting to build the reader view.") print(error) return nil - // delegate.presentError(error, from: rootViewController) } } diff --git a/ios/ReadiumView.swift b/ios/ReadiumView.swift index 7ee255d..3489849 100644 --- a/ios/ReadiumView.swift +++ b/ios/ReadiumView.swift @@ -36,12 +36,11 @@ class ReadiumView : UIView, Loggable { func loadBook(url: String) { guard let rootViewController = UIApplication.shared.delegate?.window??.rootViewController else { return } - let bookId: String = "http://www.feedbooks.com/book/54/moby-dick" let locator: Locator? = self.getLocator() self.readerService.buildViewController( url: url, - bookId: bookId, + bookId: url, locator: locator, sender: rootViewController, completion: { vc in @@ -88,7 +87,6 @@ class ReadiumView : UIView, Loggable { let userProperties = navigator.userSettings.userProperties for property in userProperties.properties { - let key = property.reference let value = settings?[property.reference] if (value == nil) { @@ -143,7 +141,16 @@ class ReadiumView : UIView, Loggable { let rootViewController = UIApplication.shared.delegate?.window??.rootViewController rootViewController?.addChild(readerViewController!) readerViewController!.view.frame = self.superview!.frame - self.addSubview(self.readerViewController!.view!) + let rootView = self.readerViewController!.view! + self.addSubview(rootView) + + // bind the reader's view to be constrained to its parent + rootView.translatesAutoresizingMaskIntoConstraints = false + rootView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true + rootView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true + rootView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true + rootView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true + self.readerViewController!.didMove(toParent: rootViewController) } } diff --git a/ios/ReadiumViewManager.swift b/ios/ReadiumViewManager.swift index d03a3a0..f91996d 100644 --- a/ios/ReadiumViewManager.swift +++ b/ios/ReadiumViewManager.swift @@ -3,6 +3,7 @@ import Foundation @objc(ReadiumViewManager) class ReadiumViewManager: RCTViewManager { override func view() -> (ReadiumView) { - return ReadiumView() + let view = ReadiumView() + return view } } diff --git a/src/index.tsx b/src/index.tsx index fc71a88..2bb5cc5 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -59,10 +59,12 @@ export const ReadiumView: React.FC = ({ return ( setDimensions({ - width: PixelRatio.getPixelSizeForLayoutSize(layout.width), - height: PixelRatio.getPixelSizeForLayoutSize(layout.height), - })} + onLayout={({ nativeEvent: { layout: { width, height } }}) => { + setDimensions({ + width: Platform.OS === 'android' ? PixelRatio.getPixelSizeForLayoutSize(width) : width, + height: Platform.OS === 'android' ? PixelRatio.getPixelSizeForLayoutSize(height) : height, + }) + }} >