//
//  LatestGamePickupsView.swift
//  Zockerhoehle
//
//  Created by Julian-Steffen Müller on 24.09.19.
//  Copyright © 2019 Julian-Steffen Müller. All rights reserved.
//

import SwiftUI
import CoreData

struct GamePickupsView: View {
    @Environment(\.managedObjectContext) private var viewContext
    @FetchRequest var games: FetchedResults<Game>
    //@ObservedObject var gameStore = GameStore(sortDescriptors: [NSSortDescriptor(key: "createdAt", ascending: false), NSSortDescriptor(key: "name", ascending: true)], fetchLimit: 50)
    
    init() {
        let gamesFR = NSFetchRequest<Game>(entityName: "Game")
        gamesFR.sortDescriptors = [NSSortDescriptor(key: "createdAt", ascending: false), NSSortDescriptor(key: "name", ascending: true)]
        gamesFR.fetchLimit = 50;
        _games = FetchRequest(fetchRequest: gamesFR)
    }
    
    var body: some View {
        List(games) { game in
            NavigationLink(destination: GameDetailView(game: game)) {
                Text("\(game.name)")
            }
        }
        .padding(.top) // Workaround, damit die Liste beim scrollen nicht unter der NavigationView angezeigt wird
        .navigationBarTitle(Text("Zuletzt gehortete Spiele"))
    }
}
