♻️ Refactor watchOS content view

This commit is contained in:
2025-10-29 01:26:27 +08:00
parent d4cf598f69
commit 1a37d384e6
18 changed files with 1118 additions and 863 deletions

View File

@@ -0,0 +1,63 @@
//
// ActivityListView.swift
// WatchRunner Watch App
//
// Created by LittleSheep on 2025/10/29.
//
import SwiftUI
// MARK: - Views
struct ActivityListView: View {
@StateObject private var viewModel: ActivityViewModel
@EnvironmentObject var appState: AppState
init(filter: String, mockActivities: [SnActivity]? = nil) {
_viewModel = StateObject(wrappedValue: ActivityViewModel(filter: filter, mockActivities: mockActivities))
}
var body: some View {
Group {
if viewModel.isLoading {
ProgressView()
} else if let errorMessage = viewModel.errorMessage {
VStack {
Text("Error fetching data")
.font(.headline)
Text(errorMessage)
.font(.caption)
.lineLimit(nil)
}
.padding()
} else if viewModel.activities.isEmpty {
Text("No activities found.")
} else {
List(viewModel.activities) { activity in
switch activity.type {
case "posts.new", "posts.new.replies":
if case .post(let post) = activity.data {
NavigationLink(destination: PostDetailView(post: post)) {
PostRowView(post: post)
}
}
case "discovery":
if case .discovery(let discoveryData) = activity.data {
DiscoveryView(discoveryData: discoveryData)
}
default:
Text("Unknown activity type: \(activity.type)")
}
}
}
}
.task {
// Only fetch if appState is ready and token/serverUrl are available
if appState.isReady, let token = appState.token, let serverUrl = appState.serverUrl {
await viewModel.fetchActivities(token: token, serverUrl: serverUrl)
}
}
.navigationTitle(viewModel.filter)
.navigationBarTitleDisplayMode(.inline)
}
}

View File

@@ -0,0 +1,38 @@
//
// AttachmentImageView.swift
// WatchRunner Watch App
//
// Created by LittleSheep on 2025/10/29.
//
import SwiftUI
struct AttachmentImageView: View {
let attachment: SnCloudFile
@EnvironmentObject var appState: AppState
@StateObject private var imageLoader = ImageLoader()
var body: some View {
Group {
if imageLoader.isLoading {
ProgressView()
} else if let image = imageLoader.image {
image
.resizable()
.aspectRatio(contentMode: .fit)
.frame(maxWidth: .infinity)
} else if let errorMessage = imageLoader.errorMessage {
Text("Failed to load attachment: \(errorMessage)")
.font(.caption)
.foregroundColor(.red)
} else {
Text("File: \(attachment.id)")
}
}
.task(id: attachment.id) {
if let serverUrl = appState.serverUrl, let imageUrl = getAttachmentUrl(for: attachment.id, serverUrl: serverUrl), let token = appState.token, attachment.mimeType?.starts(with: "image") == true {
await imageLoader.loadImage(from: imageUrl, token: token)
}
}
}
}

View File

@@ -0,0 +1,52 @@
//
// ComposePostView.swift
// WatchRunner Watch App
//
// Created by LittleSheep on 2025/10/29.
//
import SwiftUI
struct ComposePostView: View {
@StateObject private var viewModel = ComposePostViewModel()
@EnvironmentObject var appState: AppState
@Environment(\.dismiss) private var dismiss
var body: some View {
NavigationStack {
Form {
TextField("Title", text: $viewModel.title)
TextField("Content", text: $viewModel.content)
.frame(height: 100)
}
.navigationTitle("New Post")
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Cancel") {
dismiss()
}
}
ToolbarItem(placement: .confirmationAction) {
Button("Post") {
Task {
if let token = appState.token, let serverUrl = appState.serverUrl {
await viewModel.createPost(token: token, serverUrl: serverUrl)
}
}
}
.disabled(viewModel.isPosting)
}
}
.onChange(of: viewModel.didPost) {
if viewModel.didPost {
dismiss()
}
}
.alert("Error", isPresented: .constant(viewModel.errorMessage != nil), actions: {
Button("OK") { viewModel.errorMessage = nil }
}, message: {
Text(viewModel.errorMessage ?? "")
})
}
}
}

