Account screen WIP

This commit is contained in:
Thomas Ricouard 2022-11-29 12:18:06 +01:00
parent 567cb4cc47
commit 203abbcf0d
8 changed files with 98 additions and 26 deletions

View file

@ -8,7 +8,7 @@ extension View {
self.navigationDestination(for: RouteurDestinations.self) { destination in self.navigationDestination(for: RouteurDestinations.self) { destination in
switch destination { switch destination {
case let .accountDetail(id): case let .accountDetail(id):
AccountView(accountId: id) AccountDetailView(accountId: id)
case let .statusDetail(id): case let .statusDetail(id):
StatusDetailView(statusId: id) StatusDetailView(statusId: id)
} }

View file

@ -1,16 +1,25 @@
import SwiftUI import SwiftUI
import Timeline import Timeline
import Routeur import Routeur
import Network
struct TimelineTabView: View { struct TimelineTabView: View {
let tab: String let tab: String
private let client: Client
@StateObject private var routeurPath = RouterPath() @StateObject private var routeurPath = RouterPath()
init(tab: String) {
self.tab = tab
self.client = .init(server: tab)
}
var body: some View { var body: some View {
NavigationStack(path: $routeurPath.path) { NavigationStack(path: $routeurPath.path) {
TimelineView(client: .init(server: tab)) TimelineView()
.withAppRouteur() .withAppRouteur()
} }
.environmentObject(routeurPath) .environmentObject(routeurPath)
.environmentObject(client)
} }
} }

View file

@ -0,0 +1,38 @@
import SwiftUI
import Models
import Network
public struct AccountDetailView: View {
@EnvironmentObject private var client: Client
@StateObject private var viewModel: AccountDetailViewModel
public init(accountId: String) {
_viewModel = StateObject(wrappedValue: .init(accountId: accountId))
}
public var body: some View {
List {
switch viewModel.state {
case .loading:
loadingRow
case let .data(account):
Text("Account id \(account.id)")
Text("Account name \(account.displayName)")
case let .error(error):
Text("Error: \(error.localizedDescription)")
}
}
.task {
viewModel.client = client
await viewModel.fetchAccount()
}
}
private var loadingRow: some View {
HStack {
Spacer()
ProgressView()
Spacer()
}
}
}

View file

@ -0,0 +1,27 @@
import SwiftUI
import Network
import Models
@MainActor
class AccountDetailViewModel: ObservableObject {
let accountId: String
var client: Client = .init(server: "")
enum State {
case loading, data(account: Models.Account), error(error: Error)
}
@Published var state: State = .loading
init(accountId: String) {
self.accountId = accountId
}
func fetchAccount() async {
do {
state = .data(account: try await client.fetch(endpoint: Network.Account.accounts(id: accountId)))
} catch {
state = .error(error: error)
}
}
}

View file

@ -1,14 +0,0 @@
import SwiftUI
import Models
public struct AccountView: View {
private let accountId: String
public init(accountId: String) {
self.accountId = accountId
}
public var body: some View {
Text("Account id \(accountId)")
}
}

View file

@ -0,0 +1,16 @@
import Foundation
public enum Account: Endpoint {
case accounts(id: String)
public func path() -> String {
switch self {
case .accounts(let id):
return "accounts/\(id)"
}
}
public func queryItems() -> [URLQueryItem]? {
nil
}
}

View file

@ -2,12 +2,11 @@ import SwiftUI
import Network import Network
public struct TimelineView: View { public struct TimelineView: View {
@StateObject private var viewModel: TimelineViewModel @EnvironmentObject private var client: Client
@StateObject private var viewModel = TimelineViewModel()
@State private var didAppear = false @State private var didAppear = false
public init(client: Client) { public init() {}
_viewModel = StateObject(wrappedValue: TimelineViewModel(client: client))
}
public var body: some View { public var body: some View {
List { List {
@ -37,6 +36,7 @@ public struct TimelineView: View {
.navigationTitle("Public Timeline: \(viewModel.serverName)") .navigationTitle("Public Timeline: \(viewModel.serverName)")
.navigationBarTitleDisplayMode(.inline) .navigationBarTitleDisplayMode(.inline)
.task { .task {
viewModel.client = client
if !didAppear { if !didAppear {
await viewModel.refreshTimeline() await viewModel.refreshTimeline()
didAppear = true didAppear = true

View file

@ -13,7 +13,7 @@ class TimelineViewModel: ObservableObject {
case error(error: Error) case error(error: Error)
} }
private let client: Client var client: Client = .init(server: "")
private var statuses: [Status] = [] private var statuses: [Status] = []
@Published var state: State = .loading @Published var state: State = .loading
@ -22,10 +22,6 @@ class TimelineViewModel: ObservableObject {
client.server client.server
} }
init(client: Client) {
self.client = client
}
func refreshTimeline() async { func refreshTimeline() async {
do { do {
statuses = try await client.fetch(endpoint: Timeline.pub(sinceId: nil)) statuses = try await client.fetch(endpoint: Timeline.pub(sinceId: nil))