Skip to content

Commit

Permalink
Working iOS valhalla
Browse files Browse the repository at this point in the history
  • Loading branch information
Archdoog committed Feb 10, 2024
1 parent 83f835f commit 2b8b085
Show file tree
Hide file tree
Showing 23 changed files with 548 additions and 18 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
uuid = "95E7184A-5252-4166-8F01-1A0F25B80F5D"
type = "1"
version = "2.0">
</Bucket>
14 changes: 14 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pins" : [
{
"identity" : "light-swift-untar",
"kind" : "remoteSourceControl",
"location" : "https://github.com/UInt2048/Light-Swift-Untar.git",
"state" : {
"revision" : "fcff1f1b82373ea64b9565a364f63ffe7fdecf9f",
"version" : "1.0.4"
}
}
],
"version" : 2
}
22 changes: 15 additions & 7 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
// swift-tools-version:5.8
// swift-tools-version:5.9
import PackageDescription

let package = Package(
name: "ValhallaMobile",
platforms: [
.iOS(.v13)
// TODO: Add support for other platforms?
.iOS(.v13),
.watchOS(.v6),
.tvOS(.v13),
.macOS(.v10_13)
],
products: [
.library(
name: "Valhalla",
targets: ["Valhalla"])
],
dependencies: [
// Dependencies declare other packages that this package depends on.
.package(url: "https://github.com/UInt2048/Light-Swift-Untar.git", from: "1.0.4")
],
targets: [
.target(
name: "Valhalla",
dependencies: ["ValhallaObjc", "ValhallaWrapper"],
path: "apple/Sources/Valhalla"
dependencies: [
"ValhallaObjc",
"ValhallaWrapper",
.product(name: "Light-Swift-Untar", package: "Light-Swift-Untar")
],
path: "apple/Sources/Valhalla",
resources: [.copy("SupportData")]
),
.target(
name: "ValhallaObjc",
Expand All @@ -36,7 +43,8 @@ let package = Package(
.testTarget(
name: "ValhallaTests",
dependencies: ["Valhalla"],
path: "apple/Tests"
path: "apple/Tests/ValhallaTests",
resources: [.copy("TestData")]
)
],
cLanguageStandard: .gnu17,
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ Because gradle doesn't automatically handle git submodules, you will need to man

```
git submodule update --init --recursive
```
./build.sh
```

## References

- Swift Package Manager C++ <https://www.swift.org/documentation/articles/wrapping-c-cpp-library-in-swift.html>
Binary file added apple/Sources/Valhalla/SupportData/tzdata.tar
Binary file not shown.
26 changes: 26 additions & 0 deletions apple/Sources/Valhalla/TZDatabaseManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Foundation
import Light_Swift_Untar

enum TZDatabaseError: Error {
case tzdataNotFound
case libraryNotFound
}

final class TZDatabaseManager {

static func injectIntoMain() throws {
guard let tzdataFileURL = Bundle.module.url(forResource: "SupportData/tzdata", withExtension: "tar") else {
throw TZDatabaseError.tzdataNotFound
}

guard let libraryDirectory = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first else {
throw TZDatabaseError.libraryNotFound
}

let tzdataFileData = try Data(contentsOf: tzdataFileURL)
let libraryURL = libraryDirectory.appendingPathComponent("tzdata")

// Write the tar to Library/tzdata
try FileManager.default.createFilesAndDirectories(url: libraryURL, tarData: tzdataFileData)
}
}
11 changes: 9 additions & 2 deletions apple/Sources/Valhalla/Valhalla.swift
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
import ValhallaObjc

public protocol ValhallaProtocol {
public protocol ValhallaProviding {
init(configPath: String)
func route(request: String) -> String
}

class Valhalla: ValhallaProtocol {
public final class Valhalla: ValhallaProviding {

private let actor = ValhallaWrapper()
private var configPath: String

public required init(configPath: String) {
do {
try TZDatabaseManager.injectIntoMain()
} catch {
// If you're circumventing this libraries injection, download tzdata.tar and put in your bundle. https://www.iana.org/time-zones
assertionFailure("tzdata was not inject into Bundle.main. This can be avoided by including tzdata.tar in your main bundle.")
}

self.configPath = configPath
}

Expand Down
Loading

0 comments on commit 2b8b085

Please sign in to comment.