From a694c5d80c6f755510e48cd2a8938da13d690dc6 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Mon, 21 Nov 2022 13:52:13 +0100 Subject: [PATCH] Browse and connect to multiple timelines --- IceCubesApp/IceCubesAppApp.swift | 34 +++++++++++++++++-- .../Sources/Timeline/TimelineView.swift | 20 ++++++++--- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/IceCubesApp/IceCubesAppApp.swift b/IceCubesApp/IceCubesAppApp.swift index 6747bebe..b868cd8b 100644 --- a/IceCubesApp/IceCubesAppApp.swift +++ b/IceCubesApp/IceCubesAppApp.swift @@ -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") + } + } + } } } } diff --git a/Packages/Timeline/Sources/Timeline/TimelineView.swift b/Packages/Timeline/Sources/Timeline/TimelineView.swift index 4e4c8e26..424568d2 100644 --- a/Packages/Timeline/Sources/Timeline/TimelineView.swift +++ b/Packages/Timeline/Sources/Timeline/TimelineView.swift @@ -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) } } }