Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version load hack #780

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Mlem/Views/Tabs/Feeds/Feed View Logic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,28 @@
//

import Foundation
import SwiftUI

extension FeedView {
// MARK: Feed loading

func loadIfVersionResolved() {
if let siteVersion = siteInformation.version, siteInformationLoading {
siteInformationLoading = false

@AppStorage("defaultPostSorting") var defaultPostSorting: PostSortType = .hot
@AppStorage("fallbackDefaultPostSorting") var fallbackDefaultPostSorting: PostSortType = .hot
if siteVersion >= defaultPostSorting.minimumVersion {
postSortType = defaultPostSorting
} else {
postSortType = fallbackDefaultPostSorting
}
Task(priority: .userInitiated) {
await initFeed()
}
}
}

func initFeed() async {
defer { isLoading = false }
isLoading = true
Expand Down
32 changes: 18 additions & 14 deletions Mlem/Views/Tabs/Feeds/Feed View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,24 +94,28 @@ struct FeedView: View {
/// [2023.08] Set to `.visible` to workaround bug where navigation bar background may disappear on certain devices when device rotates.
.navigationBarColor(visibility: .visible)
.environmentObject(postTracker)
.task(id: siteInformation.version) {
print("new site version: \(siteInformation.version)")
// update post sort once we have siteInformation. Assumes site version won't change once we receive it.
if let siteVersion = siteInformation.version, siteInformationLoading {
siteInformationLoading = false

@AppStorage("defaultPostSorting") var defaultPostSorting: PostSortType = .hot
@AppStorage("fallbackDefaultPostSorting") var fallbackDefaultPostSorting: PostSortType = .hot
if siteVersion >= defaultPostSorting.minimumVersion {
postSortType = defaultPostSorting
.task {
// hack to load if task below fails
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
loadIfVersionResolved()
}
// fallback
DispatchQueue.main.asyncAfter(deadline: .now() + 4) {
loadIfVersionResolved()
}
// error
DispatchQueue.main.asyncAfter(deadline: .now() + 6) {
if siteInformation.version == nil {
errorDetails = ErrorDetails(title: "Failed to determine site version!")
} else {
postSortType = fallbackDefaultPostSorting
}
Task(priority: .userInitiated) {
await initFeed()
loadIfVersionResolved()
}
}
}
.task(id: siteInformation.version) {
// update post sort and load once we have siteInformation. Assumes site version won't change once we receive it.
loadIfVersionResolved()
}
.task(priority: .background) { await fetchCommunityDetails() }
// using hardRefreshFeed() for these three so that the user gets immediate feedback, also kills the ScrollViewReader
.onChange(of: feedType) { _ in
Expand Down