Avoid fetching account when unnecessary

This commit is contained in:
Justin Mazzocchi 2020-09-26 17:22:15 -07:00
parent 587b88645e
commit 236261ccdb
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
5 changed files with 49 additions and 13 deletions

View file

@ -7,25 +7,50 @@ import Mastodon
import MastodonAPI
public struct AccountStatusesService {
public let accountService: AnyPublisher<AccountService, Error>
private let accountID: String
private let mastodonAPIClient: MastodonAPIClient
private let contentDatabase: ContentDatabase
init(account: Account, mastodonAPIClient: MastodonAPIClient, contentDatabase: ContentDatabase) {
self.init(
id: account.id,
account: account,
mastodonAPIClient: mastodonAPIClient,
contentDatabase: contentDatabase)
}
init(id: String, mastodonAPIClient: MastodonAPIClient, contentDatabase: ContentDatabase) {
self.init(id: id, account: nil, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
}
private init(
id: String,
account: Account?,
mastodonAPIClient: MastodonAPIClient,
contentDatabase: ContentDatabase) {
accountID = id
self.mastodonAPIClient = mastodonAPIClient
self.contentDatabase = contentDatabase
var accountPublisher = contentDatabase.accountObservation(id: accountID)
.compactMap { $0 }
.eraseToAnyPublisher()
if let account = account {
accountPublisher = accountPublisher
.merge(with: Just(account).setFailureType(to: Error.self))
.eraseToAnyPublisher()
}
accountService = accountPublisher
.map { AccountService(account: $0, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase) }
.eraseToAnyPublisher()
}
}
public extension AccountStatusesService {
func accountService() -> AnyPublisher<AccountService, Error> {
contentDatabase.accountObservation(id: accountID)
.compactMap { $0 }
.map { AccountService(account: $0, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase) }
.eraseToAnyPublisher()
}
func statusListService(
collectionPublisher: CurrentValueSubject<AccountStatusCollection, Never>) -> StatusListService {
StatusListService(

View file

@ -63,6 +63,10 @@ public extension NavigationService {
AccountStatusesService(id: id, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
}
func accountStatusesService(account: Account) -> AccountStatusesService {
AccountStatusesService(account: account, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
}
func statusService(status: Status) -> StatusService {
StatusService(status: status, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
}
@ -110,7 +114,7 @@ private extension NavigationService {
mastodonAPIClient: mastodonAPIClient,
contentDatabase: contentDatabase))
} else if let account = results.accounts.first {
return .accountStatuses(accountStatusesService(id: account.id))
return .accountStatuses(accountStatusesService(account: account))
} else if let status = results.statuses.first {
return .statusList(
StatusListService(

View file

@ -65,12 +65,19 @@ extension AccountListViewModel: CollectionViewModel {
public func itemSelected(_ item: CollectionItem) {
switch item.kind {
case .account:
let navigationService = accountListService.navigationService
let accountStatusesService: AccountStatusesService
if let account = accounts[item.id] {
accountStatusesService = navigationService.accountStatusesService(account: account)
} else {
accountStatusesService = navigationService.accountStatusesService(id: item.id)
}
navigationEventsSubject.send(
.collectionNavigation(
AccountStatusesViewModel(
accountStatusesService: accountListService
.navigationService
.accountStatusesService(id: item.id))))
accountStatusesService: accountStatusesService)))
default:
break
}

View file

@ -22,7 +22,7 @@ public class AccountStatusesViewModel: StatusListViewModel {
$collection.sink(receiveValue: collectionSubject.send).store(in: &cancellables)
accountStatusesService.accountService()
accountStatusesService.accountService
.map(AccountViewModel.init(accountService:))
.assignErrorsToAlertItem(to: \.alertItem, on: self)
.assign(to: &$accountViewModel)

View file

@ -120,7 +120,7 @@ public extension StatusViewModel {
Just(CollectionItemEvent.navigation(
.accountStatuses(
statusService.navigationService.accountStatusesService(
id: statusService.status.displayStatus.account.id))))
account: statusService.status.displayStatus.account))))
.setFailureType(to: Error.self)
.eraseToAnyPublisher())
}