View File

@@ -0,0 +1,110 @@
//
// DiscoveryViews.swift
// WatchRunner Watch App
//
// Created by LittleSheep on 2025/10/29.
//
import SwiftUI
struct DiscoveryView: View {
let discoveryData: DiscoveryData
var body: some View {
NavigationLink(destination: DiscoveryDetailView(discoveryData: discoveryData)) {
VStack(alignment: .leading) {
Text("Discovery")
.font(.headline)
Text("\(discoveryData.items.count) new items to discover")
.font(.subheadline)
.foregroundColor(.secondary)
}
}
}
}
struct DiscoveryDetailView: View {
let discoveryData: DiscoveryData
var body: some View {
List(discoveryData.items) { item in
NavigationLink(destination: destinationView(for: item)) {
itemView(for: item)
}
}
.navigationTitle("Discovery")
}
@ViewBuilder
private func itemView(for item: DiscoveryItem) -> some View {
VStack(alignment: .leading) {
switch item.data {
case .realm(let realm):
Text("Realm").font(.headline)
Text(realm.name).foregroundColor(.secondary)
case .publisher(let publisher):
Text("Publisher").font(.headline)
Text(publisher.name).foregroundColor(.secondary)
case .article(let article):
Text("Article").font(.headline)
Text(article.title).foregroundColor(.secondary)
case .unknown:
Text("Unknown item")
}
}
}
@ViewBuilder
private func destinationView(for item: DiscoveryItem) -> some View {
switch item.data {
case .realm(let realm):
RealmDetailView(realm: realm)
case .publisher(let publisher):
PublisherDetailView(publisher: publisher)
case .article(let article):
ArticleDetailView(article: article)
case .unknown:
Text("Detail view not available")
}
}
}
struct RealmDetailView: View {
let realm: SnRealm
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(realm.name).font(.headline)
if let description = realm.description {
Text(description).font(.body)
}
}
.navigationTitle("Realm")
}
}
struct PublisherDetailView: View {
let publisher: SnPublisher
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(publisher.name).font(.headline)
if let description = publisher.description {
Text(description).font(.body)
}
}
.navigationTitle("Publisher")
}
}
struct ArticleDetailView: View {
let article: SnWebArticle
var body: some View {
VStack(alignment: .leading, spacing: 8) {
Text(article.title).font(.headline)
Text(article.url).font(.caption).foregroundColor(.secondary)
}
.navigationTitle("Article")
}
}

View File

@@ -0,0 +1,63 @@
//
// ExploreView.swift
// WatchRunner Watch App
//
// Created by LittleSheep on 2025/10/29.
//
import SwiftUI
// The main view with the TabView for filtering.
struct ExploreView: View {
@StateObject private var appState = AppState()
@State private var isComposing = false
var body: some View {
NavigationStack {
Group {
if appState.isReady {
TabView {
NavigationStack {
ActivityListView(filter: "Explore")
}
.tabItem {
Label("Explore", systemImage: "safari")
}
NavigationStack {
ActivityListView(filter: "Subscriptions")
}
.tabItem {
Label("Subscriptions", systemImage: "star")
}
NavigationStack {
ActivityListView(filter: "Friends")
}
.tabItem {
Label("Friends", systemImage: "person.2")
}
}
.environmentObject(appState)
} else {
ProgressView { Text("Connecting to phone...") }
.onAppear {
appState.requestData()
}
}
}
.navigationTitle("Explore")
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button(action: { isComposing = true }) {
Label("Compose", systemImage: "plus")
}
}
}
.sheet(isPresented: $isComposing) {
ComposePostView()
.environmentObject(appState)
}
}
}
}

View File

