ios_app/Shared/Views/Tab/MainView.swift
2025-07-25 01:58:34 +03:00

122 lines
3.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// MainView.swift
// VolnahubApp
//
// Created by cheykrym on 09/06/2025.
//
import SwiftUI
struct MainView: View {
@ObservedObject var viewModel: LoginViewModel
@State private var selectedTab: Int = 0
// Состояния для TopBarView
@State private var selectedAccount = "@user1"
@State private var accounts = ["@user1", "@user2", "@user3"]
@State private var sheetType: ProfileTab.SheetType? = nil
// Состояния для скрытия TopBar
@State private var topBarVisible = true
@State private var lastScrollY: CGFloat = 0
private var tabTitle: String {
switch selectedTab {
case 0:
return "Home"
case 1:
return "Search"
case 2:
return "Chats"
case 3:
return "Profile"
default:
return "Home"
}
}
var body: some View {
NavigationView {
VStack(spacing: 0) {
TopBarView(
title: tabTitle,
selectedAccount: $selectedAccount,
sheetType: $sheetType,
accounts: accounts,
viewModel: viewModel
)
.offset(y: topBarVisible ? 0 : -100)
.animation(.spring(response: 0.4, dampingFraction: 0.8), value: topBarVisible)
ZStack {
switch selectedTab {
case 0:
HomeTab(onScroll: handleScroll)
case 1:
SearchTab() // SearchTab не имеет скролла, поэтому onScroll не нужен
case 2:
ChatsTab() // ChatsTab тоже
case 3:
ProfileTab(
viewModel: viewModel,
sheetType: $sheetType,
selectedAccount: $selectedAccount,
accounts: $accounts,
onScroll: handleScroll
)
default:
HomeTab(onScroll: handleScroll)
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
CustomTabBar(selectedTab: $selectedTab) {
print("Create button tapped")
}
}
.ignoresSafeArea(edges: .bottom)
.navigationBarHidden(true)
.sheet(item: $sheetType) { type in
switch type {
case .accountShare:
AccountShareSheet(
isPresented: Binding(
get: { sheetType != nil },
set: { if !$0 { sheetType = nil } }
),
selectedAccount: $selectedAccount,
accounts: accounts
)
}
}
}
.navigationViewStyle(StackNavigationViewStyle())
}
private func handleScroll(offset: CGPoint) {
let currentY = offset.y
// Показываем бар, если скроллим вверх или дошли до верха
if currentY < lastScrollY || currentY <= 0 {
if !topBarVisible {
topBarVisible = true
}
}
// Скрываем, если скроллим вниз и отступили от верха
else if currentY > lastScrollY && currentY > 50 {
if topBarVisible {
topBarVisible = false
}
}
lastScrollY = currentY
}
}
struct MainView_Previews: PreviewProvider {
static var previews: some View {
let mockViewModel = LoginViewModel()
MainView(viewModel: mockViewModel)
}
}