Skip to content

Commit

Permalink
Implement name token parse
Browse files Browse the repository at this point in the history
  • Loading branch information
HackingGate committed Sep 24, 2020
1 parent 9922edd commit 1232fbb
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 44 deletions.
16 changes: 16 additions & 0 deletions MyAnimeList.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@
C7985BC524E7EB4D00355C07 /* PlayState.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7985BC424E7EB4D00355C07 /* PlayState.swift */; };
C7985BC724E7EBE200355C07 /* PlayReducer.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7985BC624E7EBE200355C07 /* PlayReducer.swift */; };
C7B6A80924E4349500D8A816 /* PlayerItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7B6A80824E4349500D8A816 /* PlayerItem.swift */; };
C7BF0BF32519E1B300C34401 /* AnimeEpisodeListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7BF0BF22519E1B300C34401 /* AnimeEpisodeListView.swift */; };
C7C7CF0524C9672500AABA89 /* SwiftUIFlux in Frameworks */ = {isa = PBXBuildFile; productRef = C7C7CF0424C9672500AABA89 /* SwiftUIFlux */; };
C7DE028824E14A1C00B4E4F6 /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = C7DE028724E14A1C00B4E4F6 /* README.md */; };
C7E9D283251A3A080056211E /* JikanCRState.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7E9D282251A3A080056211E /* JikanCRState.swift */; };
C7E9D286251A3AB80056211E /* JikanCRActions.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7E9D285251A3AB80056211E /* JikanCRActions.swift */; };
C7E9D289251A3B870056211E /* JikanCRReducer.swift in Sources */ = {isa = PBXBuildFile; fileRef = C7E9D288251A3B870056211E /* JikanCRReducer.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -63,7 +67,11 @@
C7985BC424E7EB4D00355C07 /* PlayState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayState.swift; sourceTree = "<group>"; };
C7985BC624E7EBE200355C07 /* PlayReducer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayReducer.swift; sourceTree = "<group>"; };
C7B6A80824E4349500D8A816 /* PlayerItem.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlayerItem.swift; sourceTree = "<group>"; };
C7BF0BF22519E1B300C34401 /* AnimeEpisodeListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnimeEpisodeListView.swift; sourceTree = "<group>"; };
C7DE028724E14A1C00B4E4F6 /* README.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = "<group>"; };
C7E9D282251A3A080056211E /* JikanCRState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JikanCRState.swift; sourceTree = "<group>"; };
C7E9D285251A3AB80056211E /* JikanCRActions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JikanCRActions.swift; sourceTree = "<group>"; };
C7E9D288251A3B870056211E /* JikanCRReducer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = JikanCRReducer.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -86,6 +94,7 @@
children = (
C730596324CD6195007D3A68 /* AppReducer.swift */,
C74D250C2519B6A100A6045D /* JikanReducer.swift */,
C7E9D288251A3B870056211E /* JikanCRReducer.swift */,
C730596A24CD62C6007D3A68 /* CRReducer.swift */,
C7985BC624E7EBE200355C07 /* PlayReducer.swift */,
);
Expand All @@ -97,6 +106,7 @@
children = (
C730596624CD6202007D3A68 /* AppState.swift */,
C74D25112519B9CF00A6045D /* JikanState.swift */,
C7E9D282251A3A080056211E /* JikanCRState.swift */,
C730596824CD6255007D3A68 /* CRState.swift */,
C7985BC424E7EB4D00355C07 /* PlayState.swift */,
);
Expand All @@ -107,6 +117,7 @@
isa = PBXGroup;
children = (
C71AED6A25184B3A0040931A /* JikanActions.swift */,
C7E9D285251A3AB80056211E /* JikanCRActions.swift */,
C730596D24CD6A24007D3A68 /* CRActions.swift */,
C7985BC224E7EA8100355C07 /* PlayActions.swift */,
);
Expand Down Expand Up @@ -168,6 +179,7 @@
C7E00DB524CC7E3C00A32036 /* SearchTab */,
C72495A624B9F1D000FF15A1 /* AnimeCrosslineRow.swift */,
C730596024CD4740007D3A68 /* AnimeDetailView.swift */,
C7BF0BF22519E1B300C34401 /* AnimeEpisodeListView.swift */,
C76FAA3B24E15A29007FBFCD /* FullscreenVideoPlayer.swift */,
);
path = Views;
Expand Down Expand Up @@ -303,19 +315,23 @@
C746DC6C24B7685100A91FFF /* SearchView.swift in Sources */,
C7985BC724E7EBE200355C07 /* PlayReducer.swift in Sources */,
C74D25122519B9CF00A6045D /* JikanState.swift in Sources */,
C7E9D286251A3AB80056211E /* JikanCRActions.swift in Sources */,
C76FAA3C24E15A29007FBFCD /* FullscreenVideoPlayer.swift in Sources */,
C7985BC324E7EA8100355C07 /* PlayActions.swift in Sources */,
C730596E24CD6A24007D3A68 /* CRActions.swift in Sources */,
C730596424CD6195007D3A68 /* AppReducer.swift in Sources */,
C71AED6B25184B3A0040931A /* JikanActions.swift in Sources */,
C7BF0BF32519E1B300C34401 /* AnimeEpisodeListView.swift in Sources */,
C746DC6A24B7678E00A91FFF /* HomeView.swift in Sources */,
C730596924CD6255007D3A68 /* CRState.swift in Sources */,
C72495A924B9F5BF00FF15A1 /* CRAnime.swift in Sources */,
C746DC6824B765F400A91FFF /* MyTabView.swift in Sources */,
C7985BC524E7EB4D00355C07 /* PlayState.swift in Sources */,
C74D250D2519B6A100A6045D /* JikanReducer.swift in Sources */,
C7E9D289251A3B870056211E /* JikanCRReducer.swift in Sources */,
C730596B24CD62C6007D3A68 /* CRReducer.swift in Sources */,
C72495A724B9F1D000FF15A1 /* AnimeCrosslineRow.swift in Sources */,
C7E9D283251A3A080056211E /* JikanCRState.swift in Sources */,
C746DC5924B75F7800A91FFF /* MyAnimeListApp.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
13 changes: 5 additions & 8 deletions MyAnimeList/Views/AnimeCrosslineRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,15 @@ struct AnimeDetailRowItem: View {
let anime: JikanAPIAnime
var body: some View {
Button(action: {
if let session = store.state.crState.session {
// store.dispatch(action: CRActions.ListCollections(sessionId: session.id,
// seriesId: anime.seriesId))
// store.dispatch(action: CRActions.ListMedia(sessionId: session.id,
// collectionId: anime.collectionId))
self.modalDisplayed = true
}
store.dispatch(action: JikanActions.Anime(id: anime.id,
request: .all,
params: nil))
self.modalDisplayed = true
}, label: {
Text(anime.title)
})
.sheet(isPresented: $modalDisplayed) {
// AnimeDetailView(anime: anime).environmentObject(store)
AnimeDetailView(anime: anime).environmentObject(store)
}
}
}
Expand Down
48 changes: 13 additions & 35 deletions MyAnimeList/Views/AnimeDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,32 @@
import SwiftUI
import SwiftUIFlux
import AVKit
import JikanSwift
import CrunchyrollSwiftWeb
import CrunchyrollSwift

struct AnimeDetailView: View {
@EnvironmentObject var store: Store<AppState>

let anime: CRAnime
let anime: JikanAPIAnime

var episodes: [CRAPIMedia] {
return store.state.crState.collections[anime.collectionId] ?? []
var collections: [CRAPICollection] {
if let seriesId = store.state.jikanCRState.malIdSeriesId[anime.id] {
return store.state.crState.series[seriesId] ?? []
}
return []
}

var body: some View {
List {
Text(anime.title)
ScrollView(.horizontal) {
LazyHStack(alignment: .center, spacing: 20) {
ForEach(episodes) {
EpisodeView(episode: $0)
ForEach(collections) { collection in
Text(collection.name)
ScrollView(.horizontal) {
if let collectionId = Int(collection.id) {
EpisodeListView(collectionId: collectionId)
}
}
}
}
}
}

struct EpisodeView: View {
@State var modalDisplayed = false

let episode: CRAPIMedia
var body: some View {
Button(action: {
self.modalDisplayed = true
if let episodeId = Int(episode.id), let session = store.state.crState.session {
store.dispatch(action: CRActions.Info(sessionId: session.id, mediaId: episodeId))
}
}, label: {
Text("\(episode.episodeNumber) \(episode.name)")
})
.sheet(isPresented: $modalDisplayed) {
if let mediaId = Int(episode.id) {
FullscreenVideoPlayer(mediaId: mediaId)
}
}
}
}

struct AnimeDetailView_Previews: PreviewProvider {
static var previews: some View {
AnimeDetailView(anime: CRAnime(id: 1, title: "First", seriesId: 1, collectionId: 1))
}
}
54 changes: 54 additions & 0 deletions MyAnimeList/Views/AnimeEpisodeListView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// AnimeEpisodeListView.swift
// MyAnimeList
//
// Created by HG on 2020/09/22.
//

import SwiftUI
import SwiftUIFlux
import CrunchyrollSwift

struct EpisodeListView: View {
@EnvironmentObject var store: Store<AppState>

let collectionId: Int

var episodes: [CRAPIMedia] {
return store.state.crState.collections[collectionId] ?? []
}

var body: some View {
LazyHStack(alignment: .center, spacing: 20) {
ForEach(episodes) {
EpisodeView(episode: $0)
}
}
.onAppear() {
if let session = store.state.crState.session {
store.dispatch(action: CRActions.ListMedia(sessionId: session.id, collectionId: collectionId))
}
}
}
}

struct EpisodeView: View {
@State var modalDisplayed = false

let episode: CRAPIMedia
var body: some View {
Button(action: {
self.modalDisplayed = true
if let episodeId = Int(episode.id), let session = store.state.crState.session {
store.dispatch(action: CRActions.Info(sessionId: session.id, mediaId: episodeId))
}
}, label: {
Text("\(episode.episodeNumber) \(episode.name)")
})
.sheet(isPresented: $modalDisplayed) {
if let mediaId = Int(episode.id) {
FullscreenVideoPlayer(mediaId: mediaId)
}
}
}
}
16 changes: 16 additions & 0 deletions MyAnimeList/flux/actions/JikanCRActions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// JikanCRActions.swift
// MyAnimeList
//
// Created by HG on 2020/09/22.
//

import Foundation
import SwiftUIFlux

struct JikanCRActions {
struct SetMalIdSeriesId: Action {
let malId: Int
let seriesId: Int
}
}
1 change: 1 addition & 0 deletions MyAnimeList/flux/reducers/AppReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import SwiftUIFlux
func appStateReducer(state: AppState, action: Action) -> AppState {
var state = state
state.jikanState = jikanStateReducer(state: state.jikanState, action: action)
state.jikanCRState = jikanCRStateReducer(state: state.jikanCRState, action: action)
state.crState = crStateReducer(state: state.crState, action: action)
state.playState = playStateReducer(state: state.playState, action: action)
return state
Expand Down
20 changes: 20 additions & 0 deletions MyAnimeList/flux/reducers/JikanCRReducer.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// JikanCRReducer.swift
// MyAnimeList
//
// Created by HG on 2020/09/22.
//

import Foundation
import SwiftUIFlux

func jikanCRStateReducer(state: JikanCRState, action: Action) -> JikanCRState {
var state = state
switch action {
case let action as JikanCRActions.SetMalIdSeriesId:
state.malIdSeriesId[action.malId] = action.seriesId
default:
break
}
return state
}
12 changes: 12 additions & 0 deletions MyAnimeList/flux/reducers/JikanReducer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@

import Foundation
import SwiftUIFlux
import CrunchyrollSwiftWeb

func jikanStateReducer(state: JikanState, action: Action) -> JikanState {
var state = state
switch action {
case let action as JikanActions.SetAnime:
state.animes[action.malID] = action.response

// TODO: improve
if let titleEnglish = action.response.titleEnglish {
let nameToken = CRNameParser.nameToken(titleEnglish)
if let session = store.state.crState.session,
let seriesID = CRWebParser.seriesId(nameToken) {
store.dispatch(action: JikanCRActions.SetMalIdSeriesId(malId: action.response.id, seriesId: seriesID))
store.dispatch(action: CRActions.ListCollections(sessionId: session.id,
seriesId: seriesID))
}
}
case let action as JikanActions.SetTop:
if action.page == 1 {
state.top = action.response
Expand Down
2 changes: 2 additions & 0 deletions MyAnimeList/flux/state/AppState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import SwiftUIFlux

struct AppState: FluxState, Codable {
var jikanState: JikanState
var jikanCRState: JikanCRState
var crState: CRState
var playState: PlayState

init() {
self.jikanState = JikanState()
self.jikanCRState = JikanCRState()
self.crState = CRState()
self.playState = PlayState()
}
Expand Down
14 changes: 14 additions & 0 deletions MyAnimeList/flux/state/JikanCRState.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// JikanCRState.swift
// MyAnimeList
//
// Created by HG on 2020/09/22.
//

import Foundation
import SwiftUIFlux
import JikanSwift

struct JikanCRState: FluxState, Codable {
var malIdSeriesId: [Int: Int] = [:]
}

0 comments on commit 1232fbb

Please sign in to comment.