Permalink
Cannot retrieve contributors at this time
Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up
Fetching contributors…
| /* | |
| * Copyright (C) 2015 Twitter, Inc. | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| * | |
| */ | |
| package com.example.app.tweetui; | |
| import android.app.Activity; | |
| import android.os.Bundle; | |
| import android.support.v4.widget.SwipeRefreshLayout; | |
| import android.support.v7.app.ActionBar; | |
| import android.view.View; | |
| import android.widget.AbsListView; | |
| import android.widget.ListView; | |
| import android.widget.Toast; | |
| import com.example.app.BaseActivity; | |
| import com.example.app.R; | |
| import com.example.app.twittercore.TwitterCoreMainActivity; | |
| import com.twitter.sdk.android.core.Callback; | |
| import com.twitter.sdk.android.core.Result; | |
| import com.twitter.sdk.android.core.TwitterAuthException; | |
| import com.twitter.sdk.android.core.TwitterException; | |
| import com.twitter.sdk.android.core.models.Tweet; | |
| import com.twitter.sdk.android.tweetui.SearchTimeline; | |
| import com.twitter.sdk.android.tweetui.TimelineResult; | |
| import com.twitter.sdk.android.tweetui.TweetTimelineListAdapter; | |
| import java.lang.ref.WeakReference; | |
| /** | |
| * TimelineActivity shows a full screen timeline which is useful for screenshots. | |
| */ | |
| public class TimelineActivity extends BaseActivity { | |
| final WeakReference<Activity> activityRef = new WeakReference<>(TimelineActivity.this); | |
| @Override | |
| protected void onCreate(Bundle savedInstanceState) { | |
| super.onCreate(savedInstanceState); | |
| setContentView(R.layout.tweetui_swipe_timeline); | |
| ActionBar actionBar = getSupportActionBar(); | |
| if (actionBar != null) { | |
| actionBar.setTitle(R.string.refresh_timeline_title); | |
| } | |
| // launch the app login activity when a guest user tries to favorite a Tweet | |
| final Callback<Tweet> actionCallback = new Callback<Tweet>() { | |
| @Override | |
| public void success(Result<Tweet> result) { | |
| // Intentionally blank | |
| } | |
| @Override | |
| public void failure(TwitterException exception) { | |
| if (exception instanceof TwitterAuthException) { | |
| startActivity(TwitterCoreMainActivity.newIntent(TimelineActivity.this)); | |
| } | |
| } | |
| }; | |
| final SwipeRefreshLayout swipeLayout = findViewById(R.id.swipe_layout); | |
| final View emptyView = findViewById(android.R.id.empty); | |
| final ListView listView = findViewById(android.R.id.list); | |
| listView.setEmptyView(emptyView); | |
| final SearchTimeline timeline = new SearchTimeline.Builder().query("#twitter").build(); | |
| final TweetTimelineListAdapter adapter = new TweetTimelineListAdapter.Builder(this) | |
| .setTimeline(timeline) | |
| .setViewStyle(R.style.tw__TweetLightWithActionsStyle) | |
| .setOnActionCallback(actionCallback) | |
| .build(); | |
| listView.setAdapter(adapter); | |
| swipeLayout.setColorSchemeResources(R.color.twitter_blue, R.color.twitter_dark); | |
| // set custom scroll listener to enable swipe refresh layout only when at list top | |
| listView.setOnScrollListener(new AbsListView.OnScrollListener() { | |
| boolean enableRefresh = false; | |
| @Override | |
| public void onScrollStateChanged(AbsListView view, int scrollState) {} | |
| @Override | |
| public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, | |
| int totalItemCount) { | |
| if (listView != null && listView.getChildCount() > 0) { | |
| // check that the first item is visible and that its top matches the parent | |
| enableRefresh = listView.getFirstVisiblePosition() == 0 && | |
| listView.getChildAt(0).getTop() >= 0; | |
| } else { | |
| enableRefresh = false; | |
| } | |
| swipeLayout.setEnabled(enableRefresh); | |
| } | |
| }); | |
| // specify action to take on swipe refresh | |
| swipeLayout.setOnRefreshListener(() -> { | |
| swipeLayout.setRefreshing(true); | |
| adapter.refresh(new Callback<TimelineResult<Tweet>>() { | |
| @Override | |
| public void success(Result<TimelineResult<Tweet>> result) { | |
| swipeLayout.setRefreshing(false); | |
| } | |
| @Override | |
| public void failure(TwitterException exception) { | |
| swipeLayout.setRefreshing(false); | |
| final Activity activity = activityRef.get(); | |
| if (activity != null && !activity.isFinishing()) { | |
| Toast.makeText(activity, exception.getMessage(), | |
| Toast.LENGTH_SHORT).show(); | |
| } | |
| } | |
| }); | |
| }); | |
| } | |
| } |