add animation
This commit is contained in:
parent
3d24e0afce
commit
e51a4ed6b2
@ -12,9 +12,9 @@ struct TopBarView: View {
|
|||||||
|
|
||||||
// Привязка для управления боковым меню
|
// Привязка для управления боковым меню
|
||||||
@Binding var isSideMenuPresented: Bool
|
@Binding var isSideMenuPresented: Bool
|
||||||
|
@Binding var chatSearchRevealProgress: CGFloat
|
||||||
|
|
||||||
@State private var searchText: String = ""
|
@State private var searchText: String = ""
|
||||||
@State private var isSearchBarVisible: Bool = false
|
|
||||||
|
|
||||||
var isHomeTab: Bool {
|
var isHomeTab: Bool {
|
||||||
return title == "Home"
|
return title == "Home"
|
||||||
@ -106,38 +106,47 @@ struct TopBarView: View {
|
|||||||
.padding()
|
.padding()
|
||||||
.frame(height: 50) // Стандартная высота для нав. бара
|
.frame(height: 50) // Стандартная высота для нав. бара
|
||||||
|
|
||||||
if isChatsTab && isSearchBarVisible {
|
if isChatsTab {
|
||||||
searchBar
|
revealableSearchBar
|
||||||
.padding(.horizontal)
|
|
||||||
.padding(.bottom, 8)
|
|
||||||
.transition(.move(edge: .top).combined(with: .opacity))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Divider()
|
Divider()
|
||||||
}
|
}
|
||||||
.background(Color(UIColor.systemBackground))
|
.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
|
.onChange(of: isChatsTab) { isChats in
|
||||||
if !isChats {
|
if !isChats {
|
||||||
isSearchBarVisible = false
|
withAnimation(.spring(response: 0.35, dampingFraction: 0.75)) {
|
||||||
|
chatSearchRevealProgress = 0
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private extension TopBarView {
|
private extension TopBarView {
|
||||||
|
private var normalizedRevealProgress: CGFloat {
|
||||||
|
guard isChatsTab else { return 0 }
|
||||||
|
return max(0, min(chatSearchRevealProgress, 1))
|
||||||
|
}
|
||||||
|
|
||||||
|
private var revealableSearchBar: some View {
|
||||||
|
let progress = normalizedRevealProgress
|
||||||
|
return VStack(spacing: 0) {
|
||||||
|
Spacer(minLength: 0)
|
||||||
|
searchBar
|
||||||
|
.padding(.horizontal)
|
||||||
|
.padding(.bottom, 8)
|
||||||
|
}
|
||||||
|
.frame(height: searchBarRevealHeight)
|
||||||
|
.clipped()
|
||||||
|
.scaleEffect(y: max(progress, 0.0001), anchor: .top)
|
||||||
|
.opacity(progress)
|
||||||
|
.allowsHitTesting(progress > 0.9)
|
||||||
|
.accessibilityHidden(progress < 0.9)
|
||||||
|
}
|
||||||
|
|
||||||
|
private var searchBarRevealHeight: CGFloat { 56 }
|
||||||
|
|
||||||
var searchBar: some View {
|
var searchBar: some View {
|
||||||
HStack(spacing: 8) {
|
HStack(spacing: 8) {
|
||||||
Image(systemName: "magnifyingglass")
|
Image(systemName: "magnifyingglass")
|
||||||
@ -165,7 +174,26 @@ private extension TopBarView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct TopBarView_Previews: PreviewProvider {
|
struct TopBarView_Previews: PreviewProvider {
|
||||||
|
struct Wrapper: View {
|
||||||
|
@State private var selectedAccount = "@user"
|
||||||
|
@State private var isSideMenuPresented = false
|
||||||
|
@State private var revealProgress: CGFloat = 1
|
||||||
|
@StateObject private var viewModel = LoginViewModel()
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
TopBarView(
|
||||||
|
title: "Chats",
|
||||||
|
selectedAccount: $selectedAccount,
|
||||||
|
accounts: [selectedAccount],
|
||||||
|
viewModel: viewModel,
|
||||||
|
isSideMenuPresented: $isSideMenuPresented,
|
||||||
|
chatSearchRevealProgress: $revealProgress
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static var previews: some View {
|
static var previews: some View {
|
||||||
/*@START_MENU_TOKEN@*/Text("Hello, World!")/*@END_MENU_TOKEN@*/
|
Wrapper()
|
||||||
|
.environmentObject(ThemeManager())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,10 +8,20 @@
|
|||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct ChatsTab: View {
|
struct ChatsTab: View {
|
||||||
var currentUserId: String? = nil
|
var currentUserId: String?
|
||||||
|
@Binding var searchRevealProgress: CGFloat
|
||||||
@StateObject private var viewModel = PrivateChatsViewModel()
|
@StateObject private var viewModel = PrivateChatsViewModel()
|
||||||
@State private var selectedChatId: String?
|
@State private var selectedChatId: String?
|
||||||
@State private var searchText: String = ""
|
@State private var searchText: String = ""
|
||||||
|
@State private var searchDragStartProgress: CGFloat = 0
|
||||||
|
@State private var isSearchGestureActive: Bool = false
|
||||||
|
|
||||||
|
private let searchRevealDistance: CGFloat = 90
|
||||||
|
|
||||||
|
init(currentUserId: String? = nil, searchRevealProgress: Binding<CGFloat>) {
|
||||||
|
self.currentUserId = currentUserId
|
||||||
|
self._searchRevealProgress = searchRevealProgress
|
||||||
|
}
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
content
|
content
|
||||||
@ -154,16 +164,31 @@ struct ChatsTab: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private var searchBarGesture: some Gesture {
|
private var searchBarGesture: some Gesture {
|
||||||
DragGesture(minimumDistance: 24)
|
DragGesture(minimumDistance: 10, coordinateSpace: .local)
|
||||||
.onEnded { value in
|
.onChanged { value in
|
||||||
let verticalTranslation = value.translation.height
|
let verticalTranslation = value.translation.height
|
||||||
let horizontalTranslation = value.translation.width
|
let horizontalTranslation = value.translation.width
|
||||||
guard abs(verticalTranslation) > abs(horizontalTranslation) else { return }
|
|
||||||
|
|
||||||
if verticalTranslation > 24 {
|
if !isSearchGestureActive {
|
||||||
NotificationCenter.default.post(name: .chatsTabRevealSearchBar, object: nil)
|
guard abs(verticalTranslation) > abs(horizontalTranslation) else { return }
|
||||||
} else if verticalTranslation < -24 {
|
if searchRevealProgress <= 0.001 && verticalTranslation < 0 { return }
|
||||||
NotificationCenter.default.post(name: .chatsTabHideSearchBar, object: nil)
|
isSearchGestureActive = true
|
||||||
|
searchDragStartProgress = searchRevealProgress
|
||||||
|
}
|
||||||
|
|
||||||
|
guard isSearchGestureActive else { return }
|
||||||
|
|
||||||
|
let delta = verticalTranslation / searchRevealDistance
|
||||||
|
let newProgress = searchDragStartProgress + delta
|
||||||
|
searchRevealProgress = max(0, min(1, newProgress))
|
||||||
|
}
|
||||||
|
.onEnded { _ in
|
||||||
|
guard isSearchGestureActive else { return }
|
||||||
|
isSearchGestureActive = false
|
||||||
|
|
||||||
|
let target: CGFloat = searchRevealProgress > 0.5 ? 1 : 0
|
||||||
|
withAnimation(.spring(response: 0.35, dampingFraction: 0.75)) {
|
||||||
|
searchRevealProgress = target
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -568,10 +593,18 @@ private struct ChatRowView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct ChatsTab_Previews: PreviewProvider {
|
struct ChatsTab_Previews: PreviewProvider {
|
||||||
static var previews: some View {
|
struct Wrapper: View {
|
||||||
ChatsTab()
|
@State private var progress: CGFloat = 1
|
||||||
|
|
||||||
|
var body: some View {
|
||||||
|
ChatsTab(searchRevealProgress: $progress)
|
||||||
.environmentObject(ThemeManager())
|
.environmentObject(ThemeManager())
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static var previews: some View {
|
||||||
|
Wrapper()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct ChatPlaceholderView: View {
|
private struct ChatPlaceholderView: View {
|
||||||
@ -636,6 +669,4 @@ private struct ChatPlaceholderView: View {
|
|||||||
|
|
||||||
extension Notification.Name {
|
extension Notification.Name {
|
||||||
static let debugRefreshChats = Notification.Name("debugRefreshChats")
|
static let debugRefreshChats = Notification.Name("debugRefreshChats")
|
||||||
static let chatsTabRevealSearchBar = Notification.Name("chatsTabRevealSearchBar")
|
|
||||||
static let chatsTabHideSearchBar = Notification.Name("chatsTabHideSearchBar")
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,6 +13,7 @@ struct MainView: View {
|
|||||||
// Состояния для бокового меню
|
// Состояния для бокового меню
|
||||||
@State private var isSideMenuPresented = false
|
@State private var isSideMenuPresented = false
|
||||||
@State private var menuOffset: CGFloat = 0
|
@State private var menuOffset: CGFloat = 0
|
||||||
|
@State private var chatSearchRevealProgress: CGFloat = 0
|
||||||
|
|
||||||
private var tabTitle: String {
|
private var tabTitle: String {
|
||||||
switch selectedTab {
|
switch selectedTab {
|
||||||
@ -38,7 +39,8 @@ struct MainView: View {
|
|||||||
selectedAccount: $selectedAccount,
|
selectedAccount: $selectedAccount,
|
||||||
accounts: accounts,
|
accounts: accounts,
|
||||||
viewModel: viewModel,
|
viewModel: viewModel,
|
||||||
isSideMenuPresented: $isSideMenuPresented
|
isSideMenuPresented: $isSideMenuPresented,
|
||||||
|
chatSearchRevealProgress: $chatSearchRevealProgress
|
||||||
)
|
)
|
||||||
|
|
||||||
ZStack {
|
ZStack {
|
||||||
@ -48,8 +50,12 @@ struct MainView: View {
|
|||||||
FeedbackTab()
|
FeedbackTab()
|
||||||
.opacity(selectedTab == 1 ? 1 : 0)
|
.opacity(selectedTab == 1 ? 1 : 0)
|
||||||
|
|
||||||
ChatsTab(currentUserId: viewModel.userId.isEmpty ? nil : viewModel.userId)
|
ChatsTab(
|
||||||
|
currentUserId: viewModel.userId.isEmpty ? nil : viewModel.userId,
|
||||||
|
searchRevealProgress: $chatSearchRevealProgress
|
||||||
|
)
|
||||||
.opacity(selectedTab == 2 ? 1 : 0)
|
.opacity(selectedTab == 2 ? 1 : 0)
|
||||||
|
.allowsHitTesting(selectedTab == 2)
|
||||||
|
|
||||||
ProfileTab()
|
ProfileTab()
|
||||||
.opacity(selectedTab == 3 ? 1 : 0)
|
.opacity(selectedTab == 3 ? 1 : 0)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user