|
| 1 | +// |
| 2 | +// Copyright 2025 Readium Foundation. All rights reserved. |
| 3 | +// Use of this source code is governed by the BSD-style license |
| 4 | +// available in the top-level LICENSE file of the project. |
| 5 | +// |
| 6 | + |
| 7 | +import ReadiumAdapterGCDWebServer |
| 8 | +import ReadiumNavigator |
| 9 | +import ReadiumShared |
| 10 | +import ReadiumStreamer |
| 11 | +import UIKit |
| 12 | + |
| 13 | +/// Shared Readium infrastructure for testing. |
| 14 | +@MainActor class Container { |
| 15 | + static let shared = Container() |
| 16 | + |
| 17 | + let memoryTracker = MemoryTracker() |
| 18 | + let httpClient: HTTPClient |
| 19 | + let httpServer: HTTPServer |
| 20 | + let assetRetriever: AssetRetriever |
| 21 | + let publicationOpener: PublicationOpener |
| 22 | + |
| 23 | + init() { |
| 24 | + httpClient = DefaultHTTPClient() |
| 25 | + assetRetriever = AssetRetriever(httpClient: httpClient) |
| 26 | + httpServer = GCDHTTPServer(assetRetriever: assetRetriever) |
| 27 | + |
| 28 | + publicationOpener = PublicationOpener( |
| 29 | + parser: DefaultPublicationParser( |
| 30 | + httpClient: httpClient, |
| 31 | + assetRetriever: assetRetriever, |
| 32 | + pdfFactory: DefaultPDFDocumentFactory() |
| 33 | + ), |
| 34 | + contentProtections: [] |
| 35 | + ) |
| 36 | + } |
| 37 | + |
| 38 | + func publication(at url: FileURL) async throws -> Publication { |
| 39 | + let asset = try await assetRetriever.retrieve(url: url).get() |
| 40 | + let publication = try await publicationOpener.open( |
| 41 | + asset: asset, |
| 42 | + allowUserInteraction: false, |
| 43 | + sender: nil |
| 44 | + ).get() |
| 45 | + |
| 46 | + memoryTracker.track(publication) |
| 47 | + return publication |
| 48 | + } |
| 49 | + |
| 50 | + func navigator(for publication: Publication) throws -> VisualNavigator & UIViewController { |
| 51 | + if publication.conforms(to: .epub) { |
| 52 | + return try epubNavigator(for: publication) |
| 53 | + } else if publication.conforms(to: .pdf) { |
| 54 | + return try pdfNavigator(for: publication) |
| 55 | + } else { |
| 56 | + fatalError("Publication not supported") |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + func epubNavigator(for publication: Publication) throws -> EPUBNavigatorViewController { |
| 61 | + let navigator = try EPUBNavigatorViewController( |
| 62 | + publication: publication, |
| 63 | + initialLocation: nil, |
| 64 | + config: EPUBNavigatorViewController.Configuration(), |
| 65 | + httpServer: httpServer |
| 66 | + ) |
| 67 | + memoryTracker.track(navigator) |
| 68 | + return navigator |
| 69 | + } |
| 70 | + |
| 71 | + func pdfNavigator(for publication: Publication) throws -> PDFNavigatorViewController { |
| 72 | + let navigator = try PDFNavigatorViewController( |
| 73 | + publication: publication, |
| 74 | + initialLocation: nil, |
| 75 | + httpServer: httpServer |
| 76 | + ) |
| 77 | + memoryTracker.track(navigator) |
| 78 | + return navigator |
| 79 | + } |
| 80 | +} |
0 commit comments