Skip to content

Commit ebe7a67

Browse files
committed
✨ [feat] #36 네비게이션 바 뒤로가기 버튼 조건 추가 및 alert 띄우기 구현
- 뒤로가기 버튼 클릭시 그냥 dismiss 하는 방식과 alert 띄워 액션 처리하는 방식을 모두 지원하도록 NavigationBar 컴포넌트 수정
1 parent 5d4ef99 commit ebe7a67

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

FunForYou/FunForYou/Sources/Common/DesignSystem/NavigationBar.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,22 @@ import SwiftUI
1010
/// 공통 디자인 컴포넌트 - Navigation Bar
1111
/// - 뒤로가기 버튼만 - title: nil, style: .back
1212
/// - 뒤로가기 버튼 + 타이틀 - title: "바제목", style: .backTitle
13-
/// - 뒤로가기 버튼 + 타이틀 + 활성버튼 - title: "바제목", style: .backTitleButton(title: "버튼이름", isEnabled: true, action: { })
14-
/// - 뒤로가기 버튼 + 타이틀 + 비활성버튼 - title: "바제목", style: .backTitleButton(title: "버튼이름", isEnabled: false, action: { })
13+
/// - 뒤로가기 버튼 + 타이틀 + 활성버튼 - title: "바제목", style: .backTitleButton(title: "버튼이름", isEnabled: true, action: { }), onBack: { }
14+
/// - 뒤로가기 버튼 + 타이틀 + 비활성버튼 - title: "바제목", style: .backTitleButton(title: "버튼이름", isEnabled: false, action: { }), onBack: { }
1515
struct NavigationBar: View {
1616
@Environment(\.dismiss) private var dismiss
1717

1818
let title: String?
1919
let style: NavigationStyleType
20+
var onBack: (() -> Void)?
2021

2122
init(title: String? = nil,
22-
style: NavigationStyleType
23+
style: NavigationStyleType,
24+
onBack: (() -> Void)? = nil
2325
) {
2426
self.title = title
2527
self.style = style
28+
self.onBack = onBack
2629
}
2730

2831
var body: some View {
@@ -36,7 +39,7 @@ struct NavigationBar: View {
3639

3740
HStack {
3841
Button(action: {
39-
dismiss()
42+
onBack?()
4043
}, label: {
4144
Image(systemName: "chevron.backward")
4245
.resizable()

FunForYou/FunForYou/Sources/Common/DesignSystem/PrimaryAlert.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct PrimaryAlert: View {
1515
let onPrimary: () -> Void
1616
let onSecondary: () -> Void
1717

18-
@State private var isVisible = false
18+
@Binding var isVisible: Bool
1919

2020
var body: some View {
2121
ZStack {
@@ -84,13 +84,15 @@ struct PrimaryAlert: View {
8484
}
8585

8686
#Preview {
87+
@Previewable @State var searchText = true
8788
PrimaryAlert(
8889
style:.basic,
8990
onPrimary: {
9091
print("아니요, 계속 쓸래요")
9192
},
9293
onSecondary: {
9394
print("네, 이전으로 돌아갈게요")
94-
}
95+
},
96+
isVisible: $searchText
9597
)
9698
}

FunForYou/FunForYou/Sources/Presentation/Daily/DailyWriting/DailyWritingView.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import SwiftUI
1010
struct DailyWritingView: View {
1111
@StateObject var viewModel: DailyWritingViewModel
1212
@Environment(\.modelContext) private var context
13+
@Environment(\.dismiss) private var dismiss
1314

1415
init(daily: Daily?, coordinator: Coordinator) {
1516
_viewModel = StateObject(wrappedValue: DailyWritingViewModel(daily: daily, coordinator: coordinator))
@@ -22,9 +23,15 @@ struct DailyWritingView: View {
2223
style: .backTitleButton(
2324
title: "저장하기",
2425
isEnabled: viewModel.state.isSaveEnabled
25-
) {
26-
viewModel.action(.saveDailyInspiration(context))
27-
})
26+
) { viewModel.action(.saveDailyInspiration(context)) },
27+
onBack: {
28+
if viewModel.state.isSaveEnabled {
29+
viewModel.state.isShowAlert = true
30+
} else {
31+
dismiss()
32+
}
33+
}
34+
)
2835

2936
DailyWritingContentView(
3037
dailyTitle: $viewModel.state.dailyTitle,
@@ -36,6 +43,20 @@ struct DailyWritingView: View {
3643
.sheet(isPresented: $viewModel.state.isShowImagePicker) {
3744
PhotoPicker(selectedImage: $viewModel.state.selectedImage)
3845
}
46+
.overlay {
47+
if viewModel.state.isShowAlert {
48+
PrimaryAlert(
49+
style: .deleteInspiration,
50+
onPrimary: {
51+
dismiss()
52+
},
53+
onSecondary: {
54+
viewModel.state.isShowAlert = false
55+
},
56+
isVisible: $viewModel.state.isShowAlert
57+
)
58+
}
59+
}
3960
.hideKeyboardOnTap()
4061
}
4162
}

FunForYou/FunForYou/Sources/Presentation/Daily/DailyWriting/DailyWritingViewModel.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ final class DailyWritingViewModel: ViewModelable {
1717
var dailyContent: String = ""
1818
var selectedImage: UIImage?
1919
var isShowImagePicker: Bool = false
20+
var isShowAlert: Bool = false
2021

2122
var isSaveEnabled: Bool {
2223
!dailyTitle.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ||

0 commit comments

Comments
 (0)