package jp.cordea.switter;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;

import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterApiClient;
import com.twitter.sdk.android.core.TwitterCore;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.models.Tweet;
import com.twitter.sdk.android.core.services.StatusesService;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.Bind;
import butterknife.ButterKnife;
import io.realm.Realm;
import jp.cordea.switter.realm.LocalTweet;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.toolbar)
    Toolbar toolbar;

    @Bind(R.id.fab)
    FloatingActionButton button;

    @Bind(R.id.list_view)
    ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        setSupportActionBar(toolbar);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });

        final MainListAdapter adapter = new MainListAdapter(this, new ArrayList<LocalTweet>());

        TwitterApiClient client = TwitterCore.getInstance().getApiClient();
        StatusesService service = client.getStatusesService();
        service.homeTimeline(50, null, null, false, false, false, false, new Callback<List<Tweet>>() {
            @Override
            public void success(final Result<List<Tweet>> result) {
                Realm realm = Realm.getDefaultInstance();
                realm.executeTransaction(new Realm.Transaction() {
                    @Override
                    public void execute(Realm realm) {
                        List<Tweet> tweets = result.data;
                        for (int i = 0; i < tweets.size(); i++) {
                            Tweet tweet = tweets.get(i);
                            LocalTweet localTweet = realm.createObject(LocalTweet.class);
                            DateTime dateTime = parseTwitterDate(tweet.createdAt);
                            localTweet.setEpoch(dateTime.getMillis());
                            localTweet.setText(tweet.text);
                            localTweet.setTweetId(tweet.id);
                            localTweet.setUserId(tweet.user.id);
                            localTweet.setUserName(tweet.user.name);
                            localTweet.setUserScreenName(tweet.user.screenName);
                            localTweet.setFavoriteCount(tweet.favoriteCount);
                            localTweet.setRetweetCount(tweet.retweetCount);
                            localTweet.setProfileImageUrl(tweet.user.profileImageUrl);
                        }
                    }
                });
                realm.close();
                adapter.notifyDataSetChanged();
            }

            @Override
            public void failure(TwitterException e) {
                e.printStackTrace();
            }
        });

        listView.setAdapter(adapter);
    }

    private DateTime parseTwitterDate(String twitterDate) {
        return DateTime.parse(twitterDate, DateTimeFormat.forPattern("EEE MMM dd HH:mm:ss Z yyyy").withLocale(Locale.ENGLISH));
    }
}

@@ -21,16 +21,79 @@
 itter;%0A%0A
+import android.content.Context;%0Aimport android.content.Intent;%0A
 import a
@@ -212,24 +212,77 @@
 t.Snackbar;%0A
+import android.support.v4.widget.SwipeRefreshLayout;%0A
 import andro
@@ -1211,16 +1211,91 @@
 utton;%0A%0A
+    @Bind(R.id.refresh_layout)%0A    SwipeRefreshLayout swipeRefreshLayout;%0A%0A
     @Bin
@@ -3708,16 +3708,301 @@
 dapter);
+%0A%0A        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() %7B%0A            @Override%0A            public void onRefresh() %7B%0A                adapter.notifyDataSetChanged();%0A                swipeRefreshLayout.setRefreshing(false);%0A            %7D%0A        %7D);
 %0A    %7D%0A%0A

enable swipe refresh