Skip to content

Commit

Permalink
refactor: #146 explore, report - navigation MVI 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
juri123123 committed Jan 24, 2025
1 parent 29f5753 commit bc3a8a0
Show file tree
Hide file tree
Showing 20 changed files with 242 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct FixedBottomSheetView: View {
title: "떠먹으러 가기",
disabled: $isDisabled
) {
navigationManager.selectedTab = .explore
navigationManager.dispatch(.changeTab(.explore))
}
.padding(.top, 8)
}
Expand Down
4 changes: 1 addition & 3 deletions Spoony-iOS/Spoony-iOS/Resource/Tab/Model/ViewType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@ enum ViewType: Hashable {
case searchView // 검색 화면
case locationView(title: String) // 위치 선택 화면
case detailView(postId: Int) // 상세 화면

case explore
case report
case report(postId: Int)
}
75 changes: 0 additions & 75 deletions Spoony-iOS/Spoony-iOS/Resource/Tab/NavigationManager.swift

This file was deleted.

19 changes: 19 additions & 0 deletions Spoony-iOS/Spoony-iOS/Resource/Tab/Store/NavigationIntent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// NavigationIntent.swift
// Spoony-iOS
//
// Created by 최주리 on 1/24/25.
//

import Foundation

enum NavigationIntent {
// case build
case changeTab(TabType)
case push(ViewType)
case pop(Int)
case popToRoot
case showPopup(PopupType?)
case changePath([ViewType], TabType)
case changeCurrentLocation(String?)
}
96 changes: 96 additions & 0 deletions Spoony-iOS/Spoony-iOS/Resource/Tab/Store/NavigationManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// NavigationManager.swift
// SpoonMe
//
// Created by 최주리 on 1/7/25.
//

import SwiftUI

final class NavigationManager: ObservableObject {
@Published private(set) var state = NavigationState()

func dispatch(_ intent: NavigationIntent) {
switch intent {
// case .build:
// <#code#>
case .changeTab(let tab):
state.selectedTab = tab
case .push(let nextView):
push(nextView)
case .pop(let depth):
pop(depth)
case .popToRoot:
popToRoot()
case .showPopup(let popup):
state.popup = popup
case .changePath(let path, let tab):
switch tab {
case .map:
state.mapPath = path
case .explore:
state.explorePath = path
case .register:
state.registerPath = path
}
case .changeCurrentLocation(let location):
state.currentLocation = location
}
}

@ViewBuilder
func build(_ view: ViewType) -> some View {
switch view {
case .searchView:
SearchView()
case .locationView:
Home()
case .detailView(let postId):
DetailView(postId: postId)
case .report(let postId):
Report(postId: postId)
}
}
}

extension NavigationManager {
private func push(_ view: ViewType) {
switch state.selectedTab {
case .map:
state.mapPath.append(view)
case .explore:
state.explorePath.append(view)
case .register:
state.registerPath.append(view)
}
}

private func pop(_ depth: Int) {
switch state.selectedTab {
case .map:
if state.mapPath.isEmpty || state.mapPath.contains(where: {
if case .locationView = $0 { return true }
return false
}) {
state.currentLocation = nil
}
state.mapPath.removeLast(depth)

case .explore:
state.explorePath.removeLast(depth)
case .register:
state.registerPath.removeLast(depth)
}
}

private func popToRoot() {
switch state.selectedTab {
case .map:
state.mapPath = []
case .explore:
state.explorePath = []
case .register:
state.registerPath = []
}
}
}
17 changes: 17 additions & 0 deletions Spoony-iOS/Spoony-iOS/Resource/Tab/Store/NavigationState.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// NavigationState.swift
// Spoony-iOS
//
// Created by 최주리 on 1/24/25.
//

import Foundation

