Skip to content

Commit

Permalink
fixed switching post tabs reloading feed
Browse files Browse the repository at this point in the history
  • Loading branch information
EricBAndrews committed Jan 23, 2024
1 parent 43a770d commit 189706b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
20 changes: 7 additions & 13 deletions Mlem/Views/Tabs/Feeds/Components/PostFeedView+Logic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
34 changes: 23 additions & 11 deletions Mlem/Views/Tabs/Feeds/Components/PostFeedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<PostSortType>, 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
Expand All @@ -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) }
}
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions Mlem/Views/Tabs/Feeds/Feed Types/CommunityFeedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 189706b

Please sign in to comment.