Skip to content

Instantly share code, notes, and snippets.

@rifqifadh
Last active August 3, 2021 07:40
Show Gist options
  • Select an option

  • Save rifqifadh/d5f27195abc9b5d279290b6a8c2192ae to your computer and use it in GitHub Desktop.

Select an option

Save rifqifadh/d5f27195abc9b5d279290b6a8c2192ae to your computer and use it in GitHub Desktop.
//
// DetailViewModel.swift
// Games Catalog
//
// Created by Rifqi Fadh on 04/07/20.
//
import Foundation
import CoreData
import SwiftUI
class DetailViewModel: ObservableObject {
let game: Games
@Published var detailGames: DetailGame?
@Published var screenshots: [Screenshot] = []
@Published var loading: Bool = false
@Published var isFav: Bool = false
init(game: Games) {
self.game = game
}
deinit {
self.detailGames = nil
}
func getDetailMovie() {
loading = true
if let id = game.id {
RawgService.fetch(from: .detail(id: id), response: DetailGame.self) { [weak self](response) in
if let game = response {
DispatchQueue.main.async {
self?.detailGames = game
}
}
}
RawgService.fetch(from: .screenshots(id: id), response: ScreenshotResponse.self) { [weak self](response) in
if let response = response {
DispatchQueue.main.async {
self?.screenshots = response.results
}
}
}
} else {
self.detailGames = nil
}
loading = false
}
func saveToFavorite(_ context: NSManagedObjectContext) {
let fav = Favorite(context: context)
fav.id = Int32(game.id ?? 0)
fav.name = game.name
fav.genre = game.genre
fav.backgroundImage = game.backgroundImage
do {
try context.save()
isFav = true
} catch {
fatalError("Error Save data")
}
}
func checkIsFav(_ entity: FetchedResults<Favorite>) {
debugPrint("Results: \(entity)")
if entity.count > 0 {
isFav = true
} else {
isFav = false
}
}
func deleteFav(from entity: FetchedResults<Favorite>,_ context: NSManagedObjectContext) {
context.delete(entity[0])
isFav = false
do {
try context.save()
} catch {
fatalError("Error Delete Favorite")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment