113 lines
3.7 KiB
Swift
113 lines
3.7 KiB
Swift
import SwiftUI
|
|
|
|
struct MainChatView: View {
|
|
let onTapCanCharge: () -> Void
|
|
let onTapSearch: () -> Void
|
|
|
|
@StateObject private var viewModel = MainChatViewModel()
|
|
@StateObject private var appState = AppState.shared
|
|
|
|
var body: some View {
|
|
VStack(spacing: 0) {
|
|
DefaultTitleBar(title: MainTab.chat.title) {
|
|
HStack(spacing: SodaSpacing.s14) {
|
|
Image("ic_bar_cash")
|
|
.onTapGesture { onTapCanCharge() }
|
|
|
|
Image("ic_bar_search")
|
|
.onTapGesture { onTapSearch() }
|
|
}
|
|
}
|
|
|
|
CapsuleTabBar(
|
|
items: MainChatFilter.allCases,
|
|
selectedItem: selectedFilterBinding,
|
|
title: { $0.title }
|
|
)
|
|
|
|
chatListContent
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
}
|
|
.background(Color.black.ignoresSafeArea())
|
|
.onAppear {
|
|
if applyPendingFilterIfNeeded() == false {
|
|
viewModel.fetchFirstPageIfNeeded()
|
|
}
|
|
}
|
|
.valueChanged(value: appState.pendingMainChatFilter) { _ in
|
|
_ = applyPendingFilterIfNeeded()
|
|
}
|
|
.sodaToast(
|
|
isPresented: $viewModel.isShowPopup,
|
|
message: viewModel.errorMessage,
|
|
autohideIn: 2
|
|
)
|
|
}
|
|
|
|
|
|
private var selectedFilterBinding: Binding<MainChatFilter> {
|
|
Binding(
|
|
get: { viewModel.selectedFilter },
|
|
set: { viewModel.applyFilter($0) }
|
|
)
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var chatListContent: some View {
|
|
if viewModel.isLoading && viewModel.rooms.isEmpty {
|
|
ProgressView()
|
|
.progressViewStyle(.circular)
|
|
.tint(Color.soda400)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
} else if viewModel.shouldShowEmptyState {
|
|
Text(I18n.MainChat.emptyStateMessage)
|
|
.appFont(size: 16, weight: .medium)
|
|
.foregroundColor(Color.gray500)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity)
|
|
} else {
|
|
ScrollView(showsIndicators: false) {
|
|
LazyVStack(spacing: 0) {
|
|
ForEach(viewModel.rooms) { room in
|
|
MainChatRoomListItem(room: room) { roomId, chatType in
|
|
handleChatRoomTap(roomId: roomId, chatType: chatType)
|
|
}
|
|
.onAppear {
|
|
viewModel.fetchNextPageIfNeeded(currentItem: room)
|
|
}
|
|
}
|
|
|
|
if viewModel.isLoadingNextPage {
|
|
ProgressView()
|
|
.progressViewStyle(.circular)
|
|
.tint(Color.soda400)
|
|
.padding(.vertical, SodaSpacing.s20)
|
|
}
|
|
}
|
|
.padding(.top, 0)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func handleChatRoomTap(roomId: Int, chatType: String) {
|
|
guard roomId > 0 else { return }
|
|
|
|
if chatType == "AI" {
|
|
AppState.shared.setAppStep(step: .chatRoom(id: roomId))
|
|
} else if chatType == "DM" {
|
|
AppState.shared.setAppStep(step: .userCreatorChatRoom(roomId: roomId))
|
|
}
|
|
}
|
|
|
|
private func applyPendingFilterIfNeeded() -> Bool {
|
|
guard let filter = appState.consumePendingMainChatFilter() else { return false }
|
|
viewModel.applyFilter(filter)
|
|
return true
|
|
}
|
|
}
|
|
|
|
struct MainChatView_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
MainChatView(onTapCanCharge: {}, onTapSearch: {})
|
|
}
|
|
}
|