diff --git a/yobble/Components/TopBarView.swift b/yobble/Components/TopBarView.swift index d5962f0..5bda4c5 100644 --- a/yobble/Components/TopBarView.swift +++ b/yobble/Components/TopBarView.swift @@ -14,6 +14,7 @@ struct TopBarView: View { @Binding var isSideMenuPresented: Bool @State private var searchText: String = "" + @State private var isSearchBarVisible: Bool = false var isHomeTab: Bool { return title == "Home" @@ -105,15 +106,34 @@ struct TopBarView: View { .padding() .frame(height: 50) // Стандартная высота для нав. бара - if isChatsTab { + 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 + } + } } } diff --git a/yobble/Views/Tab/ChatsTab.swift b/yobble/Views/Tab/ChatsTab.swift index 6c2ecba..c6214d6 100644 --- a/yobble/Views/Tab/ChatsTab.swift +++ b/yobble/Views/Tab/ChatsTab.swift @@ -115,6 +115,7 @@ struct ChatsTab: View { } } .listStyle(.plain) + .simultaneousGesture(searchBarGesture) // .safeAreaInset(edge: .top) { // VStack(spacing: 0) { // searchBar @@ -152,6 +153,21 @@ struct ChatsTab: View { ) } + private var searchBarGesture: some Gesture { + DragGesture(minimumDistance: 24) + .onEnded { value in + let verticalTranslation = value.translation.height + let horizontalTranslation = value.translation.width + guard abs(verticalTranslation) > abs(horizontalTranslation) else { return } + + if verticalTranslation > 24 { + NotificationCenter.default.post(name: .chatsTabRevealSearchBar, object: nil) + } else if verticalTranslation < -24 { + NotificationCenter.default.post(name: .chatsTabHideSearchBar, object: nil) + } + } + } + private var isSearching: Bool { !searchText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } @@ -620,4 +636,6 @@ private struct ChatPlaceholderView: View { extension Notification.Name { static let debugRefreshChats = Notification.Name("debugRefreshChats") + static let chatsTabRevealSearchBar = Notification.Name("chatsTabRevealSearchBar") + static let chatsTabHideSearchBar = Notification.Name("chatsTabHideSearchBar") }