diff --git a/Moco/View/User/EpisodeView.swift b/Moco/View/User/EpisodeView.swift index 90d72a2..b98770d 100644 --- a/Moco/View/User/EpisodeView.swift +++ b/Moco/View/User/EpisodeView.swift @@ -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!) diff --git a/Moco/ViewModel/EpisodeViewModel.swift b/Moco/ViewModel/EpisodeViewModel.swift index 4647130..81683bd 100644 --- a/Moco/ViewModel/EpisodeViewModel.swift +++ b/Moco/ViewModel/EpisodeViewModel.swift @@ -12,6 +12,7 @@ import SwiftData static let shared = EpisodeViewModel() var selectedEpisode: EpisodeModel? + var indexEpisodePlay: Int? var episodes: [EpisodeModel]? init(modelContext: ModelContext? = nil) { @@ -21,8 +22,9 @@ import SwiftData } } - func setSelectedEpisode(_ episode: EpisodeModel) { + func setSelectedEpisode(_ episode: EpisodeModel, _ indexEpisode: Int) { selectedEpisode = episode + indexEpisodePlay = indexEpisode } func fetchEpisodes(storyThemeId: String) { @@ -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() + } } } diff --git a/Moco/ViewModel/UserViewModel.swift b/Moco/ViewModel/UserViewModel.swift index fb0b406..00d0c17 100644 --- a/Moco/ViewModel/UserViewModel.swift +++ b/Moco/ViewModel/UserViewModel.swift @@ -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() + } } }