Timeline selector

This commit is contained in:
Lumaa 2024-03-30 14:04:38 +01:00
parent 1da0681c8d
commit 830b41437f
1 changed files with 58 additions and 22 deletions

View File

@ -7,8 +7,7 @@ struct TimelineView: View {
@State var navigator: Navigator = Navigator() @State var navigator: Navigator = Navigator()
@State private var showPicker: Bool = false @State private var showPicker: Bool = false
@State private var stringTimeline: String = "home" @State private var timelines: [TimelineFilter] = [.home, .trending, .local, .federated]
@State private var timelines: [TimelineFilter] = [.trending, .home]
@State private var loadingStatuses: Bool = false @State private var loadingStatuses: Bool = false
@State private var statuses: [Status]? @State private var statuses: [Status]?
@ -24,6 +23,12 @@ struct TimelineView: View {
self.showHero = showHero self.showHero = showHero
} }
init(filter: TimelineFilter = .home, showHero: Bool = true) {
self.timelineModel = .init(client: AccountManager.shared.forceClient())
self.filter = filter
self.showHero = showHero
}
var body: some View { var body: some View {
NavigationStack(path: $navigator.path) { NavigationStack(path: $navigator.path) {
if statuses != nil { if statuses != nil {
@ -43,26 +48,43 @@ struct TimelineView: View {
} }
} }
// if showPicker { if showPicker {
// //TODO: Fix this ViewThatFits {
// HStack {
// MetaPicker(items: timelines.map { $0.rawValue }, selectedItem: $stringTimeline) { item in ForEach(timelines, id: \.self) { t in
// let title: String = timelines.filter{ $0.rawValue == item }.first?.localizedTitle() ?? "Unknown" Button {
// Text("\(title)") Task {
// } await reloadTimeline(t)
// .padding(.bottom) }
// .onChange(of: stringTimeline) { _, newTimeline in } label: {
// let loc = timelines.filter{ $0.rawValue == newTimeline }.first?.localizedTitle() Text(t.localizedTitle())
// switch (loc) { .padding(.horizontal)
// case "home": }
// timeline = .home .buttonStyle(LargeButton(filled: t == filter, height: 7.5))
// case "trending": .disabled(t == filter)
// timeline = .trending }
// default: }
// timeline = .home
// } ScrollView(.horizontal) {
// } HStack {
// } ForEach(timelines, id: \.self) { t in
Button {
Task {
await reloadTimeline(t)
}
} label: {
Text(t.localizedTitle())
.padding(.horizontal)
}
.buttonStyle(LargeButton(filled: t == filter, height: 7.5))
.disabled(t == filter)
}
}
}
.padding(.vertical)
.scrollIndicators(.hidden)
}
}
ForEach(statuses!, id: \.id) { status in ForEach(statuses!, id: \.id) { status in
LazyVStack(alignment: .leading, spacing: 2) { LazyVStack(alignment: .leading, spacing: 2) {
@ -142,4 +164,18 @@ struct TimelineView: View {
.toolbarBackground(.visible, for: .navigationBar) .toolbarBackground(.visible, for: .navigationBar)
.safeAreaPadding() .safeAreaPadding()
} }
private func reloadTimeline(_ filter: TimelineFilter) async {
guard let client = accountManager.getClient() else { return }
statuses = nil
self.filter = filter
timelineModel.setTimelineFilter(filter)
Task {
loadingStatuses = true
statuses = await timelineModel.fetch(client: client)
lastSeen = nil
loadingStatuses = false
}
}
} }