diff --git a/GAMSS/Sources/Presentation/Archive/Detail/ArchiveDetailView.swift b/GAMSS/Sources/Presentation/Archive/Detail/ArchiveDetailView.swift index 37b479f..7713608 100644 --- a/GAMSS/Sources/Presentation/Archive/Detail/ArchiveDetailView.swift +++ b/GAMSS/Sources/Presentation/Archive/Detail/ArchiveDetailView.swift @@ -16,6 +16,7 @@ struct ArchiveDetailView: View { @State private var shredMode: CardShredMode? @State private var selectedNote: DropNote? @State private var isEmptyNotesModalPresented = false + @State private var isErrorAlertPresented = false private let title: String @@ -84,12 +85,14 @@ struct ArchiveDetailView: View { } .alert( viewModel.errorMessage ?? "", - isPresented: Binding( - get: { viewModel.errorMessage != nil }, - set: { if !$0 { viewModel.errorMessage = nil } } - ) + isPresented: $isErrorAlertPresented ) { - Button("확인", role: .cancel) {} + Button("확인", role: .cancel) { + viewModel.errorMessage = nil + } + } + .onChange(of: viewModel.errorMessage) { _, newValue in + isErrorAlertPresented = newValue != nil } .task { await viewModel.load() @@ -122,7 +125,7 @@ struct ArchiveDetailView: View { } ) } - + if let note = selectedNote { CardDetailView( viewModel: CardDetailViewModel( @@ -151,7 +154,7 @@ struct ArchiveDetailView: View { .transition(.opacity) .zIndex(1) } - + if isEmptyNotesModalPresented { ModalContainerView(isPresented: $isEmptyNotesModalPresented) { ModalContentView( @@ -169,7 +172,7 @@ struct ArchiveDetailView: View { .animation(.easeOut(duration: 0.12), value: selectedNote?.id) .hidesTabBar() } - + private func makeShredViewModel(for mode: CardShredMode) -> CardShredViewModel { let cardRepository = DefaultCardRepository(networkManager: NetworkManager.shared) switch mode { diff --git a/GAMSS/Sources/Presentation/Archive/Detail/ArchiveDetailViewModel.swift b/GAMSS/Sources/Presentation/Archive/Detail/ArchiveDetailViewModel.swift index 5f5f873..d2582e9 100644 --- a/GAMSS/Sources/Presentation/Archive/Detail/ArchiveDetailViewModel.swift +++ b/GAMSS/Sources/Presentation/Archive/Detail/ArchiveDetailViewModel.swift @@ -32,7 +32,6 @@ final class ArchiveDetailViewModel: ObservableObject { func load() async { isLoading = true - errorMessage = nil defer { isLoading = false } do { diff --git a/GAMSS/Sources/Presentation/CardShred/CardShredView.swift b/GAMSS/Sources/Presentation/CardShred/CardShredView.swift index f764002..df76062 100644 --- a/GAMSS/Sources/Presentation/CardShred/CardShredView.swift +++ b/GAMSS/Sources/Presentation/CardShred/CardShredView.swift @@ -46,6 +46,15 @@ struct CardShredView: View { paperArea .frame(maxWidth: .infinity, maxHeight: .infinity) .padding(.horizontal, 27) + .overlay(alignment: .bottom) { + ZStack { + if let toastMessage = viewModel.toastMessage { + ToastView(message: toastMessage) + .transition(.opacity) + } + } + .animation(.easeInOut(duration: 0.2), value: viewModel.toastMessage) + } shredButton .padding(.horizontal, 18) @@ -55,15 +64,6 @@ struct CardShredView: View { .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color.colorWhite) .hidesTabBar() - .alert( - viewModel.alertMessage ?? "", - isPresented: Binding( - get: { viewModel.alertMessage != nil }, - set: { if !$0 { viewModel.alertMessage = nil } } - ) - ) { - Button("확인", role: .cancel) {} - } } private var powerToggle: some View { @@ -139,6 +139,12 @@ struct CardShredView: View { let succeeded = await viewModel.shred() if succeeded { onComplete() + } else { + withAnimation(.easeInOut(duration: 0.45)) { + viewModel.reset() + } completion: { + viewModel.showShredFailureToast() + } } } } else { diff --git a/GAMSS/Sources/Presentation/CardShred/CardShredViewModel.swift b/GAMSS/Sources/Presentation/CardShred/CardShredViewModel.swift index df477f4..08112ab 100644 --- a/GAMSS/Sources/Presentation/CardShred/CardShredViewModel.swift +++ b/GAMSS/Sources/Presentation/CardShred/CardShredViewModel.swift @@ -19,12 +19,15 @@ enum CardShredMode: Hashable, Identifiable { final class CardShredViewModel: ObservableObject { @Published private(set) var step = 0 @Published private(set) var isSubmitting = false - @Published var alertMessage: String? + @Published private(set) var toastMessage: String? private let mode: CardShredMode private let deleteCardUseCase: DeleteCardUseCase? private let deleteAllCardUseCase: DeleteAllCardUseCase? private let requiredSteps = 4 + private var toastDismissTask: Task? + + static let shredFailureToastMessage = "파쇄에 실패했어요. 다시 시도해주세요" init(cardId: Int, deleteCardUseCase: DeleteCardUseCase) { self.mode = .single(cardId: cardId) @@ -55,6 +58,10 @@ final class CardShredViewModel: ObservableObject { step += 1 } + func reset() { + step = 0 + } + @discardableResult func shred() async -> Bool { guard isShredEnabled, !isSubmitting else { return false } @@ -73,8 +80,17 @@ final class CardShredViewModel: ObservableObject { } return true } catch { - alertMessage = "기록을 파쇄하지 못했어요" return false } } + + func showShredFailureToast() { + toastMessage = Self.shredFailureToastMessage + toastDismissTask?.cancel() + toastDismissTask = Task { [weak self] in + try? await Task.sleep(nanoseconds: 2_000_000_000) + guard !Task.isCancelled else { return } + self?.toastMessage = nil + } + } } diff --git a/GAMSS/Sources/Presentation/ConversationList/Components/ConversationListEmptyView.swift b/GAMSS/Sources/Presentation/ConversationList/Components/ConversationListEmptyView.swift index f47433b..eae8420 100644 --- a/GAMSS/Sources/Presentation/ConversationList/Components/ConversationListEmptyView.swift +++ b/GAMSS/Sources/Presentation/ConversationList/Components/ConversationListEmptyView.swift @@ -13,8 +13,7 @@ struct ConversationListEmptyView: View { Text("아직 나눈 대화가 없어요") .typography(.body3Regular) .foregroundStyle(Color.colorGray500) - .frame(maxWidth: .infinity) - .padding(.top, Spacing.spacing800) + .frame(maxWidth: .infinity, maxHeight: .infinity) } } diff --git a/GAMSS/Sources/Presentation/ConversationList/Components/ConversationSearchEmptyView.swift b/GAMSS/Sources/Presentation/ConversationList/Components/ConversationSearchEmptyView.swift new file mode 100644 index 0000000..611d114 --- /dev/null +++ b/GAMSS/Sources/Presentation/ConversationList/Components/ConversationSearchEmptyView.swift @@ -0,0 +1,36 @@ +// +// ConversationSearchEmptyView.swift +// GAMSS +// +// Created by 이건준 on 10/1/26. +// + +import SwiftUI + +/// 검색 결과가 없을 때 리스트 영역에 표시하는 안내 화면. +struct ConversationSearchEmptyView: View { + var body: some View { + VStack(spacing: 0) { + Image(systemName: "magnifyingglass") + .resizable() + .scaledToFit() + .frame(width: 80, height: 80) + .foregroundStyle(Color.colorGray400) + .padding(.bottom, Spacing.spacing300) + + Text("찾으시는 결과가 없어요.") + .typography(.subtitle2) + .foregroundStyle(Color.colorGray950) + .padding(.bottom, Spacing.spacing100) + + Text("다른 검색어로 다시 찾아보세요.") + .typography(.body5Regular) + .foregroundStyle(Color.colorGray500) + } + .frame(maxWidth: .infinity, maxHeight: .infinity) + } +} + +#Preview { + ConversationSearchEmptyView() +} diff --git a/GAMSS/Sources/Presentation/ConversationList/ConversationListView.swift b/GAMSS/Sources/Presentation/ConversationList/ConversationListView.swift index b373805..07e98e3 100644 --- a/GAMSS/Sources/Presentation/ConversationList/ConversationListView.swift +++ b/GAMSS/Sources/Presentation/ConversationList/ConversationListView.swift @@ -52,63 +52,19 @@ struct ConversationListView: View { .transition(.move(edge: .top).combined(with: .opacity)) } - dateHeader - .padding(.vertical, Spacing.spacing150) - .padding(.horizontal, NavigationBarMetrics.horizontalPadding) - - if !viewModel.isLoading && viewModel.displayedConversations.isEmpty { - ConversationListEmptyView() - .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top) + if viewModel.isNetworkUnreachable { + NetworkFailureView { + Task { await viewModel.retryAfterNetworkFailure() } + } + } else if isSearchResultEmpty { + ConversationSearchEmptyView() .padding(.horizontal, NavigationBarMetrics.horizontalPadding) } else { - VStack(spacing: 0) { - ScrollView { - LazyVStack(spacing: Spacing.spacing100) { - ForEach(Array(viewModel.displayedConversations.enumerated()), id: \.element.id) { index, conversation in - conversationRow(for: conversation) - .onAppear { - Task { - await viewModel.loadMoreIfNeeded(at: index) - } - } - } + dateHeader + .padding(.vertical, Spacing.spacing150) + .padding(.horizontal, NavigationBarMetrics.horizontalPadding) - if viewModel.isLoading && !viewModel.displayedConversations.isEmpty { - ProgressView() - .frame(maxWidth: .infinity) - .padding(.vertical, Spacing.spacing200) - } - } - } - .frame(maxWidth: .infinity, maxHeight: .infinity) - - if viewModel.currentMode == .delete { - Button { - isDeleteConversationPresented = true - } label: { - Text("삭제하기") - .typography(.body3Medium) - .foregroundStyle( - viewModel.isDeleteButtonEnabled - ? Color.colorWhite - : Color.colorGray300 - ) - .frame(maxWidth: .infinity) - .frame(height: 52) - .background( - viewModel.isDeleteButtonEnabled - ? Color.colorRed - : Color.colorGray075 - ) - .clipShape(RoundedRectangle(cornerRadius: 12)) - } - .padding(.top, Spacing.spacing200) - .padding(.bottom, 10) - .disabled(!viewModel.isDeleteButtonEnabled) - } - } - .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top) - .padding(.horizontal, NavigationBarMetrics.horizontalPadding) + conversationListContent } } .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top) @@ -160,6 +116,69 @@ struct ConversationListView: View { } } + private var isSearchResultEmpty: Bool { + viewModel.isSearchExecuted + && !viewModel.isLoading + && viewModel.displayedConversations.isEmpty + } + + @ViewBuilder + private var conversationListContent: some View { + if !viewModel.isLoading && viewModel.displayedConversations.isEmpty { + ConversationListEmptyView() + .padding(.horizontal, NavigationBarMetrics.horizontalPadding) + } else { + VStack(spacing: 0) { + ScrollView { + LazyVStack(spacing: Spacing.spacing100) { + ForEach(Array(viewModel.displayedConversations.enumerated()), id: \.element.id) { index, conversation in + conversationRow(for: conversation) + .onAppear { + Task { + await viewModel.loadMoreIfNeeded(at: index) + } + } + } + + if viewModel.isLoading && !viewModel.displayedConversations.isEmpty { + ProgressView() + .frame(maxWidth: .infinity) + .padding(.vertical, Spacing.spacing200) + } + } + } + .frame(maxWidth: .infinity, maxHeight: .infinity) + + if viewModel.currentMode == .delete { + Button { + isDeleteConversationPresented = true + } label: { + Text("삭제하기") + .typography(.body3Medium) + .foregroundStyle( + viewModel.isDeleteButtonEnabled + ? Color.colorWhite + : Color.colorGray300 + ) + .frame(maxWidth: .infinity) + .frame(height: 52) + .background( + viewModel.isDeleteButtonEnabled + ? Color.colorRed + : Color.colorGray075 + ) + .clipShape(RoundedRectangle(cornerRadius: 12)) + } + .padding(.top, Spacing.spacing200) + .padding(.bottom, 10) + .disabled(!viewModel.isDeleteButtonEnabled) + } + } + .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top) + .padding(.horizontal, NavigationBarMetrics.horizontalPadding) + } + } + private var dateHeader: some View { HStack { Text(ConversationListDateHeaderFormatter.string(from: Date())) diff --git a/GAMSS/Sources/Presentation/ConversationList/ConversationListViewModel.swift b/GAMSS/Sources/Presentation/ConversationList/ConversationListViewModel.swift index 10cb948..f060a9d 100644 --- a/GAMSS/Sources/Presentation/ConversationList/ConversationListViewModel.swift +++ b/GAMSS/Sources/Presentation/ConversationList/ConversationListViewModel.swift @@ -24,6 +24,7 @@ final class ConversationListViewModel: ObservableObject { @Published var editedText: String = "" @Published var isSearching: Bool = false @Published private(set) var isSearchExecuted = false + @Published private(set) var isNetworkUnreachable = false @Published private var selectedConversations = Set() var isDeleteButtonEnabled: Bool { @@ -51,10 +52,22 @@ final class ConversationListViewModel: ObservableObject { defer { isLoading = false } do { conversations = try await getIncompleteConversationsUseCase.execute() + isNetworkUnreachable = false + } catch NetworkError.noConnection { + isNetworkUnreachable = true } catch { alertMessage = "채팅방 목록을 불러오지 못했어요" } } + + /// 네트워크 끊김 화면의 "재시도" 버튼에서 호출한다. 검색 중이었으면 검색을, 아니면 목록 조회를 다시 시도한다. + func retryAfterNetworkFailure() async { + if isSearching, !editedText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { + await searchText() + } else { + await load() + } + } func isSelected(id: Int) -> Bool { selectedConversations.contains(id) @@ -129,6 +142,9 @@ final class ConversationListViewModel: ObservableObject { hasMoreSearchResults = page.hasNextPage searchResults = page.items isSearchExecuted = true + isNetworkUnreachable = false + } catch NetworkError.noConnection { + isNetworkUnreachable = true } catch { alertMessage = error.localizedDescription } @@ -154,6 +170,8 @@ final class ConversationListViewModel: ObservableObject { searchPage = page.page hasMoreSearchResults = page.hasNextPage searchResults.append(contentsOf: page.items) + } catch NetworkError.noConnection { + isNetworkUnreachable = true } catch { alertMessage = error.localizedDescription } diff --git a/GAMSS/Sources/Presentation/Setting/Account/AccountView.swift b/GAMSS/Sources/Presentation/Setting/Account/AccountView.swift index d481525..1f27f90 100644 --- a/GAMSS/Sources/Presentation/Setting/Account/AccountView.swift +++ b/GAMSS/Sources/Presentation/Setting/Account/AccountView.swift @@ -81,6 +81,15 @@ struct AccountView: View { ]) } } + .overlay(alignment: .bottom) { + ToastView(message: viewModel.toastMessage ?? "") + .padding(.bottom, Spacing.spacing200) + .opacity(viewModel.toastMessage == nil ? 0 : 1) + } + .animation( + .easeInOut(duration: 0.2), + value: viewModel.toastMessage + ) .hidesTabBar() } diff --git a/GAMSS/Sources/Presentation/Setting/Account/AccountViewModel.swift b/GAMSS/Sources/Presentation/Setting/Account/AccountViewModel.swift index dd57a14..859c197 100644 --- a/GAMSS/Sources/Presentation/Setting/Account/AccountViewModel.swift +++ b/GAMSS/Sources/Presentation/Setting/Account/AccountViewModel.swift @@ -13,8 +13,10 @@ final class AccountViewModel: ObservableObject { private let logoutUseCase: LogoutUseCase private let deleteMemberUseCase: DeleteMemberUseCase private let userManager: UserManager + private var toastDismissTask: Task? @Published var errorMessage: String? + @Published var toastMessage: String? init( logoutUseCase: LogoutUseCase, @@ -29,6 +31,8 @@ final class AccountViewModel: ObservableObject { func deleteMember() async { do { try await deleteMemberUseCase.execute() + } catch NetworkError.noConnection { + showToast("네트워크 연결을 확인해주세요") } catch { errorMessage = error.localizedDescription } @@ -38,8 +42,20 @@ final class AccountViewModel: ObservableObject { do { try await logoutUseCase.logout() userManager.user = nil + } catch NetworkError.noConnection { + showToast("네트워크 연결을 확인해주세요") } catch { errorMessage = error.localizedDescription } } + + private func showToast(_ message: String) { + toastMessage = message + toastDismissTask?.cancel() + toastDismissTask = Task { [weak self] in + try? await Task.sleep(for: .seconds(2)) + guard !Task.isCancelled else { return } + self?.toastMessage = nil + } + } } diff --git a/GAMSS/Sources/Presentation/Setting/NicknameEdit/NicknameEditView.swift b/GAMSS/Sources/Presentation/Setting/NicknameEdit/NicknameEditView.swift index 6bde118..7ea67c2 100644 --- a/GAMSS/Sources/Presentation/Setting/NicknameEdit/NicknameEditView.swift +++ b/GAMSS/Sources/Presentation/Setting/NicknameEdit/NicknameEditView.swift @@ -73,6 +73,16 @@ struct NicknameEditView: View { ) } .disabled(!viewModel.isEnabledSaveButton) + .overlay(alignment: .top) { + ZStack { + if let toastMessage = viewModel.toastMessage { + ToastView(message: toastMessage) + .transition(.opacity) + } + } + .alignmentGuide(.top) { $0[.bottom] + Spacing.spacing200 } + .animation(.easeInOut(duration: 0.2), value: viewModel.toastMessage) + } } .padding(.horizontal, Spacing.spacing350) .padding(.bottom, 12) diff --git a/GAMSS/Sources/Presentation/Setting/NicknameEdit/NicknameEditViewModel.swift b/GAMSS/Sources/Presentation/Setting/NicknameEdit/NicknameEditViewModel.swift index 00eece3..383fc85 100644 --- a/GAMSS/Sources/Presentation/Setting/NicknameEdit/NicknameEditViewModel.swift +++ b/GAMSS/Sources/Presentation/Setting/NicknameEdit/NicknameEditViewModel.swift @@ -8,16 +8,24 @@ import Combine import Foundation +@MainActor final class NicknameEditViewModel: ObservableObject { private let updateNicknameUseCase: UpdateNicknameUseCase + private var toastDismissTask: Task? + static let networkUnreachableToastMessage = "네트워크 연결을 확인해주세요" + + private let currentNickname: String @Published private(set) var editingNickname: String = "" @Published private(set) var errorMessage: String? + @Published private(set) var toastMessage: String? var isEnabledSaveButton: Bool { let length = editingNickname.count - return length >= NicknamePolicy.minimumLength - && length <= NicknamePolicy.maximumLength + + return editingNickname != currentNickname + && length >= NicknamePolicy.minimumLength + && length <= NicknamePolicy.maximumLength } init( @@ -25,6 +33,7 @@ final class NicknameEditViewModel: ObservableObject { currentNickname: String = "" ) { self.updateNicknameUseCase = updateNicknameUseCase + self.currentNickname = currentNickname self.editingNickname = currentNickname } @@ -44,9 +53,22 @@ final class NicknameEditViewModel: ObservableObject { do { try await updateNicknameUseCase.execute(editingNickname) return true + } catch NetworkError.noConnection { + showToast(Self.networkUnreachableToastMessage) + return false } catch { errorMessage = error.localizedDescription return false } } + + private func showToast(_ message: String) { + toastMessage = message + toastDismissTask?.cancel() + toastDismissTask = Task { [weak self] in + try? await Task.sleep(nanoseconds: 2_000_000_000) + guard !Task.isCancelled else { return } + self?.toastMessage = nil + } + } } diff --git a/GAMSS/Sources/Presentation/Setting/SettingItem.swift b/GAMSS/Sources/Presentation/Setting/SettingItem.swift index 5fa8e9f..3dd8b73 100644 --- a/GAMSS/Sources/Presentation/Setting/SettingItem.swift +++ b/GAMSS/Sources/Presentation/Setting/SettingItem.swift @@ -16,7 +16,7 @@ enum SettingSection: CaseIterable, Identifiable { var items: [SettingItem] { switch self { case .account: - [.accountInfo, .notification] + [.accountInfo] case .service: [.privacyPolicy, .appVersion] } @@ -25,7 +25,6 @@ enum SettingSection: CaseIterable, Identifiable { enum SettingItem: Identifiable { case accountInfo - case notification case privacyPolicy case appVersion @@ -35,8 +34,6 @@ enum SettingItem: Identifiable { switch self { case .accountInfo: "계정 정보" - case .notification: - "알림 설정" case .privacyPolicy: "개인정보 처리방침" case .appVersion: @@ -56,14 +53,14 @@ enum SettingItem: Identifiable { .navigate case .privacyPolicy: .web(Environment.privacyPolicyURL) - case .appVersion, .notification: + case .appVersion: .none } } var showsDividerBelow: Bool { switch self { - case .accountInfo, .notification: + case .accountInfo: true case .privacyPolicy, .appVersion: false diff --git a/GAMSS/Sources/Presentation/Setting/SettingView.swift b/GAMSS/Sources/Presentation/Setting/SettingView.swift index 9a405cb..c1132c8 100644 --- a/GAMSS/Sources/Presentation/Setting/SettingView.swift +++ b/GAMSS/Sources/Presentation/Setting/SettingView.swift @@ -47,7 +47,7 @@ struct SettingView: View { private func settingItem(_ item: SettingItem) -> some View { let row = MenuListItemView( title: item.title, - badgeText: item == .notification ? "OFF" : nil, + badgeText: nil, trailingText: item == .appVersion ? Environment.appVersion : nil ) @@ -94,8 +94,6 @@ struct SettingView: View { ) ) ) - case .notification: - Text("알림 설정 화면 이동") case .privacyPolicy, .appVersion: EmptyView() }