Android Open Source - SeeKampf Rest Details Fragment






From Project

Back to project page SeeKampf.

License

The source code is released under:

GNU General Public License

If you think the Android project SeeKampf 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.avedo.seekampf.fragments;
// w  w  w. j  a v a2  s  . c o m
import java.lang.reflect.ParameterizedType;

import net.avedo.seekampf.R;
import net.avedo.seekampf.core.VolleyActivity;
import net.avedo.seekampf.utils.AuthGsonRequest;
import net.avedo.seekampf.utils.Constants;
import net.avedo.seekampf.utils.VolleyErrorHelper;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import android.app.Fragment;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public abstract class RestDetailsFragment<ServiceObject extends Object> extends Fragment {
  protected SharedPreferences settings;
  protected Resources res;
  
  protected AuthGsonRequest<ServiceObject> request;
  protected boolean isFetching = false;
  protected int retryCount = 0;
  
  protected ServiceObject serviceObject;
    protected long serviceObjectId;

  @Override
  public void onActivityCreated(Bundle paramBundle) {
    // Fetch the application preferences handle ...
    this.settings = PreferenceManager.getDefaultSharedPreferences(this.getActivity());
    
    // ... and the application resources.
    this.res = getResources();

        // Get the id of the service object.
        Bundle extras = getArguments();
        if (extras == null || ((serviceObjectId = extras.getLong(Constants.INTENT_EXTRA_ID, Constants.DEFAULT_ID))
                == Constants.DEFAULT_ID)) {
            finish(Constants.ACTIVITY_RESULT_MISSING_ARGUMENT);
        }
    
    super.onActivityCreated(paramBundle);
  }

  @Override
  public void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
  }
    
    @Override
    public void onStart() {
      super.onStart();
      fetchData();
    }

    @Override
    public void onDestroyView() {
      VolleyActivity.getInstance().cancelPendingRequests(fetchServiceTag());
      isFetching = false;
        super.onDestroyView();
    }

    protected void finish(int resultCode) {
        getActivity().setResult(resultCode);
        getActivity().finish();
    }
    
    protected void fetchData() {
      // Break if already fetching data or there is nothing left to load.
      if (isFetching) {
        return;
      }
      
      // Start fetching data ...
      isFetching = true;

    // ... and generate the service url.
    String serviceURL = fetchServiceUrl();
      
    // Setup the request object.
    request = new AuthGsonRequest<ServiceObject>(Request.Method.GET, serviceURL,
        fetchServiceObjClass(), null, new Response.Listener<ServiceObject>() {
            @Override
            public void onResponse(ServiceObject serviceObject) {
                      if (RestDetailsFragment.this.getView() != null && serviceObject != null) {
                          // Reset the retry counter, ...
                          retryCount = 0;
                          
                          // ... store the current object, ...
                          RestDetailsFragment.this.serviceObject = serviceObject;
                          
                          // ... setup the main layout, ...
                          RestDetailsFragment.this.createView();
                          
                          // ... update offset and reset fetching status.
                          isFetching = false;
                      }
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                    retryCount++;
                    
                    if (retryCount < Constants.MAXIMUM_RETRIES) {
                      VolleyActivity.getInstance().addToRequestQueue(request, fetchServiceTag());
                    }
                        
                    Toast.makeText(getActivity(), 
                        VolleyErrorHelper.getMessage(error, getActivity()), 
                        Toast.LENGTH_LONG).show();
            }
        });
    
    VolleyActivity.getInstance().addToRequestQueue(request, fetchServiceTag());
    }

    protected Class<ServiceObject> fetchServiceObjClass() {
        ParameterizedType parameterizedType =
          (ParameterizedType) getClass().getGenericSuperclass();
       return (Class<ServiceObject>) parameterizedType.getActualTypeArguments()[0];
    }
    
    protected abstract String fetchServiceUrl();
    protected abstract String fetchServiceTag();

    protected abstract void createView();
}




Java Source Code List

net.avedo.seekampf.BuildConfig.java
net.avedo.seekampf.core.ChangeLog.java
net.avedo.seekampf.core.CustomAdapter.java
net.avedo.seekampf.core.MainActivity.java
net.avedo.seekampf.core.OceanView.java
net.avedo.seekampf.core.OverScrollerCompat.java
net.avedo.seekampf.core.RestDetailsActivity.java
net.avedo.seekampf.core.ScaleGestureDetectorCompat.java
net.avedo.seekampf.core.VolleyActivity.java
net.avedo.seekampf.core.Zoomer.java
net.avedo.seekampf.fragments.AboutFragment.java
net.avedo.seekampf.fragments.AllianceListFragment.java
net.avedo.seekampf.fragments.AuctionListFragment.java
net.avedo.seekampf.fragments.HomeDetailsFragment.java
net.avedo.seekampf.fragments.HomeFragment.java
net.avedo.seekampf.fragments.IslandDetailsFragment.java
net.avedo.seekampf.fragments.IslandListFragment.java
net.avedo.seekampf.fragments.MessageDetailsFragment.java
net.avedo.seekampf.fragments.MessageListFragment.java
net.avedo.seekampf.fragments.OceanFragment.java
net.avedo.seekampf.fragments.PlayerListFragment.java
net.avedo.seekampf.fragments.RestDetailsFragment.java
net.avedo.seekampf.fragments.RestListFragment.java
net.avedo.seekampf.fragments.SettingsFragment.java
net.avedo.seekampf.models.Alliance.java
net.avedo.seekampf.models.Auction.java
net.avedo.seekampf.models.BaseModel.java
net.avedo.seekampf.models.Island.java
net.avedo.seekampf.models.Message.java
net.avedo.seekampf.models.Player.java
net.avedo.seekampf.utils.AuthGsonRequest.java
net.avedo.seekampf.utils.Constants.java
net.avedo.seekampf.utils.Interfaces.java
net.avedo.seekampf.utils.VolleyErrorHelper.java