Skip to content

Commit

Permalink
Merge pull request #976 from planetary-social/fix-empty-profile-feeds
Browse files Browse the repository at this point in the history
Fix empty Profile feeds
  • Loading branch information
mplorentz authored Mar 27, 2024
2 parents 22093d0 + 1590ea4 commit 251d0ef
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 74 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Updated dark theme colors for card backgrounds, primary text, and secondary text.
- Added a new UI for replying to messages that allows attaching images and setting an expiration date.
- Fixed an issue where Profile pages could display little or no content.

## [0.1.7] - 2024-03-21Z

Expand Down
114 changes: 63 additions & 51 deletions Nos.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions Nos/Controller/PagedNoteDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class PagedNoteDataSource<Header: View, EmptyPlaceholder: View>: NSObject, UICol
private var context: NSManagedObjectContext
private var header: () -> Header
private var emptyPlaceholder: () -> EmptyPlaceholder
let pageSize = 10
let pageSize = 20

// We intentionally generate unique IDs for cell reuse to get around
// [this issue](https://github.com/planetary-social/nos/issues/873)
Expand Down Expand Up @@ -94,9 +94,7 @@ class PagedNoteDataSource<Header: View, EmptyPlaceholder: View>: NSObject, UICol
_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath
) -> UICollectionViewCell {
if indexPath.row.isMultiple(of: pageSize) {
pager?.loadMore()
}
loadMoreIfNeeded(for: indexPath)

let note = fetchedResultsController.object(at: indexPath)

Expand Down Expand Up @@ -166,6 +164,20 @@ class PagedNoteDataSource<Header: View, EmptyPlaceholder: View>: NSObject, UICol
}
}

// MARK: - Loading data

/// Instructs the pager to load more data if we are getting close to the end of the object in the list.
/// - Parameter indexPath: the indexPath last loaded by the collection view.
func loadMoreIfNeeded(for indexPath: IndexPath) {
let lastPageStartIndex = (fetchedResultsController.fetchedObjects?.count ?? 0) - pageSize
if indexPath.row > lastPageStartIndex {
// we are at the end of the list, load aggressively
pager?.loadMore()
} else if indexPath.row.isMultiple(of: pageSize / 2) {
pager?.loadMore()
}
}

// MARK: - NSFetchedResultsControllerDelegate

private var insertedIndexes = [IndexPath]()
Expand Down
26 changes: 26 additions & 0 deletions Nos/Views/Profile/ProfileFeedType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import CoreData

/// An enumeration of the different feed algorithms the user can choose to view on the Profile screen.
enum ProfileFeedType {
case activity
case notes

func databaseFilter(author: Author) -> NSFetchRequest<Event> {
author.allPostsRequest(onlyRootPosts: self == .notes)
}

func relayFilter(author: Author) -> Filter {
var kinds: [EventKind]
switch self {
case .activity:
kinds = [.text, .delete, .repost, .longFormContent]
case .notes:
kinds = [.text, .delete]
}

return Filter(
authorKeys: [author.hexadecimalPublicKey ?? "error"],
kinds: kinds
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct ProfileHeader: View {
@ObservedObject var author: Author
@Environment(CurrentUser.self) private var currentUser

@Binding private var selectedTab: ProfileHeaderTab
@Binding private var selectedTab: ProfileFeedType

var followsRequest: FetchRequest<Follow>
var followsResult: FetchedResults<Follow> { followsRequest.wrappedValue }
Expand All @@ -20,15 +20,7 @@ struct ProfileHeader: View {

@EnvironmentObject private var router: Router

enum ProfileHeaderTab {
case activity
case notes
func request(author: Author) -> NSFetchRequest<Event> {
author.allPostsRequest(onlyRootPosts: self == .notes)
}
}

init(author: Author, selectedTab: Binding<ProfileHeaderTab>) {
init(author: Author, selectedTab: Binding<ProfileFeedType>) {
self.author = author
self.followsRequest = FetchRequest(fetchRequest: Follow.followsRequest(sources: [author]))
self.followersRequest = FetchRequest(fetchRequest: Follow.followsRequest(destination: [author]))
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct ProfileView: View {
@State private var usbcBalanceTimer: Timer?
@State private var relaySubscriptions = SubscriptionCancellables()

@State private var selectedTab: ProfileHeader.ProfileHeaderTab = .notes
@State private var selectedTab: ProfileFeedType = .notes

@State private var alert: AlertState<Never>?

Expand Down Expand Up @@ -84,14 +84,9 @@ struct ProfileView: View {
var body: some View {
VStack(spacing: 0) {
VStack {
let profileNotesFilter = Filter(
authorKeys: [author.hexadecimalPublicKey ?? "error"],
kinds: [.text, .delete, .repost, .longFormContent]
)

PagedNoteListView(
databaseFilter: selectedTab.request(author: author),
relayFilter: profileNotesFilter,
databaseFilter: selectedTab.databaseFilter(author: author),
relayFilter: selectedTab.relayFilter(author: author),
context: viewContext,
tab: .profile,
header: {
Expand All @@ -108,7 +103,7 @@ struct ProfileView: View {
.frame(minHeight: 300)
},
onRefresh: {
selectedTab.request(author: author)
selectedTab.databaseFilter(author: author)
}
)
.padding(0)
Expand Down

0 comments on commit 251d0ef

Please sign in to comment.