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
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 {
WindowGroup {
TimelineView(kind: .pub)
.environmentObject(client)
TabView {
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
StatusRowView(status: status)
}
.listStyle(.plain)
.navigationTitle("Public Timeline: \(client.server)")
.navigationBarTitleDisplayMode(.inline)
.task {
do {
self.statuses = try await client.fetchArray(endpoint: Timeline.pub)
} catch {
print(error.localizedDescription)
}
await refreshTimeline()
}
.refreshable {
await refreshTimeline()
}
}
private func refreshTimeline() async {
do {
self.statuses = try await client.fetchArray(endpoint: Timeline.pub)
} catch {
print(error.localizedDescription)
}
}
}