ir.isilearning.lmsapp.fragment.QuizFragment.java Source code

Java tutorial

Introduction

Here is the source code for ir.isilearning.lmsapp.fragment.QuizFragment.java

Source

/*
 * Copyright 2015 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ir.isilearning.lmsapp.fragment;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.animation.FastOutLinearInInterpolator;
import android.support.v7.app.AlertDialog;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterViewAnimator;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

import ir.isilearning.lmsapp.R;
import ir.isilearning.lmsapp.activity.QuizActivity;
import ir.isilearning.lmsapp.adapter.QuizAdapter;
import ir.isilearning.lmsapp.adapter.ScoreAdapter;
import ir.isilearning.lmsapp.helper.ApiLevelHelper;
import ir.isilearning.lmsapp.helper.ConnectionHelper;
import ir.isilearning.lmsapp.helper.JsonHelper;
import ir.isilearning.lmsapp.helper.PreferencesHelper;
import ir.isilearning.lmsapp.helper.ServerAnswerHelper;
import ir.isilearning.lmsapp.model.Answer;
import ir.isilearning.lmsapp.model.Category;
import ir.isilearning.lmsapp.model.Player;
import ir.isilearning.lmsapp.model.Theme;
import ir.isilearning.lmsapp.model.quiz.Quiz;
import ir.isilearning.lmsapp.persistence.LMSAppDatabaseHelper;
import ir.isilearning.lmsapp.webservice.UserUtils;
import ir.isilearning.lmsapp.webservice.caller.SendUserQuestionAnswersCaller;
import ir.isilearning.lmsapp.webservice.gmodel.gLogin;
import ir.isilearning.lmsapp.widget.AvatarView;
import ir.isilearning.lmsapp.widget.quiz.AbsQuizView;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Encapsulates Quiz solving and displays it to the user.
 */
public class QuizFragment extends android.support.v4.app.Fragment {

    private static final String KEY_USER_INPUT = "USER_INPUT";
    private TextView mProgressText;
    private int mQuizSize;
    private ProgressBar mProgressBar;
    private Category mCategory;
    private AdapterViewAnimator mQuizView;
    private ScoreAdapter mScoreAdapter;
    private QuizAdapter mQuizAdapter;
    private SolvedStateListener mSolvedStateListener;
    private TextView mExamTimer;

