Oddbean new post about | logout
  --
//  ContentView.swift
//  Trivia_App
//
//  Created by Maciej Zak on 12/05/19.
//

import SwiftUI

struct ContentView: View {
    @State private var score = 0
    @State private var isCorrect = false

    let categories = ["Science", "History", "Geography", "Art", "Music"]

    var body: some View {
        NavigationView {
            ScrollView {
                VStack(spacing: 10) {
                    Text("Welcome to Trivia App!")
                        .font(.title)
                        .padding()

                    ForEach(categories, id: \.self) { category in
                        SectionHeader(category: category)

                        ScrollView {
                            ForEach(questionsByCategory[category] ?? [], id: \.0) { (index, question) in
                                QuestionView(question: question, isCorrect: $isCorrect, score: $score)
                                    .onReceive(isCorrect.$isCorrect) { _ in
                                        if isCorrect {
                                            score += 1
                                            isCorrect = false
                                        }
                                    }
                                .padding()
                            }
                        }
                    }

                    Text("\(score) out of \(categories.count)")
                        .font(.title2)
                        .padding()
                }
                .onAppear {
                    isCorrect = false
                }
            }
            .navigationBarTitle("Trivia App")
        }
    }
}