com.sweetiepiggy.littlepro.SubmitQuizFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.sweetiepiggy.littlepro.SubmitQuizFragment.java

Source

/*
Copyright (C) 2014-2015 Sweetie Piggy Apps <sweetiepiggyapps@gmail.com>
    
This file is part of Little Pro.
    
Little Pro is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
    
Little Pro is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Little Pro; if not, see <http://www.gnu.org/licenses/>.
*/

package com.sweetiepiggy.littlepro;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class SubmitQuizFragment extends Fragment
        implements View.OnClickListener, UploadQuizAnswersTask.OnUploadedListener {
    private SubmitQuizListener mSubmitQuizListener = null;
    private List<TextView> mAnswerViews = null;
    private Map<Long, String> mQuestionIdAndAnswers = null;
    private String mEmail;
    private long mCourseId = -1;
    private long mSubjectId = -1;
    private long mQuizId = -1;
    private boolean mSubmitted = false;

    private final String ARG_EMAIL = "email";

    public interface SubmitQuizListener {
        public List<Pair<Question, String>> getQuestionAndAnswers();

        public void onSubmit(Map<Long, String> questionIdAndCorrectAnswers);
    }

    public static SubmitQuizFragment newInstance(SubmitQuizListener submitQuizListener, String email, long courseId,
            long subjectId, long quizId, boolean submitted) {
        SubmitQuizFragment fragment = new SubmitQuizFragment();
        fragment.mSubmitQuizListener = submitQuizListener;
        fragment.mAnswerViews = new ArrayList<TextView>();
        fragment.mQuestionIdAndAnswers = new HashMap<Long, String>();
        fragment.mEmail = email;
        fragment.mCourseId = courseId;
        fragment.mSubjectId = subjectId;
        fragment.mQuizId = quizId;
        fragment.mSubmitted = submitted;

        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState == null) {
            Bundle bundle = getArguments();
            if (bundle != null) {
                loadState(bundle);
            }
        } else {
            loadState(savedInstanceState);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putString(ARG_EMAIL, mEmail);
        savedInstanceState.putLong("courseId", mCourseId);
        savedInstanceState.putLong("subjectId", mSubjectId);
        savedInstanceState.putLong("quizId", mQuizId);
        savedInstanceState.putBoolean("submitted", mSubmitted);
        super.onSaveInstanceState(savedInstanceState);
    }

    private void loadState(Bundle bundle) {
        mEmail = bundle.getString(ARG_EMAIL);
        mCourseId = bundle.getLong("courseId", -1);
        mSubjectId = bundle.getLong("subjectId", -1);
        mQuizId = bundle.getLong("quizId", -1);
        mSubmitted = bundle.getBoolean("submitted", false);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_submit_quiz, container, false);
        ((Button) view.findViewById(R.id.submit_button)).setOnClickListener(this);
        ViewGroup questionsLayout = (ViewGroup) view.findViewById(R.id.questions_layout);

        if (mSubmitQuizListener != null) {
            for (Pair<Question, String> questionAndAnswer : mSubmitQuizListener.getQuestionAndAnswers()) {
                Question question = questionAndAnswer.first;
                String answer = questionAndAnswer.second;
                TextView answerView = new TextView(getActivity());
                answerView.setText(answer);
                mAnswerViews.add(answerView);
                questionsLayout.addView(answerView);
                mQuestionIdAndAnswers.put(question.getId(), answer);
            }
        }

        if (mSubmitted) {
            view.findViewById(R.id.submit_button).setVisibility(View.GONE);
        }

        return view;
    }

    @Override
    public void onClick(View view) {
        UploadQuizAnswersTask uploadQuizAnswersTask = new UploadQuizAnswersTask(getActivity(), this, mEmail,
                mCourseId, mSubjectId, mQuizId, mQuestionIdAndAnswers);
        uploadQuizAnswersTask.execute();
    }

    public void onUploaded(Map<Long, String> questionIdAndCorrectAnswers) {
        mSubmitted = true;
        boolean allowUnlock = update(questionIdAndCorrectAnswers);
        mSubmitQuizListener.onSubmit(questionIdAndCorrectAnswers);

        DbAdapter dbAdapter = new DbAdapter();
        dbAdapter.openReadWrite(getActivity().getApplicationContext());
        dbAdapter.setAllowUnlock(allowUnlock);
        dbAdapter.close();
    }

    public void update() {
        update(null);
    }

    private boolean update(Map<Long, String> questionIdAndCorrectAnswers) {
        View view = getView();
        if (view == null) {
            boolean allowUnlock = false;
            return allowUnlock;
        }

        ViewGroup questionsLayout = (ViewGroup) view.findViewById(R.id.questions_layout);
        mAnswerViews.clear();
        questionsLayout.removeAllViews();

        int correctPoints = 0;
        int totalPoints = 0;

        for (Pair<Question, String> questionAndAnswer : mSubmitQuizListener.getQuestionAndAnswers()) {
            Question question = questionAndAnswer.first;
            String answer = questionAndAnswer.second;
            String correctAnswer = null;
            int points = question.getPoints();
            totalPoints += points;
            if (questionIdAndCorrectAnswers != null) {
                correctAnswer = questionIdAndCorrectAnswers.get(question.getId());
            }
            TextView answerView = new TextView(getActivity());
            if (mSubmitted) {
                if (answer.equals(correctAnswer)) {
                    answerView.setText(answer + ": Correct");
                    correctPoints += points;
                } else {
                    answerView.setText(answer + ": Wrong");
                }
            } else {
                answerView.setText(answer);
            }
            mAnswerViews.add(answerView);
            questionsLayout.addView(answerView);
            mQuestionIdAndAnswers.put(question.getId(), answer);
        }

        if (mSubmitted) {
            view.findViewById(R.id.submit_button).setVisibility(View.GONE);
        }

        double percentCorrect = (totalPoints == 0) ? 0. : (100. * correctPoints) / totalPoints;
        boolean allowUnlock = percentCorrect >= 75.;
        return allowUnlock;
    }
}