Android Open Source - MentalMathX Highscores Activity






From Project

Back to project page MentalMathX.

License

The source code is released under:

GNU General Public License

If you think the Android project MentalMathX listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.schlingel.bplaced.mentalmathx;
//from www  . j a v a2 s . c o  m
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.RelativeLayout;

import net.schlingel.bplaced.mentalmathx.controller.HighscoreController;
import net.schlingel.bplaced.mentalmathx.controller.impl.HighscoreControllerImpl;
import net.schlingel.bplaced.mentalmathx.game.Mode;
import net.schlingel.bplaced.mentalmathx.model.Score;
import net.schlingel.bplaced.mentalmathx.utils.MarathonScoreComparator;
import net.schlingel.bplaced.mentalmathx.utils.RegularScoreComparator;
import net.schlingel.bplaced.mentalmathx.view.HighscoresView;
import net.schlingel.bplaced.mentalmathx.view.impl.FragmentHighscoresSubView_;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;

@EActivity(R.layout.activity_highscores)
public class HighscoresActivity extends ActionBarActivity implements HighscoresView {
    private static final String TAG = HighscoresActivity.class.getSimpleName();
    private static final Mode[] VISIBLE_SCORES = new Mode[] { Mode.TenRounds, Mode.HoundredRounds, Mode.Marathon };
    private static final Comparator<Score>[] COMPARATORS = new Comparator[] { new RegularScoreComparator(), new RegularScoreComparator(), new MarathonScoreComparator() };
    private HighscoreController controller;

    private static class ScorePagesAdapter extends FragmentPagerAdapter implements ViewPager.OnPageChangeListener {
        private FragmentHighscoresSubView_[] highscoresSubViewse;
        private Score[] scores;

        public ScorePagesAdapter(FragmentManager fm, Score[] scores) {
            super(fm);

            if(scores == null) {
                throw new IllegalArgumentException("Scores mut not be null!");
            }

            this.scores = scores;
            this.highscoresSubViewse = new FragmentHighscoresSubView_[VISIBLE_SCORES.length];
        }

        @Override
        public Fragment getItem(int position) {
            if(highscoresSubViewse[position] == null) {
                highscoresSubViewse[position] = new FragmentHighscoresSubView_();
                highscoresSubViewse[position].setMode(VISIBLE_SCORES[position]);
                highscoresSubViewse[position].show(filterScoresForMode(scores, VISIBLE_SCORES[position], COMPARATORS[position]));
            }

            return highscoresSubViewse[position];
        }

        @Override
        public int getCount() {
            return VISIBLE_SCORES.length;
        }

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

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    }

    @ViewById(R.id.vwPgrHighscores)
    ViewPager vwPgrHighscres;

    @ViewById(R.id.rlWaiting)
    RelativeLayout rlWaiting;

    @AfterViews
    public void init() {
        controller = new HighscoreControllerImpl(this, this);
        controller.fetchScores();
    }

    @Override
    public void show(Score[] scores) {
        if(scores == null) {
            return;
        }

        rlWaiting.setVisibility(View.GONE);
        vwPgrHighscres.setVisibility(View.VISIBLE);

        ScorePagesAdapter adapter = new ScorePagesAdapter(getSupportFragmentManager(), scores);
        vwPgrHighscres.setAdapter(adapter);
        vwPgrHighscres.setCurrentItem(0);

        adapter.notifyDataSetChanged();
    }

    private static Score[] filterScoresForMode(Score[] allScores, Mode mode, Comparator<Score> comparator) {
        ArrayList<Score> scores = new ArrayList<Score>();
        Score[] scoresArr = null;

        for(Score s : allScores) {
            if(s.getGameType() == mode) {
                scores.add(s);
            }
        }

        scoresArr = scores.toArray(new Score[0]);
        Arrays.sort(scoresArr, comparator);

        return scoresArr;
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    public static Intent asIntent(Context sender) {
        Intent i = new Intent(sender, HighscoresActivity_.class);
        return i;
    }
}




Java Source Code List

net.schlingel.bplaced.mentalmathx.AboutActivity.java
net.schlingel.bplaced.mentalmathx.GameActivity.java
net.schlingel.bplaced.mentalmathx.HighscoresActivity.java
net.schlingel.bplaced.mentalmathx.NewGameActivity.java
net.schlingel.bplaced.mentalmathx.SelectDifficultyActivity.java
net.schlingel.bplaced.mentalmathx.controller.GameController.java
net.schlingel.bplaced.mentalmathx.controller.HighscoreController.java
net.schlingel.bplaced.mentalmathx.controller.impl.GameControllerImpl.java
net.schlingel.bplaced.mentalmathx.controller.impl.HighscoreControllerImpl.java
net.schlingel.bplaced.mentalmathx.game.Difficulty.java
net.schlingel.bplaced.mentalmathx.game.Mode.java
net.schlingel.bplaced.mentalmathx.game.logic.FiniteGameLogic.java
net.schlingel.bplaced.mentalmathx.game.logic.GameLogic.java
net.schlingel.bplaced.mentalmathx.game.logic.HoundredRoundsGameLogic.java
net.schlingel.bplaced.mentalmathx.game.logic.InfiniteGameLogic.java
net.schlingel.bplaced.mentalmathx.game.logic.TenExercisesGameLogic.java
net.schlingel.bplaced.mentalmathx.game.logic.TenRoundsGameLogic.java
net.schlingel.bplaced.mentalmathx.game.strategy.EasyExerciseStrategy.java
net.schlingel.bplaced.mentalmathx.game.strategy.ExerciseStrategyFactory.java
net.schlingel.bplaced.mentalmathx.game.strategy.ExerciseStrategy.java
net.schlingel.bplaced.mentalmathx.game.strategy.HardExerciseStrategy.java
net.schlingel.bplaced.mentalmathx.game.strategy.MediumExerciseStrategy.java
net.schlingel.bplaced.mentalmathx.game.strategy.OneOOneExerciseStrategy.java
net.schlingel.bplaced.mentalmathx.math.Calculation.java
net.schlingel.bplaced.mentalmathx.math.Calculations.java
net.schlingel.bplaced.mentalmathx.math.Number.java
net.schlingel.bplaced.mentalmathx.math.Operator.java
net.schlingel.bplaced.mentalmathx.math.Term.java
net.schlingel.bplaced.mentalmathx.model.Result.java
net.schlingel.bplaced.mentalmathx.model.Score.java
net.schlingel.bplaced.mentalmathx.model.adapters.ScoreAdapter.java
net.schlingel.bplaced.mentalmathx.utils.DatabaseHelper.java
net.schlingel.bplaced.mentalmathx.utils.DelayedTask.java
net.schlingel.bplaced.mentalmathx.utils.LabelHelper.java
net.schlingel.bplaced.mentalmathx.utils.MarathonScoreComparator.java
net.schlingel.bplaced.mentalmathx.utils.RegularScoreComparator.java
net.schlingel.bplaced.mentalmathx.view.DisplayMode.java
net.schlingel.bplaced.mentalmathx.view.GameView.java
net.schlingel.bplaced.mentalmathx.view.HighscoresView.java
net.schlingel.bplaced.mentalmathx.view.ResultsView.java
net.schlingel.bplaced.mentalmathx.view.impl.DialogResultsView.java
net.schlingel.bplaced.mentalmathx.view.impl.FragmentHighscoresSubView.java