package com.sedsoftware.xyzreader.ui;

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import butterknife.BindInt;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.sedsoftware.xyzreader.R;
import com.sedsoftware.xyzreader.data.DataManager;
import com.sedsoftware.xyzreader.data.RequestState;
import com.sedsoftware.xyzreader.ui.ArticlesAdapter.OnArticleClickListener;
import io.reactivex.Completable;
import io.reactivex.disposables.Disposable;
import javax.inject.Inject;
import timber.log.Timber;

@SuppressWarnings("ConstantConditions")
public class ArticleListActivity extends BaseActivity implements
    OnArticleClickListener, OnRefreshListener {

  private Completable articlesSync;
  private Disposable articlesSubscribtion;

  @Inject
  DataManager dataManager;

  @BindInt(R.integer.list_column_count)
  int columnsCount;

  @BindView(R.id.toolbar)
  Toolbar toolbar;
  @BindView(R.id.app_bar)
  AppBarLayout appbar;
  @BindView(R.id.recycler_view)
  RecyclerView recyclerView;
  @BindView(R.id.swipe_refresh_layout)
  SwipeRefreshLayout swipeRefreshLayout;

  private ArticlesAdapter adapter;

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

    getActivityComponent().inject(this);

    setContentView(R.layout.activity_article_list);
    ButterKnife.bind(this);

    articlesSync = dataManager
        .syncArticles()
        .doOnSubscribe(disposable -> Timber.d("Sync started..."))
        .doOnComplete(() -> Timber.d("Sync completed"));

    if (savedInstanceState == null && articlesSubscribtion == null) {
      articlesSubscribtion = articlesSync.subscribe();
    }

    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    handleLoadingIndicator(swipeRefreshLayout);

    StaggeredGridLayoutManager sglm =
        new StaggeredGridLayoutManager(columnsCount, StaggeredGridLayoutManager.VERTICAL);

    adapter = new ArticlesAdapter(this);
    adapter.setHasStableIds(true);

    recyclerView.setLayoutManager(sglm);
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(adapter);

    swipeRefreshLayout.setOnRefreshListener(this);

    dataManager.getArticlesObservableStream()
        .doOnSubscribe(disposable -> adapter.clearList())
        .doOnNext(article -> Timber.d("Fetch article from db: " + article.title()))
        .subscribe(article -> adapter.addArticle(article));
  }

  @Override
  public void onRefresh() {
    if (articlesSubscribtion != null && !articlesSubscribtion.isDisposed()) {
      articlesSubscribtion.dispose();
    }
    articlesSubscribtion = articlesSync.subscribe();
  }

  @Override
  public void articleClicked(int id) {
    Intent intent = ArticleDetailActivity.prepareIntent(this, id);

    if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
      startActivity(intent,
          ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
    } else {
      startActivity(intent);
    }
  }

  private void handleLoadingIndicator(SwipeRefreshLayout layout) {
    dataManager.getRequestState().subscribe(state -> {
      switch (state) {
        case RequestState.IDLE:
          break;
        case RequestState.LOADING:
          layout.setRefreshing(true);
          break;
        case RequestState.COMPLETED:
          layout.setRefreshing(false);
          break;
        case RequestState.ERROR:
          layout.setRefreshing(false);
          // Show some error message here
          break;
      }
    });
  }
}

@@ -1213,32 +1213,69 @@
 posable 
-articles
+syncSubscription;%0A  private Disposable stream
 Subscri
-b
+p
 tion;%0A%0A 
@@ -2097,40 +2097,8 @@
 e ==
- null && articlesSubscribtion ==
  nul
@@ -2108,32 +2108,28 @@
 %7B%0A      
-articles
+sync
 Subscri
-b
+p
 tion = a
@@ -2683,24 +2683,45 @@
 (this);%0A%0A   
+ streamSubscription =
  dataManager
@@ -3006,32 +3006,28 @@
     if (
-articles
+sync
 Subscri
-b
+p
 tion != 
@@ -3035,32 +3035,28 @@
 ull && !
-articles
+sync
 Subscri
-b
+p
 tion.isD
@@ -3074,32 +3074,28 @@
 %7B%0A      
-articles
+sync
 Subscri
-b
+p
 tion.dis
@@ -3107,24 +3107,43 @@
 );%0A    %7D%0A   
+ syncSubscription =
  articlesSub
@@ -3144,47 +3144,251 @@
 lesS
+ync.s
 ubscrib
-tion = articlesSync.subscribe();
+e();%0A  %7D%0A%0A  @Override%0A  protected void onDestroy() %7B%0A    super.onDestroy();%0A    if (syncSubscription != null) %7B%0A      syncSubscription.dispose();%0A    %7D%0A    if (streamSubscription != null) %7B%0A      streamSubscription.dispose();%0A    %7D
 %0A  %7D

Add dispose into onDestroy