Skip to content

Commit

Permalink
Add favorite feature, connect with rootview (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
ry-itto authored Jun 23, 2024
1 parent 870fcf0 commit 79a978a
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 18 deletions.
7 changes: 7 additions & 0 deletions app-ios/App/App.xctestplan
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
"identifier" : "TimetableDetailFeatureTests",
"name" : "TimetableDetailFeatureTests"
}
},
{
"target" : {
"containerPath" : "container:",
"identifier" : "FavoriteFeatureTests",
"name" : "FavoriteFeatureTests"
}
}
],
"version" : 1
Expand Down
23 changes: 22 additions & 1 deletion app-ios/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ let package = Package(
.library(
name: "AboutFeature",
targets: ["AboutFeature"]
)
),
.library(
name: "FavoriteFeature",
targets: ["FavoriteFeature"]
),
],
dependencies: [
.package(url: "https://github.com/pointfreeco/swift-composable-architecture.git", exact: "1.10.2"),
Expand All @@ -40,6 +44,8 @@ let package = Package(
.target(
name: "App",
dependencies: [
.aboutFeature,
.favoriteFeature,
.timetableFeature,
.timetableDetailFeature,
.tca,
Expand Down Expand Up @@ -111,6 +117,20 @@ let package = Package(
]
),

.target(
name: "FavoriteFeature",
dependencies: [
.tca,
]
),
.testTarget(
name: "FavoriteFeatureTests",
dependencies: [
.favoriteFeature,
.tca
]
),

// Please run ./gradlew app-ios-shared:assembleSharedXCFramework first
.binaryTarget(name: "KmpModule", path: "../app-ios-shared/build/XCFrameworks/debug/shared.xcframework"),
]
Expand All @@ -133,6 +153,7 @@ extension Target.Dependency {
static let timetableDetailFeature: Target.Dependency = "TimetableDetailFeature"
static let timetableFeature: Target.Dependency = "TimetableFeature"
static let aboutFeature: Target.Dependency = "AboutFeature"
static let favoriteFeature: Target.Dependency = "FavoriteFeature"
static let kmpModule: Target.Dependency = "KmpModule"

static let firebaseAuth: Target.Dependency = .product(name: "FirebaseAuth", package: "firebase-ios-sdk")
Expand Down
18 changes: 17 additions & 1 deletion app-ios/Sources/App/RootReducer.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import AboutFeature
import ComposableArchitecture
import FavoriteFeature
import TimetableFeature

@Reducer
Expand All @@ -9,19 +11,27 @@ public struct RootReducer {
public struct State: Equatable {
public var appDelegate: AppDelegateReducer.State
public var timetable: TimetableReducer.State
public var favorite: FavoriteReducer.State
public var about: AboutReducer.State

public init(
appDelegate: AppDelegateReducer.State = .init(),
timetable: TimetableReducer.State = .init()
timetable: TimetableReducer.State = .init(),
favorite: FavoriteReducer.State = .init(),
about: AboutReducer.State = .init()
) {
self.appDelegate = appDelegate
self.timetable = timetable
self.favorite = favorite
self.about = about
}
}

public enum Action {
case appDelegate(AppDelegateReducer.Action)
case timetable(TimetableReducer.Action)
case favorite(FavoriteReducer.Action)
case about(AboutReducer.Action)
}

public var body: some ReducerOf<Self> {
Expand All @@ -31,5 +41,11 @@ public struct RootReducer {
Scope(state: \.timetable, action: \.timetable) {
TimetableReducer()
}
Scope(state: \.favorite, action: \.favorite) {
FavoriteReducer()
}
Scope(state: \.about, action: \.about) {
AboutReducer()
}
}
}
44 changes: 28 additions & 16 deletions app-ios/Sources/App/RootView.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import AboutFeature
import FavoriteFeature
import ComposableArchitecture
import SwiftUI
import TimetableFeature
Expand Down Expand Up @@ -43,23 +45,33 @@ public struct RootView: View {
)
}

Text("Favorite Feature")
.tag(Tab.favorite)
.tabItem {
Label(
title: { Text("Favorite") },
icon: { Image(systemName: "42.circle") }
)
}
FavoriteScreen(
store: store.scope(
state: \.favorite,
action: \.favorite
)
)
.tag(Tab.favorite)
.tabItem {
Label(
title: { Text("Favorite") },
icon: { Image(systemName: "42.circle") }
)
}

Text("About Feature")
.tag(Tab.about)
.tabItem {
Label(
title: { Text("About") },
icon: { Image(systemName: "42.circle") }
)
}
AboutView(
store: store.scope(
state: \.about,
action: \.about
)
)
.tag(Tab.about)
.tabItem {
Label(
title: { Text("About") },
icon: { Image(systemName: "42.circle") }
)
}

Text("ID Card Feature")
.tag(Tab.idCard)
Expand Down
30 changes: 30 additions & 0 deletions app-ios/Sources/FavoriteFeature/FavoriteReducer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import ComposableArchitecture

@Reducer
public struct FavoriteReducer {
public init() { }

@ObservableState
public struct State: Equatable {
public var text: String

public init(text: String = "") {
self.text = text
}
}

public enum Action {
case onAppear
}

public var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .onAppear:
state.text = "Favorite Feature"
return .none
}
}
}
}

26 changes: 26 additions & 0 deletions app-ios/Sources/FavoriteFeature/FavoriteScreen.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import ComposableArchitecture
import SwiftUI

public struct FavoriteScreen: View {
private let store: StoreOf<FavoriteReducer>

public init(store: StoreOf<FavoriteReducer>) {
self.store = store
}

public var body: some View {
Text(store.text)
.onAppear {
store.send(.onAppear)
}
}
}

#Preview {
FavoriteScreen(
store: .init(
initialState: .init(),
reducer: {}
)
)
}
16 changes: 16 additions & 0 deletions app-ios/Tests/FavoriteFeatureTests/FavoriteTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import XCTest
import ComposableArchitecture
@testable import FavoriteFeature

final class FavoriteTests: XCTestCase {
@MainActor func testExample() async throws {
let store = TestStore(
initialState: FavoriteReducer.State(text: "Test")
) {
FavoriteReducer()
}
await store.send(.onAppear) {
$0.text = "Favorite Feature"
}
}
}

0 comments on commit 79a978a

Please sign in to comment.