content: package com.androidavanzado.cl05_minitwitter;
  
  import android.content.Context;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v7.widget.GridLayoutManager;
  import android.support.v7.widget.LinearLayoutManager;
  import android.support.v7.widget.RecyclerView;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Toast;
  
  import com.androidavanzado.cl05_minitwitter.retrofit.AuthTwitterClient;
  import com.androidavanzado.cl05_minitwitter.retrofit.AuthTwitterService;
  import com.androidavanzado.cl05_minitwitter.retrofit.response.Tweet;
  
  import java.util.List;
  
  import retrofit2.Call;
  import retrofit2.Callback;
  import retrofit2.Response;
  import retrofit2.Retrofit;
  
  
  public class TweetListFragment extends Fragment {
  
      // TODO: Customize parameter argument names
      private static final String ARG_COLUMN_COUNT = "column-count";
      // TODO: Customize parameters
      private int mColumnCount = 1;
      RecyclerView recyclerView;
      MyTweetRecyclerViewAdapter adapter;
      List<Tweet> tweetList;
      AuthTwitterService authTwitterService;
      AuthTwitterClient authTwitterClient;
  
      /**
       * Mandatory empty constructor for the fragment manager to instantiate the
       * fragment (e.g. upon screen orientation changes).
       */
      public TweetListFragment() {
      }
  
      // TODO: Customize parameter initialization
      @SuppressWarnings("unused")
      public static TweetListFragment newInstance(int columnCount) {
          TweetListFragment fragment = new TweetListFragment();
          Bundle args = new Bundle();
          args.putInt(ARG_COLUMN_COUNT, columnCount);
          fragment.setArguments(args);
          return fragment;
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          if (getArguments() != null) {
              mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);
          }
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_tweet_list, container, false);
  
          // Set the adapter
          if (view instanceof RecyclerView) {
              Context context = view.getContext();
              recyclerView = (RecyclerView) view;
              if (mColumnCount <= 1) {
                  recyclerView.setLayoutManager(new LinearLayoutManager(context));
              } else {
                  recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
              }
  
              retrofitInit();
              loadTweetData();
          }
          return view;
      }
  
      private void retrofitInit() {
          authTwitterClient = AuthTwitterClient.getInstance();
          authTwitterService = authTwitterClient.getAuthTwitterService();
      }
  
      private void loadTweetData() {
          Call<List<Tweet>> call = authTwitterService.getAllTweets();
          call.enqueue(new Callback<List<Tweet>>() {
              @Override
              public void onResponse(Call<List<Tweet>> call, Response<List<Tweet>> response) {
                  if(response.isSuccessful()) {
                      tweetList = response.body();
                      adapter = new MyTweetRecyclerViewAdapter(
                              getActivity(),
                              tweetList
                      );
                      recyclerView.setAdapter(adapter);
                  } else {
                      Toast.makeText(getActivity(), "Algo ha ido mal", Toast.LENGTH_SHORT).show();
                  }
              }
  
              @Override
              public void onFailure(Call<List<Tweet>> call, Throwable t) {
                  Toast.makeText(getActivity(), "Error en la conexión", Toast.LENGTH_SHORT).show();
              }
          });
  
      }
  
  }
  