package com.example.android.sunshine2;

import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.android.sunshine2.data.WeatherContract;

/**
 * Encapsulates fetching the forecast and displaying it as a {@link ListView} layout.
 */
public class ForecastFragment extends Fragment {

    private ForecastAdapter mForecastAdapter;

    public ForecastFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Add this line in order for this fragment to handle menu events.
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.forecastfragment, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_refresh) {
            updateWeather();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        String locationSetting = Utility.getPreferredLocation(getActivity());

        // Sort order:  Ascending, by date.
        String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + " ASC";
        Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(
                locationSetting, System.currentTimeMillis());

        Cursor cur = getActivity().getContentResolver().query(weatherForLocationUri,
                null, null, null, sortOrder);

        // The CursorAdapter will take data from our cursor and populate the ListView
        // However, we cannot use FLAG_AUTO_REQUERY since it is deprecated, so we will end
        // up with an empty list the first time we run.
        mForecastAdapter = new ForecastAdapter(getActivity(), cur, 0);

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        // Get a reference to the ListView, and attach this adapter to it.
        ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
        listView.setAdapter(mForecastAdapter);

        return rootView;
    }

    private void updateWeather() {
        FetchWeatherTask weatherTask = new FetchWeatherTask(getActivity());
        String location = Utility.getPreferredLocation(getActivity());
        weatherTask.execute(location);
    }

    @Override
    public void onStart() {
        super.onStart();
        updateWeather();
    }
}

@@ -33,16 +33,58 @@
 hine2;%0A%0A
+import android.content.SharedPreferences;%0A
 import a
@@ -197,16 +197,151 @@
 agment;%0A
+import android.support.v4.app.LoaderManager;%0Aimport android.support.v4.content.CursorLoader;%0Aimport android.support.v4.content.Loader;%0A
 import a
@@ -749,18 +749,117 @@
 ragment 
-%7B%0A
+implements LoaderManager.LoaderCallbacks%3CCursor%3E %7B%0A%0A    private static final int FORECAST_LOADER = 0;
 %0A    pri
@@ -1947,727 +1947,85 @@
     
-String locationSetting = Utility.getPreferredLocation(getActivity());%0A%0A        // Sort order:  Ascending, by date.%0A        String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + %22 ASC%22;%0A        Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(%0A                locationSetting, System.currentTimeMillis());%0A%0A        Cursor cur = getActivity().getContentResolver().query(weatherForLocationUri,%0A                null, null, null, sortOrder);%0A%0A        // The CursorAdapter will take data from our cursor and populate the ListView%0A        // However, we cannot use FLAG_AUTO_REQUERY since it is deprecated, so we will end%0A        // up with an empty list the first time we run
+// The CursorAdapter will take data from our cursor and populate the ListView
 .%0A  
@@ -2084,19 +2084,20 @@
 vity(), 
-cur
+null
 , 0);%0A%0A 
@@ -2417,24 +2417,229 @@
 iew;%0A    %7D%0A%0A
+    @Override%0A    public void onActivityCreated(Bundle savedInstanceState) %7B%0A        getLoaderManager().initLoader(FORECAST_LOADER, null, this);%0A        super.onActivityCreated(savedInstanceState);%0A    %7D%0A%0A
     private 
@@ -2951,11 +2951,909 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public Loader%3CCursor%3E onCreateLoader(int i, Bundle bundle) %7B%0A        String locationSetting = Utility.getPreferredLocation(getActivity());%0A%0A        // Sort order:  Ascending, by date.%0A        String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + %22 ASC%22;%0A        Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(%0A                locationSetting, System.currentTimeMillis());%0A%0A        return new CursorLoader(getActivity(),%0A                weatherForLocationUri,%0A                null,%0A                null,%0A                null,%0A                sortOrder);%0A    %7D%0A%0A    @Override%0A    public void onLoadFinished(Loader%3CCursor%3E cursorLoader, Cursor cursor) %7B%0A        mForecastAdapter.swapCursor(cursor);%0A    %7D%0A%0A    @Override%0A    public void onLoaderReset(Loader%3CCursor%3E cursorLoader) %7B%0A        mForecastAdapter.swapCursor(null);%0A    %7D
 %0A%7D%0A

add loader