Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
244 lines
7.2 KiB
Swift
Executable File
244 lines
7.2 KiB
Swift
Executable File
// NotificationsView — your SoundCloud notifications: who liked, reposted or
|
|
// commented on your tracks and playlists, and who followed you. The feed
|
|
// behind the bell on soundcloud.com (api-v2 /activities), opened from the
|
|
// header's NotificationBell.
|
|
//
|
|
// HistoryView's shape: one load on first appearance, pull-to-refresh, and
|
|
// further pages as the footer scrolls into view. Opening the page is what
|
|
// "reads" the notifications: the newest one shown clears the bell's dot.
|
|
|
|
import CrossSoundKit
|
|
import Inject
|
|
import RedWhiteUI
|
|
import SwiftUI
|
|
|
|
@MainActor
|
|
public struct NotificationsView: View {
|
|
@Environment(\.rw) private var p
|
|
@Environment(AppServices.self) private var services
|
|
|
|
private enum Phase {
|
|
case loading
|
|
case failed(String)
|
|
case loaded([SCActivity])
|
|
}
|
|
|
|
@State private var phase: Phase = .loading
|
|
/// Pagination cursor (nil = no more) + in-flight guard.
|
|
@State private var next: String?
|
|
@State private var loadingMore = false
|
|
/// One-shot guard for the first load (.task re-runs on every reappear).
|
|
@State private var hasLoaded = false
|
|
|
|
public init() {}
|
|
|
|
@ObserveInjection var inject
|
|
|
|
public var body: some View {
|
|
Group {
|
|
switch phase {
|
|
case .loading:
|
|
LoadingView()
|
|
case .failed(let message):
|
|
ErrorStateView(message: message) { Task { await load() } }
|
|
case .loaded(let items) where items.isEmpty:
|
|
EmptyStateView(
|
|
icon: "bell",
|
|
title: "No notifications yet",
|
|
message: "Likes, reposts, comments and new followers show up here.")
|
|
case .loaded(let items):
|
|
list(items)
|
|
}
|
|
}
|
|
.background(p.bg)
|
|
.task {
|
|
guard !hasLoaded else { return }
|
|
hasLoaded = true
|
|
await load()
|
|
}
|
|
.enableInjection()
|
|
}
|
|
|
|
// MARK: - Loading
|
|
|
|
private func load() async {
|
|
phase = .loading
|
|
await fetch()
|
|
}
|
|
|
|
/// First load and pull-to-refresh. Refresh keeps the list on screen.
|
|
private func fetch() async {
|
|
do {
|
|
let page = try await services.api.getActivitiesPage(limit: 30)
|
|
CSLog.shared.log("notifications", "loaded \(page.activities.count)")
|
|
next = page.nextHref
|
|
phase = .loaded(page.activities)
|
|
NotificationsState.shared.markSeen(page.activities.first?.createdAt)
|
|
} catch is CancellationError {
|
|
// Left before the fetch landed: let the next appearance retry.
|
|
hasLoaded = false
|
|
} catch {
|
|
CSLog.shared.log("notifications", "load failed: \(error.localizedDescription)")
|
|
phase = .failed(error.localizedDescription)
|
|
}
|
|
}
|
|
|
|
private func loadMore() async {
|
|
guard !loadingMore, let cursor = next, case .loaded(let existing) = phase else { return }
|
|
loadingMore = true
|
|
defer { loadingMore = false }
|
|
do {
|
|
let page = try await services.api.getActivitiesPage(limit: 30, nextHref: cursor)
|
|
next = page.nextHref
|
|
phase = .loaded(existing + page.activities)
|
|
} catch {
|
|
CSLog.shared.log("notifications", "load-more failed: \(error.localizedDescription)")
|
|
}
|
|
}
|
|
|
|
// MARK: - List
|
|
|
|
private func list(_ items: [SCActivity]) -> some View {
|
|
ScrollView {
|
|
LazyVStack(alignment: .leading, spacing: 0) {
|
|
VStack(alignment: .leading, spacing: 6) {
|
|
Text("Notifications")
|
|
.rw(.headlineLarge)
|
|
AccentBar(width: 40)
|
|
}
|
|
.padding(.horizontal, RwSizes.pagePadding)
|
|
.padding(.top, RwSizes.pagePadding)
|
|
.padding(.bottom, RwSizes.gap)
|
|
|
|
ForEach(items) { activity in
|
|
NotificationRow(activity: activity)
|
|
Rectangle()
|
|
.fill(p.hair)
|
|
.frame(height: 1)
|
|
.padding(.leading, RwSizes.pagePadding)
|
|
}
|
|
InfiniteScrollFooter(hasMore: next != nil, isLoading: loadingMore) {
|
|
await loadMore()
|
|
}
|
|
}
|
|
}
|
|
.pullToRefresh { await fetch() }
|
|
}
|
|
}
|
|
|
|
// MARK: - Row
|
|
|
|
/// "Soldier Boy liked your track", what it was done to, a comment's text and
|
|
/// how long ago, with the cover on the right. The avatar opens the person;
|
|
/// the rest opens the track or playlist (the person again, for a follow).
|
|
private struct NotificationRow: View {
|
|
@Environment(\.rw) private var p
|
|
let activity: SCActivity
|
|
|
|
@ObserveInjection var inject
|
|
|
|
var body: some View {
|
|
HStack(alignment: .top, spacing: RwSizes.gap) {
|
|
NavigationLink(value: AppDestination.artist(activity.user)) {
|
|
avatar
|
|
}
|
|
.buttonStyle(.bareRow())
|
|
.accessibilityLabel(activity.user.username)
|
|
|
|
NavigationLink(value: destination) {
|
|
HStack(alignment: .top, spacing: RwSizes.gap) {
|
|
VStack(alignment: .leading, spacing: 3) {
|
|
Text("\(Text(activity.user.username).fontWeight(.bold)) \(action)")
|
|
.rw(.bodyMedium)
|
|
.lineLimit(2)
|
|
if let title {
|
|
Text(title)
|
|
.rw(.labelMedium)
|
|
.lineLimit(1)
|
|
}
|
|
if let comment = activity.comment, !comment.isEmpty {
|
|
Text("“\(comment)”")
|
|
.rw(.labelMedium)
|
|
.lineLimit(3)
|
|
}
|
|
if let ago = shortTimeAgo(activity.createdAt) {
|
|
Text(ago)
|
|
.rw(.labelSmall)
|
|
}
|
|
}
|
|
.multilineTextAlignment(.leading)
|
|
Spacer(minLength: RwSizes.gap)
|
|
if let url = artworkURL {
|
|
artwork(url)
|
|
}
|
|
}
|
|
.contentShape(Rectangle())
|
|
}
|
|
.buttonStyle(.bareRow())
|
|
}
|
|
.padding(.horizontal, RwSizes.pagePadding)
|
|
.padding(.vertical, 10)
|
|
.enableInjection()
|
|
}
|
|
|
|
/// Where the row (not the avatar) leads.
|
|
private var destination: AppDestination {
|
|
if let track = activity.track { return .track(track) }
|
|
if let playlist = activity.playlist { return .playlist(playlist) }
|
|
return .artist(activity.user)
|
|
}
|
|
|
|
private var thing: String {
|
|
guard let playlist = activity.playlist else { return "track" }
|
|
return playlist.isAlbum ? "album" : "playlist"
|
|
}
|
|
|
|
private var action: String {
|
|
switch activity.kind {
|
|
case .like: "liked your \(thing)"
|
|
case .repost: "reposted your \(thing)"
|
|
case .comment: "commented on your \(thing)"
|
|
case .follow: "started following you"
|
|
case .other: activity.type.replacingOccurrences(of: "-", with: " ")
|
|
}
|
|
}
|
|
|
|
private var title: String? {
|
|
activity.track?.title ?? activity.playlist?.title
|
|
}
|
|
|
|
private var artworkURL: URL? {
|
|
let raw = activity.track?.artworkUrlSmall ?? rewriteArtwork(activity.playlist?.artworkUrl, to: "large")
|
|
return raw.flatMap(URL.init(string:))
|
|
}
|
|
|
|
private var avatar: some View {
|
|
AsyncImage(url: rewriteArtwork(activity.user.avatarUrl, to: "large").flatMap(URL.init(string:))) {
|
|
image in
|
|
image.resizable().scaledToFill()
|
|
} placeholder: {
|
|
ZStack {
|
|
p.bg2.color
|
|
Image(systemName: "person.fill")
|
|
.font(.system(size: 15))
|
|
.foregroundStyle(p.iconInactive)
|
|
}
|
|
}
|
|
.frame(width: 40, height: 40)
|
|
.clipShape(Circle())
|
|
.avatarBorder()
|
|
}
|
|
|
|
/// The cover, square with the 2pt hair edge every artwork wears.
|
|
private func artwork(_ url: URL) -> some View {
|
|
AsyncImage(url: url) { image in
|
|
image.resizable().scaledToFill()
|
|
} placeholder: {
|
|
p.bg2.color
|
|
}
|
|
.frame(width: 40, height: 40)
|
|
.clipShape(Rectangle())
|
|
.rwBorder(.accent, width: 2)
|
|
}
|
|
}
|