Browse and connect to multiple timelines

This commit is contained in:
Thomas Ricouard 2022-11-21 13:52:13 +01:00
parent 864a0b3969
commit a694c5d80c
2 changed files with 46 additions and 8 deletions

View file

@ -4,12 +4,40 @@ import Network
@main @main
struct IceCubesAppApp: App { struct IceCubesAppApp: App {
@StateObject private var client = Client(server: "mastodon.social") @State private var tabs: [String] = ["mastodon.social"]
@State private var isServerSelectDisplayed: Bool = false
@State private var newServerURL: String = ""
var body: some Scene { var body: some Scene {
WindowGroup { WindowGroup {
TimelineView(kind: .pub) TabView {
.environmentObject(client) ForEach(tabs, id: \.self) { tab in
NavigationStack {
TimelineView(kind: .pub)
.environmentObject(Client(server: tab))
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
isServerSelectDisplayed.toggle()
} label: {
Image(systemName: "globe")
}
}
}
.alert("Connect to another server", isPresented: $isServerSelectDisplayed) {
TextField(tab, text: $newServerURL)
Button("Connect", action: {
tabs.append(newServerURL)
newServerURL = ""
})
Button("Cancel", role: .cancel, action: {})
}
}
.tabItem {
Label(tab, systemImage: "globe")
}
}
}
} }
} }
} }

View file

@ -20,12 +20,22 @@ public struct TimelineView: View {
List(statuses) { status in List(statuses) { status in
StatusRowView(status: status) StatusRowView(status: status)
} }
.listStyle(.plain)
.navigationTitle("Public Timeline: \(client.server)")
.navigationBarTitleDisplayMode(.inline)
.task { .task {
do { await refreshTimeline()
self.statuses = try await client.fetchArray(endpoint: Timeline.pub) }
} catch { .refreshable {
print(error.localizedDescription) await refreshTimeline()
} }
}
private func refreshTimeline() async {
do {
self.statuses = try await client.fetchArray(endpoint: Timeline.pub)
} catch {
print(error.localizedDescription)
} }
} }
} }