mirror of
https://github.com/cranci1/Sora.git
synced 2026-04-21 00:22:12 +00:00
Fixed season cache
This commit is contained in:
parent
df9bc6db67
commit
32be5059e4
1 changed files with 43 additions and 53 deletions
|
|
@ -580,7 +580,7 @@ struct MediaInfoView: View {
|
||||||
let seasons = groupedEpisodes()
|
let seasons = groupedEpisodes()
|
||||||
if seasons.count > 1 {
|
if seasons.count > 1 {
|
||||||
Menu {
|
Menu {
|
||||||
ForEach(0..<seasons.count, id: \.self) { index in
|
ForEach(0..<seasons.count, id: \..self) { index in
|
||||||
Button(action: { selectedSeason = index }) {
|
Button(action: { selectedSeason = index }) {
|
||||||
Text(String(format: NSLocalizedString("Season %d", comment: ""), index + 1))
|
Text(String(format: NSLocalizedString("Season %d", comment: ""), index + 1))
|
||||||
}
|
}
|
||||||
|
|
@ -602,7 +602,7 @@ struct MediaInfoView: View {
|
||||||
@ViewBuilder
|
@ViewBuilder
|
||||||
private var rangeSelectorStyled: some View {
|
private var rangeSelectorStyled: some View {
|
||||||
Menu {
|
Menu {
|
||||||
ForEach(episodeRanges, id: \.self) { range in
|
ForEach(generateRanges(), id: \..self) { range in
|
||||||
Button(action: { selectedRange = range }) {
|
Button(action: { selectedRange = range }) {
|
||||||
Text("\(range.lowerBound + 1)-\(range.upperBound)")
|
Text("\(range.lowerBound + 1)-\(range.upperBound)")
|
||||||
}
|
}
|
||||||
|
|
@ -661,12 +661,10 @@ struct MediaInfoView: View {
|
||||||
|
|
||||||
@ViewBuilder
|
@ViewBuilder
|
||||||
private var flatEpisodeList: some View {
|
private var flatEpisodeList: some View {
|
||||||
ScrollView {
|
VStack(spacing: 15) {
|
||||||
VStack(spacing: 15) {
|
ForEach(episodeLinks.indices.filter { selectedRange.contains($0) }, id: \.self) { i in
|
||||||
ForEach(currentEpisodeList.indices.filter { selectedRange.contains($0) }, id: \.self) { i in
|
let ep = episodeLinks[i]
|
||||||
let ep = currentEpisodeList[i]
|
createEpisodeCell(episode: ep, index: i, season: 1)
|
||||||
createEpisodeCell(episode: ep, index: i, season: isGroupedBySeasons ? selectedSeason + 1 : 1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -675,12 +673,9 @@ struct MediaInfoView: View {
|
||||||
private var seasonsEpisodeList: some View {
|
private var seasonsEpisodeList: some View {
|
||||||
let seasons = groupedEpisodes()
|
let seasons = groupedEpisodes()
|
||||||
if !seasons.isEmpty, selectedSeason < seasons.count {
|
if !seasons.isEmpty, selectedSeason < seasons.count {
|
||||||
ScrollView {
|
VStack(spacing: 15) {
|
||||||
VStack(spacing: 15) {
|
ForEach(seasons[selectedSeason]) { ep in
|
||||||
ForEach(seasons[selectedSeason].indices.filter { selectedRange.contains($0) }, id: \.self) { i in
|
createEpisodeCell(episode: ep, index: selectedSeason, season: selectedSeason + 1)
|
||||||
let ep = seasons[selectedSeason][i]
|
|
||||||
createEpisodeCell(episode: ep, index: i, season: selectedSeason + 1)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -764,7 +759,7 @@ struct MediaInfoView: View {
|
||||||
}
|
}
|
||||||
|
|
||||||
LazyVStack(spacing: 15) {
|
LazyVStack(spacing: 15) {
|
||||||
ForEach(chapters.indices.filter { selectedChapterRange.contains($0) }, id: \.self) { i in
|
ForEach(chapters.indices.filter { selectedChapterRange.contains($0) }, id: \..self) { i in
|
||||||
let chapter = chapters[i]
|
let chapter = chapters[i]
|
||||||
let _ = refreshTrigger
|
let _ = refreshTrigger
|
||||||
if let href = chapter["href"] as? String,
|
if let href = chapter["href"] as? String,
|
||||||
|
|
@ -818,7 +813,7 @@ struct MediaInfoView: View {
|
||||||
@ViewBuilder
|
@ViewBuilder
|
||||||
private var chapterRangeSelectorStyled: some View {
|
private var chapterRangeSelectorStyled: some View {
|
||||||
Menu {
|
Menu {
|
||||||
ForEach(generateChapterRanges(), id: \.self) { range in
|
ForEach(generateChapterRanges(), id: \..self) { range in
|
||||||
Button(action: { selectedChapterRange = range }) {
|
Button(action: { selectedChapterRange = range }) {
|
||||||
Text("\(range.lowerBound + 1)-\(range.upperBound)")
|
Text("\(range.lowerBound + 1)-\(range.upperBound)")
|
||||||
}
|
}
|
||||||
|
|
@ -1750,37 +1745,38 @@ struct MediaInfoView: View {
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchMalIDFromAniList(anilistID: Int, completion: @escaping (Int?) -> Void) {
|
func fetchMalIDFromAniList(anilistID: Int, completion: @escaping (Int?) -> Void) {
|
||||||
let query = """
|
let query = """
|
||||||
query {
|
query {
|
||||||
Media(id: \(anilistID)) {
|
Media(id: \(anilistID)) {
|
||||||
idMal
|
idMal
|
||||||
}
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
guard let url = URL(string: "https://graphql.anilist.co") else {
|
|
||||||
completion(nil)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
var request = URLRequest(url: url)
|
|
||||||
request.httpMethod = "POST"
|
|
||||||
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
||||||
request.httpBody = try? JSONSerialization.data(withJSONObject: ["query": query])
|
|
||||||
|
|
||||||
URLSession.shared.dataTask(with: request) { data, _, _ in
|
|
||||||
var malID: Int? = nil
|
|
||||||
if let data = data,
|
|
||||||
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
|
|
||||||
let dataDict = json["data"] as? [String: Any],
|
|
||||||
let media = dataDict["Media"] as? [String: Any],
|
|
||||||
let idMal = media["idMal"] as? Int {
|
|
||||||
malID = idMal
|
|
||||||
}
|
}
|
||||||
DispatchQueue.main.async {
|
|
||||||
completion(malID)
|
|
||||||
}
|
}
|
||||||
}.resume()
|
"""
|
||||||
}
|
guard let url = URL(string: "https://graphql.anilist.co") else {
|
||||||
|
completion(nil)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var request = URLRequest(url: url)
|
||||||
|
request.httpMethod = "POST"
|
||||||
|
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
||||||
|
request.httpBody = try? JSONSerialization.data(withJSONObject: ["query": query])
|
||||||
|
|
||||||
|
URLSession.custom.dataTask(with: request) { data, _, _ in
|
||||||
|
var malID: Int? = nil
|
||||||
|
if let data = data,
|
||||||
|
let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any],
|
||||||
|
let dataDict = json["data"] as? [String: Any],
|
||||||
|
let media = dataDict["Media"] as? [String: Any],
|
||||||
|
let idMal = media["idMal"] as? Int {
|
||||||
|
malID = idMal
|
||||||
|
}
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
completion(malID)
|
||||||
|
}
|
||||||
|
}.resume()
|
||||||
|
}
|
||||||
|
|
||||||
private func fetchTMDBPosterImageAndSet() {
|
private func fetchTMDBPosterImageAndSet() {
|
||||||
guard let tmdbID = tmdbID, let tmdbType = tmdbType else { return }
|
guard let tmdbID = tmdbID, let tmdbType = tmdbType else { return }
|
||||||
let apiType = tmdbType.rawValue
|
let apiType = tmdbType.rawValue
|
||||||
|
|
@ -2305,14 +2301,8 @@ struct MediaInfoView: View {
|
||||||
completion(nil as EpisodeMetadataInfo?)
|
completion(nil as EpisodeMetadataInfo?)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
fetchEpisodeMetadataFromNetwork(anilistId: anilistId, episodeNumber: episode.number) { info in
|
|
||||||
if let info = info, let enTitle = info.title["en"] {
|
fetchEpisodeMetadataFromNetwork(anilistId: anilistId, episodeNumber: episode.number, completion: completion)
|
||||||
DispatchQueue.main.async {
|
|
||||||
episodeTitleCache[episode.number] = enTitle
|
|
||||||
}
|
|
||||||
}
|
|
||||||
completion(info)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private func fetchEpisodeMetadataFromNetwork(anilistId: Int, episodeNumber: Int, completion: @escaping (EpisodeMetadataInfo?) -> Void) {
|
private func fetchEpisodeMetadataFromNetwork(anilistId: Int, episodeNumber: Int, completion: @escaping (EpisodeMetadataInfo?) -> Void) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue