Skip to content

Commit

Permalink
feat: workshop1
Browse files Browse the repository at this point in the history
* Convert to TCA

* Add test

* Add ._printChanges() on reducer

* Revise RepositoryModel
  • Loading branch information
dy-kim authored Nov 28, 2022
1 parent bb9386c commit e539367
Show file tree
Hide file tree
Showing 16 changed files with 889 additions and 6 deletions.
548 changes: 548 additions & 0 deletions final/GitHubSearch-final.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"pins" : [
{
"identity" : "combine-schedulers",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/combine-schedulers",
"state" : {
"revision" : "882ac01eb7ef9e36d4467eb4b1151e74fcef85ab",
"version" : "0.9.1"
}
},
{
"identity" : "swift-case-paths",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-case-paths",
"state" : {
"revision" : "bb436421f57269fbcfe7360735985321585a86e5",
"version" : "0.10.1"
}
},
{
"identity" : "swift-clocks",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-clocks",
"state" : {
"revision" : "20b25ca0dd88ebfb9111ec937814ddc5a8880172",
"version" : "0.2.0"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections",
"state" : {
"revision" : "f504716c27d2e5d4144fa4794b12129301d17729",
"version" : "1.0.3"
}
},
{
"identity" : "swift-composable-architecture",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-composable-architecture",
"state" : {
"revision" : "52dca7e5edd6ec9e0b529380843a8cb13f57d7d7",
"version" : "0.46.0"
}
},
{
"identity" : "swift-custom-dump",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-custom-dump",
"state" : {
"revision" : "ead7d30cc224c3642c150b546f4f1080d1c411a8",
"version" : "0.6.1"
}
},
{
"identity" : "swift-identified-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-identified-collections",
"state" : {
"revision" : "a08887de589e3829d488e0b4b707b2ca804b1060",
"version" : "0.5.0"
}
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "5a5457a744239896e9b0b03a8e1a5069c3e7b91f",
"version" : "0.6.0"
}
}
],
"version" : 2
}
5 changes: 5 additions & 0 deletions final/GitHubSearch/APIError.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

enum APIError: Error {
case invalidURL
}
17 changes: 17 additions & 0 deletions final/GitHubSearch/GitHubSearchApp.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import SwiftUI

import ComposableArchitecture

@main
struct GitHubSearchApp: App {
var body: some Scene {
WindowGroup {
RepoSearchView(
store: Store(
initialState: RepoSearch.State(),
reducer: RepoSearch()._printChanges()
)
)
}
}
}
34 changes: 34 additions & 0 deletions final/GitHubSearch/Repository/RepoSearch.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import ComposableArchitecture

struct RepoSearch: ReducerProtocol {
struct State: Equatable {
var keyword = ""
var searchResults = [String]()
}

enum Action: Equatable {
case keywordChanged(String)
case search
}

func reduce(into state: inout State, action: Action) -> EffectTask<Action> {
switch action {
case let .keywordChanged(keyword):
state.keyword = keyword
return .none

case .search:
state.searchResults = self.sampleRepoLists.filter {
$0.contains(state.keyword)
}
return .none
}
}

private let sampleRepoLists = [
"Swift",
"SwiftyJSON",
"SwiftGuide",
"SwiftterSwift",
]
}
52 changes: 52 additions & 0 deletions final/GitHubSearch/Repository/RepoSearchClient.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Foundation

import ComposableArchitecture
import XCTestDynamicOverlay

struct RepoSearchClient {
var search: @Sendable (String) async throws -> RepositoryModel
}

extension DependencyValues {
var repoSearchClient: RepoSearchClient {
get { self[RepoSearchClient.self] }
set { self[RepoSearchClient.self] = newValue }
}
}

// MARK: - Live API implementation

extension RepoSearchClient: DependencyKey {
static let liveValue = RepoSearchClient(
search: { keyword in
guard let url = URL(string: "https://api.github.com/search/repositories?q=\(keyword)") else {
throw APIError.invalidURL
}
let (data, _) = try await URLSession.shared.data(from: url)
return try JSONDecoder().decode(RepositoryModel.self, from: data)
}
)
}

