Skip to content

Commit

Permalink
Merge pull request #256 from APP-iOS5th/Fix/CustomTabView
Browse files Browse the repository at this point in the history
Fix: Resetting the view on tab clicks
  • Loading branch information
JeongWo authored Nov 18, 2024
2 parents 886a4d2 + 2a11192 commit 4a3a6ed
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ struct CustomTabView: View {
var body: some View {
ZStack(alignment: .bottom) {
TabView(selection: $customTabViewModel.selectedTab) {
LetterBoxView()
LetterBoxView(customTabViewModel: customTabViewModel)
.tag(0)

Color.clear
.tag(1)

ProfileView()
ProfileView(customTabViewModel: customTabViewModel)
.tag(2)
}
.overlay(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class CustomTabViewModel: ObservableObject {
@Published var isLetterWrite: Bool = false
@Published var previousTab: Int?

static let profileTabTappedNotification = Notification.Name("profileTabTapped")
static let resetProfileNavigationNotification = Notification.Name("resetProfileNavigation")

private var lastTabSelectionTime: Date?
private let doubleTapInterval: TimeInterval = 0.2
Expand Down Expand Up @@ -50,30 +50,24 @@ final class CustomTabViewModel: ObservableObject {
}

func handleTabSelection(_ tab: Int) {
if tab == selectedTab {
if tab == 2 {
NotificationCenter.default.post(name: CustomTabViewModel.profileTabTappedNotification, object: nil)
}
if tab == 0 {
letterBoxNavigationPath.removeLast(letterBoxNavigationPath.count)
}
} else if tab == 1 {
withAnimation(.easeInOut(duration: 0.3)) {
showOptions = true
}
} else {
selectedTab = tab
if tab == selectedTab {
if tab == 2 {
NotificationCenter.default.post(name: CustomTabViewModel.resetProfileNavigationNotification, object: nil)
}
if tab == 0 {
letterBoxNavigationPath.removeLast(letterBoxNavigationPath.count)
}
} else if tab == 1 {
withAnimation(.easeInOut(duration: 0.3)) {
showOptions = true
}
} else {
selectedTab = tab
if tab == 0 {
letterBoxNavigationPath.removeLast(letterBoxNavigationPath.count)
} else if tab == 2 {
profileNavigationPath.removeLast(profileNavigationPath.count)
}
}

private func resetNavigationForTab(_ tab: Int) {
switch tab {
case 0:
letterBoxNavigationPath.removeLast(letterBoxNavigationPath.count)
case 2:
profileNavigationPath.removeLast(profileNavigationPath.count)
default:
break
}
}

Expand Down
16 changes: 13 additions & 3 deletions Kabinett/Presentation/View/LetterBox/LetterBoxView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ struct LetterBoxView: View {
@StateObject private var calendarViewModel = CalendarViewModel()
@StateObject private var searchBarViewModel = SearchBarViewModel()

init() {
@ObservedObject var customTabViewModel: CustomTabViewModel

init(customTabViewModel: CustomTabViewModel) {
self.customTabViewModel = customTabViewModel

@Injected(LetterBoxUseCaseKey.self) var letterBoxUseCase: LetterBoxUseCase
_letterBoxViewModel = StateObject(wrappedValue: LetterBoxViewModel(letterBoxUseCase: letterBoxUseCase))

Expand All @@ -25,7 +29,7 @@ struct LetterBoxView: View {

var body: some View {
ZStack {
NavigationStack {
NavigationStack(path: $customTabViewModel.letterBoxNavigationPath) {
ZStack {
Color.background
.ignoresSafeArea()
Expand All @@ -34,7 +38,7 @@ struct LetterBoxView: View {
ForEach(LetterType.allCases, id: \.self) { type in
let unreadCount = letterBoxViewModel.getIsReadLetters(for: type)

NavigationLink(destination: LetterBoxDetailView(viewModel: letterBoxDetailViewModel, calendarViewModel: calendarViewModel, searchBarViewModel: searchBarViewModel)) {
NavigationLink(value: type) {
LetterBoxCell(viewModel: letterBoxViewModel, type: type, unreadCount: unreadCount)
}
.simultaneousGesture(TapGesture().onEnded {
Expand All @@ -43,6 +47,12 @@ struct LetterBoxView: View {
}
}
.padding(.top, LayoutHelper.shared.getSize(forSE: 0.035, forOthers: 0.035))
.navigationDestination(for: LetterType.self) { type in
LetterBoxDetailView(viewModel: letterBoxDetailViewModel, calendarViewModel: calendarViewModel, searchBarViewModel: searchBarViewModel)
.onAppear {
letterBoxDetailViewModel.currentLetterType = type
}
}

VStack {
Spacer()
Expand Down
24 changes: 15 additions & 9 deletions Kabinett/Presentation/View/Profile/ProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ import Kingfisher
struct ProfileView: View {
//
@StateObject private var viewModel: ProfileViewModel
@ObservedObject var customTabViewModel: CustomTabViewModel

init() {
init(customTabViewModel: CustomTabViewModel) {
self.customTabViewModel = customTabViewModel

@Injected(ProfileUseCaseKey.self)
var profileUseCase: ProfileUseCase

Expand All @@ -23,21 +26,21 @@ struct ProfileView: View {
}

var body: some View {
NavigationStack {
NavigationStack(path: $customTabViewModel.profileNavigationPath) {
Group {
if case .toLogin = viewModel.navigateState {
SignUpView()
} else {
ZStack {
Color.background.ignoresSafeArea(.all)
if viewModel.profileUpdateError != nil {
// VStack {
// Text("프로필을 불러오는 데 문제가 발생했어요.")
// .fontWeight(.regular)
// .foregroundColor(.alert)
// .font(.headline)
// .padding()
// }
// VStack {
// Text("프로필을 불러오는 데 문제가 발생했어요.")
// .fontWeight(.regular)
// .foregroundColor(.alert)
// .font(.headline)
// .padding()
// }
} else {
VStack {
if let image = viewModel.currentWriter.imageUrlString {
Expand Down Expand Up @@ -95,5 +98,8 @@ struct ProfileView: View {
SettingsView(viewModel: viewModel)
}
}
.onReceive(NotificationCenter.default.publisher(for: CustomTabViewModel.resetProfileNavigationNotification)) { _ in
viewModel.showSettingsView = false
}
}
}

0 comments on commit 4a3a6ed

Please sign in to comment.