package com.jonasgerdes.schauburgr.usecase.home.movies;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.jonasgerdes.schauburgr.R;
import com.jonasgerdes.schauburgr.model.schauburg.entity.Movie;
import com.jonasgerdes.schauburgr.model.schauburg.entity.MovieCategory;
import com.jonasgerdes.schauburgr.usecase.home.movies.movie_list.CompactMovieHolder;
import com.jonasgerdes.schauburgr.usecase.home.movies.movie_list.MovieCategoryView;
import com.jonasgerdes.schauburgr.usecase.home.movies.movie_list.MovieListAdapter;
import com.jonasgerdes.schauburgr.usecase.movie_detail.MovieDetailActivity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.disposables.CompositeDisposable;

/**
 * Created by jonas on 05.03.2017.
 */

public class MoviesView extends Fragment implements MoviesContract.View,
        MovieListAdapter.MovieClickedListener {

    @BindView(R.id.coordinator)
    CoordinatorLayout mCoordinatorLayout;

    @BindView(R.id.categoryContainer)
    LinearLayout mCategoryContainer;

    private MoviesContract.Presenter mPresenter;
    private CompositeDisposable mDisposables = new CompositeDisposable();

    public static MoviesView newInstance() {
        Bundle args = new Bundle();

        MoviesView fragment = new MoviesView();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return LayoutInflater.from(getContext()).inflate(R.layout.home_movies, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        getActivity().setTitle(R.string.title_movies);
        ButterKnife.bind(this, view);

        new MoviesPresenter().attachView(this);
    }




    @Override
    public void onDestroyView() {
        mDisposables.dispose();
        mPresenter.detachView();
        super.onDestroyView();
    }


    @Override
    public void setPresenter(MoviesContract.Presenter presenter) {
        mPresenter = presenter;
    }


    @Override
    public void addMovieCategory(Observable<MovieCategory> category) {
        MovieCategoryView movieCategoryView = new MovieCategoryView(getContext());
        movieCategoryView.setLayoutParams(new LinearLayoutCompat.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        ));
        movieCategoryView.setMovieSelectedListener(this);
        mCategoryContainer.addView(movieCategoryView);
        mDisposables.add(
                category.subscribe(movieCategoryView::bindCategory)
        );
    }

    @Override
    public void showMovieCategories(List<Observable<MovieCategory>> categories) {
        categories.forEach(this::addMovieCategory);
    }

    @Override
    public void onMovieClicked(Movie movie, CompactMovieHolder holder) {
        MovieDetailActivity.start(getActivity(), movie, holder.getPosterView());
    }
}

@@ -1507,24 +1507,315 @@
 isposable();
+%0A    /**%0A     * Whether opening of a detail activity is currently in process. Since it takes a moment to load%0A     * poster with glide, user can click fast leading to multiple instances of%0A     * %7B@link MovieDetailActivity%7D to be opened%0A     */%0A    private boolean mIsOpeningDetails = false;
 %0A%0A    public
@@ -2532,17 +2532,125 @@
 %0A    %7D%0A%0A
-%0A
+    @Override%0A    public void onResume() %7B%0A        mIsOpeningDetails = false;%0A        super.onResume();%0A    %7D
 %0A%0A    @O
@@ -3743,24 +3743,140 @@
 r holder) %7B%0A
+        //prevent opening details twice%0A        if (!mIsOpeningDetails) %7B%0A            mIsOpeningDetails = true;%0A    
         Movi
@@ -3940,20 +3940,30 @@
 terView());%0A
+        %7D%0A
     %7D%0A%7D%0A

Fix clicking fast leading to opening movie details twice