Created
April 13, 2018 18:02
-
-
Save eltonjhony/e7ceed59f6f8efe4f675a8de2d8b3937 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.raphaelli.bestnews; | |
/** | |
* Created by Raphaelli on 12/04/2018. | |
*/ | |
import android.content.Context; | |
import android.net.ConnectivityManager; | |
import android.net.NetworkInfo; | |
import android.net.Uri; | |
import android.support.annotation.Nullable; | |
import android.os.Bundle; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.app.LoaderManager; | |
import android.support.v4.content.Loader; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class TechnologyFragment extends Fragment implements LoaderManager.LoaderCallbacks<List<Report>> { | |
private static final String THE_GUARDIAN_BASE_URL = "https://content.guardianapis.com/search"; | |
private static final String API_KEY = "7f497332-f1f5-4b2b-b30b-3b343464fc2b"; | |
private static final int REPORT_LOADER_ID = 1; | |
private static final String LOG_TAG = TechnologyFragment.class.getName(); | |
/** Adapter da lista de reports */ | |
private ReportAdapter mAdapter; | |
/** TextView that is displayed when the list is empty */ | |
private TextView mEmptyStateTextView; | |
private View mLoadingIndicator; | |
/** List of reports */ | |
private ListView mReportListView; | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { | |
View rootView = inflater.inflate(R.layout.fragment_technology, container, false); | |
// Find a reference to the {@link ListView} in the layout | |
mReportListView = (ListView) rootView.findViewById(R.id.list); | |
mAdapter = new ReportAdapter(getActivity(), new ArrayList<Report>()); | |
mReportListView.setAdapter(mAdapter); | |
// Find the reference to the empty text view in a layout and set empty view | |
mEmptyStateTextView = (TextView) rootView.findViewById(R.id.empty_view); | |
mLoadingIndicator = (View) rootView.findViewById(R.id.progress_bar); | |
return rootView; | |
} | |
@Override | |
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { | |
super.onViewCreated(view, savedInstanceState); | |
if (isConnected()) { | |
getLoaderManager().initLoader(REPORT_LOADER_ID, null, this); | |
} else { | |
mEmptyStateTextView.setVisibility(View.VISIBLE); | |
mReportListView.setVisibility(View.GONE); | |
mEmptyStateTextView.setText("Não foi possível conectar"); | |
} | |
} | |
// Helper method to check network connection | |
private boolean isConnected() { | |
ConnectivityManager connectivityManager = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE); | |
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); | |
return (networkInfo != null && networkInfo.isConnected()); | |
} | |
@Override | |
public Loader<List<Report>> onCreateLoader(int id, Bundle args) { | |
// Parse breaks apart the URI string that is passed into its parameter | |
Uri baseUri = Uri.parse(THE_GUARDIAN_BASE_URL); | |
// buildUpon prepares the baseUri that we just parsed so we can add query parameters to it | |
Uri.Builder uriBuilder = baseUri.buildUpon(); | |
uriBuilder.appendQueryParameter("q", ""); | |
uriBuilder.appendQueryParameter("format","json"); | |
uriBuilder.appendQueryParameter("api-key", API_KEY); | |
Log.e(LOG_TAG,uriBuilder.toString()); | |
// Create a new loader for the given URL | |
return new ReportLoader(getActivity(), uriBuilder.toString()); | |
} | |
@Override | |
public void onLoadFinished(android.support.v4.content.Loader<List<Report>> loader, List<Report> reports) { | |
mLoadingIndicator.setVisibility(View.GONE); | |
mEmptyStateTextView.setText("No news founded!"); | |
// Limpa o adapter dos antigos dados de report | |
mAdapter.clear(); | |
// Se há uma lista válida de {@link Report}s, então adiciona-os ao data set do adapter. | |
// Isso ativará a atualização da ListView. | |
if (reports != null && !reports.isEmpty()) { | |
mAdapter.addAll(reports); | |
} | |
} | |
@Override | |
public void onLoaderReset(android.support.v4.content.Loader<List<Report>> loader) { | |
mAdapter.clear(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment