From 33f026f9ccfbe20ad87817739ae4d8cee6dac1c7 Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Fri, 2 Oct 2020 00:41:30 -0700 Subject: [PATCH] Load more WIP --- DB/Sources/DB/Content/ContentDatabase.swift | 18 ++++++++++++++---- .../DB/Extensions/Timeline+Extensions.swift | 4 ---- Localizations/Localizable.strings | 1 + View Controllers/TableViewController.swift | 6 ++++++ .../Sources/ViewModels/LoadMoreViewModel.swift | 5 +++++ .../ViewModels/StatusListViewModel.swift | 2 ++ Views/LoadMoreCell.swift | 4 ++++ 7 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 ViewModels/Sources/ViewModels/LoadMoreViewModel.swift diff --git a/DB/Sources/DB/Content/ContentDatabase.swift b/DB/Sources/DB/Content/ContentDatabase.swift index bdf9161..fc5a093 100644 --- a/DB/Sources/DB/Content/ContentDatabase.swift +++ b/DB/Sources/DB/Content/ContentDatabase.swift @@ -42,13 +42,23 @@ public extension ContentDatabase { func insert(statuses: [Status], timeline: Timeline) -> AnyPublisher { databaseWriter.writePublisher { - try timeline.save($0) + let timelineRecord = TimelineRecord(timeline: timeline) + + try timelineRecord.save($0) + + let maxIDPresent = try String.fetchOne($0, timelineRecord.statuses.select(max(StatusRecord.Columns.id))) for status in statuses { try status.save($0) try TimelineStatusJoin(timelineId: timeline.id, statusId: status.id).save($0) } + + if let maxIDPresent = maxIDPresent, + let minIDInserted = statuses.map(\.id).min(), + minIDInserted > maxIDPresent { + try LoadMore(timelineId: timeline.id, afterStatusId: minIDInserted).save($0) + } } .ignoreOutput() .eraseToAnyPublisher() @@ -117,7 +127,7 @@ public extension ContentDatabase { func setLists(_ lists: [List]) -> AnyPublisher { databaseWriter.writePublisher { for list in lists { - try Timeline.list(list).save($0) + try TimelineRecord(timeline: Timeline.list(list)).save($0) } try TimelineRecord @@ -130,7 +140,7 @@ public extension ContentDatabase { } func createList(_ list: List) -> AnyPublisher { - databaseWriter.writePublisher(updates: Timeline.list(list).save) + databaseWriter.writePublisher(updates: TimelineRecord(timeline: Timeline.list(list)).save) .ignoreOutput() .eraseToAnyPublisher() } @@ -191,7 +201,7 @@ public extension ContentDatabase { guard let index = timelineItems.firstIndex(where: { guard case let .status(configuration) = $0 else { return false } - return loadMore.afterStatusId < configuration.status.id + return loadMore.afterStatusId > configuration.status.id }) else { continue } timelineItems.insert(.loadMore(loadMore), at: index) diff --git a/DB/Sources/DB/Extensions/Timeline+Extensions.swift b/DB/Sources/DB/Extensions/Timeline+Extensions.swift index 20d245b..fc7522f 100644 --- a/DB/Sources/DB/Extensions/Timeline+Extensions.swift +++ b/DB/Sources/DB/Extensions/Timeline+Extensions.swift @@ -5,10 +5,6 @@ import GRDB import Mastodon extension Timeline { - func save(_ db: Database) throws { - try TimelineRecord(timeline: self).save(db) - } - init?(record: TimelineRecord) { switch (record.id, record.listId, diff --git a/Localizations/Localizable.strings b/Localizations/Localizable.strings index 46be720..7f73efa 100644 --- a/Localizations/Localizable.strings +++ b/Localizations/Localizable.strings @@ -28,6 +28,7 @@ "identities.browsing" = "Browsing"; "identities.pending" = "Pending"; "lists.new-list-title" = "New List Title"; +"load-more" = "Load More"; "pending.pending-confirmation" = "Your account is pending confirmation"; "preferences" = "Preferences"; "preferences.posting-reading" = "Posting and Reading"; diff --git a/View Controllers/TableViewController.swift b/View Controllers/TableViewController.swift index 9908f8a..e4d7757 100644 --- a/View Controllers/TableViewController.swift +++ b/View Controllers/TableViewController.swift @@ -27,6 +27,12 @@ class TableViewController: UITableViewController { statusListCell.viewModel = statusViewModel case (let accountListCell as AccountListCell, let accountViewModel as AccountViewModel): accountListCell.viewModel = accountViewModel + case (let loadMoreCell as LoadMoreCell, let loadMoreViewModel as LoadMoreViewModel): + var contentConfiguration = loadMoreCell.defaultContentConfiguration() + + contentConfiguration.text = NSLocalizedString("load-more", comment: "") + + loadMoreCell.contentConfiguration = contentConfiguration default: return nil } diff --git a/ViewModels/Sources/ViewModels/LoadMoreViewModel.swift b/ViewModels/Sources/ViewModels/LoadMoreViewModel.swift new file mode 100644 index 0000000..4bb1bf6 --- /dev/null +++ b/ViewModels/Sources/ViewModels/LoadMoreViewModel.swift @@ -0,0 +1,5 @@ +// Copyright © 2020 Metabolist. All rights reserved. + +public struct LoadMoreViewModel { + +} diff --git a/ViewModels/Sources/ViewModels/StatusListViewModel.swift b/ViewModels/Sources/ViewModels/StatusListViewModel.swift index b0431e4..63ce984 100644 --- a/ViewModels/Sources/ViewModels/StatusListViewModel.swift +++ b/ViewModels/Sources/ViewModels/StatusListViewModel.swift @@ -85,6 +85,8 @@ extension StatusListViewModel: CollectionViewModel { switch item.kind { case .status: return statusViewModel(item: item) + case .loadMore: + return LoadMoreViewModel() default: return nil } diff --git a/Views/LoadMoreCell.swift b/Views/LoadMoreCell.swift index ecad9cb..1a774a8 100644 --- a/Views/LoadMoreCell.swift +++ b/Views/LoadMoreCell.swift @@ -3,5 +3,9 @@ import UIKit class LoadMoreCell: UITableViewCell { + override func layoutSubviews() { + super.layoutSubviews() + separatorInset.left = UIDevice.current.userInterfaceIdiom == .phone ? 0 : layoutMargins.left + } }