    public static QuizFragment newInstance(String categoryId, SolvedStateListener solvedStateListener) {
        if (categoryId == null) {
            throw new IllegalArgumentException("The category can not be null");
        }
        Bundle args = new Bundle();
        args.putString(Category.TAG, categoryId);
        QuizFragment fragment = new QuizFragment();
        if (solvedStateListener != null) {
            fragment.mSolvedStateListener = solvedStateListener;
        }
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        String categoryId = getArguments().getString(Category.TAG);
        mCategory = LMSAppDatabaseHelper.getCategoryWith(getActivity(), categoryId);

        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        // Create a themed Context and custom LayoutInflater
        // to get nicely themed views in this Fragment.
        final Theme theme = mCategory.getTheme();
        final ContextThemeWrapper context = new ContextThemeWrapper(getActivity(), theme.getStyleId());
        final LayoutInflater themedInflater = LayoutInflater.from(context);
        return themedInflater.inflate(R.layout.fragment_quiz, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mQuizView = (AdapterViewAnimator) view.findViewById(R.id.quiz_view);
        decideOnViewToDisplay();
        setQuizViewAnimations();
        final AvatarView avatar = (AvatarView) view.findViewById(R.id.avatar);
        setAvatarDrawable(avatar);
        initProgressToolbar(view);
        super.onViewCreated(view, savedInstanceState);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void setQuizViewAnimations() {
        if (ApiLevelHelper.isLowerThan(Build.VERSION_CODES.LOLLIPOP)) {
            return;
        }
        mQuizView.setInAnimation(getActivity(), R.animator.slide_in_bottom);
        mQuizView.setOutAnimation(getActivity(), R.animator.slide_out_top);
    }

    private void initProgressToolbar(View view) {
        final int firstUnsolvedQuizPosition = mCategory.getFirstUnsolvedQuizPosition();
        final List<Quiz> quizzes = mCategory.getQuizzes();
        mQuizSize = quizzes.size();
        mProgressText = (TextView) view.findViewById(R.id.progress_text);
        mProgressBar = ((ProgressBar) view.findViewById(R.id.progress));
        mExamTimer = (TextView) view.findViewById(R.id.Timer);
        long millis = TimeUnit.MINUTES.toMillis(mCategory.getCategoryRemainTime());
        new CountDownTimer(millis, 1000) {
            @SuppressLint("DefaultLocale")
            @Override
            public void onTick(long millisUntilFinished) {
                mExamTimer.setText(String.format("%02d:%02d",
                        new Object[] { TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                                TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) - TimeUnit.MINUTES
                                        .toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished)) }));
            }

            @Override
            public void onFinish() {
                mExamTimer.setText("Time's UP!");
            }
        }.start();
        mProgressBar.setMax(mQuizSize);
        setProgress(firstUnsolvedQuizPosition);

    }

    private void setProgress(int currentQuizPosition) {
        if (!isAdded()) {
            return;
        }
        mProgressText.setText(getString(R.string.quiz_of_quizzes, currentQuizPosition, mQuizSize));
        mProgressBar.setProgress(currentQuizPosition);
    }

    @SuppressWarnings("ConstantConditions")
    private void setAvatarDrawable(AvatarView avatarView) {
        Player player = PreferencesHelper.getPlayer(getActivity());
        avatarView.setAvatar(player.getAvatar().getDrawableId());
        ViewCompat.animate(avatarView).setInterpolator(new FastOutLinearInInterpolator()).setStartDelay(500)
                .scaleX(1).scaleY(1).start();
    }

    public void decideOnViewToDisplay() {
        final boolean isSolved = mCategory.isSolved();
        if (isSolved) {
            showSummary();
            if (null != mSolvedStateListener) {
                mSolvedStateListener.onCategorySolved();
            }
        } else {
            mQuizView.setAdapter(getQuizAdapter());
            mQuizView.setSelection(mCategory.getFirstUnsolvedQuizPosition());
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        View focusedChild = mQuizView.getFocusedChild();
        if (focusedChild instanceof ViewGroup) {
            View currentView = ((ViewGroup) focusedChild).getChildAt(0);
            if (currentView instanceof AbsQuizView) {
                outState.putBundle(KEY_USER_INPUT, ((AbsQuizView) currentView).getUserInput());
            }
        }
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onViewStateRestored(Bundle savedInstanceState) {
        restoreQuizState(savedInstanceState);
        super.onViewStateRestored(savedInstanceState);
    }

    private void restoreQuizState(final Bundle savedInstanceState) {
        if (null == savedInstanceState) {
            return;
        }
        mQuizView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop,
                    int oldRight, int oldBottom) {
                mQuizView.removeOnLayoutChangeListener(this);
                View currentChild = mQuizView.getChildAt(0);
                if (currentChild instanceof ViewGroup) {
                    final View potentialQuizView = ((ViewGroup) currentChild).getChildAt(0);
                    if (potentialQuizView instanceof AbsQuizView) {
                        ((AbsQuizView) potentialQuizView)
                                .setUserInput(savedInstanceState.getBundle(KEY_USER_INPUT));
                    }
                }
            }
        });

    }

    private QuizAdapter getQuizAdapter() {
        if (null == mQuizAdapter) {
            mQuizAdapter = new QuizAdapter(getActivity(), mCategory);
        }
        return mQuizAdapter;
    }

    /**
     * Displays the next page.
     *
     * @return <code>true</code> if there's another quiz to solve, else <code>false</code>.
     */
    public boolean showNextPage() {
        if (null == mQuizView) {
            return false;
        }
        int nextItem = mQuizView.getDisplayedChild() + 1;
        setProgress(nextItem);
        final int count = mQuizView.getAdapter().getCount();
        if (nextItem < count) {
            mQuizView.showNext();
            LMSAppDatabaseHelper.updateCategory(getActivity(), mCategory); //saving answers to database
            return true;
        }
        markCategorySolved();
        return false;
    }

    private void markCategorySolved() {
        mCategory.setSolved(true);
        LMSAppDatabaseHelper.updateCategory(getActivity(), mCategory);
    }

    public void showSummary() {
        @SuppressWarnings("ConstantConditions")
        final ListView scorecardView = (ListView) getView().findViewById(R.id.scorecard);
        mScoreAdapter = getScoreAdapter();
        scorecardView.setAdapter(mScoreAdapter);
        scorecardView.setVisibility(View.GONE);
        mQuizView.setVisibility(View.GONE);
        //        mCategory = LMSAppDatabaseHelper.getCategoryWith(QuizActivity, mCategory.getId());
        SendExamAnswersToServer();
        //        ActivityCompat.finishAfterTransition(QuizActivity.this);
    }

    private void SendExamAnswersToServer() {
        Context context = getActivity().getApplicationContext();
        ///////////////
        if (ConnectionHelper.hasActiveInternetConnection(context)) {
            gLogin logStatus = null;
            try {
                logStatus = UserUtils.CheckUserStatus(context);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (logStatus != null) {
                if (logStatus.getStatusID() == 1) {

                    try {
                        List<Answer> answerList = ServerAnswerHelper
                                .GenerateServerAnswerList(mCategory.getQuizzes());
                        SendUserQuestionAnswersCaller sendUserQuestionAnswersCaller = new SendUserQuestionAnswersCaller(
                                context, JsonHelper.GenerateServerAnswerListJson(answerList));
                        sendUserQuestionAnswersCaller.setServerExamSendAnswersResultCaller("START");
                        sendUserQuestionAnswersCaller.join();
                        sendUserQuestionAnswersCaller.start();
                        while (sendUserQuestionAnswersCaller.getServerExamSendAnswersResultCaller()
                                .equals("START")) {
                            try {
                                Thread.sleep(15);
                            } catch (Exception ignored) {
                            }
                        }
                        Gson gson = new Gson();
                        gLogin ServerResponse = gson.fromJson(
                                sendUserQuestionAnswersCaller.getServerExamSendAnswersResultCaller(), gLogin.class);
                        if (ServerResponse.getStatusID() == 1) {
                            AlertDialog alertDialog = new AlertDialog.Builder(context).create();
                            alertDialog.setTitle(" ");
                            alertDialog.setMessage("     ");
                            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                            getActivity().getFragmentManager().popBackStack();
                                        }
                                    });
                            alertDialog.show();
                        } else {
                            AlertDialog alertDialog = new AlertDialog.Builder(context).create();
                            alertDialog.setTitle(" ");
                            alertDialog.setMessage(ServerResponse.getStatusMessage());
                            alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "",
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int which) {
                                            dialog.dismiss();
                                            getActivity().getFragmentManager().popBackStack();
                                        }
                                    });
                            alertDialog.show();
                        }
                    } catch (Exception ignored) {
                    }
                } else {
                    AlertDialog alertDialog = new AlertDialog.Builder(context).create();
                    alertDialog.setTitle(" : " + logStatus.getStatusID());
                    alertDialog.setMessage(logStatus.getStatusMessage());
                    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    alertDialog.show();
                }
            } else {
                AlertDialog alertDialog = new AlertDialog.Builder(context).create();
                alertDialog.setTitle("");
                alertDialog.setMessage("    ");
                alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                alertDialog.show();
            }
            //////////////////////
        }
    }

    public boolean hasSolvedStateListener() {
        return mSolvedStateListener != null;
    }

    public void setSolvedStateListener(SolvedStateListener solvedStateListener) {
        mSolvedStateListener = solvedStateListener;
        if (mCategory.isSolved() && null != mSolvedStateListener) {
            mSolvedStateListener.onCategorySolved();
        }
    }

    private ScoreAdapter getScoreAdapter() {
        if (null == mScoreAdapter) {
            mScoreAdapter = new ScoreAdapter(mCategory);
        }
        return mScoreAdapter;
    }

    /**
     * Interface definition for a callback to be invoked when the quiz is started.
     */
    public interface SolvedStateListener {

        /**
         * This method will be invoked when the category has been solved.
         */
        void onCategorySolved();
    }
}