This repository has been archived on 2025-11-05. You can view files and clone it, but cannot push or open issues or pull requests.
ios_app/Shared/Views/Tab/NewHomeTab.swift
2025-08-13 02:12:23 +03:00

100 lines
3.5 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 NewHomeTab: View {
@ObservedObject var viewModel: NewHomeTabViewModel
private var column1Posts: [Post] {
viewModel.posts.enumerated().filter { $0.offset % 2 == 0 }.map { $0.element }
}
private var column2Posts: [Post] {
viewModel.posts.enumerated().filter { $0.offset % 2 != 0 }.map { $0.element }
}
var body: some View {
VStack {
if viewModel.isLoading {
ProgressView("Загрузка ленты...")
} else {
ScrollView {
HStack(alignment: .top, spacing: 2) {
LazyVStack(spacing: 8) {
ForEach(column1Posts) { post in
PostGridItem(post: post)
}
}
LazyVStack(spacing: 8) {
ForEach(column2Posts) { post in
PostGridItem(post: post)
}
}
}
.padding(.horizontal, 2)
}
}
}
.onAppear {
viewModel.fetchDataIfNeeded()
}
}
}
struct PostGridItem: View {
let post: Post
private var randomHeight: CGFloat {
// Мы можем сделать высоту зависимой от типа контента, если нужно
// пока оставим случайной для визуального разнообразия
CGFloat.random(in: 150...300)
}
var body: some View {
VStack(alignment: .leading, spacing: 8) {
// 1. Медиа контент
if let _ = post.media.first {
Image("placeholderPhoto") // Используем локальный плейсхолдер
.resizable()
.aspectRatio(contentMode: .fill)
.frame(height: randomHeight)
.clipped()
.cornerRadius(10)
}
// 2. Название поста
if let title = post.title, !title.isEmpty {
Text(title)
.font(.headline)
.lineLimit(2)
}
// 3. Информация об авторе и лайки
HStack {
// Аватар и имя пользователя
HStack(spacing: 4) {
Image(systemName: "person.circle.fill")
.resizable()
.frame(width: 24, height: 24)
.foregroundColor(.gray)
Text(post.authorUsername)
.font(.subheadline)
.lineLimit(1)
}
Spacer()
// Лайки
HStack(spacing: 4) {
Image(systemName: post.isLikedByCurrentUser ? "heart.fill" : "heart")
.foregroundColor(post.isLikedByCurrentUser ? .red : .primary)
Text("\(post.likes)")
.font(.subheadline)
}
}
}
.padding(8)
.background(Color(UIColor.systemBackground)) // Для поддержки темной/светлой темы
.cornerRadius(12)
.shadow(color: Color.black.opacity(0.1), radius: 5, x: 0, y: 2)
}
}