Skip to content

Commit

Permalink
fix: array 기반 Stack으로 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Monsteel committed Aug 7, 2023
1 parent 2fe49e7 commit 2b5a6f5
Show file tree
Hide file tree
Showing 9 changed files with 380 additions and 489 deletions.
97 changes: 49 additions & 48 deletions Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,113 +9,114 @@ import SwiftUI
import RouteStack

struct ContentView: View {
@State var routePaths: RoutePaths<Path> = .init()
@State var routePaths: [RoutePath<Path>] = .init()

@ViewBuilder
func root() -> some View {
let _ = print(routePaths.count)
VStack(alignment: .leading, spacing: 24) {
Text("Root View 입니다.")
.font(.title)
.foregroundStyle(.red)

VStack(alignment: .leading) {
Text("transection")
.font(.subheadline)

Button("custom-sheet -> push -> push") {
routePaths.moveTo([
routePaths.append(contentsOf: [
.init(data: Path.first("custom-sheet"), style: .sheet([.medium, .large], .visible)),
.init(data: Path.second("push"), style: .push),
.init(data: Path.third("push"), style: .push),
])
}

Button("sheet -> push -> push") {
routePaths.moveTo([
routePaths.append(contentsOf: [
.init(data: Path.first("sheet"), style: .sheet()),
.init(data: Path.second("push"), style: .push),
.init(data: Path.third("push"), style: .push),
])
}

Button("cover -> push -> push") {
routePaths.moveTo([
routePaths.append(contentsOf: [
.init(data: Path.first("cover"), style: .cover),
.init(data: Path.second("push"), style: .push),
.init(data: Path.third("push"), style: .push),
])
}

Button("push -> push -> push") {
routePaths.moveTo([
routePaths.append(contentsOf: [
.init(data: Path.first("push"), style: .push),
.init(data: Path.second("push"), style: .push),
.init(data: Path.third("push"), style: .push),
])
}

Button("push -> sheet -> push") {
routePaths.moveTo([
routePaths.append(contentsOf: [
.init(data: Path.first("push"), style: .push),
.init(data: Path.third("sheet"), style: .sheet()),
.init(data: Path.first("push"), style: .push),
])
}
}

VStack(alignment: .leading) {
Text("normal")
.font(.subheadline)

Button("push") {
routePaths.moveTo(.init(data: Path.first("push"), style: .push))
routePaths.append(.init(data: Path.first("push"), style: .push))
}

Button("custom-sheet") {
routePaths.moveTo(.init(data: Path.first("custom-sheet"), style: .sheet([.medium, .large], .visible)))
routePaths.append(.init(data: Path.first("custom-sheet"), style: .sheet([.medium, .large], .visible)))
}

Button("normal-sheet") {
routePaths.moveTo(.init(data: Path.first("normal-sheet"), style: .sheet()))
routePaths.append(.init(data: Path.first("normal-sheet"), style: .sheet()))
}

Button("cover") {
routePaths.moveTo(.init(data: Path.first("cover"), style: .cover))
routePaths.append(.init(data: Path.first("cover"), style: .cover))
}
}
}
}

var body: some View {
RouteStack($routePaths, root: root) { id, path in
switch path {
case let .first(value):
VStack(alignment: .leading) {
Text("First View 입니다.")
.font(.title)

Text("\(value) 로 열렸습니다.")
.font(.subheadline)

Button("back") {
// Deeplink를 통해 routePaths에 직접 접근하지 않고 이동할 수 있습니다.
UIApplication.shared.open(URL(string: "routeStackExample://back")!)
}

Button("push") {
routePaths.moveTo(.init(data: Path.second("push"), style: .push))
routePaths.append(.init(data: Path.second("push"), style: .push))
}

Button("custom-sheet") {
routePaths.moveTo(.init(data: Path.second("custom-sheet"), style: .sheet([.medium, .large], .visible)))
routePaths.append(.init(data: Path.second("custom-sheet"), style: .sheet([.medium, .large], .visible)))
}

Button("normal-sheet") {
routePaths.moveTo(.init(data: Path.second("normal-sheet"), style: .sheet()))
routePaths.append(.init(data: Path.second("normal-sheet"), style: .sheet()))
}

Button("cover") {
routePaths.moveTo(.init(data: Path.second("cover"), style: .cover))
routePaths.append(.init(data: Path.second("cover"), style: .cover))
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
Expand All @@ -124,29 +125,29 @@ struct ContentView: View {
VStack(alignment: .leading) {
Text("Second View 입니다.")
.font(.title)

Text("\(value) 로 열렸습니다.")
.font(.subheadline)

Button("back") {
// Deeplink를 통해 routePaths에 직접 접근하지 않고 이동할 수 있습니다.
UIApplication.shared.open(URL(string: "routeStackExample://back")!)
}

Button("push") {
routePaths.moveTo(.init(data: Path.third("push"), style: .push))
routePaths.append(.init(data: Path.third("push"), style: .push))
}

Button("custom-sheet") {
routePaths.moveTo(.init(data: Path.third("custom-sheet"), style: .sheet([.medium, .large], .visible)))
routePaths.append(.init(data: Path.third("custom-sheet"), style: .sheet([.medium, .large], .visible)))
}

Button("normal-sheet") {
routePaths.moveTo(.init(data: Path.third("normal-sheet"), style: .sheet()))
routePaths.append(.init(data: Path.third("normal-sheet"), style: .sheet()))
}

Button("cover") {
routePaths.moveTo(.init(data: Path.third("cover"), style: .cover))
routePaths.append(.init(data: Path.third("cover"), style: .cover))
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
Expand All @@ -155,15 +156,15 @@ struct ContentView: View {
VStack(alignment: .leading) {
Text("Third View 입니다.")
.font(.title)

Text("\(value) 로 열렸습니다.")
.font(.subheadline)

Button("back") {
// Deeplink를 통해 routePaths에 직접 접근하지 않고 이동할 수 있습니다.
UIApplication.shared.open(URL(string: "routeStackExample://back")!)
}

Button("back To root") {
// Deeplink를 통해 routePaths에 직접 접근하지 않고 이동할 수 있습니다.
UIApplication.shared.open(URL(string: "routeStackExample://backToRoot")!)
Expand All @@ -176,9 +177,9 @@ struct ContentView: View {
.onOpenURL { url in
switch url.host {
case "back":
routePaths.back()
routePaths.removeLast()
case "backToRoot":
routePaths.backToRoot()
routePaths.removeAll()
default: break
}
}
Expand Down
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ enum Path: Hashable {
}

struct ContentView: View {
@State var routePaths: RoutePaths<Path> = .init()
@State var routePaths: [RoutePath<Path>] = .init()

@ViewBuilder
func root() -> some View {
Expand All @@ -49,46 +49,44 @@ struct ContentView: View {

### 기본적인 화면 전환 방법

제공되는 API를 통해, routePaths를 업데이트 할 수 있습니다.

> ⚠️ 제공되는 API이외에, Array 기반 함수를 직접적으로 사용할 경우 예기치 못한 작동을 일으킬 수 있습니다.
Array 기반 API를 통해, routePaths를 업데이트 할 수 있습니다.

```swift
struct ContentView: View {
@State var routePaths: RoutePaths<Path> = .init()
@State var routePaths: [RoutePath<Path>] = .init()

@ViewBuilder
func root() -> some View {
Button("push") {
routePaths.moveTo(.init(data: Path.first("value"), style: .push))
routePaths.append(.init(data: Path.first("value"), style: .push))
}

Button("custom-sheet") {
routePaths.moveTo(.init(data: Path.first("value"), style: .sheet([.medium, .large], .visible)))
routePaths.append(.init(data: Path.first("value"), style: .sheet([.medium, .large], .visible)))
}

Button("normal") {
routePaths.moveTo(.init(data: Path.first("value"), style: .sheet()))
routePaths.append(.init(data: Path.first("value"), style: .sheet()))
}

Button("cover") {
routePaths.moveTo(.init(data: Path.first("value"), style: .push))
routePaths.append(.init(data: Path.first("value"), style: .push))
}

Button("cover -> push -> push") {
routePaths.moveTo([
routePaths.append(contentsOf: [
.init(data: Path.first("value"), style: .cover),
.init(data: Path.first("value"), style: .push),
.init(data: Path.first("value"), style: .push),
])
}

Button("backToRoot") {
routePaths.backToRoot()
routePaths.removeAll()
}

Button("back") {
routePaths.back()
routePaths.removeLast()
}
}

Expand All @@ -105,7 +103,7 @@ routePaths에 직접적으로 접근하지 않고, deeplink를 활용해 화면

```swift
struct ContentView: View {
@State var routePaths: RoutePaths<Path> = .init()
@State var routePaths: [RoutePath<Path>] = .init()

@ViewBuilder
func root() -> some View {
Expand Down
22 changes: 10 additions & 12 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,46 +48,44 @@ struct ContentView: View {

### Basic Screen Transition Methods

You can update routePaths using the provided API:

> ⚠️ Be cautious when using Array-based functions directly as they may cause unexpected behavior.
You can update routePaths using the Array-based functions API:

```swift
struct ContentView: View {
@State var routePaths: RoutePaths<Path> = .init()
@State var routePaths: [RoutePath<Path>] = .init()

@ViewBuilder
func root() -> some View {
Button("push") {
routePaths.moveTo(.init(data: Path.first("value"), style: .push))
routePaths.append(.init(data: Path.first("value"), style: .push))
}

Button("custom-sheet") {
routePaths.moveTo(.init(data: Path.first("value"), style: .sheet([.medium, .large], .visible)))
routePaths.append(.init(data: Path.first("value"), style: .sheet([.medium, .large], .visible)))
}

Button("normal") {
routePaths.moveTo(.init(data: Path.first("value"), style: .sheet()))
routePaths.append(.init(data: Path.first("value"), style: .sheet()))
}

Button("cover") {
routePaths.moveTo(.init(data: Path.first("value"), style: .push))
routePaths.append(.init(data: Path.first("value"), style: .push))
}

Button("cover -> push -> push") {
routePaths.moveTo([
routePaths.append(contentsOf: [
.init(data: Path.first("value"), style: .cover),
.init(data: Path.first("value"), style: .push),
.init(data: Path.first("value"), style: .push),
])
}

Button("backToRoot") {
routePaths.backToRoot()
routePaths.removeAll()
}

Button("back") {
routePaths.back()
routePaths.removeLast()
}
}

Expand All @@ -104,7 +102,7 @@ You can use deeplinks to transition between views without directly accessing rou

```swift
struct ContentView: View {
@State var routePaths: RoutePaths<Path> = .init()
@State var routePaths: [RoutePath<Path>] = .init()

@ViewBuilder
func root() -> some View {
Expand Down
Loading

0 comments on commit 2b5a6f5

Please sign in to comment.