mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-22 08:10:59 +00:00
Avoid fetching account when unnecessary
This commit is contained in:
parent
587b88645e
commit
236261ccdb
5 changed files with 49 additions and 13 deletions
|
@ -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(
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue