com.sweetiepiggy.littlepro.QuizActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.sweetiepiggy.littlepro.QuizActivity.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.List;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Pair;

public class QuizActivity extends LockedActionBarActivity implements DownloadQuestionListTask.OnDownloadedListener,
        ViewPager.OnPageChangeListener, SubmitQuizFragment.SubmitQuizListener {
    private QuizPagerAdapter mQuizPagerAdapter = null;
    private List<Long> mQuestionIds = null;
    private String mEmail = null;
    private long mCourseId = -1;
    private long mSubjectId = -1;
    private long mQuizId = -1;
    private boolean mSubmitted;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quiz);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        if (savedInstanceState == null) {
            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                loadState(bundle);
            }
        } else {
            loadState(savedInstanceState);
        }

        DownloadQuestionListTask downloadQuestionListTask = new DownloadQuestionListTask(this, this, mCourseId,
                mSubjectId, mQuizId);
        downloadQuestionListTask.execute();
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putString("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("email");
        mCourseId = bundle.getLong("courseId", -1);
        mSubjectId = bundle.getLong("subjectId", -1);
        mQuizId = bundle.getLong("quizId", -1);
        mSubmitted = bundle.getBoolean("submitted", false);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        loadState(savedInstanceState);
    }

    public void onDownloaded(List<Question> questions) {
        mQuizPagerAdapter = new QuizPagerAdapter(questions, getSupportFragmentManager(), this, this, mEmail,
                mCourseId, mSubjectId, mQuizId, mSubmitted);
        if (mSubmitted) {
            onSubmit(new HashMap<Long, String>());
        }
        ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(mQuizPagerAdapter);
        viewPager.setOnPageChangeListener(this);

        onPageSelected(viewPager.getCurrentItem());
    }

    @Override
    public void onSubmit(Map<Long, String> questionIdAndCorrectAnswers) {
        mSubmitted = true;
        mQuizPagerAdapter.onSubmit(questionIdAndCorrectAnswers);
    }

    @Override
    public void onPageSelected(int position) {
        setTitle(mQuizPagerAdapter.getPageTitle(position));
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public List<Pair<Question, String>> getQuestionAndAnswers() {
        return mQuizPagerAdapter.getQuestionAndAnswers();
    }

    private static class QuizPagerAdapter extends FragmentStatePagerAdapter {
        private List<QuestionFragment> mQuestionFragments;
        private SubmitQuizFragment mSubmitQuizFragment;
        private Context mContext;
        private boolean mSubmitted;

        public QuizPagerAdapter(List<Question> questions, FragmentManager fm, Context context,
                SubmitQuizFragment.SubmitQuizListener submitQuizListener, String email, long courseId,
                long subjectId, long quizId, boolean submitted) {
            super(fm);
            mQuestionFragments = new ArrayList<QuestionFragment>();
            mSubmitted = submitted;
            if (questions != null) {
                for (Question question : questions) {
                    mQuestionFragments.add(QuestionFragment.newInstance(question, mSubmitted));
                }
            }
            mSubmitQuizFragment = SubmitQuizFragment.newInstance(submitQuizListener, email, courseId, subjectId,
                    quizId, submitted);
            mContext = context;
        }

        @Override
        public Fragment getItem(int position) {
            if (position == mQuestionFragments.size()) {
                return mSubmitQuizFragment;
            } else {
                QuestionFragment questionFragment = mQuestionFragments.get(position);
                //            if (mSubmitted) {
                //               questionFragment.onSubmit();
                //            }
                return questionFragment;
            }
        }

        @Override
        public int getCount() {
            return mQuestionFragments.size() + 1;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Resources resources = mContext.getResources();
            if (position == mQuestionFragments.size()) {
                mSubmitQuizFragment.update();
                return resources.getString(R.string.submit_quiz);
            } else {
                return resources.getString(R.string.question) + " " + (position + 1) + " "
                        + resources.getString(R.string.of) + " " + mQuestionFragments.size();
            }
        }

        public List<Pair<Question, String>> getQuestionAndAnswers() {
            List<Pair<Question, String>> questionAndAnswers = new ArrayList<Pair<Question, String>>();
            for (QuestionFragment questionFragment : mQuestionFragments) {
                Question question = questionFragment.getQuestion();
                if (question != null) {
                    String answer = questionFragment.getAnswer();
                    questionAndAnswers.add(new Pair<Question, String>(question, answer));
                }
            }

            return questionAndAnswers;
        }

        public void onSubmit(Map<Long, String> questionIdAndCorrectAnswers) {
            mSubmitted = true;
            for (QuestionFragment questionFragment : mQuestionFragments) {
                Long questionId = questionFragment.getQuestionId();
                String correctAnswer = questionIdAndCorrectAnswers.get(questionId);
                questionFragment.onSubmit(correctAnswer);
            }
        }
    }
}