Last active
August 29, 2015 14:12
-
-
Save IslamBesto/237bba11c31f40970634 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package app.toptweets.android.example.com.toptweets; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.os.AsyncTask; | |
import android.os.Bundle; | |
import android.preference.PreferenceManager; | |
import android.support.v4.app.Fragment; | |
import android.util.Log; | |
import android.view.LayoutInflater; | |
import android.view.Menu; | |
import android.view.MenuInflater; | |
import android.view.MenuItem; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.AdapterView; | |
import android.widget.ListView; | |
import android.widget.Toast; | |
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.Search; | |
import com.twitter.sdk.android.core.models.Tweet; | |
import com.twitter.sdk.android.core.services.SearchService; | |
import com.twitter.sdk.android.tweetcomposer.TweetComposer; | |
import com.twitter.sdk.android.tweetui.CompactTweetView; | |
import com.twitter.sdk.android.tweetui.TweetViewAdapter; | |
import java.util.Arrays; | |
import java.util.Comparator; | |
import java.util.List; | |
import io.fabric.sdk.android.Fabric; | |
/** | |
* A simple {@link Fragment} subclass. | |
*/ | |
public class Tweets extends Fragment { | |
List<Tweet> tweets2 = null; | |
TweetViewAdapter adapter; | |
public Tweets() { | |
// Required empty public constructor | |
} | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
// Inflate the layout for this fragment | |
View root = inflater.inflate(R.layout.fragment_tweets, container, false); | |
ListView tweetLV = (ListView) root.findViewById(R.id.list_tweets); | |
adapter = new TweetViewAdapter<CompactTweetView>(getActivity()); | |
tweetLV.setAdapter(adapter); | |
tweetLV.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { | |
String hashtag = adapter.getItem(i); | |
Intent intent = new Intent(getActivity(), DetailActivity.class) | |
.putExtra(Intent.EXTRA_TEXT, hashtag); | |
startActivity(intent); | |
} | |
}); | |
return root; | |
} | |
private void updateTweet() { | |
LoadTweets loadTweets = new LoadTweets(); | |
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity()); | |
String hashtag = prefs.getString(getString(R.string.pref_hashtag_key), | |
getString(R.string.pref_hashtag_default)); | |
Log.e("HASHTAG",hashtag); | |
loadTweets.execute(hashtag); | |
} | |
@Override | |
public void onStart() { | |
super.onStart(); | |
updateTweet(); | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
switch(item.getItemId()) { | |
case R.id.action_refresh: { | |
updateTweet(); | |
return true; | |
} | |
case R.id.action_add_tweet:{ | |
Fabric.with(getActivity(), new TweetComposer()); | |
TweetComposer.Builder builder = new TweetComposer.Builder(getActivity()) | |
.text("just setting up my Fabric.") | |
; | |
builder.show(); | |
} | |
default:return super.onOptionsItemSelected(item); | |
} | |
} | |
@Override | |
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { | |
inflater.inflate(R.menu.tweets_refresh, menu); | |
} | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setHasOptionsMenu(true); | |
} | |
public class LoadTweets extends AsyncTask<String, Void, List<Tweet>> { | |
@Override | |
protected List<Tweet> doInBackground(String... params) { | |
if (params.length == 0) { | |
return null; | |
} | |
TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient(); | |
final SearchService service = twitterApiClient.getSearchService(); | |
service.tweets(params[0], null, null, null, "popular", 100, null, null, | |
null, true, new Callback<Search>() { | |
@Override | |
public void success(Result<Search> searchResult) { | |
final List<Tweet> tweets = searchResult.data.tweets; | |
if (tweets.isEmpty()) { | |
Log.e("Islam", "Empty"); | |
Toast toast = Toast.makeText(getActivity(), "no tweets with this hashtag", Toast.LENGTH_LONG); | |
toast.show(); | |
} else { | |
Log.e("Islam", String.valueOf(tweets.size())); | |
Tweet tweetsArray[] = new Tweet[tweets.size()]; | |
tweets.toArray(tweetsArray); | |
Comparator<Tweet> comparator = new Comparator<Tweet>() { | |
@Override | |
public int compare(Tweet o1, Tweet o2) { | |
return ((Integer) o2.retweetCount).compareTo((Integer) o1.retweetCount); | |
} | |
}; | |
Arrays.sort(tweetsArray, comparator); | |
tweets2 = Arrays.asList(tweetsArray); | |
adapter.getTweets().clear(); | |
adapter.getTweets().addAll(tweets2); | |
adapter.notifyDataSetChanged(); | |
Log.e("ADAPTER", String.valueOf(adapter.getTweets().size())); | |
} | |
} | |
@Override | |
public void failure(TwitterException e) { | |
Log.e("Islam", "Failed"); | |
Toast toast = Toast.makeText(getActivity(), "Failed to load Tweets", Toast.LENGTH_LONG); | |
toast.show(); | |
} | |
}); | |
return tweets2; | |
} | |
@Override | |
protected void onPostExecute(List<Tweet> tweets) { | |
if (tweets != null) { | |
adapter.getTweets().clear(); | |
for (Tweet tweet : tweets) { | |
adapter.getTweets().add(tweet); | |
adapter.notifyDataSetChanged(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment