/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.android.sunshine.app.fragment;

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.sunshine.app.ForecastAdapter;
import com.example.android.sunshine.app.R;
import com.example.android.sunshine.app.Utility;
import com.example.android.sunshine.app.data.WeatherContract;
import com.example.android.sunshine.app.task.FetchWeatherTask;

/**
 * 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();
    }
}
@@ -787,16 +787,151 @@
 agment;%0A
+import android.support.v4.app.LoaderManager;%0Aimport android.support.v4.content.CursorLoader;%0Aimport android.support.v4.content.Loader;%0A
 import a
@@ -1554,18 +1554,114 @@
 ragment 
-%7B%0A
+implements LoaderManager.LoaderCallbacks%3CCursor%3E %7B%0A%0A    private static int WEATHER_LOADER_ID = 42;
 %0A    pri
@@ -2108,32 +2108,239 @@
 , menu);%0A    %7D%0A%0A
+    @Override%0A    public void onActivityCreated(Bundle savedInstanceState) %7B%0A        super.onActivityCreated(savedInstanceState);%0A        getLoaderManager().initLoader(WEATHER_LOADER_ID, null, this);%0A    %7D%0A%0A
     @Override%0A  
@@ -2948,504 +2948,8 @@
 ) %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        Cursor cur = getActivity().getContentResolver().query(weatherForLocationUri,%0A                null, null, null, sortOrder);%0A%0A
     
@@ -3239,19 +3239,20 @@
 vity(), 
-cur
+null
 , 0);%0A%0A 
@@ -3901,10 +3901,765 @@
 );%0A    %7D
+%0A%0A    @Override%0A    public Loader%3CCursor%3E onCreateLoader(int id, Bundle args) %7B%0A        String locationSetting = Utility.getPreferredLocation(getActivity());%0A        String sortOrder = WeatherContract.WeatherEntry.COLUMN_DATE + %22 ASC%22;%0A        Uri weatherForLocationUri = WeatherContract.WeatherEntry.buildWeatherLocationWithStartDate(%0A                locationSetting, System.currentTimeMillis());%0A        return new CursorLoader(getActivity(), weatherForLocationUri, null, null, null, sortOrder);%0A    %7D%0A%0A    @Override%0A    public void onLoadFinished(Loader%3CCursor%3E loader, Cursor data) %7B%0A        mForecastAdapter.swapCursor(data);%0A    %7D%0A%0A    @Override%0A    public void onLoaderReset(Loader%3CCursor%3E loader) %7B%0A        mForecastAdapter.swapCursor(null);%0A    %7D
 %0A%7D

Implement loader