From 99f2e4a854e3409bfc6c9a56aab2959b18a3c3f9 Mon Sep 17 00:00:00 2001 From: sunxiaojian Date: Thu, 18 Feb 2021 12:49:24 +0800 Subject: [PATCH] chore: code format and define onceRequestTootMaxCount --- .../Diffiable/Section/TimelineSection.swift | 22 ++++++++++--------- ...omeTimelineViewModel+LoadLatestState.swift | 2 +- .../TimelineMiddleLoaderTableViewCell.swift | 2 +- .../APIService/APIService+Favorite.swift | 2 +- .../APIService/APIService+HomeTimeline.swift | 2 +- .../APIService+PublicTimeline.swift | 2 +- Mastodon/Service/APIService/APIService.swift | 5 +++++ .../Persist/APIService+Persist+Timeline.swift | 2 +- 8 files changed, 23 insertions(+), 16 deletions(-) diff --git a/Mastodon/Diffiable/Section/TimelineSection.swift b/Mastodon/Diffiable/Section/TimelineSection.swift index 59d4ce10..c9adbd83 100644 --- a/Mastodon/Diffiable/Section/TimelineSection.swift +++ b/Mastodon/Diffiable/Section/TimelineSection.swift @@ -45,19 +45,19 @@ extension TimelineSection { // configure cell managedObjectContext.performAndWait { let toot = managedObjectContext.object(with: objectID) as! Toot - TimelineSection.configure(cell: cell, timestampUpdatePublisher: timestampUpdatePublisher, toot: toot, requestUserID:requestUserID) + TimelineSection.configure(cell: cell, timestampUpdatePublisher: timestampUpdatePublisher, toot: toot, requestUserID: requestUserID) } cell.delegate = timelinePostTableViewCellDelegate return cell case .publicMiddleLoader(let upperTimelineTootID): let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineMiddleLoaderTableViewCell.self), for: indexPath) as! TimelineMiddleLoaderTableViewCell cell.delegate = timelineMiddleLoaderTableViewCellDelegate - timelineMiddleLoaderTableViewCellDelegate?.configure(cell: cell, upperTimelineTootID: upperTimelineTootID,timelineIndexobjectID: nil) + timelineMiddleLoaderTableViewCellDelegate?.configure(cell: cell, upperTimelineTootID: upperTimelineTootID, timelineIndexobjectID: nil) return cell case .homeMiddleLoader(let upperTimelineIndexObjectID): let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineMiddleLoaderTableViewCell.self), for: indexPath) as! TimelineMiddleLoaderTableViewCell cell.delegate = timelineMiddleLoaderTableViewCellDelegate - timelineMiddleLoaderTableViewCellDelegate?.configure(cell: cell, upperTimelineTootID: nil,timelineIndexobjectID: upperTimelineIndexObjectID) + timelineMiddleLoaderTableViewCellDelegate?.configure(cell: cell, upperTimelineTootID: nil, timelineIndexobjectID: upperTimelineIndexObjectID) return cell case .bottomLoader: let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self), for: indexPath) as! TimelineBottomLoaderTableViewCell @@ -83,39 +83,41 @@ extension TimelineSection { ) // set text cell.timelinePostView.activeTextLabel.config(content: toot.content) - + // toolbar - let isLike = (toot.reblog ?? toot).favouritedBy.flatMap({ $0.contains(where: { $0.id == requestUserID }) }) ?? false + let isLike = (toot.reblog ?? toot).favouritedBy.flatMap { $0.contains(where: { $0.id == requestUserID }) } ?? false let favoriteCountTitle: String = { let count = (toot.reblog ?? toot).favouritesCount.intValue return TimelineSection.formattedNumberTitleForActionButton(count) }() cell.timelinePostView.actionToolbarContainer.starButton.setTitle(favoriteCountTitle, for: .normal) cell.timelinePostView.actionToolbarContainer.isStarButtonHighlight = isLike + // set date let createdAt = (toot.reblog ?? toot).createdAt + cell.timelinePostView.dateLabel.text = createdAt.shortTimeAgoSinceNow timestampUpdatePublisher .sink { _ in cell.timelinePostView.dateLabel.text = createdAt.shortTimeAgoSinceNow } .store(in: &cell.disposeBag) - + // observe model change ManagedObjectObserver.observe(object: toot.reblog ?? toot) .receive(on: DispatchQueue.main) .sink { _ in // do nothing } receiveValue: { change in - guard case let .update(object) = change.changeType, + guard case .update(let object) = change.changeType, let newToot = object as? Toot else { return } let targetToot = newToot.reblog ?? newToot - - let isLike = targetToot.favouritedBy.flatMap({ $0.contains(where: { $0.id == requestUserID }) }) ?? false + + let isLike = targetToot.favouritedBy.flatMap { $0.contains(where: { $0.id == requestUserID }) } ?? false let favoriteCount = targetToot.favouritesCount.intValue let favoriteCountTitle = TimelineSection.formattedNumberTitleForActionButton(favoriteCount) cell.timelinePostView.actionToolbarContainer.starButton.setTitle(favoriteCountTitle, for: .normal) cell.timelinePostView.actionToolbarContainer.isStarButtonHighlight = isLike - os_log("%{public}s[%{public}ld], %{public}s: like count label for tweet %s did update: %ld", ((#file as NSString).lastPathComponent), #line, #function, targetToot.id, favoriteCount ) + os_log("%{public}s[%{public}ld], %{public}s: like count label for toot %s did update: %ld", (#file as NSString).lastPathComponent, #line, #function, targetToot.id, favoriteCount) } .store(in: &cell.disposeBag) } diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift index 63300011..beb95d2a 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewModel+LoadLatestState.swift @@ -65,7 +65,7 @@ extension HomeTimelineViewModel.LoadLatestState { let endFetch = CACurrentMediaTime() os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: collect timelineIndexes cost: %.2fs", ((#file as NSString).lastPathComponent), #line, #function, endFetch - start) latestTootIDs = timelineIndexes - .prefix(200) // avoid performance issue + .prefix(APIService.onceRequestTootMaxCount) // avoid performance issue .compactMap { timelineIndex in timelineIndex.value(forKeyPath: #keyPath(HomeTimelineIndex.toot.id)) as? Toot.ID } diff --git a/Mastodon/Scene/Share/View/TableviewCell/TimelineMiddleLoaderTableViewCell.swift b/Mastodon/Scene/Share/View/TableviewCell/TimelineMiddleLoaderTableViewCell.swift index 28ffc35a..d768865d 100644 --- a/Mastodon/Scene/Share/View/TableviewCell/TimelineMiddleLoaderTableViewCell.swift +++ b/Mastodon/Scene/Share/View/TableviewCell/TimelineMiddleLoaderTableViewCell.swift @@ -11,7 +11,7 @@ import os.log import UIKit protocol TimelineMiddleLoaderTableViewCellDelegate: class { - func configure(cell: TimelineMiddleLoaderTableViewCell, upperTimelineTootID: String?,timelineIndexobjectID:NSManagedObjectID?) + func configure(cell: TimelineMiddleLoaderTableViewCell, upperTimelineTootID: String?, timelineIndexobjectID:NSManagedObjectID?) func timelineMiddleLoaderTableViewCell(_ cell: TimelineMiddleLoaderTableViewCell, loadMoreButtonDidPressed button: UIButton) } diff --git a/Mastodon/Service/APIService/APIService+Favorite.swift b/Mastodon/Service/APIService/APIService+Favorite.swift index 69c78ed7..8dd4839a 100644 --- a/Mastodon/Service/APIService/APIService+Favorite.swift +++ b/Mastodon/Service/APIService/APIService+Favorite.swift @@ -124,7 +124,7 @@ extension APIService { extension APIService { func likeList( - limit: Int = 200, + limit: Int = onceRequestTootMaxCount, userID: String, maxID: String? = nil, mastodonAuthenticationBox: AuthenticationService.MastodonAuthenticationBox diff --git a/Mastodon/Service/APIService/APIService+HomeTimeline.swift b/Mastodon/Service/APIService/APIService+HomeTimeline.swift index 06157a14..0112a9da 100644 --- a/Mastodon/Service/APIService/APIService+HomeTimeline.swift +++ b/Mastodon/Service/APIService/APIService+HomeTimeline.swift @@ -19,7 +19,7 @@ extension APIService { domain: String, sinceID: Mastodon.Entity.Status.ID? = nil, maxID: Mastodon.Entity.Status.ID? = nil, - limit: Int = 100, + limit: Int = onceRequestTootMaxCount, local: Bool? = nil, authorizationBox: AuthenticationService.MastodonAuthenticationBox ) -> AnyPublisher, Error> { diff --git a/Mastodon/Service/APIService/APIService+PublicTimeline.swift b/Mastodon/Service/APIService/APIService+PublicTimeline.swift index 26fd91f5..bfa3bb26 100644 --- a/Mastodon/Service/APIService/APIService+PublicTimeline.swift +++ b/Mastodon/Service/APIService/APIService+PublicTimeline.swift @@ -21,7 +21,7 @@ extension APIService { domain: String, sinceID: Mastodon.Entity.Status.ID? = nil, maxID: Mastodon.Entity.Status.ID? = nil, - limit: Int = 100 + limit: Int = onceRequestTootMaxCount ) -> AnyPublisher, Error> { let query = Mastodon.API.Timeline.PublicTimelineQuery( local: nil, diff --git a/Mastodon/Service/APIService/APIService.swift b/Mastodon/Service/APIService/APIService.swift index 36778a3b..655684cc 100644 --- a/Mastodon/Service/APIService/APIService.swift +++ b/Mastodon/Service/APIService/APIService.swift @@ -43,6 +43,11 @@ final class APIService { } +extension APIService { + public static let onceRequestTootMaxCount = 100 + public static let onceRequestUserMaxCount = 100 +} + extension APIService { public enum Persist { } public enum CoreData { } diff --git a/Mastodon/Service/APIService/Persist/APIService+Persist+Timeline.swift b/Mastodon/Service/APIService/Persist/APIService+Persist+Timeline.swift index 651a847d..460cab02 100644 --- a/Mastodon/Service/APIService/Persist/APIService+Persist+Timeline.swift +++ b/Mastodon/Service/APIService/Persist/APIService+Persist+Timeline.swift @@ -423,7 +423,7 @@ extension APIService.Persist { let timelineIndex = status.homeTimelineIndexes? .first { $0.userID == requestMastodonUserID } if timelineIndex == nil { - let timelineIndexProperty = HomeTimelineIndex.Property(domain: domain,userID: requestMastodonUserID) + let timelineIndexProperty = HomeTimelineIndex.Property(domain: domain, userID: requestMastodonUserID) let _ = HomeTimelineIndex.insert( into: managedObjectContext,