package hh.michelson;

import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class WavesFragment extends ListFragment {
	OnFragmentInteractionListener mListener;
	boolean selected = false;

	@Override
	public void onAttach(Activity context) {
		super.onAttach(context);
		setListAdapter(new ArrayAdapter<>(getActivity(), R.layout.wave_list_row, Wave.titles));
		mListener = (OnFragmentInteractionListener)context;
		setRetainInstance(true);    // zachovat fragment při otáčení obrazovky
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.list_view, container, false);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		mListener.setNewWave(position);
		selected = true;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if(!selected) mListener.setNewWave(-1);
		mListener = null;
	}

	public static interface OnFragmentInteractionListener {
		public void setNewWave(int position);
	}
}
