Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use quran.app backend instead of Apple on-demand resources #592

Merged
merged 1 commit into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/ReadingService.xcscheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
version = "1.7">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ReadingService"
BuildableName = "ReadingService"
BlueprintName = "ReadingService"
ReferencedContainer = "container:">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
shouldAutocreateTestPlan = "YES">
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "ReadingService"
BuildableName = "ReadingService"
BlueprintName = "ReadingService"
ReferencedContainer = "container:">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
21 changes: 0 additions & 21 deletions Core/SystemDependencies/BundleResourceRequest.swift

This file was deleted.

13 changes: 13 additions & 0 deletions Core/SystemDependencies/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public protocol FileSystem: Sendable {
func createDirectory(at url: URL, withIntermediateDirectories createIntermediates: Bool) throws
func copyItem(at srcURL: URL, to dstURL: URL) throws
func removeItem(at url: URL) throws
func moveItem(at src: URL, to dst: URL) throws
func contentsOfDirectory(at url: URL, includingPropertiesForKeys keys: [URLResourceKey]?) throws -> [URL]
func resourceValues(at url: URL, forKeys keys: Set<URLResourceKey>) throws -> ResourceValues

Expand All @@ -34,6 +35,10 @@ public struct DefaultFileSystem: FileSystem {
try FileManager.default.removeItem(at: url)
}

public func moveItem(at src: URL, to dst: URL) throws {
try FileManager.default.moveItem(at: src, to: dst)
}

public func contentsOfDirectory(at url: URL, includingPropertiesForKeys keys: [URLResourceKey]?) throws -> [URL] {
try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil)
}
Expand Down Expand Up @@ -73,4 +78,12 @@ public extension FileSystem {
func removeItem(at path: RelativeFilePath) throws {
try removeItem(at: path.url)
}

func createDirectory(at path: RelativeFilePath, withIntermediateDirectories: Bool) throws {
try createDirectory(at: path.url, withIntermediateDirectories: withIntermediateDirectories)
}

func moveItem(at src: URL, to dst: RelativeFilePath) throws {
try moveItem(at: src, to: dst.url)
}
}
25 changes: 25 additions & 0 deletions Core/SystemDependencies/Zipper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Zipper.swift
//
//
// Created by Mohamed Afifi on 2023-11-22.
//

import Foundation
import Zip

public protocol Zipper {
func unzipFile(_ zipFile: URL, destination: URL, overwrite: Bool, password: String?) throws
}

public struct DefaultZipper: Zipper {
// MARK: Lifecycle

public init() { }

// MARK: Public

public func unzipFile(_ zipFile: URL, destination: URL, overwrite: Bool, password: String?) throws {
try Zip.unzipFile(zipFile, destination: destination, overwrite: overwrite, password: password)
}
}
46 changes: 0 additions & 46 deletions Core/SystemDependenciesFake/BundleResourceRequestFake.swift

This file was deleted.

