mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-25 17:50: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
|
import MastodonAPI
|
||||||
|
|
||||||
public struct AccountStatusesService {
|
public struct AccountStatusesService {
|
||||||
|
public let accountService: AnyPublisher<AccountService, Error>
|
||||||
|
|
||||||
private let accountID: String
|
private let accountID: String
|
||||||
private let mastodonAPIClient: MastodonAPIClient
|
private let mastodonAPIClient: MastodonAPIClient
|
||||||
private let contentDatabase: ContentDatabase
|
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) {
|
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
|
accountID = id
|
||||||
self.mastodonAPIClient = mastodonAPIClient
|
self.mastodonAPIClient = mastodonAPIClient
|
||||||
self.contentDatabase = contentDatabase
|
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 {
|
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(
|
func statusListService(
|
||||||
collectionPublisher: CurrentValueSubject<AccountStatusCollection, Never>) -> StatusListService {
|
collectionPublisher: CurrentValueSubject<AccountStatusCollection, Never>) -> StatusListService {
|
||||||
StatusListService(
|
StatusListService(
|
||||||
|
|
|
@ -63,6 +63,10 @@ public extension NavigationService {
|
||||||
AccountStatusesService(id: id, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
|
AccountStatusesService(id: id, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func accountStatusesService(account: Account) -> AccountStatusesService {
|
||||||
|
AccountStatusesService(account: account, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
|
||||||
|
}
|
||||||
|
|
||||||
func statusService(status: Status) -> StatusService {
|
func statusService(status: Status) -> StatusService {
|
||||||
StatusService(status: status, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
|
StatusService(status: status, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
|
||||||
}
|
}
|
||||||
|
@ -110,7 +114,7 @@ private extension NavigationService {
|
||||||
mastodonAPIClient: mastodonAPIClient,
|
mastodonAPIClient: mastodonAPIClient,
|
||||||
contentDatabase: contentDatabase))
|
contentDatabase: contentDatabase))
|
||||||
} else if let account = results.accounts.first {
|
} 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 {
|
} else if let status = results.statuses.first {
|
||||||
return .statusList(
|
return .statusList(
|
||||||
StatusListService(
|
StatusListService(
|
||||||
|
|
|
@ -65,12 +65,19 @@ extension AccountListViewModel: CollectionViewModel {
|
||||||
public func itemSelected(_ item: CollectionItem) {
|
public func itemSelected(_ item: CollectionItem) {
|
||||||
switch item.kind {
|
switch item.kind {
|
||||||
case .account:
|
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(
|
navigationEventsSubject.send(
|
||||||
.collectionNavigation(
|
.collectionNavigation(
|
||||||
AccountStatusesViewModel(
|
AccountStatusesViewModel(
|
||||||
accountStatusesService: accountListService
|
accountStatusesService: accountStatusesService)))
|
||||||
.navigationService
|
|
||||||
.accountStatusesService(id: item.id))))
|
|
||||||
default:
|
default:
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@ public class AccountStatusesViewModel: StatusListViewModel {
|
||||||
|
|
||||||
$collection.sink(receiveValue: collectionSubject.send).store(in: &cancellables)
|
$collection.sink(receiveValue: collectionSubject.send).store(in: &cancellables)
|
||||||
|
|
||||||
accountStatusesService.accountService()
|
accountStatusesService.accountService
|
||||||
.map(AccountViewModel.init(accountService:))
|
.map(AccountViewModel.init(accountService:))
|
||||||
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
.assignErrorsToAlertItem(to: \.alertItem, on: self)
|
||||||
.assign(to: &$accountViewModel)
|
.assign(to: &$accountViewModel)
|
||||||
|
|
|
@ -120,7 +120,7 @@ public extension StatusViewModel {
|
||||||
Just(CollectionItemEvent.navigation(
|
Just(CollectionItemEvent.navigation(
|
||||||
.accountStatuses(
|
.accountStatuses(
|
||||||
statusService.navigationService.accountStatusesService(
|
statusService.navigationService.accountStatusesService(
|
||||||
id: statusService.status.displayStatus.account.id))))
|
account: statusService.status.displayStatus.account))))
|
||||||
.setFailureType(to: Error.self)
|
.setFailureType(to: Error.self)
|
||||||
.eraseToAnyPublisher())
|
.eraseToAnyPublisher())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue