Skip to content

Commit

Permalink
fix: 코드리뷰 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
Jihyun247 committed Aug 15, 2024
1 parent a3a3fdc commit 76daeed
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 46 deletions.
8 changes: 4 additions & 4 deletions Projects/Feature/Feature/Sources/AppCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@ public struct AppCore {
state.onboarding = OnboardingCore.State()
return .none

case .splash:
return .none

case .home:
return .none

// case .onboarding:
// return .none

default:
case .onboarding:
return .none
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ public struct OnboardingCore {
@ObservableState
public struct State: Equatable {
public init() { }
var data = RandomAccessEquatableItems<OnboardingItem>(elements: OnboardingItemsData)
var fakedData: RandomAccessEquatableItems<OnboardingItem> = .init(elements: [])
var data: [OnboardingItem] = OnboardingItemsData
var fakedData: [OnboardingItem] = []
var width: CGFloat = .zero
var currentIdx: Int = 0
var currentItemID: String = ""
}

public enum Action: BindableAction {
case onApear
case setIndex(Int)
case calculateOffset(CGFloat, OnboardingItem)
case dragStart
case _timerStart
Expand All @@ -37,33 +36,31 @@ public struct OnboardingCore {

public init() { }

private enum CancelID { case timer }
@Dependency(\.continuousClock) var clock
@Dependency(\.mainQueue) var mainQueue

public var body: some ReducerOf<Self> {
BindingReducer()
Reduce(self.core)
}

private func core(_ state: inout State, _ action: Action) -> EffectOf<Self> {
enum CancelID { case timer, timerDebounce }

switch action {
case .onApear:
state.currentItemID = state.data.first!.id.uuidString
state.fakedData.elements = state.data.elements
state.fakedData = state.data
guard var first = state.data.first,
var last = state.data.last else { return .none }
first.id = .init()
last.id = .init()
state.fakedData.elements.append(first)
state.fakedData.elements.insert(last, at: 0)
state.fakedData.append(first)
state.fakedData.insert(last, at: 0)
return .run { send in
await send(._timerStart)
}

case .setIndex(let idx):
state.currentIdx = idx
return .none

case .calculateOffset(let minX, let item):
let fakeIndex = state.fakedData.firstIndex(of: item) ?? 0
state.currentIdx = state.data.firstIndex { item in
Expand All @@ -73,21 +70,21 @@ public struct OnboardingCore {

let pageProgress: CGFloat = pageOffset / state.width
if -pageProgress < 1.0 {
if state.fakedData.elements.indices.contains(state.fakedData.count - 1) {
if state.fakedData.indices.contains(state.fakedData.count - 1) {
state.currentItemID = state.fakedData[state.fakedData.count - 1].id.uuidString
}
}
if -pageProgress > CGFloat(state.fakedData.count - 1) {
if state.fakedData.elements.indices.contains(1) {
if state.fakedData.indices.contains(1) {
state.currentItemID = state.fakedData[1].id.uuidString
}
}
return .none

case .dragStart:
let timerEndAction: Effect<Action> = .send(._timerEnd)
let timerStartAction: Effect<Action> = .send(._timerStart)
.debounce(id: "timerStart", for: .seconds(2), scheduler: DispatchQueue.main)
let timerEndAction: Effect<Action> = .run { send in await send(._timerEnd) }
let timerStartAction: Effect<Action> = .run { send in await send(._timerStart) }
.debounce(id: CancelID.timerDebounce, for: .seconds(2), scheduler: self.mainQueue)
return .merge(
timerEndAction,
timerStartAction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI

import DesignSystem

public struct OnboardingItem: Hashable, Identifiable {
public struct OnboardingItem: Equatable, Identifiable {
public var id: UUID = .init()
let image: UIImage
var title: String
Expand All @@ -33,27 +33,3 @@ let OnboardingItemsData: [OnboardingItem] = [
subTitle: "일정 시간 집중과 휴식을 반복해 번아웃을 방지하고\n짧은 시간의 몰입을 경험해보세요.")
]

public struct RandomAccessEquatableItems<Element>: RandomAccessCollection, Equatable where Element: Equatable {
var elements: [Element]
public init(elements: [Element]) {
self.elements = elements
}

public var startIndex: Int { elements.startIndex }
public var endIndex: Int { elements.endIndex }

public subscript(position: Int) -> Element {
return elements[position]
}

public static func == (lhs: RandomAccessEquatableItems, rhs: RandomAccessEquatableItems) -> Bool {
guard lhs.count == rhs.count else { return false }
for (index, element) in lhs.enumerated() {
if element != rhs[index] {
return false
}
}
return true
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public struct OnboardingView: View {
Spacer()
VStack(spacing: 0) {
TabView(selection: $store.currentItemID) {
ForEach(store.fakedData, id: \.hashValue) { item in
ForEach(store.fakedData) { item in
OnboardingCarouselContentView(width: $store.width, item: item)
.tag(item.id.uuidString)
.offsetX(store.currentItemID == item.id.uuidString) { minX in
Expand Down

0 comments on commit 76daeed

Please sign in to comment.