struct NavigationState {
var selectedTab: TabType = .map
var mapPath: [ViewType] = []
var explorePath: [ViewType] = []
var registerPath: [ViewType] = []
var currentLocation: String?
var popup: PopupType?
}
19 changes: 10 additions & 9 deletions Spoony-iOS/Spoony-iOS/Source/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ struct ContentView: View {
@StateObject private var navigationManager = NavigationManager()

var body: some View {
TabView(selection: $navigationManager.selectedTab) {
NavigationStack(path: $navigationManager.mapPath) {
Home()
.navigationDestination(for: ViewType.self) { viewType in
navigationManager.build(viewType)
}
}
}
.environmentObject(navigationManager)
// TabView(selection: $navigationManager.selectedTab) {
// NavigationStack(path: $navigationManager.mapPath) {
// Home()
// .navigationDestination(for: ViewType.self) { viewType in
// navigationManager.build(viewType)
// }
// }
// }
// .environmentObject(navigationManager)
Text("")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct DetailView: View {
style: .detailWithChip,
spoonCount: store.state.spoonCount,
onBackTapped: {
navigationManager.pop(1)
navigationManager.dispatch(.pop(1))
}
)
ScrollView(.vertical) {
Expand Down Expand Up @@ -261,9 +261,10 @@ extension DetailView {
if store.state.isScoop {
store.send(intent: .pathInfoInNaverMaps)
} else {
navigationManager.popup = .useSpoon(action: {
// action 어떻게 하묘
navigationManager.dispatch(.showPopup(.useSpoon(action: {
store.send(intent: .scoopButtonDidTap)
})
})))
}

print("⭐️")
Expand All @@ -285,7 +286,7 @@ extension DetailView {
items: ["신고하기"],
isPresented: $isPresented
) { _ in
navigationManager.push(.report)
navigationManager.dispatch(.push(.report(postId: store.state.postId)))
}
.frame(alignment: .topTrailing)
.padding(.top, 48.adjustedH)
Expand Down
7 changes: 3 additions & 4 deletions Spoony-iOS/Spoony-iOS/Source/Feature/Explore/Explore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import SwiftUI
import Lottie

struct Explore: View {
@EnvironmentObject private var navigationManager: NavigationManager
@StateObject private var store: ExploreStore = ExploreStore()
@StateObject private var store: ExploreStore

var body: some View {
VStack(spacing: 0) {
Expand Down Expand Up @@ -114,7 +113,7 @@ extension Explore {
title: "등록하러 가기",
disabled: .constant(false)
) {
navigationManager.selectedTab = .register
store.dispatch(.goRegisterButtonTapped)
}
.padding(.top, 18)

Expand All @@ -130,7 +129,7 @@ extension Explore {
.padding(.bottom, 12)
.padding(.horizontal, 20)
.onTapGesture {
navigationManager.push(.detailView(postId: list.postId))
store.dispatch(.cellTapped(list))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ enum ExploreIntent {
// Main
case categoryTapped(CategoryChip)
case cellTapped(FeedEntity)
case goRegisterButtonTapped

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import SwiftUI
final class ExploreStore: ObservableObject {
private let network: ExploreProtocol = DefaultExploreService()
// private let network: ExploreProtocol = MockExploreService()
//TODO: navigation MVI 리팩토링은 나중에 할거다..
// let navigationManager: NavigationManager

private let navigationManager: NavigationManager

@Published private(set) var state: ExploreState = ExploreState()
init(navigationManager: NavigationManager) {
self.navigationManager = navigationManager
}

// init(navigationManager: NavigationManager) {
// self.navigationManager = navigationManager
// }
@Published private(set) var state: ExploreState = ExploreState()

func dispatch(_ intent: ExploreIntent) {
switch intent {
Expand All @@ -40,13 +40,14 @@ final class ExploreStore: ObservableObject {
state.isPresentedFilter = false
case .categoryTapped(let category):
changeCategory(category: category)
case .cellTapped(let feed): break
//추후 feed 정보 detialview에 전달
// navigationManager.push(.detailView)
case .cellTapped(let feed):
navigationManager.dispatch(.push(.detailView(postId: feed.postId)))
case .isPresentedLocationChanged(let newValue):
state.isPresentedLocation = newValue
case .isPresentedFilterChanged(let newValue):
state.isPresentedFilter = newValue
case .goRegisterButtonTapped:
navigationManager.dispatch(.changeTab(.register))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ struct PlaceCard: View {
.padding(.horizontal, 26)
.onTapGesture {
let postId = places[index].postId
navigationManager.push(.detailView(postId: postId))
navigationManager.dispatch(.push(.detailView(postId: postId)))
}
}
}
Expand Down
Loading

0 comments on commit bc3a8a0

Please sign in to comment.