21 changes: 20 additions & 1 deletion Core/SystemDependenciesFake/FileSystemFake.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public final class FileSystemFake: FileSystem, Sendable {

enum FileSystemError: Error {
case noResourceValues
case general(String)
}

// MARK: Lifecycle
Expand Down Expand Up @@ -64,7 +65,22 @@ public final class FileSystemFake: FileSystem, Sendable {

public func removeItem(at url: URL) throws {
removedItems.append(url)
files.remove(url)
for file in files {
if url.isParent(of: file) {
files.remove(file)
}
}
}

public func moveItem(at src: URL, to dst: URL) throws {
if !files.contains(src) {
throw FileSystemError.general("Source file doesn't exist: \(src)")
}
if files.contains(dst) {
throw FileSystemError.general("Destination file exists: \(src)")
}
files.remove(src)
files.insert(dst)
}

public func contentsOfDirectory(at url: URL, includingPropertiesForKeys keys: [URLResourceKey]?) throws -> [URL] {
Expand Down Expand Up @@ -95,6 +111,9 @@ public final class FileSystemFake: FileSystem, Sendable {
}

public func writeToFile(at path: URL, content: String) throws {
if files.contains(path) {
throw FileSystemError.general("Cannot overwrite file at \(path)")
}
files.insert(path)
}

Expand Down
47 changes: 47 additions & 0 deletions Core/SystemDependenciesFake/ZipperFake.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// ZipperFake.swift
//
//
// Created by Mohamed Afifi on 2023-11-22.
//

import Foundation
import SystemDependencies

public final class ZipperFake: Zipper {
// MARK: Lifecycle

public init(fileManager: FileSystem) {
self.fileManager = fileManager
}

// MARK: Public

public var failures: [Error] = []
public var unzippedFiles: [URL] = []

public func unzipFile(_ zipFile: URL, destination: URL, overwrite: Bool, password: String?) throws {
if let failure = failures.first {
failures.remove(at: 0)
throw failure
}

for file in zipContents(zipFile) {
try fileManager.writeToFile(at: file, content: "unzipped")
}
unzippedFiles.append(zipFile)
}

public func zipContents(_ zipFile: URL) -> Set<URL> {
let directory = zipFile.deletingPathExtension()
return [
directory.appendingPathComponent("text.txt"),
directory.appendingPathComponent("database.db"),
directory.appendingPathComponent("image.png"),
]
}

// MARK: Private

private let fileManager: FileSystem
}
8 changes: 0 additions & 8 deletions Core/Utilities/Sources/Features/RelativeFilePath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ public extension FileManager {
try removeItem(at: path.url)
}

func createDirectory(at path: RelativeFilePath, withIntermediateDirectories: Bool, attributes: [FileAttributeKey: Any]? = nil) throws {
try createDirectory(at: path.url, withIntermediateDirectories: withIntermediateDirectories, attributes: attributes)
}

func moveItem(at src: URL, to dst: RelativeFilePath) throws {
try moveItem(at: src, to: dst.url)
}

func copyItem(at src: URL, to dst: RelativeFilePath) throws {
try copyItem(at: src, to: dst.url)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ actor DownloadBatchDataController {
logger.info("Batching \(batchRequest.requests.count) to download.")
// save to persistence
let batch = try await persistence.insert(batch: batchRequest)
logger.info("Batch assigned Id = \(batch.id).")

// create the response
let response = await createResponse(forBatch: batch)
Expand Down Expand Up @@ -134,7 +135,7 @@ actor DownloadBatchDataController {
// Wait until sequence completes
for try await _ in response.progress { }
} catch {
logger.error("Batch failed to download with error: \(error)")
logger.error("Batch \(response.batchId) failed to download with error: \(error)")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public actor DownloadBatchResponse {

// MARK: Internal

let batchId: Int64
nonisolated let batchId: Int64

var runningTasks: Int {
responses.values.filter { $0.isInProgress && $0.taskId != nil }.count
Expand Down Expand Up @@ -195,7 +195,9 @@ public actor DownloadBatchResponse {
}

if let error = firstError {
crasher.recordError(error, reason: "Download failed \(batchId)")
if !(error is CancellationError) {
crasher.recordError(error, reason: "Download failed \(batchId)")
}

// Cancel other tasks if any has failed
for request in requests {
Expand Down
6 changes: 4 additions & 2 deletions Data/BatchDownloader/Sources/Downloader/DownloadManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import Crashing
import Foundation
import NetworkSupport
import SystemDependencies
import Utilities
import VLogging

Expand Down Expand Up @@ -50,15 +51,16 @@ public final class DownloadManager: Sendable {
init(
maxSimultaneousDownloads: Int,
sessionFactory: @escaping SessionFactory,
persistence: DownloadsPersistence
persistence: DownloadsPersistence,
fileManager: FileSystem = DefaultFileSystem()
) {
let dataController = DownloadBatchDataController(
maxSimultaneousDownloads: maxSimultaneousDownloads,
persistence: persistence
)
self.dataController = dataController
self.sessionFactory = sessionFactory
handler = DownloadSessionDelegate(dataController: dataController)
handler = DownloadSessionDelegate(dataController: dataController, fileManager: fileManager)
}

// MARK: Public
Expand Down
Loading
Loading