package com.example.rover;
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 com.google.android.gms.location.Geofence;
import java.util.ArrayList;
import java.util.List;
import io.rover.Rover;
import io.rover.RoverObserver;
/**
* A fragment representing a list of Items.
* <p>
* Activities containing this fragment MUST implement the {@link com.example.rover.RegionFragment.OnRegionFragmentInteractionListener}
* interface.
*/
public class RegionFragment extends Fragment {
// TODO: Customize parameter argument names
private static final String ARG_COLUMN_COUNT = "column-count";
// TODO: Customize parameters
private int mColumnCount = 1;
private OnRegionFragmentInteractionListener mListener;
private RoverObserver mRoverObserver;
private RegionRecyclerViewAdapter mAdapter;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public RegionFragment() {
}
// TODO: Customize parameter initialization
@SuppressWarnings("unused")
public static RegionFragment newInstance(int columnCount) {
RegionFragment fragment = new RegionFragment();
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_region_list, container, false);
// Set the adapter
if (view instanceof RecyclerView) {
Context context = view.getContext();
RecyclerView recyclerView = (RecyclerView) view;
if (mColumnCount <= 1) {
recyclerView.setLayoutManager(new LinearLayoutManager(context));
} else {
recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
}
mAdapter = new RegionRecyclerViewAdapter(new ArrayList<Geofence>(), mListener);
recyclerView.setAdapter(mAdapter);
}
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
mAdapter = null;
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnRegionFragmentInteractionListener) {
mListener = (OnRegionFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnRegionFragmentInteractionListener");
}
mRoverObserver = new MyRoverObserver();
Rover.addObserver(mRoverObserver);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
Rover.deleteObserver(mRoverObserver);
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnRegionFragmentInteractionListener {
void onRegionFragmentEnterClick(String id);
void onRegionFragmentExitClick(String id);
}
private class MyRoverObserver implements RoverObserver.GeofenceRegistrationObserver {
@Override
public void onRegisteredGeofences(List<Geofence> geofences) {
if (mAdapter != null) {
mAdapter.clear();
mAdapter.addAll(geofences);
}
}
}
}