summaryrefslogtreecommitdiff
path: root/Jel/Views/Item/Series/ItemSeriesSelectableEpisodesView.swift
blob: 30cb97d22ebdc1d1dbe863d9831b9fcf1145ddfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//
//  ItemSeriesSelectableEpisodesView.swift
//  Jel
//
//  Created by zerocool on 2/24/24.
//

import SwiftUI
import JellyfinKit

struct Season: Identifiable, Hashable {
  var name: String
  var id: String
  var season: BaseItemDto
}

extension Season {
  static let emptySelection = Season(name: "", id: "", season: BaseItemDto())
}

struct ItemSeriesSelectableEpisodesView: View {
  @EnvironmentObject var jellyfinClient: JellyfinClientController
  @StateObject var authState: AuthStateController = AuthStateController.shared
  
  var item: BaseItemDto // series
  
  @State var seasons: [Season] = []
  @State var currentSeason: Season = .emptySelection
  @State var currentSeasonCopy: Season = .emptySelection
  
  @State var loading: Bool = true
  var body: some View {
    VStack(alignment: .leading) {
      HStack {
        Text("Episodes")
          .font(.title2)
          .padding(.horizontal)
        
        Picker("Season", selection: $currentSeason) {
          ForEach(seasons, id: \.self) {season in
            Text(season.name).tag(season)
          }
        }
      }
      
      ItemSeriesEpisodesView(item: currentSeasonCopy.season)
        .padding(.horizontal)
    }
    .if(loading) {view in
      view.redacted(reason: .placeholder)
    }
    .onChange(of: currentSeason) {
      currentSeasonCopy = currentSeason
    }
    .onAppear {
      Task {
        let parameters = Paths.GetItemsParameters(
          userID: authState.userId ?? "",
          parentID: item.id ?? ""
        )
        let req = Paths.getItems(parameters: parameters)
        
        do {
          let res = try await jellyfinClient.send(req)
          seasons = []
          for season in res.value.items ?? [] {
            let newSeason = Season(
              name: season.name ?? "---",
              id: season.id ?? "",
              season: season
            )
            seasons.append(newSeason)
            currentSeason = seasons.first ?? .emptySelection
            loading = false
          }
        } catch {}
      }
    }
  }
}

//#Preview {
//    ItemSeriesSelectableEpisodesView()
//}