package com.benoitletondor.easybudgetapp.view.selectcurrency;


import android.app.Dialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
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.benoitletondor.easybudgetapp.R;
import com.benoitletondor.easybudgetapp.helper.CurrencyHelper;

import java.util.Currency;
import java.util.List;

/**
 * Fragment that contains UI for user to chose its currency.<br />
 * You should listen to the {@link #CURRENCY_SELECTED_INTENT} intent to get notified when
 * the selected currency has changed. The newly selected currency ISO code is available in
 * the {@link #CURRENCY_ISO_EXTRA} string extra.<br />
 * <br />
 * NB: The {@link com.benoitletondor.easybudgetapp.helper.CurrencyHelper#setUserCurrency(Context, Currency)}
 * method is automaticaly called by the fragment on selection, you don't have to do it yourself.
 *
 * @author Benoit LETONDOR
 */
public class SelectCurrencyFragment extends DialogFragment
{
    /**
     * Action of the intent broadcasted when selected currency has changed
     */
    public static final String CURRENCY_SELECTED_INTENT = "currency.selected";
    /**
     * Key to retrieve the newly selected currency ISO code in Intent extras
     */
    public final static String CURRENCY_ISO_EXTRA       = "currency.iso.key";

// -------------------------------------->

    public SelectCurrencyFragment()
    {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        if( getShowsDialog() )
        {
            return null;
        }

        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_select_currency, container, false);

        setupRecyclerView(v);

        return v;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        View v = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_select_currency, null, false);
        setupRecyclerView(v);

        // Put some padding between title and content
        v.setPadding(0, getActivity().getResources().getDimensionPixelSize(R.dimen.select_currency_dialog_padding_top), 0, 0);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        builder.setView(v);
        builder.setTitle(R.string.setting_category_currency_change_dialog_title);

        return builder.create();
    }

// ------------------------------------->

    /**
     * Setup the recycler view
     *
     * @param v inflated view
     */
    private void setupRecyclerView(final View v)
    {
        final RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.select_currency_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(v.getContext()));

        // Load available currencies asynchronously
        new AsyncTask<Void, Void, List<Currency>>()
        {
            @Override
            protected List<Currency> doInBackground(Void... voids)
            {
                return CurrencyHelper.getAvailableCurrencies();
            }

            @Override
            protected void onPostExecute(List<Currency> currencies)
            {
                SelectCurrencyRecyclerViewAdapter adapter = new SelectCurrencyRecyclerViewAdapter(currencies);
                recyclerView.setAdapter(adapter);

                recyclerView.scrollToPosition(adapter.getSelectedCurrencyPosition(v.getContext()));
            }
        }.execute();
    }
}

@@ -260,16 +260,53 @@
 agment;%0A
+import android.support.v4.util.Pair;%0A
 import a
@@ -3335,29 +3335,63 @@
 , Void, 
-List%3CCurrency
+Pair%3CSelectCurrencyRecyclerViewAdapter, Integer
 %3E%3E()%0A   
@@ -3441,29 +3441,63 @@
 otected 
-List%3CCurrency
+Pair%3CSelectCurrencyRecyclerViewAdapter, Integer
 %3E doInBa
@@ -3554,285 +3554,374 @@
     
-return CurrencyHelper.getAvailableCurrencies();%0A            %7D%0A%0A            @Override%0A            protected void onPostExecute(List%3CCurrency%3E currencies)%0A            %7B%0A                SelectCurrencyRecyclerViewAdapter adapter = new SelectCurrencyRecyclerViewAdapter(currencies);
+SelectCurrencyRecyclerViewAdapter adapter = new SelectCurrencyRecyclerViewAdapter(CurrencyHelper.getAvailableCurrencies());%0A                return Pair.create(adapter, adapter.getSelectedCurrencyPosition(getContext()));%0A            %7D%0A%0A            @Override%0A            protected void onPostExecute(Pair%3CSelectCurrencyRecyclerViewAdapter, Integer%3E data)%0A            %7B
 %0A   
@@ -3961,19 +3961,21 @@
 ter(
-a
 da
-pter
+ta.first
 );%0A
-%0A
     
@@ -4020,59 +4020,19 @@
 ion(
-a
 da
-pter.getSelectedCurrencyPosition(v.getContext())
+ta.second
 );%0A 

Optimize Select currency fragment async load