From 67c88163a0eb55da3bb2742541cb56b9f6f0b315 Mon Sep 17 00:00:00 2001 From: Tong Date: Sun, 7 Jan 2024 22:35:59 +0900 Subject: [PATCH] Support JikanAPI v4 (#21) --- Packages/JikanSwift | 2 +- Shared/Views/AnimeInformationView.swift | 2 +- Shared/Views/HomeTab/HomeView.swift | 76 +++++++++---------------- Shared/flux/actions/JikanActions.swift | 13 +++-- Shared/flux/reducers/JikanReducer.swift | 34 ++++------- Shared/flux/state/JikanState.swift | 12 ++-- iOS/ImageTextView.swift | 2 +- 7 files changed, 54 insertions(+), 87 deletions(-) diff --git a/Packages/JikanSwift b/Packages/JikanSwift index 0fe11b9..b36e128 160000 --- a/Packages/JikanSwift +++ b/Packages/JikanSwift @@ -1 +1 @@ -Subproject commit 0fe11b9967143ad71ad36c37cc94cac7346fe632 +Subproject commit b36e12890a3720063a979826e813c08608db0f37 diff --git a/Shared/Views/AnimeInformationView.swift b/Shared/Views/AnimeInformationView.swift index 0433501..e2071a8 100644 --- a/Shared/Views/AnimeInformationView.swift +++ b/Shared/Views/AnimeInformationView.swift @@ -38,7 +38,7 @@ struct AnimeInformationView: View { } } Spacer() - if let imageURL = URL(string: animeDetail.imageURL) { + if let imageURL = URL(string: animeDetail.images.webp.imageURL) { KFImage(imageURL) .resizable() .aspectRatio(contentMode: .fill) diff --git a/Shared/Views/HomeTab/HomeView.swift b/Shared/Views/HomeTab/HomeView.swift index 06659a5..ddc9516 100644 --- a/Shared/Views/HomeTab/HomeView.swift +++ b/Shared/Views/HomeTab/HomeView.swift @@ -12,23 +12,17 @@ import JikanSwift struct HomeView: View { @EnvironmentObject private var store: Store - private var topBypopularity: [JikanAPIAnime]? { - store.state.jikanState.topBypopularity?.top + private var topAnimeTvBypopularity: [JikanAPIAnime]? { + store.state.jikanState.topAnimeTvBypopularity?.data } - private var topAiring: [JikanAPIAnime]? { - store.state.jikanState.topAiring?.top + private var topAnimeTvAiring: [JikanAPIAnime]? { + store.state.jikanState.topAnimeTvAiring?.data } - private var topTv: [JikanAPIAnime]? { - store.state.jikanState.topTv?.top + private var topAnimeMovieBypopularity: [JikanAPIAnime]? { + store.state.jikanState.topAnimeMovieBypopularity?.data } - private var topMovie: [JikanAPIAnime]? { - store.state.jikanState.topMovie?.top - } - private var topOva: [JikanAPIAnime]? { - store.state.jikanState.topOva?.top - } - private var topUpcoming: [JikanAPIAnime]? { - store.state.jikanState.topUpcoming?.top + private var topAnimeMovieAiring: [JikanAPIAnime]? { + store.state.jikanState.topAnimeMovieAiring?.data } var body: some View { @@ -37,51 +31,37 @@ struct HomeView: View { // TODO: Add ForEach here LazyVStack { AnimeCrosslineRow( - title: "Most Popular", - animes: topBypopularity - ) - AnimeCrosslineRow( - title: "Top Airing", - animes: topAiring - ) - AnimeCrosslineRow( - title: "Top TV Series", - animes: topTv + title: "Most Popular TV", + animes: topAnimeTvBypopularity ) AnimeCrosslineRow( - title: "Top Movies", - animes: topMovie + title: "Top Airing TV", + animes: topAnimeTvAiring ) AnimeCrosslineRow( - title: "Top OVAs", - animes: topOva + title: "Most Popular Movie", + animes: topAnimeMovieBypopularity ) AnimeCrosslineRow( - title: "Top Upcoming", - animes: topUpcoming + title: "Top Airing Movie", + animes: topAnimeMovieAiring ) } } }, title: "Home") .onAppear { - store.dispatch(action: JikanActions.Top(type: .anime, - page: 1, - subtype: .bypopularity)) - store.dispatch(action: JikanActions.Top(type: .anime, - page: 1, - subtype: .airing)) - store.dispatch(action: JikanActions.Top(type: .anime, - page: 1, - subtype: .tv)) - store.dispatch(action: JikanActions.Top(type: .anime, - page: 1, - subtype: .movie)) - store.dispatch(action: JikanActions.Top(type: .anime, - page: 1, - subtype: .ova)) - store.dispatch(action: JikanActions.Top(type: .anime, - page: 1, - subtype: .upcoming)) + store.dispatch(action: JikanActions.Top(type: .tv, + filter: .bypopularity, + page: 1)) + store.dispatch(action: JikanActions.Top(type: .tv, + filter: .airing, + page: 1)) + store.dispatch(action: JikanActions.Top(type: .movie, + filter: .bypopularity, + page: 1)) + store.dispatch(action: JikanActions.Top(type: .movie, + filter: .airing, + page: 1)) } } } diff --git a/Shared/flux/actions/JikanActions.swift b/Shared/flux/actions/JikanActions.swift index e7fc4be..06e7f3a 100644 --- a/Shared/flux/actions/JikanActions.swift +++ b/Shared/flux/actions/JikanActions.swift @@ -19,7 +19,7 @@ struct JikanActions { var params: [String: String] = [:] func execute(state: FluxState?, dispatch: @escaping DispatchFunction) { - JikanAPIService.shared.loadAnime( + JikanAPIService.shared.getAnimeFull( id: id, request: request, params: params) { @@ -36,20 +36,20 @@ struct JikanActions { struct Top: AsyncAction { let type: JikanAPITopType + let filter: JikanAPITopFilter let page: Int - let subtype: JikanAPITopSubtype var params: [String: String] = [:] func execute(state: FluxState?, dispatch: @escaping DispatchFunction) { - JikanAPIService.shared.loadTop( + JikanAPIService.shared.getTopAnime( type: type, + filter: filter, page: page, - subtype: subtype, params: params) { (result: Result, JikanAPIService.APIError>) in switch result { case let .success(response): - dispatch(SetTop(page: page, subtype: subtype, response: response)) + dispatch(SetTop(page: page, filter: filter, type: type, response: response)) case .failure(_): break } @@ -64,7 +64,8 @@ struct JikanActions { struct SetTop: Action { let page: Int - let subtype: JikanAPITopSubtype + let filter: JikanAPITopFilter + let type: JikanAPITopType let response: JikanAPITop<[JikanAPIAnime]> } } diff --git a/Shared/flux/reducers/JikanReducer.swift b/Shared/flux/reducers/JikanReducer.swift index 8c363e7..ef88a27 100644 --- a/Shared/flux/reducers/JikanReducer.swift +++ b/Shared/flux/reducers/JikanReducer.swift @@ -15,28 +15,18 @@ func jikanStateReducer(state: JikanState, action: Action) -> JikanState { state.animes[action.malID] = action.response case let action as JikanActions.SetTop: if action.page == 1 { - switch action.subtype { - case .airing: - state.topAiring = action.response - case .upcoming: - state.topUpcoming = action.response - case .tv: - state.topTv = action.response - case .movie: - state.topMovie = action.response - case .ova: - state.topOva = action.response - case .special: - state.topSpecial = action.response - case .bypopularity: - state.topBypopularity = action.response - case .favorite: - state.topFavorite = action.response - default: - fatalError("Unsupported subtype") - } - if action.subtype == .airing { - state.topAiring = action.response + if action.filter == .airing { + if action.type == .tv { + state.topAnimeTvAiring = action.response + } else if action.type == .movie { + state.topAnimeMovieAiring = action.response + } + } else if action.filter == .bypopularity { + if action.type == .tv { + state.topAnimeTvBypopularity = action.response + } else if action.type == .movie { + state.topAnimeMovieBypopularity = action.response + } } } else { // TODO: append diff --git a/Shared/flux/state/JikanState.swift b/Shared/flux/state/JikanState.swift index 425e0f2..ce4161d 100644 --- a/Shared/flux/state/JikanState.swift +++ b/Shared/flux/state/JikanState.swift @@ -10,14 +10,10 @@ import SwiftUIFlux import JikanSwift struct JikanState: FluxState, Codable { - var topAiring: JikanAPITop<[JikanAPIAnime]>? - var topUpcoming: JikanAPITop<[JikanAPIAnime]>? - var topTv: JikanAPITop<[JikanAPIAnime]>? - var topMovie: JikanAPITop<[JikanAPIAnime]>? - var topOva: JikanAPITop<[JikanAPIAnime]>? - var topSpecial: JikanAPITop<[JikanAPIAnime]>? - var topBypopularity: JikanAPITop<[JikanAPIAnime]>? - var topFavorite: JikanAPITop<[JikanAPIAnime]>? + var topAnimeTvAiring: JikanAPITop<[JikanAPIAnime]>? + var topAnimeTvBypopularity: JikanAPITop<[JikanAPIAnime]>? + var topAnimeMovieAiring: JikanAPITop<[JikanAPIAnime]>? + var topAnimeMovieBypopularity: JikanAPITop<[JikanAPIAnime]>? var animes: [Int: JikanAPIAnime] = [:] } diff --git a/iOS/ImageTextView.swift b/iOS/ImageTextView.swift index 2664372..c15f2c0 100644 --- a/iOS/ImageTextView.swift +++ b/iOS/ImageTextView.swift @@ -51,7 +51,7 @@ struct ImageTextView: View { private var imageURL: String? { if let anime = data as? JikanAPIAnime { - return anime.imageURL + return anime.images.webp.imageURL } if let episode = data as? CRAPIMedia { return episode.screenshotImage?.fwideUrl