Skip to content

Commit

Permalink
Merge pull request #111 from moco-team/feat/fixing-index-play-episode
Browse files Browse the repository at this point in the history
feat: fixing indexPlayEpisode
  • Loading branch information
LordRonz authored Nov 27, 2023
2 parents a90879d + 35ffff8 commit 1f32573
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Moco/View/User/EpisodeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct EpisodeView: View {
) {
if episode.isAvailable || index < userViewModel.userLogin!.availableEpisodeSum {
Task {
episodeViewModel.setSelectedEpisode(episode)
episodeViewModel.setSelectedEpisode(episode, index)

// open new story page
storyViewModel.fetchStory(0, episodeViewModel.selectedEpisode!)
Expand Down
10 changes: 7 additions & 3 deletions Moco/ViewModel/EpisodeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import SwiftData
static let shared = EpisodeViewModel()

var selectedEpisode: EpisodeModel?
var indexEpisodePlay: Int?
var episodes: [EpisodeModel]?

init(modelContext: ModelContext? = nil) {
Expand All @@ -21,8 +22,9 @@ import SwiftData
}
}

func setSelectedEpisode(_ episode: EpisodeModel) {
func setSelectedEpisode(_ episode: EpisodeModel, _ indexEpisode: Int) {
selectedEpisode = episode
indexEpisodePlay = indexEpisode
}

func fetchEpisodes(storyThemeId: String) {
Expand Down Expand Up @@ -51,8 +53,10 @@ import SwiftData
fetchEpisodes(storyThemeId: selectedStoryTheme.uid)

if let availableEpisode = fetchAvailableEpisodes(storyThemeId: selectedStoryTheme.uid) {
episodes![availableEpisode.count].isAvailable = true
try? modelContext?.save()
if EpisodeViewModel.shared.indexEpisodePlay == availableEpisode.count - 1 {
episodes![availableEpisode.count].isAvailable = true
try? modelContext?.save()
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions Moco/ViewModel/UserViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ import SwiftData

func addingAvailableEpisode() {
if let userLogin = userLogin {
userLogin.availableEpisodeSum += 1
try? modelContext?.save()
if EpisodeViewModel.shared.indexEpisodePlay == userLogin.availableEpisodeSum - 1 {
userLogin.availableEpisodeSum += 1
try? modelContext?.save()
}
}
}

Expand Down

0 comments on commit 1f32573

Please sign in to comment.