49 lines
1.3 KiB
Swift
49 lines
1.3 KiB
Swift
import SwiftUI
|
|
|
|
struct HomeTab: View {
|
|
@State private var posts: [Post] = []
|
|
@State private var isLoading = true
|
|
@State private var isRefreshing = false
|
|
|
|
var body: some View {
|
|
VStack {
|
|
if isLoading {
|
|
ProgressView("Загрузка ленты...")
|
|
} else {
|
|
RefreshableScrollView(isRefreshing: $isRefreshing, onRefresh: {
|
|
fetchData()
|
|
}) {
|
|
LazyVStack(spacing: 24) {
|
|
ForEach(posts) { post in
|
|
PostDetailView(post: post)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
.onAppear {
|
|
if posts.isEmpty {
|
|
fetchData(isInitialLoad: true)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func fetchData(isInitialLoad: Bool = false) {
|
|
if isInitialLoad {
|
|
isLoading = true
|
|
} else {
|
|
isRefreshing = true
|
|
}
|
|
|
|
PostService.shared.fetchAllPosts { fetchedPosts in
|
|
self.posts = fetchedPosts
|
|
|
|
if isInitialLoad {
|
|
print("content updated")
|
|
self.isLoading = false
|
|
}
|
|
self.isRefreshing = false
|
|
}
|
|
}
|
|
}
|