Android Open Source - MatchHistory-League Match Update Task






From Project

Back to project page MatchHistory-League.

License

The source code is released under:

MIT License

If you think the Android project MatchHistory-League 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 com.lando.matchhistory.AsyncTask;
// w  ww.ja v  a2s.co m
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.lando.matchhistory.ApiClient.ApiClient;
import com.lando.matchhistory.MainActivity;
import com.lando.matchhistory.Models.Champion;
import com.lando.matchhistory.Models.Match;
import com.lando.matchhistory.Models.Summoner;
import com.lando.matchhistory.R;

import java.util.List;
import java.util.Map;

import io.realm.Realm;


/**
 * Created by Lando on 1/2/2015.
 */
public class MatchUpdateTask extends AsyncTask<Void,Void,Boolean> {

    protected MatchUpdateResponse mListener=null;

    public interface MatchUpdateResponse {
        void processFinish(boolean finished);
    }
    public void setListener(MatchUpdateResponse listener){
        mListener = listener;
    }

    private long mSummonerId;
    private final String mRegion;
    private final Context mContext;

    public MatchUpdateTask(Context context, long id, String region){
        this.mContext = context;
        this.mSummonerId = id;
        this.mRegion = region;
    }
    /**
     * Try to improve this method
     */
    @Override
    protected Boolean doInBackground(Void... params) {
        Realm realm = null;
        boolean finish = true;
        try {
            realm = Realm.getInstance(mContext);
            realm.beginTransaction();
            Summoner summoner = realm.where(Summoner.class).equalTo("id",mSummonerId).findFirst();
            Map<String,List<Match>> matches = ApiClient.getLeagueApiClient().getMatches(mRegion,mSummonerId, 0,5,mContext.getResources().getString(R.string.api_key));
            Log.v(MainActivity.DEBUG_INFO,"Starting the loop");
            List<Match> matchlist = matches.get("matches");

            for (int i = 0; i < matchlist.size(); i++) {
                Match match;
                if ((match = realm.where(Match.class).equalTo("matchId", matchlist.get(i).getMatchId()).findFirst()) != null) {
                    if(summoner.getMatches().where().equalTo("matchId",match.getMatchId()).findFirst()!=null){
                        continue;
                    }
                }else {
                    match = ApiClient.getLeagueApiClient().getMatch(mRegion, matchlist.get(i).getMatchId(), mContext.getResources().getString(R.string.api_key));
                    match = realm.copyToRealm(match);
                }
                summoner.getMatches().add(match);
                Log.v(MainActivity.DEBUG_INFO,"Added Match "+match.getMatchId());
            }
            realm.commitTransaction();
        } catch (Exception e){
            Log.e(MainActivity.DEBUG_INFO,e.getMessage());
            realm.cancelTransaction();
            finish = false;
        }finally {
            if (realm != null) {
                realm.close();
            }
        }
        return finish;
    }
    @Override
    protected void onPostExecute(Boolean finish) {
        super.onPostExecute(finish);
        if(mListener != null)
            mListener.processFinish(finish);
    }
}




Java Source Code List

com.lando.matchhistory.ApplicationTest.java
com.lando.matchhistory.MainActivity.java
com.lando.matchhistory.SummonerActivity.java
com.lando.matchhistory.Adapter.DrawerAdapter.java
com.lando.matchhistory.Adapter.MatchAdapter.java
com.lando.matchhistory.ApiClient.ApiClient.java
com.lando.matchhistory.AsyncTask.BaseTask.java
com.lando.matchhistory.AsyncTask.MatchUpdateTask.java
com.lando.matchhistory.AsyncTask.SummonerUpdateTask.java
com.lando.matchhistory.AsyncTask.VersionUpdateTask.java
com.lando.matchhistory.ContentProvider.SummonerProvider.java
com.lando.matchhistory.Fragment.MasteriesFragment.java
com.lando.matchhistory.Fragment.MatchHistoryFragment.java
com.lando.matchhistory.Fragment.ProfileFragment.java
com.lando.matchhistory.Fragment.RuneFragment.java
com.lando.matchhistory.Models.Champion.java
com.lando.matchhistory.Models.CreepsPerMinDeltas.java
com.lando.matchhistory.Models.DamageTakenPerMinDeltas.java
com.lando.matchhistory.Models.GoldPerMinDeltas.java
com.lando.matchhistory.Models.Image.java
com.lando.matchhistory.Models.Item.java
com.lando.matchhistory.Models.Mastery.java
com.lando.matchhistory.Models.Match.java
com.lando.matchhistory.Models.ParticipantIdentity.java
com.lando.matchhistory.Models.Participant.java
com.lando.matchhistory.Models.Player.java
com.lando.matchhistory.Models.ProfileIcon.java
com.lando.matchhistory.Models.Rune.java
com.lando.matchhistory.Models.Stats.java
com.lando.matchhistory.Models.SummonerSpell.java
com.lando.matchhistory.Models.Summoner.java
com.lando.matchhistory.Models.Timeline.java
com.lando.matchhistory.Models.Version.java
com.lando.matchhistory.Models.XpPerMinDeltas.java
com.lando.matchhistory.Provider.RecentSuggestionProvider.java