
- 创建SettingPage视图,包含用户信息管理、头像设置、昵称编辑等功能。 - 实现SettingViewModel,处理设置页面的业务逻辑,包括头像上传、昵称更新等。 - 添加相机和相册选择功能,支持头像更换。 - 更新MainPage和MainViewModel,添加导航逻辑以支持设置页面的访问。 - 完善本地化支持,确保多语言兼容性。 - 新增相关测试建议,确保功能完整性和用户体验。
248 lines
8.8 KiB
Swift
248 lines
8.8 KiB
Swift
import SwiftUI
|
||
|
||
// MARK: - MomentListItem
|
||
struct MomentListItem: View {
|
||
let moment: MomentsInfo
|
||
|
||
init(moment: MomentsInfo) {
|
||
self.moment = moment
|
||
}
|
||
|
||
var body: some View {
|
||
ZStack {
|
||
// 背景层
|
||
RoundedRectangle(cornerRadius: 12)
|
||
.fill(Color.clear)
|
||
.overlay(
|
||
RoundedRectangle(cornerRadius: 12)
|
||
.stroke(Color.white.opacity(0.1), lineWidth: 1)
|
||
)
|
||
.shadow(color: Color(red: 0.43, green: 0.43, blue: 0.43, opacity: 0.34), radius: 10.7, x: 0, y: 1.9)
|
||
|
||
// 内容层
|
||
VStack(alignment: .leading, spacing: 10) {
|
||
// 用户信息
|
||
HStack(alignment: .top) {
|
||
// 头像
|
||
CachedAsyncImage(url: moment.avatar) { image in
|
||
image
|
||
.resizable()
|
||
.aspectRatio(contentMode: .fill)
|
||
} placeholder: {
|
||
Circle()
|
||
.fill(Color.gray.opacity(0.3))
|
||
.overlay(
|
||
Text(String(moment.nick.prefix(1)))
|
||
.font(.system(size: 16, weight: .medium))
|
||
.foregroundColor(.white)
|
||
)
|
||
}
|
||
.frame(width: 40, height: 40)
|
||
.clipShape(Circle())
|
||
|
||
VStack(alignment: .leading, spacing: 2) {
|
||
Text(moment.nick)
|
||
.font(.system(size: 16, weight: .medium))
|
||
.foregroundColor(.white)
|
||
UserIDDisplay(uid: moment.uid, fontSize: 12, textColor: .white.opacity(0.6))
|
||
}
|
||
Spacer()
|
||
// 时间
|
||
Text(formatDisplayTime(moment.publishTime))
|
||
.font(.system(size: 12, weight: .bold))
|
||
.foregroundColor(.white.opacity(0.8))
|
||
.padding(.horizontal, 6)
|
||
.padding(.vertical, 2)
|
||
.background(Color.white.opacity(0.15))
|
||
.cornerRadius(4)
|
||
}
|
||
|
||
// 动态内容
|
||
if !moment.content.isEmpty {
|
||
Text(moment.content)
|
||
.font(.system(size: 14))
|
||
.foregroundColor(.white.opacity(0.9))
|
||
.multilineTextAlignment(.leading)
|
||
.padding(.leading, 40 + 8) // 与用户名左边对齐
|
||
}
|
||
|
||
// 图片网格
|
||
if let images = moment.dynamicResList, !images.isEmpty {
|
||
MomentImageGrid(images: images)
|
||
.padding(.leading, 40 + 8)
|
||
.padding(.bottom, images.count == 2 ? 30 : 0) // 两张图片时增加底部间距
|
||
}
|
||
|
||
// 互动按钮
|
||
HStack(spacing: 20) {
|
||
// Like 按钮与用户名左侧对齐
|
||
HStack(spacing: 4) {
|
||
Image(systemName: moment.isLike ? "heart.fill" : "heart")
|
||
.font(.system(size: 16))
|
||
Text("\(moment.likeCount)")
|
||
.font(.system(size: 14))
|
||
}
|
||
.foregroundColor(moment.isLike ? .red : .white.opacity(0.8))
|
||
.padding(.leading, 40 + 8) // 与用户名左侧对齐(头像宽度 + 间距)
|
||
Spacer()
|
||
}
|
||
.padding(.top, 8)
|
||
}
|
||
.padding(16)
|
||
}
|
||
}
|
||
|
||
// MARK: - 时间显示逻辑
|
||
private func formatDisplayTime(_ timestamp: Int) -> String {
|
||
let date = Date(timeIntervalSince1970: TimeInterval(timestamp) / 1000.0)
|
||
let formatter = DateFormatter()
|
||
formatter.locale = Locale(identifier: "zh_CN")
|
||
let now = Date()
|
||
let interval = now.timeIntervalSince(date)
|
||
let calendar = Calendar.current
|
||
if calendar.isDateInToday(date) {
|
||
if interval < 60 {
|
||
return "刚刚"
|
||
} else if interval < 3600 {
|
||
return "\(Int(interval / 60))分钟前"
|
||
} else {
|
||
return "\(Int(interval / 3600))小时前"
|
||
}
|
||
} else {
|
||
formatter.dateFormat = "MM/dd"
|
||
return formatter.string(from: date)
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - 图片网格组件
|
||
struct MomentImageGrid: View {
|
||
let images: [MomentsPicture]
|
||
|
||
var body: some View {
|
||
GeometryReader { geometry in
|
||
let availableWidth = max(geometry.size.width, 1)
|
||
let spacing: CGFloat = 8
|
||
if availableWidth < 10 {
|
||
Color.clear.frame(height: 1)
|
||
} else {
|
||
switch images.count {
|
||
case 1:
|
||
let imageSize: CGFloat = min(availableWidth * 0.6, 200)
|
||
HStack {
|
||
Spacer()
|
||
MomentSquareImageView(image: images[0], size: imageSize)
|
||
Spacer()
|
||
}
|
||
case 2:
|
||
let imageSize: CGFloat = (availableWidth - spacing) / 2
|
||
HStack(spacing: spacing) {
|
||
MomentSquareImageView(image: images[0], size: imageSize)
|
||
MomentSquareImageView(image: images[1], size: imageSize)
|
||
}
|
||
case 3:
|
||
let imageSize: CGFloat = (availableWidth - spacing * 2) / 3
|
||
HStack(spacing: spacing) {
|
||
ForEach(Array(images.prefix(3).enumerated()), id: \.element.id) { _, image in
|
||
MomentSquareImageView(image: image, size: imageSize)
|
||
}
|
||
}
|
||
default:
|
||
let imageSize: CGFloat = (availableWidth - spacing * 2) / 3
|
||
let columns = Array(repeating: GridItem(.fixed(imageSize), spacing: spacing), count: 3)
|
||
LazyVGrid(columns: columns, spacing: spacing) {
|
||
ForEach(Array(images.prefix(9).enumerated()), id: \.element.id) { _, image in
|
||
MomentSquareImageView(image: image, size: imageSize)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
.frame(height: calculateGridHeight())
|
||
}
|
||
|
||
private func calculateGridHeight() -> CGFloat {
|
||
switch images.count {
|
||
case 1:
|
||
return 200
|
||
case 2:
|
||
return 120
|
||
case 3:
|
||
return 100
|
||
case 4...6:
|
||
return 216
|
||
default:
|
||
return 340
|
||
}
|
||
}
|
||
}
|
||
|
||
// MARK: - 正方形图片视图组件
|
||
struct MomentSquareImageView: View {
|
||
let image: MomentsPicture
|
||
let size: CGFloat
|
||
|
||
var body: some View {
|
||
let safeSize = size.isFinite && size > 0 ? size : 100
|
||
CachedAsyncImage(url: image.resUrl ?? "") { imageView in
|
||
imageView
|
||
.resizable()
|
||
.aspectRatio(contentMode: .fill)
|
||
} placeholder: {
|
||
Rectangle()
|
||
.fill(Color.gray.opacity(0.3))
|
||
.overlay(
|
||
ProgressView()
|
||
.progressViewStyle(CircularProgressViewStyle(tint: .white.opacity(0.6)))
|
||
.scaleEffect(0.8)
|
||
)
|
||
}
|
||
.frame(width: safeSize, height: safeSize)
|
||
.clipped()
|
||
.cornerRadius(8)
|
||
}
|
||
}
|
||
|
||
#Preview {
|
||
// 创建测试数据
|
||
let testMoment = MomentsInfo(
|
||
dynamicId: 1,
|
||
uid: 123456,
|
||
nick: "测试用户",
|
||
avatar: "",
|
||
type: 0,
|
||
content: "这是一条测试动态内容,用来测试 MomentListItem 的显示效果。",
|
||
likeCount: 42,
|
||
isLike: false,
|
||
commentCount: 5,
|
||
publishTime: Int(Date().timeIntervalSince1970 * 1000),
|
||
worldId: 1,
|
||
status: 1,
|
||
playCount: nil,
|
||
dynamicResList: [
|
||
MomentsPicture(id: 1, resUrl: "https://picsum.photos/300/300", format: "jpg", width: 300, height: 300, resDuration: nil),
|
||
MomentsPicture(id: 2, resUrl: "https://picsum.photos/301/301", format: "jpg", width: 301, height: 301, resDuration: nil)
|
||
],
|
||
gender: nil,
|
||
squareTop: nil,
|
||
topicTop: nil,
|
||
newUser: nil,
|
||
defUser: nil,
|
||
scene: nil,
|
||
userVipInfoVO: nil,
|
||
headwearPic: nil,
|
||
headwearEffect: nil,
|
||
headwearType: nil,
|
||
headwearName: nil,
|
||
headwearId: nil,
|
||
experLevelPic: nil,
|
||
charmLevelPic: nil,
|
||
isCustomWord: nil,
|
||
labelList: nil
|
||
)
|
||
|
||
MomentListItem(moment: testMoment)
|
||
.padding()
|
||
.background(Color.black)
|
||
}
|