Skip to content

Commit

Permalink
Support JikanAPI v4 (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
HackingGate authored Jan 7, 2024
1 parent af1d57f commit 67c8816
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 87 deletions.
2 changes: 1 addition & 1 deletion Shared/Views/AnimeInformationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
76 changes: 28 additions & 48 deletions Shared/Views/HomeTab/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,17 @@ import JikanSwift
struct HomeView: View {
@EnvironmentObject private var store: Store<AppState>

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 {
Expand All @@ -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))
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions Shared/flux/actions/JikanActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<JikanAPITop<[JikanAPIAnime]>, 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
}
Expand All @@ -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]>
}
}
34 changes: 12 additions & 22 deletions Shared/flux/reducers/JikanReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions Shared/flux/state/JikanState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [:]
}
2 changes: 1 addition & 1 deletion iOS/ImageTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct ImageTextView<D: Codable, Content: View>: 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
Expand Down

0 comments on commit 67c8816

Please sign in to comment.