Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ [Feat] modifying Router & Tabbar #20

Merged
merged 5 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Projects/App/Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import DesignSystem

struct ContentView: View {
var body: some View {
RouterView {
TabbarMainView()
VStack {
Text("dd")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,53 +9,42 @@
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
final class Router: ObservableObject {
enum Route: Hashable, Identifiable {
var id: Self { self }

case productDetail
case productReview
case categoryFilter
}

@Published public var path: NavigationPath = NavigationPath()
@Published var path: NavigationPath = NavigationPath()
@Published var defaultView: Tabbar = .home

@ViewBuilder public func view(for route: Route) -> some View {
@ViewBuilder 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")
case .productDetail:
Text("제품 상세뷰")
case .productReview:
Text("제품 리뷰뷰")
case .categoryFilter:
Text("카테고리 선택뷰")
}
}

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

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

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

public func replaceNavigationStack(_ page: Route) {
func replaceNavigationStack(_ page: Route) {
path.removeLast(path.count)
path.append(page)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,8 @@ struct RouterView<Content: View>: View {
.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
Expand Up @@ -10,48 +10,43 @@ 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")
Text("GO TO ANOTHERVIEW")
.font(.largeTitle)
.onTapGesture {
viewModel.selected = .category
router.navigateTo(.categoryFilter)
}
.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")
Text("GO TO ANOTHERVIEW")
.onTapGesture {
viewModel.selected = .mypage
router.navigateTo(.productDetail)
}
}
}
}

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

var body: some View {
ZStack {
Color.green
.ignoresSafeArea()
Text("GO TO Another View")
Text("GO TO PRODUCTDETAIL")
.onTapGesture {
router.replaceNavigationStack(.review)
router.replaceNavigationStack(.productDetail)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import SwiftUI
import DesignSystem

enum Tabbar: CaseIterable {
case home, category, mypage
Expand All @@ -15,11 +16,17 @@ enum Tabbar: CaseIterable {
var view: some View {
switch self {
case .home:
HomeView()
RouterView {
HomeView()
}
case .category:
CategoryView()
RouterView {
CategoryView()
}
case .mypage:
MypageView()
RouterView {
MypageView()
}
}
}

Expand All @@ -34,25 +41,25 @@ enum Tabbar: CaseIterable {
}
}

// var image: Image {
// switch self {
// case .home:
// <#code#>
// case .category:
// <#code#>
// case .mypage:
// <#code#>
// }
// }
var image_default: Image {
switch self {
case .home:
ZerosomeTab.ic_home
case .category:
ZerosomeTab.ic_category
case .mypage:
ZerosomeTab.ic_mypage
}
}

// var image_fill: Image {
// switch self {
// case .home:
// <#code#>
// case .category:
// <#code#>
// case .mypage:
// <#code#>
// }
// }
var image_fill: Image {
switch self {
case .home:
ZerosomeTab.ic_home_fill
case .category:
ZerosomeTab.ic_category_fill
case .mypage:
ZerosomeTab.ic_mpyage_fill
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,20 @@ 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)
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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,44 @@
//

import SwiftUI
import DesignSystem

struct TabbarView: View {
@ObservedObject var viewModel: TabbarViewModel
@EnvironmentObject var router: Router

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)
Rectangle()
.fill(Color.white)
.shadow(color: .black.opacity(0.01), radius: 1, y: -2.0)
.blur(radius: 8)
.shadow(radius: 10)
.frame(height: 66)
.overlay {
HStack {
ForEach(Tabbar.allCases, id: \.self) { item in
VStack(spacing: 5) {
(viewModel.selected == item ? item.image_fill : item.image_default)
.frame(width: 24, height: 24)
Text(item.title)
.applyFont(font: .label1)
.foregroundStyle(viewModel.selected == item ? Color.primaryFF6972 : Color.neutral400)
}
.frame(maxWidth: .infinity, alignment: .center)
.contentShape(Rectangle())
.padding(.bottom, 10)
.onTapGesture {
viewModel.selected = item
}
// .simultaneousGesture(TapGesture(count: 2).onEnded {
// router.popToRoot()
// })
}
}
.frame(maxWidth: .infinity, alignment: .center)
.padding(.bottom, 10)
.onTapGesture {
viewModel.selected = item
}
.padding(.top, 10)
.background(.white)
}
}

}
}

Expand Down
2 changes: 1 addition & 1 deletion Projects/App/Sources/ZerosomeApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import KakaoSDKAuth
struct ZerosomeApp: App {
var body: some Scene {
WindowGroup {
ContentView()
TabbarMainView()
}
}
}
2 changes: 1 addition & 1 deletion Projects/DesignSystem/Sources/Assets/Tab/Tabbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ public enum ZerosomeTab {
public static let ic_category = DesignSystemAsset.Assets.icCategoryEmpty.swiftUIImage
public static let ic_category_fill = DesignSystemAsset.Assets.icCategoryFill.swiftUIImage
public static let ic_mypage = DesignSystemAsset.Assets.icMypageEmpty.swiftUIImage
public static let ic_mpyage_fi = DesignSystemAsset.Assets.icMypageFill.swiftUIImage
public static let ic_mpyage_fill = DesignSystemAsset.Assets.icMypageFill.swiftUIImage
}