Android Open Source - NetworkFacade Headless Fragment






From Project

Back to project page NetworkFacade.

License

The source code is released under:

Apache License

If you think the Android project NetworkFacade 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.saguinav.networkfacade.sample;
//from   w ww.j a v  a2  s.c  o  m
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.saguinav.networkfacade.HttpClient;
import com.saguinav.networkfacade.HttpError;
import com.saguinav.networkfacade.HttpRequest;

import java.lang.reflect.Type;
import java.util.List;


public class HeadlessFragment extends Fragment {

    public interface Presenter {

        public void showProgress();
        public void showRepositories(List<Repository> repositories);
        public void showError(String error);

    }

    private final Presenter mDummyPresenter = new Presenter() {

        @Override
        public void showProgress() {}

        @Override
        public void showRepositories(List<Repository> repositories) {}

        @Override
        public void showError(String error) {}

    };
    private Presenter mPresenter = mDummyPresenter;

    private boolean mIsLoading = false;
    private List<Repository> mRepositories;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (activity instanceof Presenter) {
            mPresenter = (Presenter) activity;
        }

        if (mIsLoading) {
            mPresenter.showProgress();
        } else if (mRepositories != null) {
            mPresenter.showRepositories(mRepositories);
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return null;
    }

    @Override
    public void onDetach() {
        super.onDetach();

        mPresenter = mDummyPresenter;
    }

    public void searchRepositories(String query) {
        final String url = new SimpleURLBuilder("https", "api.github.com")
            .pathSegment("search")
            .pathSegment("repositories")
            .queryParam("q", query)
            .build();

        final HttpRequest request = new HttpRequest.Builder(HttpRequest.Method.GET, url)
            .header("User-Agent", "NetworkFacade-App")
            .build();

        mIsLoading = true;
        mPresenter.showProgress();

        HttpClientSingleton.getHttpClient().performRequest(request, new HttpClient.ResponseListener() {

            @Override
            public void onSuccess(String response) {
                Type listType = new TypeToken<List<Repository>>(){}.getType();

                final Gson gson = new GsonBuilder().registerTypeAdapter(listType, new RepositoriesDeserializer()).create();

                mRepositories = gson.fromJson(response, listType);

                mIsLoading = false;
                mPresenter.showRepositories(mRepositories);
            }

            @Override
            public void onFailure(HttpError error) {
                mIsLoading = false;
                mPresenter.showError(error.getMessage());
            }

        });
    }

}




Java Source Code List

com.android.volley.toolbox.AuthenticationChallengesProofHurlStack.java
com.saguinav.networkfacade.ApplicationTest.java
com.saguinav.networkfacade.HttpBody.java
com.saguinav.networkfacade.HttpClient.java
com.saguinav.networkfacade.HttpError.java
com.saguinav.networkfacade.HttpRequest.java
com.saguinav.networkfacade.RetryPolicy.java
com.saguinav.networkfacade.sample.HeadlessFragment.java
com.saguinav.networkfacade.sample.HttpClientSingleton.java
com.saguinav.networkfacade.sample.MainActivity.java
com.saguinav.networkfacade.sample.OkHttpApplication.java
com.saguinav.networkfacade.sample.OkHttpClient.java
com.saguinav.networkfacade.sample.RepositoriesDeserializer.java
com.saguinav.networkfacade.sample.Repository.java
com.saguinav.networkfacade.sample.SimpleURLBuilder.java
com.saguinav.networkfacade.sample.VolleyApplication.java
com.saguinav.networkfacade.sample.VolleyHttpClient.java