ios_app_v2/yobble/Components/TopBarView.swift
2025-10-07 04:03:07 +03:00

172 lines
6.0 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.

import SwiftUI
struct TopBarView: View {
var title: String
// Состояния для ProfileTab
@Binding var selectedAccount: String
// @Binding var sheetType: ProfileTab.SheetType?
var accounts: [String]
// var viewModel: LoginViewModel
@ObservedObject var viewModel: LoginViewModel
// Привязка для управления боковым меню
@Binding var isSideMenuPresented: Bool
@State private var searchText: String = ""
@State private var isSearchBarVisible: Bool = false
var isHomeTab: Bool {
return title == "Home"
}
var isChatsTab: Bool {
return title == "Chats"
}
var isProfileTab: Bool {
return title == "Profile"
}
var body: some View {
VStack(spacing: 0) {
HStack {
// Кнопка "Гамбургер" для открытия меню
Button(action: {
withAnimation {
isSideMenuPresented.toggle()
}
}) {
Image(systemName: "line.horizontal.3")
.imageScale(.large)
.foregroundColor(.primary)
}
// Spacer()
if isHomeTab{
Text("Yobble")
.font(.largeTitle)
.fontWeight(.bold)
Spacer()
} else if isProfileTab {
Spacer()
Button(action: { }) {
HStack(spacing: 4) {
Text("@\(viewModel.username)")
.font(.headline)
.foregroundColor(.primary)
Image(systemName: "chevron.down")
.font(.subheadline)
.foregroundColor(.gray)
}
}
Spacer()
} else {
Text(title)
.font(.largeTitle)
.fontWeight(.bold)
Spacer()
}
if isHomeTab{
HStack(spacing: 20) {
// Кнопка поиска
Button(action: {
// пока ничего не делаем
}) {
Image(systemName: "magnifyingglass")
.imageScale(.large)
.foregroundColor(.primary)
}
// Кнопка уведомлений
Button(action: {
// пока ничего не делаем
}) {
Image(systemName: "bell")
.imageScale(.large)
.foregroundColor(.primary)
}
}
} else if isChatsTab {
Button(action: {
NotificationCenter.default.post(name: .debugRefreshChats, object: nil)
}) {
Text(NSLocalizedString("DEBUG UPDATE", comment: ""))
.foregroundColor(.primary)
}
} else if isProfileTab {
NavigationLink(destination: SettingsView(viewModel: viewModel)) {
Image(systemName: "wrench")
.imageScale(.large)
.foregroundColor(.primary)
}
}
}
.padding()
.frame(height: 50) // Стандартная высота для нав. бара
if isChatsTab && isSearchBarVisible {
searchBar
.padding(.horizontal)
.padding(.bottom, 8)
.transition(.move(edge: .top).combined(with: .opacity))
}
Divider()
}
.background(Color(UIColor.systemBackground))
.animation(.spring(response: 0.35, dampingFraction: 0.85), value: isSearchBarVisible)
.onReceive(NotificationCenter.default.publisher(for: .chatsTabRevealSearchBar)) { _ in
guard isChatsTab else { return }
withAnimation {
isSearchBarVisible = true
}
}
.onReceive(NotificationCenter.default.publisher(for: .chatsTabHideSearchBar)) { _ in
guard isChatsTab else { return }
withAnimation {
isSearchBarVisible = false
}
}
.onChange(of: isChatsTab) { isChats in
if !isChats {
isSearchBarVisible = false
}
}
}
}
private extension TopBarView {
var searchBar: some View {
HStack(spacing: 8) {
Image(systemName: "magnifyingglass")
.foregroundColor(.secondary)
TextField(NSLocalizedString("Поиск", comment: ""), text: $searchText)
.textFieldStyle(.plain)
.textInputAutocapitalization(.never)
.autocorrectionDisabled()
if !searchText.isEmpty {
Button(action: { searchText = "" }) {
Image(systemName: "xmark.circle.fill")
.foregroundColor(.secondary)
}
.buttonStyle(.plain)
}
}
.padding(.horizontal, 12)
.padding(.vertical, 6)
.frame(minHeight: 36)
.background(
RoundedRectangle(cornerRadius: 12, style: .continuous)
.fill(Color(UIColor.secondarySystemBackground))
)
}
}
struct TopBarView_Previews: PreviewProvider {
static var previews: some View {
/*@START_MENU_TOKEN@*/Text("Hello, World!")/*@END_MENU_TOKEN@*/
}
}