// Path: app/src/main/java/cat/ppicas/cleanarch/app/ServiceContainer.java
// public interface ServiceContainer {
// 
//     CityRepository getCityRepository();
// 
//     CurrentWeatherRepository getCurrentWeatherRepository();
// 
//     DailyForecastRepository getDailyForecastRepository();
// 
//     TaskExecutor getTaskExecutor();
// 
// }
// 
// Path: app/src/main/java/cat/ppicas/cleanarch/app/ServiceContainers.java
// public final class ServiceContainers {
// 
//     private ServiceContainers() {
//         throw new RuntimeException("Instances are not allowed for this class");
//     }
// 
//     public static ServiceContainer getFromApp(Context context) {
//         Context app = context.getApplicationContext();
//         ServiceContainerProvider provider = (ServiceContainerProvider) app;
//         return provider.getServiceContainer();
//     }
// }
// 
// Path: app/src/main/java/cat/ppicas/cleanarch/ui/presenter/CityCurrentWeatherPresenter.java
// public class CityCurrentWeatherPresenter extends Presenter<CityCurrentWeatherVista> {
// 
//     private final TaskExecutor mTaskExecutor;
//     private final CurrentWeatherRepository mRepository;
//     private final String mCityId;
// 
//     private GetCurrentWeatherTask mGetCurrentWeatherTask;
//     private CurrentWeather mLastCurrentWeather;
// 
//     public CityCurrentWeatherPresenter(TaskExecutor taskExecutor,
//             CurrentWeatherRepository repository, String cityId) {
//         mTaskExecutor = taskExecutor;
//         mRepository = repository;
//         mCityId = cityId;
//     }
// 
//     @Override
//     public void onStart(CityCurrentWeatherVista vista) {
//         if (mLastCurrentWeather != null) {
//             updateVista(vista, mLastCurrentWeather);
//         } else {
//             vista.displayLoading(true);
//             if (!mTaskExecutor.isRunning(mGetCurrentWeatherTask)) {
//                 mGetCurrentWeatherTask = new GetCurrentWeatherTask(mRepository, mCityId);
//                 mTaskExecutor.execute(mGetCurrentWeatherTask, new GetCurrentWeatherTaskCallback());
//             }
//         }
//     }
// 
//     private void updateVista(CityCurrentWeatherVista vista, CurrentWeather cw) {
//         vista.setCurrentTemp(NumberFormat.formatTemperature(cw.getCurrentTemp()));
//         vista.setHumidity(NumberFormat.formatHumidity(cw.getHumidity()));
//         vista.setWindSpeed(NumberFormat.formatWindSpeed(cw.getWindSpeed()));
//     }
// 
//     private class GetCurrentWeatherTaskCallback extends DisplayErrorTaskCallback<CurrentWeather> {
//         public GetCurrentWeatherTaskCallback() {
//             super(CityCurrentWeatherPresenter.this);
//         }
// 
//         @Override
//         public void onSuccess(CurrentWeather cw) {
//             mLastCurrentWeather = cw;
//             CityCurrentWeatherVista vista = getVista();
//             if (vista != null) {
//                 vista.displayLoading(false);
//                 updateVista(vista, cw);
//             }
//         }
//     }
// }
// 
// Path: app/src/main/java/cat/ppicas/framework/ui/PresenterFactory.java
// public interface PresenterFactory<T extends Presenter<?>> {
// 
//     T createPresenter();
// 
//     String getPresenterTag();
// 
// }
// 
// Path: app/src/main/java/cat/ppicas/framework/ui/PresenterHolder.java
// public interface PresenterHolder {
// 
//     <T extends Presenter<?>> T getOrCreatePresenter(PresenterFactory<T> presenterFactory);
// 
//     void destroyPresenter(PresenterFactory<?> presenterFactory);
// 
// }
// 
// Path: app/src/main/java/cat/ppicas/cleanarch/ui/vista/CityCurrentWeatherVista.java
// public interface CityCurrentWeatherVista extends TaskResultVista {
// 
//     void setCurrentTemp(String temp);
// 
//     void setHumidity(String humidity);
// 
//     void setWindSpeed(String windSpeed);
// 
// }


