view GeoQuiz/ProfileModalView.swift @ 17:8dac58bb4569

fix build bug
author Dennis C. M. <dennis@denniscm.com>
date Thu, 20 Oct 2022 18:07:51 +0200
parents 1011e56b7832
children f140bb277c96
line wrap: on
line source

//
//  ProfileModalView.swift
//  GeoQuiz
//
//  Created by Dennis Concepción Martín on 25/9/22.
//

import SwiftUI
import PhotosUI

struct ProfileModalView: View {
    @ObservedObject var user: User
    @ObservedObject var storeKitRC: StoreKitRC
    
    @Environment(\.dismiss) var dismiss
    @Environment(\.managedObjectContext) var moc
    
    @FetchRequest(sortDescriptors: [
        SortDescriptor(\.date, order: .reverse),
    ]) var playedGames: FetchedResults<PlayedGame>
    
    @State private var showingEditModalView = false
    
    var body: some View {
        NavigationView {
            List {
                Section {
                    UserProfile(user: user, storeKitRC: storeKitRC)
                }
                
                Section {
                    UserProgress(playedGames: playedGames, gameType: .guessTheFlag)
                    UserProgress(playedGames: playedGames, gameType: .guessTheCapital)
                    UserProgress(playedGames: playedGames, gameType: .guessTheCountry)
                    UserProgress(playedGames: playedGames, gameType: .guessThePopulation)
                } header: {
                    Text("Progress")
                }
                
                Section {
                    ForEach(playedGames) { playedGame in
                        RecentGame(game: playedGame)
                    }
                    .onDelete(perform: deleteGame)
                } header: {
                    Text("Recent games")
                }
            }
            .background(.customBackground)
            .navigationTitle("Profile")
            .navigationBarTitleDisplayMode(.inline)
            .toolbar {
                ToolbarItem(placement: .cancellationAction) {
                    Button {
                        dismiss()
                    } label: {
                        Label("Exit", systemImage: "multiply")
                    }
                }
                
                ToolbarItem(placement: .navigationBarTrailing) {
                    Button("Edit") {
                        showingEditModalView = true
                    }
                }
            }
            
            .sheet(isPresented: $showingEditModalView) {
                ProfileEditModalView(user: user)
            }
        }
        
    }
    
    private func deleteGame(at offsets: IndexSet) {
        for offset in offsets {
            let game = playedGames[offset]
            moc.delete(game)
        }
        
        try? moc.save()
    }
}

struct ProfileView_Previews: PreviewProvider {
    static var previews: some View {
        ProfileModalView(user: User(), storeKitRC: StoreKitRC())
            .environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
    }
}