package com.chetbox.twitter.android;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.twitter.sdk.android.Twitter;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.models.Tweet;
import com.twitter.sdk.android.tweetcomposer.TweetComposer;
import com.twitter.sdk.android.tweetui.TimelineResult;
import com.twitter.sdk.android.tweetui.TweetTimelineListAdapter;
import com.twitter.sdk.android.tweetui.UserTimeline;

import java.util.List;

import twitter4j.Status;
import twitter4j.TwitterFactory;
import twitter4j.conf.Configuration;
import twitter4j.conf.ConfigurationBuilder;


public class TweetListActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    private ListView tweetList;
    private SwipeRefreshLayout swipeRefresh;
    private TweetTimelineListAdapter timelineAdapter;
    private Button composeButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tweet_list);

        tweetList = (ListView) findViewById(android.R.id.list);
        swipeRefresh = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh);
        composeButton = (Button) findViewById(R.id.compose);

        TwitterSession session = Twitter.getSessionManager().getActiveSession();

        setTitle(session.getUserName());

        UserTimeline userTimeline = new UserTimeline.Builder()
                .screenName(session.getUserName())
                .build();
        timelineAdapter = new TweetTimelineListAdapter(this, new HomeTimeline(this));
        tweetList.setAdapter(timelineAdapter);

        swipeRefresh.setOnRefreshListener(this);

        composeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TweetComposer.Builder(TweetListActivity.this).show();
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_tweet_list, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_logout) {
            Twitter.getSessionManager().clearActiveSession();

            Intent launchAppIntent = new Intent(this, LaunchActivity.class);
            startActivity(launchAppIntent);

            finish();

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onRefresh() {
        swipeRefresh.setRefreshing(true);
        timelineAdapter.refresh(new Callback<TimelineResult<Tweet>>() {
            @Override
            public void success(Result result) {
                swipeRefresh.setRefreshing(false);
            }

            @Override
            public void failure(TwitterException e) {
                swipeRefresh.setRefreshing(false);
                Toast.makeText(TweetListActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

@@ -190,16 +190,41 @@
 tivity;%0A
+import android.util.Log;%0A
 import a
@@ -241,16 +241,16 @@
 w.Menu;%0A
-
 import a
@@ -913,173 +913,8 @@
 e;%0A%0A
-import java.util.List;%0A%0Aimport twitter4j.Status;%0Aimport twitter4j.TwitterFactory;%0Aimport twitter4j.conf.Configuration;%0Aimport twitter4j.conf.ConfigurationBuilder;%0A%0A%0A
 publ
@@ -1183,16 +1183,69 @@
 utton;%0A%0A
+    private static final int COMPOSE_REQ_CODE = 10;%0A%0A
     @Ove
@@ -1738,148 +1738,8 @@
 );%0A%0A
-        UserTimeline userTimeline = new UserTimeline.Builder()%0A                .screenName(session.getUserName())%0A                .build();%0A
     
@@ -2067,16 +2067,39 @@
         
+ Intent composeIntent =
  new Twe
@@ -2141,21 +2141,102 @@
 y.this).
-show(
+createIntent();%0A                startActivityForResult(composeIntent, COMPOSE_REQ_CODE
 );%0A     
@@ -3376,32 +3376,32 @@
 ;%0A            %7D%0A
-
         %7D);%0A    
@@ -3401,11 +3401,922 @@
 );%0A    %7D
+%0A%0A    @Override%0A    protected void onActivityResult(int requestCode, int resultCode, Intent data) %7B%0A        super.onActivityResult(requestCode, resultCode, data);%0A%0A        if (requestCode == COMPOSE_REQ_CODE && resultCode == RESULT_OK) %7B%0A            new Thread(new Runnable() %7B%0A                @Override%0A                public void run() %7B%0A                    // TODO: Fix race condition. Should check that tweet has been successfully sent.%0A                    // (TweetComposer posts tweets asynchronously)%0A                    try %7B%0A                        Thread.sleep(2000);%0A                    %7D catch (InterruptedException _) %7B%7D%0A                    runOnUiThread(new Runnable() %7B%0A                        @Override%0A                        public void run() %7B%0A                            onRefresh();%0A                        %7D%0A                    %7D);%0A                %7D%0A            %7D).start();%0A        %7D%0A    %7D
 %0A%7D%0A

refresh after posting tweet