Prevent extra view model allocations

This commit is contained in:
Justin Mazzocchi 2020-10-28 18:55:34 -07:00
parent 2b8f29d397
commit 668d4f2577
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
3 changed files with 15 additions and 10 deletions

View file

@ -8,17 +8,28 @@ import ServiceLayer
public final class NavigationViewModel: ObservableObject { public final class NavigationViewModel: ObservableObject {
public let identification: Identification public let identification: Identification
@Published public private(set) var recentIdentities = [Identity]() @Published public private(set) var recentIdentities = [Identity]()
@Published public var timeline: Timeline @Published public var timeline: Timeline {
didSet {
timelineViewModel = CollectionItemsViewModel(
collectionService: identification.service.service(timeline: timeline),
identification: identification)
}
}
@Published public private(set) var timelinesAndLists: [Timeline] @Published public private(set) var timelinesAndLists: [Timeline]
@Published public var presentingSecondaryNavigation = false @Published public var presentingSecondaryNavigation = false
@Published public var alertItem: AlertItem? @Published public var alertItem: AlertItem?
public var selectedTab: Tab? = .timelines public var selectedTab: Tab? = .timelines
public private(set) var timelineViewModel: CollectionItemsViewModel
private var cancellables = Set<AnyCancellable>() private var cancellables = Set<AnyCancellable>()
public init(identification: Identification) { public init(identification: Identification) {
self.identification = identification self.identification = identification
timeline = identification.identity.authenticated ? .home : .local let timeline: Timeline = identification.identity.authenticated ? .home : .local
self.timeline = timeline
timelineViewModel = CollectionItemsViewModel(
collectionService: identification.service.service(timeline: timeline),
identification: identification)
timelinesAndLists = identification.identity.authenticated timelinesAndLists = identification.identity.authenticated
? Timeline.authenticatedDefaults ? Timeline.authenticatedDefaults
: Timeline.unauthenticatedDefaults : Timeline.unauthenticatedDefaults
@ -89,12 +100,6 @@ public extension NavigationViewModel {
.sink { _ in } receiveValue: { _ in } .sink { _ in } receiveValue: { _ in }
.store(in: &cancellables) .store(in: &cancellables)
} }
func viewModel(timeline: Timeline) -> CollectionItemsViewModel {
CollectionItemsViewModel(
collectionService: identification.service.service(timeline: timeline),
identification: identification)
}
} }
public extension NavigationViewModel { public extension NavigationViewModel {

View file

@ -60,7 +60,7 @@ private extension TabNavigationView {
func view(tab: NavigationViewModel.Tab) -> some View { func view(tab: NavigationViewModel.Tab) -> some View {
switch tab { switch tab {
case .timelines: case .timelines:
TableView(viewModel: viewModel.viewModel(timeline: viewModel.timeline)) TableView(viewModel: viewModel.timelineViewModel)
.id(viewModel.timeline.id) .id(viewModel.timeline.id)
.edgesIgnoringSafeArea(.all) .edgesIgnoringSafeArea(.all)
.navigationTitle(viewModel.timeline.title) .navigationTitle(viewModel.timeline.title)

View file

@ -21,7 +21,7 @@ import PreviewViewModels
struct StatusListView_Previews: PreviewProvider { struct StatusListView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
TableView(viewModel: NavigationViewModel(identification: .preview).viewModel(timeline: .home)) TableView(viewModel: NavigationViewModel(identification: .preview).timelineViewModel)
} }
} }
#endif #endif