From 189706b33c3b0f74481c357720ee8b3520731260 Mon Sep 17 00:00:00 2001 From: Eric Andrews Date: Mon, 22 Jan 2024 21:56:36 -0500 Subject: [PATCH] fixed switching post tabs reloading feed --- .../Feeds/Components/PostFeedView+Logic.swift | 20 ++++------- .../Tabs/Feeds/Components/PostFeedView.swift | 34 +++++++++++++------ .../Feeds/Feed Types/CommunityFeedView.swift | 1 + 3 files changed, 31 insertions(+), 24 deletions(-) diff --git a/Mlem/Views/Tabs/Feeds/Components/PostFeedView+Logic.swift b/Mlem/Views/Tabs/Feeds/Components/PostFeedView+Logic.swift index 86c93081a..63875622e 100644 --- a/Mlem/Views/Tabs/Feeds/Components/PostFeedView+Logic.swift +++ b/Mlem/Views/Tabs/Feeds/Components/PostFeedView+Logic.swift @@ -9,19 +9,13 @@ import Dependencies import SwiftUI extension PostFeedView { - func setDefaultSortMode() { - @Dependency(\.siteInformation) var siteInformationn - - @AppStorage("defaultPostSorting") var defaultPostSorting: PostSortType = .hot - @AppStorage("fallbackDefaultPostSorting") var fallbackDefaultPostSorting: PostSortType = .hot - - if let siteVersion = siteInformation.version, siteVersion < defaultPostSorting.minimumVersion { - postSortType = fallbackDefaultPostSorting - } else { - postSortType = defaultPostSorting - } - - if siteInformation.version != nil { + func setDefaultSortMode() async { + if let siteVersion = siteInformation.version, !siteVersionResolved { + let newPostSort = siteVersion < defaultPostSorting.minimumVersion ? fallbackDefaultPostSorting : defaultPostSorting + + // manually change the tracker sort type here so that view is not redrawn by `onChange(of: postSortType)` + await postTracker.changeSortType(to: newPostSort) + postSortType = newPostSort siteVersionResolved = true } } diff --git a/Mlem/Views/Tabs/Feeds/Components/PostFeedView.swift b/Mlem/Views/Tabs/Feeds/Components/PostFeedView.swift index 487b1991e..8b9baf87e 100644 --- a/Mlem/Views/Tabs/Feeds/Components/PostFeedView.swift +++ b/Mlem/Views/Tabs/Feeds/Components/PostFeedView.swift @@ -26,9 +26,21 @@ struct PostFeedView: View { @Binding var postSortType: PostSortType let showCommunity: Bool - @State var siteVersionResolved: Bool = false + @State var siteVersionResolved: Bool + @State var shouldPerformInitialLoad: Bool // used to display appropriate loading indicator when version resolved on initial appear + @State var errorDetails: ErrorDetails? + init(postSortType: Binding, showCommunity: Bool) { + @Dependency(\.siteInformation) var siteInformation + + let siteVersionResolved = siteInformation.version != nil + self._siteVersionResolved = .init(wrappedValue: siteVersionResolved) + self._shouldPerformInitialLoad = .init(wrappedValue: siteVersionResolved) + self._postSortType = postSortType + self.showCommunity = showCommunity + } + var body: some View { content .onChange(of: showReadPosts) { newValue in @@ -38,17 +50,17 @@ struct PostFeedView: View { Task { await postTracker.addFilter(.read) } } } - .task(id: siteInformation.version) { - // when site version changes, check if it's resolved; if so, update sort type and siteVersionResolved - if let siteVersion = siteInformation.version, !siteVersionResolved { - let newPostSort = siteVersion < defaultPostSorting.minimumVersion ? fallbackDefaultPostSorting : defaultPostSorting - - // manually change the tracker sort type here so that view is not redrawn by `onChange(of: postSortType)` - await postTracker.changeSortType(to: newPostSort, forceRefresh: true) - postSortType = newPostSort - siteVersionResolved = true + .task { + if shouldPerformInitialLoad { + if postTracker.items.isEmpty { + await postTracker.loadMoreItems() + } + shouldPerformInitialLoad = false } } + .task(id: siteInformation.version) { + await setDefaultSortMode() + } .onChange(of: postSortType) { newValue in Task { await postTracker.changeSortType(to: newValue) } } @@ -107,7 +119,7 @@ struct PostFeedView: View { private func noPostsView() -> some View { VStack { // don't show posts until site information loads to avoid jarring redraw - if postTracker.loadingState == .loading || !siteVersionResolved { + if postTracker.loadingState == .loading || !siteVersionResolved || shouldPerformInitialLoad { LoadingView(whatIsLoading: .posts) .frame(maxWidth: .infinity, maxHeight: .infinity) .transition(.opacity) diff --git a/Mlem/Views/Tabs/Feeds/Feed Types/CommunityFeedView.swift b/Mlem/Views/Tabs/Feeds/Feed Types/CommunityFeedView.swift index 505acd04f..7ccfeba5b 100644 --- a/Mlem/Views/Tabs/Feeds/Feed Types/CommunityFeedView.swift +++ b/Mlem/Views/Tabs/Feeds/Feed Types/CommunityFeedView.swift @@ -97,6 +97,7 @@ struct CommunityFeedView: View { .refreshable { await Task { do { + print("DEBUG refreshing from refreshable") _ = try await postTracker.refresh(clearBeforeRefresh: false) } catch { errorHandler.handle(error)