package de.metalmatze.krautreporter.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import com.crashlytics.android.Crashlytics;

import butterknife.ButterKnife;
import butterknife.InjectView;
import de.metalmatze.krautreporter.R;
import de.metalmatze.krautreporter.adapters.ArticlesAdapter;
import de.metalmatze.krautreporter.api.Api;
import de.metalmatze.krautreporter.models.Article;
import de.metalmatze.krautreporter.models.Author;
import io.fabric.sdk.android.Fabric;
import io.realm.Realm;
import io.realm.RealmChangeListener;
import io.realm.RealmResults;

public class MainActivity extends ActionBarActivity implements ArticlesAdapter.OnItemClickListener {

    private static final String LOG_TAG = MainActivity.class.getSimpleName();

    @InjectView(R.id.recyclerView) RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Fabric.with(this, new Crashlytics());

        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        final Realm realm = Realm.getInstance(this);

        RealmResults<Article> articles = realm.where(Article.class).findAll();
        articles.sort("order", RealmResults.SORT_ORDER_DESCENDING);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        final ArticlesAdapter articlesAdapter = new ArticlesAdapter(getApplicationContext(), this, articles);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(articlesAdapter);

        Api.with(realm).updateAuthors();
        Api.with(realm).updateArticles();

        Log.d(LOG_TAG + " articles: ", String.valueOf(realm.where(Article.class).count()));
        Log.d(LOG_TAG + " authors: ", String.valueOf(realm.where(Author.class).count()));

        realm.addChangeListener(new RealmChangeListener() {
            @Override
            public void onChange() {
                articlesAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onItemClick(Article article) {
        Intent intent = new Intent(this, ArticleActivity.class);
        intent.putExtra("id", article.getId());

        startActivity(intent);
    }
}

@@ -468,25 +468,24 @@
 ters.Article
-s
 Adapter;%0Aimp
@@ -477,24 +477,24 @@
 cleAdapter;%0A
+
 import de.me
@@ -820,25 +820,24 @@
 ents Article
-s
 Adapter.OnIt
@@ -1624,25 +1624,24 @@
 inal Article
-s
 Adapter arti
@@ -1639,25 +1639,24 @@
 pter article
-s
 Adapter = ne
@@ -1660,25 +1660,24 @@
  new Article
-s
 Adapter(getA
@@ -1701,21 +1701,21 @@
 (), 
-this, 
 article
+s, thi
 s);%0A
@@ -1804,25 +1804,24 @@
 pter(article
-s
 Adapter);%0A%0A 
@@ -2201,24 +2201,24 @@
 nChange() %7B%0A
+
             
@@ -2228,17 +2228,16 @@
  article
-s
 Adapter.

Add refactoring changes from MainActivity