extension RepoSearchClient: TestDependencyKey {
static let previewValue = Self(
search: { _ in .mock }
)

static let testValue = Self(
search: unimplemented("\(Self.self).search")
)
}

// Workshop 진행 중에 임시로 활용할 코드
//
//func sampleSearchRequest(keyword: String, send: Send<RepoSearch.Action>) async throws {
// guard let url = URL(string: "https://api.github.com/search/repositories?q=\(keyword)") else {
// await send(RepoSearch.Action.dataLoaded(.failure(APIError.invalidUrlError)))
// return
// }
// let (data, _) = try await URLSession.shared.data(from: url)
// let result = await TaskResult { try JSONDecoder().decode(RepositoryModel.self, from: data) }
//
// await send(RepoSearch.Action.dataLoaded(result))
//}
50 changes: 50 additions & 0 deletions final/GitHubSearch/Repository/RepoSearchView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import SwiftUI

import ComposableArchitecture

struct RepoSearchView: View {
let store: StoreOf<RepoSearch>

var body: some View {
WithViewStore(self.store) { viewStore in
NavigationView {
VStack {
HStack {
TextField(
"Search repo",
text: Binding(
get: { viewStore.keyword },
set: { viewStore.send(.keywordChanged($0)) }
)
)
.textFieldStyle(.roundedBorder)

Button("Search") {
viewStore.send(.search)
}
.buttonStyle(.borderedProminent)
}
.padding()

List {
ForEach(viewStore.searchResults, id: \.self) {
Text($0)
}
}
}
.navigationTitle("Github Search")
}
}
}
}

struct RepoSearchView_Previews: PreviewProvider {
static var previews: some View {
RepoSearchView(
store: Store(
initialState: RepoSearch.State(),
reducer: RepoSearch()
)
)
}
}
23 changes: 23 additions & 0 deletions final/GitHubSearch/Repository/RepositoryModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
struct RepositoryModel: Decodable, Equatable, Sendable {
var items: [Result]

struct Result: Decodable, Equatable, Sendable {
var name: String

enum CodingKeys: String, CodingKey {
case name = "full_name"
}
}
}

// MARK: - Mock data

extension RepositoryModel {
static let mock = Self(
items: [
RepositoryModel.Result(name: "Swift"),
RepositoryModel.Result(name: "SwiftLint"),
RepositoryModel.Result(name: "SwiftAlgorithm")
]
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"images" : [
{
"idiom" : "universal",
"platform" : "ios",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
6 changes: 6 additions & 0 deletions final/GitHubSearch/Resources/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
28 changes: 28 additions & 0 deletions final/GitHubSearchTests/RepositoryTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import XCTest

import ComposableArchitecture

@testable import GitHubSearch_final

@MainActor
final class RepositoryTests: XCTestCase {
func test_user_get_repoSearchResults_when_search() async {
let store = TestStore(
initialState: RepoSearch.State(),
reducer: RepoSearch()
)

await store.send(.keywordChanged("Swift")) {
$0.keyword = "Swift"
}

await store.send(.search) {
$0.searchResults = [
"Swift",
"SwiftyJSON",
"SwiftGuide",
"SwiftterSwift",
]
}
}
}
10 changes: 4 additions & 6 deletions starter/GitHubSearch/Repository/RepositoryModel.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
struct RepositoryModel: Decodable, Equatable, Sendable {
var items: [Result]

struct Result: Decodable, Equatable, Identifiable, Sendable {
struct Result: Decodable, Equatable, Sendable {
var name: String
var id: Int

enum CodingKeys: String, CodingKey {
case name = "full_name"
case id
}
}
}
Expand All @@ -17,9 +15,9 @@ struct RepositoryModel: Decodable, Equatable, Sendable {
extension RepositoryModel {
static let mock = Self(
items: [
RepositoryModel.Result(name: "Swift", id: 1),
RepositoryModel.Result(name: "SwiftLint", id: 2),
RepositoryModel.Result(name: "SwiftAlgorithm", id: 3)
RepositoryModel.Result(name: "Swift"),
RepositoryModel.Result(name: "SwiftLint"),
RepositoryModel.Result(name: "SwiftAlgorithm")
]
)
}

0 comments on commit e539367

Please sign in to comment.