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

Init coordinator pattern #6

Merged
merged 4 commits into from
Jun 30, 2023
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
54 changes: 54 additions & 0 deletions Projects/Presentation/Sources/Coordinator/Coordinator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// LearningTabCoordinator.swift
// Presentation
//
// Created by 김동락 on 2023/06/27.
// Copyright © 2023 Lito. All rights reserved.
//

import SwiftUI

// 탭별로 화면을 나눠야하할까
// 근데 B탭 화면에서 A탭 화면으로 이동하는게 있다면?
enum Page: Hashable {
case ATabFirstView
case ATabSecondView(str: String)
case BTabFirstView
case BTabSecondView
case BTabThirdView
}

@available(iOS 16.0, *)
class Coordinator: ObservableObject {
@Published var path = NavigationPath()

func push(_ page: Page) {
path.append(page)
}

func pop() {
path.removeLast()
}

// 여러 뷰 없애는거면 애니메이션 효과가 적용되지 않음
// 커스텀 애니메이션 필요?
func popToRoot() {
path.removeLast(path.count)
}

@ViewBuilder
func build(page: Page) -> some View {
switch page {
case .ATabFirstView:
ATabFirstView()
case .ATabSecondView(let str):
ATabSecondView(str: str)
case .BTabFirstView:
BTabFirstView()
case .BTabSecondView:
BTabSecondView()
case .BTabThirdView:
BTabThirdView()
}
}
}
15 changes: 9 additions & 6 deletions Projects/Presentation/Sources/Home/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ public struct HomeView: View {
}

public var body: some View {
VStack{
content
.padding(.bottom, 30)
Button("Change Quote") {
self.viewModel.loadSlip()
}
// VStack{
// content
// .padding(.bottom, 30)
// Button("Change Quote") {
// self.viewModel.loadSlip()
// }
// }
if #available(iOS 16.0, *) {
RootTabView()
}

}
Expand Down
32 changes: 32 additions & 0 deletions Projects/Presentation/Sources/TestViews/ATabFirstView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// FirstView.swift
// Presentation
//
// Created by 김동락 on 2023/06/27.
// Copyright © 2023 Lito. All rights reserved.
//

import SwiftUI

@available(iOS 16.0, *)
struct ATabFirstView: View {

@EnvironmentObject private var coordinator: Coordinator

var body: some View {
VStack {
Text("This is ATab First View")
Button {
coordinator.push(.ATabSecondView(str: "Hi"))
} label: {
Text("Move to Second View")
}
}
}
}

//struct FirstView_Previews: PreviewProvider {
// static var previews: some View {
// ATabFirstView()
// }
//}
39 changes: 39 additions & 0 deletions Projects/Presentation/Sources/TestViews/ATabSecondView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// ATabSeconddView.swift
// Presentation
//
// Created by 김동락 on 2023/06/27.
// Copyright © 2023 Lito. All rights reserved.
//

import SwiftUI

@available(iOS 16.0, *)
struct ATabSecondView: View {

@EnvironmentObject private var coordinator: Coordinator
var str: String

init(str: String) {
self.str = str
}


var body: some View {
VStack {
Text("This is ATab Second View")
Text("From previous view: " + str)
Button {
coordinator.pop()
} label: {
Text("Pop to Root")
}
}
}
}

//struct ATabSeconddView_Previews: PreviewProvider {
// static var previews: some View {
// ATabSecondView(str: "")
// }
//}
38 changes: 38 additions & 0 deletions Projects/Presentation/Sources/TestViews/BTabFirstView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// BTabFirstView.swift
// Presentation
//
// Created by 김동락 on 2023/06/27.
// Copyright © 2023 Lito. All rights reserved.
//

import SwiftUI

@available(iOS 16.0, *)
struct BTabFirstView: View {

@EnvironmentObject private var coordinator: Coordinator

var body: some View {
VStack {
Text("This is BTab First View")
Button {
coordinator.push(.BTabSecondView)
} label: {
Text("Move to Second View")
}
Button {
coordinator.push(.BTabSecondView)
coordinator.push(.BTabThirdView)
} label: {
Text("Move to Third View (Deep Link)")
}
}
}
}

//struct BTabFirstView_Previews: PreviewProvider {
// static var previews: some View {
// BTabFirstView()
// }
//}
32 changes: 32 additions & 0 deletions Projects/Presentation/Sources/TestViews/BTabSecondView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// BTabSecondView.swift
// Presentation
//
// Created by 김동락 on 2023/06/27.
// Copyright © 2023 Lito. All rights reserved.
//

import SwiftUI

@available(iOS 16.0, *)
struct BTabSecondView: View {

@EnvironmentObject private var coordinator: Coordinator

var body: some View {
VStack {
Text("This is BTab Second View")
Button {
coordinator.push(.BTabThirdView)
} label: {
Text("Move to Third View")
}
}
}
}

//struct BTabSecondView_Previews: PreviewProvider {
// static var previews: some View {
// BTabSecondView()
// }
//}
32 changes: 32 additions & 0 deletions Projects/Presentation/Sources/TestViews/BTabThirdView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// BTabThirdView.swift
// Presentation
//
// Created by 김동락 on 2023/06/27.
// Copyright © 2023 Lito. All rights reserved.
//

import SwiftUI

@available(iOS 16.0, *)
struct BTabThirdView: View {

@EnvironmentObject private var coordinator: Coordinator

var body: some View {
VStack {
Text("This is BTab Third View")
Button {
coordinator.popToRoot()
} label: {
Text("Pop to Root")
}
}
}
}

//struct BTabThirdView_Previews: PreviewProvider {
// static var previews: some View {
// BTabThirdView()
// }
//}
37 changes: 37 additions & 0 deletions Projects/Presentation/Sources/TestViews/RootTabView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// TabView.swift
// Presentation
//
// Created by 김동락 on 2023/06/27.
// Copyright © 2023 Lito. All rights reserved.
//

import SwiftUI

@available(iOS 16.0, *)
struct RootTabView: View {

// swinject로 의존성 주입 받기?
@StateObject private var coordinator = Coordinator()

var body: some View {
NavigationStack(path: $coordinator.path) {
TabView {
coordinator.build(page: .ATabFirstView)
.tabItem { Text("ATab") }
coordinator.build(page: .BTabFirstView)
.tabItem { Text("BTab") }
}
.navigationDestination(for: Page.self) { page in
coordinator.build(page: page)
}
}
.environmentObject(coordinator)
}
}

//struct TabView_Previews: PreviewProvider {
// static var previews: some View {
// RootTabView()
// }
//}