Skip to content

Commit

Permalink
✨[Feat] Routert 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
syss220211 committed Jun 13, 2024
1 parent 10f90e6 commit b04100f
Show file tree
Hide file tree
Showing 10 changed files with 370 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Projects/App/Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import DesignSystem

struct ContentView: View {
var body: some View {
Text("Hello, World!")
.applyFont(font: .heading1)
Text("Hello, World!")
RouterView {
TabbarMainView()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Ex+View+Navigation.swift
// DesignSystem
//
// Created by 박서연 on 2024/06/12.
// Copyright © 2024 iOS. All rights reserved.
//

import SwiftUI

public extension View {
func navigationBackButton(_ action: @escaping () -> Void) -> some View {
self.navigationBarBackButtonHidden()
.toolbar {
ToolbarItem(placement: .topBarLeading) {
Button(action: action, label: {
// TODO: - 백버튼 이미지 수정

Image("arrowBack")
.resizable()
.frame(width: 24, height: 24)
})
}
}
}

func navigationTitle(with text: Text) -> some View {
VStack(spacing: 31) {
HStack(spacing: 0) {
text
.applyFont(font: .heading1)
.frame(height: 47)
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.init(top: 10, leading: 22, bottom: 10, trailing: 0))
Spacer()
}
self
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Router.swift
// DesignSystem
//
// Created by 박서연 on 2024/06/12.
// Copyright © 2024 iOS. All rights reserved.
//

import Foundation
import SwiftUI

public class Router: ObservableObject {
public enum Route: Hashable, Identifiable {
public var id: Self { self }

case tabView
case login
case home
case category
case mypage
case review
case setting
}

@Published public var path: NavigationPath = NavigationPath()

@ViewBuilder public func view(for route: Route) -> some View {
switch route {
case .tabView:
TabbarMainView()
case .login:
Text("login")
case .home:
HomeView()
case .category:
CategoryView()
case .mypage:
MypageView()
case .review:
AnotherView()
case .setting:
Text("setting")
}
}

public func navigateTo(_ page: Route) {
path.append(page)
}

public func navigateBack() {
path.removeLast()
}

public func popToRoot() {
path.removeLast(path.count)
}

public func replaceNavigationStack(_ page: Route) {
path.removeLast(path.count)
path.append(page)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// RouterView.swift
// DesignSystem
//
// Created by 박서연 on 2024/06/12.
// Copyright © 2024 iOS. All rights reserved.
//

import SwiftUI

struct RouterView<Content: View>: View {
@StateObject var router: Router = Router()
private let content: Content

init(@ViewBuilder content: @escaping () -> Content) {
self.content = content()
}

var body: some View {
NavigationStack(path: $router.path) {
VStack {
content
}
.navigationDestination(for: Router.Route.self) { route in
router.view(for: route)
}
.onAppear {
router.navigateTo(.tabView)
}
.navigationBarTitle("", displayMode: .inline)
}
.environmentObject(router)
}
}

//#Preview {
// RouterView()
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// AnotherView.swift
// App
//
// Created by 박서연 on 2024/06/13.
// Copyright © 2024 iOS. All rights reserved.
//

import SwiftUI

struct AnotherView: View {
var body: some View {
Text("Saaaaample Viewwwwww")
}
}

#Preview {
AnotherView()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// HomeView.swift
// DesignSystem
//
// Created by 박서연 on 2024/06/12.
// Copyright © 2024 iOS. All rights reserved.
//

import SwiftUI

struct HomeView: View {
@EnvironmentObject var router: Router
@EnvironmentObject var viewModel: TabbarViewModel

var body: some View {
ZStack {
Color.red
.ignoresSafeArea()
Text("Go TO CATEGORY VIEW")
.onTapGesture {
viewModel.selected = .category
}
.foregroundStyle(.white)
}
}
}

struct CategoryView: View {
@EnvironmentObject var router: Router
@EnvironmentObject var viewModel: TabbarViewModel

var body: some View {
ZStack {
Color.yellow
.ignoresSafeArea()
Text("GO TO MYPAGEVIEW")
.onTapGesture {
viewModel.selected = .mypage
}
}
}
}

struct MypageView: View {
@EnvironmentObject var router: Router
@EnvironmentObject var viewModel: TabbarViewModel

var body: some View {
ZStack {
Color.green
.ignoresSafeArea()
Text("GO TO Another View")
.onTapGesture {
router.replaceNavigationStack(.review)
}
}
}
}

#Preview {
HomeView()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// Tabbar.swift
// DesignSystem
//
// Created by 박서연 on 2024/06/12.
// Copyright © 2024 iOS. All rights reserved.
//

import SwiftUI

enum Tabbar: CaseIterable {
case home, category, mypage

@ViewBuilder
var view: some View {
switch self {
case .home:
HomeView()
case .category:
CategoryView()
case .mypage:
MypageView()
}
}

var title: String {
switch self {
case .home:
""
case .category:
"키테고리 검색"
case .mypage:
"마이페이지"
}
}

// var image: Image {
// switch self {
// case .home:
// <#code#>
// case .category:
// <#code#>
// case .mypage:
// <#code#>
// }
// }

// var image_fill: Image {
// switch self {
// case .home:
// <#code#>
// case .category:
// <#code#>
// case .mypage:
// <#code#>
// }
// }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// TabbarMainView.swift
// DesignSystem
//
// Created by 박서연 on 2024/06/12.
// Copyright © 2024 iOS. All rights reserved.
//

import SwiftUI

struct TabbarMainView: View {
@StateObject public var viewModel = TabbarViewModel()

var body: some View {
NavigationStack {
TabView(selection: $viewModel.selected) {
ForEach(Tabbar.allCases, id: \.self) { tab in
tab.view
}
.toolbarBackground(.hidden, for: .tabBar)
}
}
.overlay {
VStack {
Spacer()
TabbarView(viewModel: viewModel)
}
}
.environmentObject(viewModel)
}
}

#Preview {
TabbarMainView()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// TabbarView.swift
// DesignSystem
//
// Created by 박서연 on 2024/06/12.
// Copyright © 2024 iOS. All rights reserved.
//

import SwiftUI

struct TabbarView: View {
@ObservedObject var viewModel: TabbarViewModel

var body: some View {
HStack {
ForEach(Tabbar.allCases, id: \.self) { item in
Button {
viewModel.selected = item
} label: {
VStack(spacing: 0) {
Image(systemName: "house")
.frame(width: 39, height: 39)
Text(item.title)
.applyFont(font: .label1)
.foregroundStyle(Color.neutral400)
}
}
.frame(maxWidth: .infinity, alignment: .center)
.padding(.bottom, 10)
.onTapGesture {
viewModel.selected = item
}
}
}
}
}

#Preview {
TabbarView(viewModel: TabbarViewModel())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// TabbarViewModel.swift
// App
//
// Created by 박서연 on 2024/06/13.
// Copyright © 2024 iOS. All rights reserved.
//

import SwiftUI

final class TabbarViewModel: ObservableObject {
@Published var selected: Tabbar = .home
}

0 comments on commit b04100f

Please sign in to comment.