IceCubesApp/Packages/Notifications/Sources/Notifications/NotificationRowView.swift

183 lines
6.4 KiB
Swift
Raw Normal View History

2022-12-19 11:28:55 +00:00
import DesignSystem
import EmojiText
2023-01-17 10:36:01 +00:00
import Env
import Models
2023-02-18 06:26:48 +00:00
import Network
2023-01-17 10:36:01 +00:00
import Status
import SwiftUI
2022-12-19 11:28:55 +00:00
struct NotificationRowView: View {
2022-12-24 14:09:17 +00:00
@EnvironmentObject private var theme: Theme
2022-12-19 11:28:55 +00:00
@Environment(\.redactionReasons) private var reasons
2023-01-17 10:36:01 +00:00
let notification: ConsolidatedNotification
let client: Client
let routerPath: RouterPath
let followRequests: [Account]
2023-01-17 10:36:01 +00:00
2022-12-19 11:28:55 +00:00
var body: some View {
HStack(alignment: .top, spacing: 8) {
if notification.accounts.count == 1 {
makeAvatarView(type: notification.type)
} else {
makeNotificationIconView(type: notification.type)
.frame(width: AvatarView.Size.status.size.width,
height: AvatarView.Size.status.size.height)
}
VStack(alignment: .leading, spacing: 2) {
makeMainLabel(type: notification.type)
makeContent(type: notification.type)
if notification.type == .follow_request,
2023-02-18 06:26:48 +00:00
followRequests.map(\.id).contains(notification.accounts[0].id)
{
FollowRequestButtons(account: notification.accounts[0])
2022-12-19 11:28:55 +00:00
}
}
}
2023-01-30 06:27:06 +00:00
.alignmentGuide(.listRowSeparatorLeading) { _ in
-100
}
2022-12-19 11:28:55 +00:00
}
2023-01-17 10:36:01 +00:00
2022-12-24 09:14:47 +00:00
private func makeAvatarView(type: Models.Notification.NotificationType) -> some View {
ZStack(alignment: .topLeading) {
AvatarView(url: notification.accounts[0].avatar)
makeNotificationIconView(type: type)
.offset(x: -8, y: -8)
2022-12-24 09:14:47 +00:00
}
.contentShape(Rectangle())
.onTapGesture {
routerPath.navigate(to: .accountDetailWithAccount(account: notification.accounts[0]))
}
}
private func makeNotificationIconView(type: Models.Notification.NotificationType) -> some View {
ZStack(alignment: .center) {
Circle()
.strokeBorder(Color.white, lineWidth: 1)
.background(Circle().foregroundColor(type.tintColor(isPrivate: notification.status?.visibility == .priv)))
.frame(width: 24, height: 24)
Image(systemName: type.iconName(isPrivate: notification.status?.visibility == .priv))
.resizable()
.scaledToFit()
.frame(width: 12, height: 12)
.foregroundColor(.white)
}
2022-12-24 09:14:47 +00:00
}
2023-01-17 10:36:01 +00:00
2022-12-24 09:14:47 +00:00
private func makeMainLabel(type: Models.Notification.NotificationType) -> some View {
VStack(alignment: .leading, spacing: 4) {
if notification.accounts.count > 1 {
ScrollView(.horizontal, showsIndicators: false) {
LazyHStack(spacing: 8) {
ForEach(notification.accounts) { account in
AvatarView(url: account.avatar)
.contentShape(Rectangle())
.onTapGesture {
routerPath.navigate(to: .accountDetailWithAccount(account: account))
}
}
}
.padding(.leading, 1)
.frame(height: AvatarView.Size.status.size.height + 2)
}.offset(y: -1)
}
2022-12-24 09:14:47 +00:00
HStack(spacing: 0) {
EmojiTextApp(.init(stringValue: notification.accounts[0].safeDisplayName),
emojis: notification.accounts[0].emojis,
append: {
(notification.accounts.count > 1
? Text("notifications-others-count \(notification.accounts.count - 1)")
.font(.scaledSubheadline)
.fontWeight(.regular)
: Text(" ")) +
Text(type.label(count: notification.accounts.count))
.font(.scaledSubheadline)
2023-01-17 10:36:01 +00:00
.fontWeight(.regular) +
Text("")
.font(.scaledFootnote)
2023-01-17 10:36:01 +00:00
.fontWeight(.regular)
.foregroundColor(.gray) +
Text(notification.createdAt.relativeFormatted)
.font(.scaledFootnote)
2023-01-17 10:36:01 +00:00
.fontWeight(.regular)
.foregroundColor(.gray)
})
.font(.scaledSubheadline)
2023-02-22 21:13:46 +00:00
.emojiSize(Font.scaledSubheadlinePointSize)
2023-01-17 10:36:01 +00:00
.fontWeight(.semibold)
.lineLimit(3)
.fixedSize(horizontal: false, vertical: true)
if let status = notification.status, notification.type == .mention {
2023-01-21 14:16:52 +00:00
Group {
Text("")
Text(Image(systemName: status.visibility.iconName))
}
.font(.scaledFootnote)
.fontWeight(.regular)
.foregroundColor(.gray)
}
2022-12-24 09:14:47 +00:00
Spacer()
}
}
.contentShape(Rectangle())
.onTapGesture {
if notification.accounts.count == 1 {
routerPath.navigate(to: .accountDetailWithAccount(account: notification.accounts[0]))
} else {
routerPath.navigate(to: .accountsList(accounts: notification.accounts))
}
}
2022-12-24 09:14:47 +00:00
}
2023-01-17 10:36:01 +00:00
2022-12-24 09:14:47 +00:00
@ViewBuilder
private func makeContent(type: Models.Notification.NotificationType) -> some View {
if let status = notification.status {
2022-12-29 16:22:07 +00:00
HStack {
2023-01-17 06:54:59 +00:00
if type == .mention {
StatusRowView(viewModel: { .init(status: status,
client: client,
routerPath: routerPath,
showActions: true) })
2023-01-17 06:54:59 +00:00
} else {
StatusRowView(viewModel: { .init(status: status,
client: client,
routerPath: routerPath,
showActions: false) })
2023-01-21 14:16:52 +00:00
.lineLimit(4)
2023-01-17 06:54:59 +00:00
.foregroundColor(.gray)
}
2022-12-29 16:22:07 +00:00
Spacer()
}
.environment(\.isCompact, true)
2022-12-24 09:14:47 +00:00
} else {
Group {
Text("@\(notification.accounts[0].acct)")
.font(.scaledCallout)
2022-12-24 09:14:47 +00:00
.foregroundColor(.gray)
2023-01-17 10:36:01 +00:00
if type == .follow {
EmojiTextApp(notification.accounts[0].note,
emojis: notification.accounts[0].emojis)
.lineLimit(3)
.font(.scaledCallout)
2023-02-22 21:13:46 +00:00
.emojiSize(Font.scaledCalloutPointSize)
.foregroundColor(.gray)
.environment(\.openURL, OpenURLAction { url in
routerPath.handle(url: url)
})
}
}
.contentShape(Rectangle())
.onTapGesture {
if notification.accounts.count == 1 {
routerPath.navigate(to: .accountDetailWithAccount(account: notification.accounts[0]))
} else {
routerPath.navigate(to: .accountsList(accounts: notification.accounts))
}
2022-12-24 09:14:47 +00:00
}
}
}
2022-12-19 11:28:55 +00:00
}