Skip to content

Commit

Permalink
Refactor BibleClient
Browse files Browse the repository at this point in the history
  • Loading branch information
p-larson committed Aug 8, 2023
1 parent b2dd6c0 commit 2802ca3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 56 deletions.
Binary file not shown.
34 changes: 34 additions & 0 deletions BibleCore/Sources/BibleClient/Client.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// BibleClient.swift
// TCA-Bible
//
// Created by Peter Larson on 7/18/23.
//

import Foundation
import BibleCore
import ComposableArchitecture

public struct BibleClient {
public var translations: @Sendable () async throws -> [Translation]
public var translation: @Sendable (TranslationID) async throws -> Translation
public var genres: @Sendable () async throws -> [Genre]
public var genre: @Sendable (GenreID) async throws -> Genre
public var books: @Sendable () async throws -> [Book]
public var book: @Sendable (BookID) async throws -> Book
public var chapters: @Sendable (BookID) async throws -> [Chapter]
public var verses: @Sendable (BookID, ChapterID) async throws -> [Verse]
public var verse: @Sendable (BookID, ChapterID, VerseID) async throws -> Verse
}

extension DependencyValues {
public var bible: BibleClient {
get {
self[BibleClient.self]
}

set {
self[BibleClient.self] = newValue
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,7 @@
//
// BibleClient.swift
// TCA-Bible
//
// Created by Peter Larson on 7/18/23.
//

import Foundation
import BibleCore
import ComposableArchitecture

public struct BibleClient {
public var translations: @Sendable () async throws -> [Translation]
public var translation: @Sendable (TranslationID) async throws -> Translation
public var genres: @Sendable () async throws -> [Genre]
public var genre: @Sendable (GenreID) async throws -> Genre
public var books: @Sendable () async throws -> [Book]
public var book: @Sendable (BookID) async throws -> Book
public var chapters: @Sendable (BookID) async throws -> [Chapter]
public var verses: @Sendable (BookID, ChapterID) async throws -> [Verse]
public var verse: @Sendable (BookID, ChapterID, VerseID) async throws -> Verse
}

fileprivate let decoder = JSONDecoder()

extension BibleClient: DependencyKey {
Expand Down Expand Up @@ -107,30 +88,6 @@ extension BibleClient: DependencyKey {
)
}

enum BibleClientError: Error {
case unknown
}

extension BibleClient {
public static var testValue: BibleClient = Self(
translations: { [.mock] },
translation: { _ in .mock },
genres: { [.mock] },
genre: { _ in .mock },
books: { [.genesis, .exodus, .leviticus] },
book: { id in
guard let book = IdentifiedArray(uniqueElements: [Book].mock)[id: id] else {
throw BibleClientError.unknown
}

return book
},
chapters: { _ in [.mock] },
verses: { _, _ in [.mock] },
verse: { _, _, _ in .mock }
)
}

//extension BibleClient {
// public static var previewValue: BibleClient = Self(
// translations: <#@Sendable () async throws -> [Translation]#>,
Expand All @@ -144,16 +101,3 @@ extension BibleClient {
// verse: <#@Sendable (BookID, ChapterID, VerseID) async throws -> Verse#>
// )
//}

extension DependencyValues {
public var bible: BibleClient {
get {
self[BibleClient.self]
}

set {
self[BibleClient.self] = newValue
}
}
}

27 changes: 27 additions & 0 deletions BibleCore/Sources/BibleClient/Test.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import BibleCore
import Foundation
import ComposableArchitecture

enum BibleClientError: Error {
case unknown
}

extension BibleClient {
public static var testValue: BibleClient = Self(
translations: { [.mock] },
translation: { _ in .mock },
genres: { [.mock] },
genre: { _ in .mock },
books: { [.genesis, .exodus, .leviticus] },
book: { id in
guard let book = IdentifiedArray(uniqueElements: [Book].mock)[id: id] else {
throw BibleClientError.unknown
}

return book
},
chapters: { _ in [.mock] },
verses: { _, _ in [.mock] },
verse: { _, _, _ in .mock }
)
}

0 comments on commit 2802ca3

Please sign in to comment.