import cat.ppicas.framework.ui.PresenterHolder;
import cat.ppicas.cleanarch.ui.vista.CityCurrentWeatherVista;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import cat.ppicas.cleanarch.R;
import cat.ppicas.cleanarch.app.ServiceContainer;
import cat.ppicas.cleanarch.app.ServiceContainers;
import cat.ppicas.cleanarch.ui.presenter.CityCurrentWeatherPresenter;
import cat.ppicas.framework.ui.PresenterFactory;
/*
 * Copyright (C) 2015 Pau Picas Sans <pau.picas@gmail.com>
 *
 * 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 cat.ppicas.cleanarch.ui.fragment;



public class CityCurrentWeatherFragment extends Fragment implements CityCurrentWeatherVista,
        PresenterFactory<CityCurrentWeatherPresenter> {

    private static final String ARG_CITY_ID = "cityId";

    private String mCityId;
    private CityCurrentWeatherPresenter mPresenter;

    private TextView mCurrent;
    private TextView mHumidity;
    private TextView mWindSpeed;
    private View mLoading;

    public static CityCurrentWeatherFragment newInstance(String cityId) {
        Bundle args = new Bundle();
        args.putString(ARG_CITY_ID, cityId);
        CityCurrentWeatherFragment fragment = new CityCurrentWeatherFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();
        if (args == null || !args.containsKey(ARG_CITY_ID)) {
            throw new IllegalArgumentException("Invalid Fragment arguments");
        }

        mCityId = args.getString(ARG_CITY_ID);

// Path: app/src/main/java/cat/ppicas/cleanarch/app/ServiceContainer.java
// public interface ServiceContainer {
// 
//     CityRepository getCityRepository();
// 
//     CurrentWeatherRepository getCurrentWeatherRepository();
// 
//     DailyForecastRepository getDailyForecastRepository();
// 
//     TaskExecutor getTaskExecutor();
// 
// }
// 
// Path: app/src/main/java/cat/ppicas/cleanarch/app/ServiceContainers.java
// public final class ServiceContainers {
// 
//     private ServiceContainers() {
//         throw new RuntimeException("Instances are not allowed for this class");
//     }
// 
//     public static ServiceContainer getFromApp(Context context) {
//         Context app = context.getApplicationContext();
//         ServiceContainerProvider provider = (ServiceContainerProvider) app;
//         return provider.getServiceContainer();
//     }
// }
// 
// Path: app/src/main/java/cat/ppicas/cleanarch/ui/presenter/CityCurrentWeatherPresenter.java
// public class CityCurrentWeatherPresenter extends Presenter<CityCurrentWeatherVista> {
// 
//     private final TaskExecutor mTaskExecutor;
//     private final CurrentWeatherRepository mRepository;
//     private final String mCityId;
// 
//     private GetCurrentWeatherTask mGetCurrentWeatherTask;
//     private CurrentWeather mLastCurrentWeather;
// 
//     public CityCurrentWeatherPresenter(TaskExecutor taskExecutor,
//             CurrentWeatherRepository repository, String cityId) {
//         mTaskExecutor = taskExecutor;
//         mRepository = repository;
//         mCityId = cityId;
//     }
// 
//     @Override
//     public void onStart(CityCurrentWeatherVista vista) {
//         if (mLastCurrentWeather != null) {
//             updateVista(vista, mLastCurrentWeather);
//         } else {
//             vista.displayLoading(true);
//             if (!mTaskExecutor.isRunning(mGetCurrentWeatherTask)) {
//                 mGetCurrentWeatherTask = new GetCurrentWeatherTask(mRepository, mCityId);
//                 mTaskExecutor.execute(mGetCurrentWeatherTask, new GetCurrentWeatherTaskCallback());
//             }
//         }
//     }
// 
//     private void updateVista(CityCurrentWeatherVista vista, CurrentWeather cw) {
//         vista.setCurrentTemp(NumberFormat.formatTemperature(cw.getCurrentTemp()));
//         vista.setHumidity(NumberFormat.formatHumidity(cw.getHumidity()));
//         vista.setWindSpeed(NumberFormat.formatWindSpeed(cw.getWindSpeed()));
//     }
// 
//     private class GetCurrentWeatherTaskCallback extends DisplayErrorTaskCallback<CurrentWeather> {
//         public GetCurrentWeatherTaskCallback() {
//             super(CityCurrentWeatherPresenter.this);
//         }
// 
//         @Override
//         public void onSuccess(CurrentWeather cw) {
//             mLastCurrentWeather = cw;
//             CityCurrentWeatherVista vista = getVista();
//             if (vista != null) {
//                 vista.displayLoading(false);
//                 updateVista(vista, cw);
//             }
//         }
//     }
// }
// 
// Path: app/src/main/java/cat/ppicas/framework/ui/PresenterFactory.java
// public interface PresenterFactory<T extends Presenter<?>> {
// 
//     T createPresenter();
// 
//     String getPresenterTag();
// 
// }
// 
// Path: app/src/main/java/cat/ppicas/framework/ui/PresenterHolder.java
// public interface PresenterHolder {
// 
//     <T extends Presenter<?>> T getOrCreatePresenter(PresenterFactory<T> presenterFactory);
// 
//     void destroyPresenter(PresenterFactory<?> presenterFactory);
// 
// }
// 
// Path: app/src/main/java/cat/ppicas/cleanarch/ui/vista/CityCurrentWeatherVista.java
// public interface CityCurrentWeatherVista extends TaskResultVista {
// 
//     void setCurrentTemp(String temp);
// 
//     void setHumidity(String humidity);
// 
//     void setWindSpeed(String windSpeed);
// 
// }


// Path: app/src/main/java/cat/ppicas/cleanarch/ui/fragment/CityCurrentWeatherFragment.java
import cat.ppicas.framework.ui.PresenterHolder;
import cat.ppicas.cleanarch.ui.vista.CityCurrentWeatherVista;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import cat.ppicas.cleanarch.R;
import cat.ppicas.cleanarch.app.ServiceContainer;
import cat.ppicas.cleanarch.app.ServiceContainers;
import cat.ppicas.cleanarch.ui.presenter.CityCurrentWeatherPresenter;
import cat.ppicas.framework.ui.PresenterFactory;

/*
 * Copyright (C) 2015 Pau Picas Sans <pau.picas@gmail.com>
 *
 * 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 cat.ppicas.cleanarch.ui.fragment;



public class CityCurrentWeatherFragment extends Fragment implements CityCurrentWeatherVista,
        PresenterFactory<CityCurrentWeatherPresenter> {

    private static final String ARG_CITY_ID = "cityId";

    private String mCityId;
    private CityCurrentWeatherPresenter mPresenter;

    private TextView mCurrent;
    private TextView mHumidity;
    private TextView mWindSpeed;
    private View mLoading;

    public static CityCurrentWeatherFragment newInstance(String cityId) {
        Bundle args = new Bundle();
        args.putString(ARG_CITY_ID, cityId);
        CityCurrentWeatherFragment fragment = new CityCurrentWeatherFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle args = getArguments();
        if (args == null || !args.containsKey(ARG_CITY_ID)) {
            throw new IllegalArgumentException("Invalid Fragment arguments");
        }

        mCityId = args.getString(ARG_CITY_ID);

        mPresenter = ((PresenterHolder) getActivity()).getOrCreatePresenter(this);