Skip to content

Commit 32be505

Browse files
committed
Fixed season cache
1 parent df9bc6d commit 32be505

1 file changed

Lines changed: 43 additions & 53 deletions

File tree

Sora/Views/MediaInfoView/MediaInfoView.swift

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ struct MediaInfoView: View {
580580
let seasons = groupedEpisodes()
581581
if seasons.count > 1 {
582582
Menu {
583-
ForEach(0..<seasons.count, id: \.self) { index in
583+
ForEach(0..<seasons.count, id: \..self) { index in
584584
Button(action: { selectedSeason = index }) {
585585
Text(String(format: NSLocalizedString("Season %d", comment: ""), index + 1))
586586
}
@@ -602,7 +602,7 @@ struct MediaInfoView: View {
602602
@ViewBuilder
603603
private var rangeSelectorStyled: some View {
604604
Menu {
605-
ForEach(episodeRanges, id: \.self) { range in
605+
ForEach(generateRanges(), id: \..self) { range in
606606
Button(action: { selectedRange = range }) {
607607
Text("\(range.lowerBound + 1)-\(range.upperBound)")
608608
}
@@ -661,12 +661,10 @@ struct MediaInfoView: View {
661661

662662
@ViewBuilder
663663
private var flatEpisodeList: some View {
664-
ScrollView {
665-
VStack(spacing: 15) {
666-
ForEach(currentEpisodeList.indices.filter { selectedRange.contains($0) }, id: \.self) { i in
667-
let ep = currentEpisodeList[i]
668-
createEpisodeCell(episode: ep, index: i, season: isGroupedBySeasons ? selectedSeason + 1 : 1)
669-
}
664+
VStack(spacing: 15) {
665+
ForEach(episodeLinks.indices.filter { selectedRange.contains($0) }, id: \.self) { i in
666+
let ep = episodeLinks[i]
667+
createEpisodeCell(episode: ep, index: i, season: 1)
670668
}
671669
}
672670
}
@@ -675,12 +673,9 @@ struct MediaInfoView: View {
675673
private var seasonsEpisodeList: some View {
676674
let seasons = groupedEpisodes()
677675
if !seasons.isEmpty, selectedSeason < seasons.count {
678-
ScrollView {
679-
VStack(spacing: 15) {
680-
ForEach(seasons[selectedSeason].indices.filter { selectedRange.contains($0) }, id: \.self) { i in
681-
let ep = seasons[selectedSeason][i]
682-
createEpisodeCell(episode: ep, index: i, season: selectedSeason + 1)
683-
}
676+
VStack(spacing: 15) {
677+
ForEach(seasons[selectedSeason]) { ep in
678+
createEpisodeCell(episode: ep, index: selectedSeason, season: selectedSeason + 1)
684679
}
685680
}
686681
} else {
@@ -764,7 +759,7 @@ struct MediaInfoView: View {
764759
}
765760

766761
LazyVStack(spacing: 15) {
767-
ForEach(chapters.indices.filter { selectedChapterRange.contains($0) }, id: \.self) { i in
762+
ForEach(chapters.indices.filter { selectedChapterRange.contains($0) }, id: \..self) { i in
768763
let chapter = chapters[i]
769764
let _ = refreshTrigger
770765
if let href = chapter["href"] as? String,
@@ -818,7 +813,7 @@ struct MediaInfoView: View {
818813
@ViewBuilder
819814
private var chapterRangeSelectorStyled: some View {
820815
Menu {
821-
ForEach(generateChapterRanges(), id: \.self) { range in
816+
ForEach(generateChapterRanges(), id: \..self) { range in
822817
Button(action: { selectedChapterRange = range }) {
823818
Text("\(range.lowerBound + 1)-\(range.upperBound)")
824819
}
@@ -1750,37 +1745,38 @@ struct MediaInfoView: View {
17501745
}
17511746

17521747
func fetchMalIDFromAniList(anilistID: Int, completion: @escaping (Int?) -> Void) {
1753-
let query = """
1754-
query {
1755-
Media(id: \(anilistID)) {
1756-
idMal
1757-
}
1758-
}
1759-
"""
1760-
guard let url = URL(string: "https://graphql.anilist.co") else {
1761-
completion(nil)
1762-
return
1763-
}
1764-
1765-
var request = URLRequest(url: url)
1766-
request.httpMethod = "POST"
1767-
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
1768-
request.httpBody = try? JSONSerialization.data(withJSONObject: ["query": query])
1769-
1770-
URLSession.shared.dataTask(with: request) { data, _, _ in
1771-
var malID: Int? = nil
1772-
if let data = data,
1773-
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
1774-
let dataDict = json["data"] as? [String: Any],
1775-
let media = dataDict["Media"] as? [String: Any],
1776-
let idMal = media["idMal"] as? Int {
1777-
malID = idMal
1748+
let query = """
1749+
query {
1750+
Media(id: \(anilistID)) {
1751+
idMal
17781752
}
1779-
DispatchQueue.main.async {
1780-
completion(malID)
17811753
}
1782-
}.resume()
1783-
}
1754+
"""
1755+
guard let url = URL(string: "https://graphql.anilist.co") else {
1756+
completion(nil)
1757+
return
1758+
}
1759+
1760+
var request = URLRequest(url: url)
1761+
request.httpMethod = "POST"
1762+
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
1763+
request.httpBody = try? JSONSerialization.data(withJSONObject: ["query": query])
1764+
1765+
URLSession.custom.dataTask(with: request) { data, _, _ in
1766+
var malID: Int? = nil
1767+
if let data = data,
1768+
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
1769+
let dataDict = json["data"] as? [String: Any],
1770+
let media = dataDict["Media"] as? [String: Any],
1771+
let idMal = media["idMal"] as? Int {
1772+
malID = idMal
1773+
}
1774+
DispatchQueue.main.async {
1775+
completion(malID)
1776+
}
1777+
}.resume()
1778+
}
1779+
17841780
private func fetchTMDBPosterImageAndSet() {
17851781
guard let tmdbID = tmdbID, let tmdbType = tmdbType else { return }
17861782
let apiType = tmdbType.rawValue
@@ -2305,14 +2301,8 @@ struct MediaInfoView: View {
23052301
completion(nil as EpisodeMetadataInfo?)
23062302
return
23072303
}
2308-
fetchEpisodeMetadataFromNetwork(anilistId: anilistId, episodeNumber: episode.number) { info in
2309-
if let info = info, let enTitle = info.title["en"] {
2310-
DispatchQueue.main.async {
2311-
episodeTitleCache[episode.number] = enTitle
2312-
}
2313-
}
2314-
completion(info)
2315-
}
2304+
2305+
fetchEpisodeMetadataFromNetwork(anilistId: anilistId, episodeNumber: episode.number, completion: completion)
23162306
}
23172307

23182308
private func fetchEpisodeMetadataFromNetwork(anilistId: Int, episodeNumber: Int, completion: @escaping (EpisodeMetadataInfo?) -> Void) {

0 commit comments

Comments
 (0)