jellyflood/Shared/ViewModels/ItemViewModel/CollectionItemViewModel.swift
Joe Kribs 6700ce969c
Complete Collections' ItemView (#1500)
* WIP

* Remove Divider

* self deprecation message

* `OrderedDictionary<BaseItemKind, [BaseItemDto]>`

* Localization fun

* cleanup

* Remove play button items & order by BaseItemKind.

* Fix AttributesHStack on iPad and make sure they align to the correct side. Looks jarring on Collections since Collections are often more limited on AttributesHStack items.

* Localization fix

* cleanup

* cleanup

---------

Co-authored-by: Ethan Pippin <ethanpippin2343@gmail.com>
2025-05-17 11:34:04 -04:00

62 lines
1.7 KiB
Swift

//
// Swiftfin is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2025 Jellyfin & Jellyfin Contributors
//
import Combine
import Foundation
import JellyfinAPI
import OrderedCollections
final class CollectionItemViewModel: ItemViewModel {
// MARK: - Published Collection Items
@Published
private(set) var collectionItems: OrderedDictionary<BaseItemKind, [BaseItemDto]> = [:]
override var presentPlayButton: Bool {
false
}
// MARK: - On Refresh
override func onRefresh() async throws {
let collectionItems = try await self.getCollectionItems()
await MainActor.run {
self.collectionItems = collectionItems
}
}
// MARK: - Get Collection Items
private func getCollectionItems() async throws -> OrderedDictionary<BaseItemKind, [BaseItemDto]> {
var parameters = Paths.GetItemsByUserIDParameters()
parameters.fields = .MinimumFields
parameters.includeItemTypes = BaseItemKind.supportedCases
.appending(.episode)
parameters.parentID = item.id
let request = Paths.getItemsByUserID(
userID: userSession.user.id,
parameters: parameters
)
let response = try await userSession.client.send(request)
let items = response.value.items ?? []
let result = OrderedDictionary<BaseItemKind?, [BaseItemDto]>(
grouping: items,
by: \.type
)
.compactKeys()
.sortedKeys { $0.rawValue < $1.rawValue }
return result
}
}