import React from "react" import { View, ScrollView, StyleSheet, StatusBar, Text, SafeAreaView, Dimensions, ImageBackground, BackHandler } from "react-native" import { AdMobBanner } from "expo-ads-admob" import { Button, ButtonContainer } from "../components/Button" import { texts, colors, examScheme, credentials } from "../components/Variables" import aerodynamicsQuestions from "../data/aerodynamics" import firstAidQuestions from "../data/firstAid" import flightSafetyQuestions from "../data/flightSafety" import instrumentsQuestions from "../data/instruments" import legislationQuestions from "../data/legislation" import materialsQuestions from "../data/materials" import meteorologyQuestions from "../data/meteorology" import physiopathologyQuestions from "../data/physiopathology" import pilotingTechniquesQuestions from "../data/pilotingTechniques" const allQuestions = { aerodynamics: aerodynamicsQuestions, firstAid: firstAidQuestions, flightSafety: flightSafetyQuestions, instruments: instrumentsQuestions, legislation: legislationQuestions, materials: materialsQuestions, meteorology: meteorologyQuestions, physiopathology: physiopathologyQuestions, pilotingTechniques: pilotingTechniquesQuestions } const bgImage = require("../assets/bg.jpg") const screen = Dimensions.get("window") const styles = StyleSheet.create({ container: { flex: 1 }, text: { color: colors.white, fontSize: 20, textAlign: "center", fontWeight: "600", paddingTop: 5, paddingBottom: 20 }, textCode: { color: colors.white, fontSize: 12, textAlign: "center", fontWeight: "500", paddingTop: 20, paddingBottom: 0 }, textAnswer: { color: colors.black, backgroundColor: colors.white, fontSize: 18, textAlign: "center", fontWeight: "400", marginTop: 20, paddingVertical: 20, paddingHorizontal: 20, borderRadius: 10 }, safearea: { flex: 1, paddingHorizontal: 20, paddingTop: 40, justifyContent: "space-between" }, box: { width: screen.width, paddingVertical: 10, overflow: "hidden" }, scrollView: { //margin: 10, height: screen.height-150 }, bg: { width: "100%", height: "100%" }, bannerContainer: { flex: 1, alignItems: "center", justifyContent: "center" }, banner: { flex: 1, alignItems: "center", justifyContent: "center", width: 320 } }) class Quiz extends React.Component { state = { correctCount: 0, wrongCount: 0, wrongAnswers: [], totalCount: this.props.navigation.getParam("questions", []).length, availableIds: this.props.navigation.getParam("questions", []).map(a => a.id), activeQuestionId: this.props.navigation.getParam("questions", [])[ Math.floor(Math.random() * this.props.navigation.getParam("questions", []).length) ].id, activeAnswerId: Math.floor(Math.random() * 3), clickedAnswer: false, answered: false, results: false } bannerError = (e) => { //console.log("Banner error (footer): ", e) } componentDidMount() { BackHandler.addEventListener('hardwareBackPress', this.handleBackButton) } componentWillUnmount() { BackHandler.removeEventListener('hardwareBackPress', this.handleBackButton) } handleBackButton = () => { let tmpQuestions = [] let fullQuestions = [] examScheme.forEach( (elem) => { let currentSection = allQuestions[elem.section] for(let i=0; i index != currentIndex) } this.props.navigation.navigate("Splash", { trueFalseQuestions: tmpQuestions }) return true } answer = (answer, correct, id, question) => { this.setState( state => { const nextState = { answered: true, clickedId: id, clickedAnswer: answer } if ((correct && answer) || (!correct && !answer)) { //console.log('ok') nextState.correctCount = state.correctCount + 1 } else { //console.log('ko') nextState.wrongCount = state.wrongCount + 1 nextState.wrongAnswers = state.wrongAnswers nextState.wrongAnswers.push( { question: question.question, id: question.id, clicked: question.answers[state.activeAnswerId].id, answers: question.answers } ) } return nextState }, () => { setTimeout(() => this.nextQuestion(), 2000) } ) } nextQuestion = () => { const updatedIndexes = this.state.availableIds.filter( item => item != this.state.activeQuestionId) const nextId = updatedIndexes[Math.floor(Math.random() * updatedIndexes.length)] if (!updatedIndexes.length) { //console.log(this.state.wrongAnswers) this.props.navigation.navigate("ResultsTrueFalse", { results: { isExam: false, total: this.state.totalCount, correct: this.state.correctCount, wrong: this.state.wrongCount, wrongAnswers: this.state.wrongAnswers } }) } else { this.setState( (state) => { return { availableIds: updatedIndexes, activeQuestionId: nextId, activeAnswerId: Math.floor(Math.random() * 3), answered: false } }) } } render() { const questions = this.props.navigation.getParam("questions", []) const question = questions.filter(item => item.id == this.state.activeQuestionId)[0] || questions[0] const randomAnswer = question.answers[this.state.activeAnswerId] //console.log({id: randomAnswer.id, clicked: this.state.clickedId, answered: this.state.answered, isCorrect: randomAnswer.correct || false}) return ( {!this.state.results ? {question.id} {question.question} {randomAnswer.text}