package ch.cern.cvmfs.fragments;


import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import ch.cern.cvmfs.R;
import ch.cern.cvmfs.model.RepositoryDescription;
import ch.cern.cvmfs.model.RepositoryManager;

public class RepositorySelectionFragment extends CVMFSFragment {

	private View mView;
	private Spinner repositorySpinner;
	private RepositoryDescription[] repositoryDescriptions;
	private RepositoryDescription selectedRepository;
	private Button enterButton;
	private RepositorySelectionListener mCallback;

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

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		repositoryDescriptions = RepositoryManager.getRepositoryList(getActivity());
		super.onViewCreated(view, savedInstanceState);
	}

	@Override
	public void onAttach(Activity activity) {
		try {
			mCallback = (RepositorySelectionListener) getParentFragment();
		} catch (ClassCastException e) {
			throw new RuntimeException(getParentFragment() + " must implement " +
					RepositorySelectionListener.class.getName());
		}
		super.onAttach(activity);
	}

	@Override
	protected void onPrepareInterface() {
		repositorySpinner = (Spinner) mView.findViewById(R.id.repo_selection_fqrn_spinner);
		enterButton = (Button) mView.findViewById(R.id.repo_selection_enter_button);

		ArrayAdapter<RepositoryDescription> arrayAdapter =
				new ArrayAdapter<>(getActivity(), R.layout.spinner_textview);
		arrayAdapter.addAll(repositoryDescriptions);
		arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		repositorySpinner.setAdapter(arrayAdapter);
		repositorySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				selectedRepository = repositoryDescriptions[position];
				enterButton.setEnabled(true);
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				selectedRepository = null;
				enterButton.setEnabled(false);
			}
		});
		enterButton.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				mCallback.repositoryChosen(selectedRepository);
			}
		});
	}

	public interface RepositorySelectionListener {
		void repositoryChosen(RepositoryDescription chosen);
	}
}

@@ -57,16 +57,48 @@
 tivity;%0A
+import android.content.Context;%0A
 import a
@@ -769,16 +769,48 @@
 allback;
+%0A    private int chosenPosition;
 %0A%0A%09@Over
@@ -1120,141 +1120,550 @@
 ) %7B%0A
-%09%09repositoryDescriptions = RepositoryManager.getRepositoryList(getActivity());%0A%09%09super.onViewCreated(view, savedInstanceState);%0A%09%7D%0A%0A%09
+        chosenPosition = retrieveChosenPosition();%0A        super.onViewCreated(view, savedInstanceState);%0A%09%7D%0A%0A    @Override%0A    public void onDestroy() %7B%0A        storeSelectedPosition();%0A        super.onDestroy();%0A    %7D%0A%0A    private void storeSelectedPosition() %7B%0A        getActivity().getPreferences(Context.MODE_PRIVATE)%0A                .edit().putInt(%22position%22, chosenPosition).commit();%0A    %7D%0A%0A    private int retrieveChosenPosition() %7B%0A        return getActivity().getPreferences(Context.MODE_PRIVATE).getInt(%22position%22, 0);%0A    %7D%0A%0A    
 @Ove
@@ -2026,24 +2026,109 @@
 terface() %7B%0A
+        repositoryDescriptions = RepositoryManager.getRepositoryList(getActivity());%0A
 %09%09repository
@@ -2580,16 +2580,72 @@
 apter);%0A
+        repositorySpinner.setSelection(chosenPosition);%0A
 %09%09reposi
@@ -2916,24 +2916,67 @@
 bled(true);%0A
+                chosenPosition = position;%0A
 %09%09%09%7D%0A%0A%09%09%09@Ov

Store last selected repository