generated from StanfordBDHG/SwiftPackageTemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve a lot of todos and restructure some parts
Showing
16 changed files
with
477 additions
and
274 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// | ||
// This source file is part of the Stanford Spezi open-source project | ||
// | ||
// SPDX-FileCopyrightText: 2024 Stanford University and the project authors (see CONTRIBUTORS.md) | ||
// | ||
// SPDX-License-Identifier: MIT | ||
// | ||
|
||
import OSLog | ||
|
||
|
||
struct SavableCollection<Element: Codable> { | ||
private let storage: [Element] | ||
|
||
var values: [Element] { | ||
storage | ||
} | ||
|
||
init(_ elements: [Element] = []) { | ||
self.storage = elements | ||
} | ||
} | ||
|
||
|
||
extension SavableCollection: RandomAccessCollection { | ||
public var startIndex: Int { | ||
storage.startIndex | ||
} | ||
|
||
public var endIndex: Int { | ||
storage.endIndex | ||
} | ||
|
||
public func index(after index: Int) -> Int { | ||
storage.index(after: index) | ||
} | ||
|
||
public subscript(position: Int) -> Element { | ||
storage[position] | ||
} | ||
} | ||
|
||
|
||
extension SavableCollection: ExpressibleByArrayLiteral { | ||
init(arrayLiteral elements: Element...) { | ||
self.init(elements) | ||
} | ||
} | ||
|
||
|
||
extension SavableCollection: RawRepresentable { | ||
private static var logger: Logger { | ||
Logger(subsystem: "edu.stanford.spezi.SpeziDevices", category: "\(Self.self)") | ||
} | ||
|
||
var rawValue: String { | ||
let data: Data | ||
do { | ||
data = try JSONEncoder().encode(storage) | ||
} catch { | ||
Self.logger.error("Failed to encode \(Self.self): \(error)") | ||
return "[]" | ||
} | ||
guard let rawValue = String(data: data, encoding: .utf8) else { | ||
Self.logger.error("Failed to convert data of \(Self.self) to string: \(data)") | ||
return "[]" | ||
} | ||
|
||
return rawValue | ||
} | ||
|
||
init?(rawValue: String) { | ||
guard let data = rawValue.data(using: .utf8) else { | ||
Self.logger.error("Failed to convert string of \(Self.self) to data: \(rawValue)") | ||
return nil | ||
} | ||
|
||
do { | ||
self.storage = try JSONDecoder().decode([Element].self, from: data) | ||
} catch { | ||
Self.logger.error("Failed to decode \(Self.self): \(error)") | ||
return nil | ||
} | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters