Merge branch 'develop' into IOS-37_Widgets

This commit is contained in:
Marcus Kida 2023-02-07 08:18:35 +01:00
commit 0647cae24a
No known key found for this signature in database
GPG Key ID: 19FF64E08013CA40
5 changed files with 29 additions and 3 deletions

View File

@ -518,6 +518,12 @@ extension HomeTimelineViewController: UITableViewDelegate, AutoGenerateTableView
} }
// sourcery:end // sourcery:end
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == tableView.numberOfRows(inSection: indexPath.section) - 1 {
viewModel.timelineDidReachEnd()
}
}
} }
// MARK: - TimelineMiddleLoaderTableViewCellDelegate // MARK: - TimelineMiddleLoaderTableViewCellDelegate

View File

@ -65,7 +65,7 @@ extension HomeTimelineViewModel.LoadLatestState {
guard let viewModel else { return } guard let viewModel else { return }
let latestFeedRecords = viewModel.fetchedResultsController.records.prefix(APIService.onceRequestStatusMaxCount) let latestFeedRecords = viewModel.fetchedResultsController.records.prefix(APIService.onceRequestStatusMaxCount)
let parentManagedObjectContext = viewModel.fetchedResultsController.fetchedResultsController.managedObjectContext let parentManagedObjectContext = viewModel.fetchedResultsController.managedObjectContext
let managedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType) let managedObjectContext = NSManagedObjectContext(concurrencyType: .privateQueueConcurrencyType)
managedObjectContext.parent = parentManagedObjectContext managedObjectContext.parent = parentManagedObjectContext

View File

@ -67,7 +67,7 @@ extension HomeTimelineViewModel.LoadOldestState {
} }
Task { Task {
let managedObjectContext = viewModel.fetchedResultsController.fetchedResultsController.managedObjectContext let managedObjectContext = viewModel.fetchedResultsController.managedObjectContext
let _maxID: Mastodon.Entity.Status.ID? = try await managedObjectContext.perform { let _maxID: Mastodon.Entity.Status.ID? = try await managedObjectContext.perform {
guard let feed = lastFeedRecord.object(in: managedObjectContext), guard let feed = lastFeedRecord.object(in: managedObjectContext),
let status = feed.status let status = feed.status

View File

@ -119,6 +119,12 @@ extension HomeTimelineViewModel {
} }
} }
extension HomeTimelineViewModel {
func timelineDidReachEnd() {
fetchedResultsController.fetchNextBatch()
}
}
extension HomeTimelineViewModel { extension HomeTimelineViewModel {
// load timeline gap // load timeline gap

View File

@ -16,11 +16,19 @@ import MastodonSDK
final public class FeedFetchedResultsController: NSObject { final public class FeedFetchedResultsController: NSObject {
private enum Constants {
static let defaultFetchLimit = 100
}
public let logger = Logger(subsystem: "FeedFetchedResultsController", category: "DB") public let logger = Logger(subsystem: "FeedFetchedResultsController", category: "DB")
var disposeBag = Set<AnyCancellable>() var disposeBag = Set<AnyCancellable>()
public let fetchedResultsController: NSFetchedResultsController<Feed> private let fetchedResultsController: NSFetchedResultsController<Feed>
public var managedObjectContext: NSManagedObjectContext {
fetchedResultsController.managedObjectContext
}
// input // input
@Published public var predicate = Feed.predicate(kind: .none, acct: .none) @Published public var predicate = Feed.predicate(kind: .none, acct: .none)
@ -28,6 +36,11 @@ final public class FeedFetchedResultsController: NSObject {
// output // output
private let _objectIDs = PassthroughSubject<[NSManagedObjectID], Never>() private let _objectIDs = PassthroughSubject<[NSManagedObjectID], Never>()
@Published public var records: [ManagedObjectRecord<Feed>] = [] @Published public var records: [ManagedObjectRecord<Feed>] = []
public func fetchNextBatch() {
fetchedResultsController.fetchRequest.fetchLimit += Constants.defaultFetchLimit
try? fetchedResultsController.performFetch()
}
public init(managedObjectContext: NSManagedObjectContext) { public init(managedObjectContext: NSManagedObjectContext) {
self.fetchedResultsController = { self.fetchedResultsController = {
@ -36,6 +49,7 @@ final public class FeedFetchedResultsController: NSObject {
fetchRequest.returnsObjectsAsFaults = false fetchRequest.returnsObjectsAsFaults = false
fetchRequest.shouldRefreshRefetchedObjects = true fetchRequest.shouldRefreshRefetchedObjects = true
fetchRequest.fetchBatchSize = 15 fetchRequest.fetchBatchSize = 15
fetchRequest.fetchLimit = Constants.defaultFetchLimit
let controller = NSFetchedResultsController( let controller = NSFetchedResultsController(
fetchRequest: fetchRequest, fetchRequest: fetchRequest,
managedObjectContext: managedObjectContext, managedObjectContext: managedObjectContext,