Skip to content

Commit

Permalink
Merge pull request #827 from planetary-social/limit-notifications-view
Browse files Browse the repository at this point in the history
Limit NotificationsView to 100 results.
  • Loading branch information
mplorentz authored Jan 19, 2024
2 parents 0cb8496 + 8039f81 commit 08be660
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]


- Optimized loading of the Notifications tab
- Updated suggested users for discovery tab.
- Show the profile view when a search matches a valid User ID (npub).
- Added tabs to Profiles to filter posts.
Expand Down
27 changes: 14 additions & 13 deletions Nos/Models/CoreData/Event+CoreDataClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,6 @@ public enum EventKind: Int64, CaseIterable, Hashable {
case notificationServiceRegistration = 6666
}

extension FetchedResults where Element == Event {
var unmuted: [Event] {
filter {
if let author = $0.author {
let notDeleted = $0.deletedOn.count == 0
return !author.muted && notDeleted
}
return false
}
}
}

// swiftlint:disable type_body_length
@objc(Event)
@Observable
Expand Down Expand Up @@ -298,9 +286,22 @@ public class Event: NosManagedObject {
)
}

@nonobjc public class func all(notifying user: Author, since: Date? = nil) -> NSFetchRequest<Event> {
/// A request for all events that the given user should receive a notification for.
/// - Parameters:
/// - user: the author you want to view notifications for.
/// - since: a date that will be used as a lower bound for the request.
/// - limit: a max number of events to fetch.
/// - Returns: A fetch request for the events described.
@nonobjc public class func all(
notifying user: Author,
since: Date? = nil,
limit: Int? = nil
) -> NSFetchRequest<Event> {
let fetchRequest = NSFetchRequest<Event>(entityName: "Event")
fetchRequest.sortDescriptors = [NSSortDescriptor(keyPath: \Event.createdAt, ascending: false)]
if let limit {
fetchRequest.fetchLimit = limit
}

let mentionsPredicate = allMentionsPredicate(for: user)
let repliesPredicate = allRepliesPredicate(for: user)
Expand Down
15 changes: 10 additions & 5 deletions Nos/Views/NotificationsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,22 @@ struct NotificationsView: View {
@Dependency(\.pushNotificationService) private var pushNotificationService
@Dependency(\.persistenceController) private var persistenceController

private var eventRequest: FetchRequest<Event> = FetchRequest(fetchRequest: Event.emptyRequest())
private var events: FetchedResults<Event> { eventRequest.wrappedValue }
@FetchRequest private var events: FetchedResults<Event>
@State private var relaySubscriptions = SubscriptionCancellables()
@State private var isVisible = false

@State private var concecutiveTapsCancellable: AnyCancellable?

// Probably the logged in user should be in the @Environment eventually
private var user: Author?
private let maxNotificationsToShow = 100

init(user: Author?) {
self.user = user
if let user {
eventRequest = FetchRequest(fetchRequest: Event.all(notifying: user))
_events = FetchRequest(fetchRequest: Event.all(notifying: user, limit: maxNotificationsToShow))
} else {
_events = FetchRequest(fetchRequest: Event.emptyRequest())
}
}

Expand Down Expand Up @@ -74,8 +76,11 @@ struct NotificationsView: View {
NavigationStack(path: $router.notificationsPath) {
ScrollView(.vertical) {
LazyVStack {
ForEach(events.unmuted) { event in
if let user {
/// The fetch request for events has a `fetchLimit` set but it doesn't work, so we limit the
/// number of views displayed here and that appears to prevent @FetchRequest from loading all the
/// records into memory.
ForEach(0..<maxNotificationsToShow, id: \.self) { index in
if let event = events[safe: index], let user {
NotificationCard(viewModel: NotificationViewModel(note: event, user: user))
.padding(.horizontal, 15)
.padding(.bottom, 10)
Expand Down

0 comments on commit 08be660

Please sign in to comment.