@@ -0,0 +1,142 @@
//
// PostViews.swift
// WatchRunner Watch App
//
// Created by LittleSheep on 2025/10/29.
//
import SwiftUI
struct PostRowView: View {
let post: SnPost
@EnvironmentObject var appState: AppState
@StateObject private var imageLoader = ImageLoader() // Instantiate ImageLoader
var body: some View {
VStack(alignment: .leading, spacing: 4) {
HStack {
if let serverUrl = appState.serverUrl, let pictureId = post.publisher.picture?.id, let imageUrl = getAttachmentUrl(for: pictureId, serverUrl: serverUrl), let token = appState.token {
if imageLoader.isLoading {
ProgressView()
.frame(width: 24, height: 24)
} else if let image = imageLoader.image {
image
.resizable()
.frame(width: 24, height: 24)
.clipShape(Circle())
} else if let errorMessage = imageLoader.errorMessage {
Text("Failed: \(errorMessage)")
.font(.caption)
.foregroundColor(.red)
.frame(width: 24, height: 24)
} else {
// Placeholder if no image and not loading
Image(systemName: "person.circle.fill")
.resizable()
.frame(width: 24, height: 24)
.clipShape(Circle())
.foregroundColor(.gray)
}
}
Text(post.publisher.nick ?? post.publisher.name)
.font(.subheadline)
.bold()
}
.task(id: post.publisher.picture?.id) { // Use task(id:) to reload image when pictureId changes
if let serverUrl = appState.serverUrl, let pictureId = post.publisher.picture?.id, let imageUrl = getAttachmentUrl(for: pictureId, serverUrl: serverUrl), let token = appState.token {
await imageLoader.loadImage(from: imageUrl, token: token)
}
}
if let title = post.title, !title.isEmpty {
Text(title)
.font(.headline)
}
if let content = post.content, !content.isEmpty {
Text(content)
.font(.body)
}
}
}
}
struct PostDetailView: View {
let post: SnPost
@EnvironmentObject var appState: AppState
@StateObject private var publisherImageLoader = ImageLoader() // Instantiate ImageLoader for publisher avatar
var body: some View {
ScrollView {
VStack(alignment: .leading, spacing: 8) {
HStack {
if let serverUrl = appState.serverUrl, let pictureId = post.publisher.picture?.id, let imageUrl = getAttachmentUrl(for: pictureId, serverUrl: serverUrl), let token = appState.token {
if publisherImageLoader.isLoading {
ProgressView()
.frame(width: 32, height: 32)
} else if let image = publisherImageLoader.image {
image
.resizable()
.frame(width: 32, height: 32)
.clipShape(Circle())
} else if let errorMessage = publisherImageLoader.errorMessage {
Text("Failed: \(errorMessage)")
.font(.caption)
.foregroundColor(.red)
.frame(width: 32, height: 32)
} else {
Image(systemName: "person.circle.fill")
.resizable()
.frame(width: 32, height: 32)
.clipShape(Circle())
.foregroundColor(.gray)
}
}
Text("@\(post.publisher.name)")
.font(.headline)
}
.task(id: post.publisher.picture?.id) { // Use task(id:) to reload image when pictureId changes
if let serverUrl = appState.serverUrl, let pictureId = post.publisher.picture?.id, let imageUrl = getAttachmentUrl(for: pictureId, serverUrl: serverUrl), let token = appState.token {
await publisherImageLoader.loadImage(from: imageUrl, token: token)
}
}
if let title = post.title, !title.isEmpty {
Text(title)
.font(.title2)
.bold()
}
if let content = post.content, !content.isEmpty {
Text(content)
.font(.body)
}
if !post.attachments.isEmpty {
Divider()
Text("Attachments").font(.headline)
ForEach(post.attachments) { attachment in
AttachmentImageView(attachment: attachment)
}
}
if !post.tags.isEmpty {
Divider()
Text("Tags").font(.headline)
FlowLayout(alignment: .leading, spacing: 4) {
ForEach(post.tags) { tag in
Text("#\(tag.name ?? tag.slug)")
.font(.caption)
.padding(.horizontal, 6)
.padding(.vertical, 3)
.background(Capsule().fill(Color.accentColor.opacity(0.2)))
.cornerRadius(5)
}
}
}
}
.padding()
}
.navigationTitle("Post")
}
}