com.twitter.sdk.android.core.models.Tweet Java Examples

The following examples show how to use com.twitter.sdk.android.core.models.Tweet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BaseTweetViewTest.java    From twitter-kit-android with Apache License 2.0 6 votes vote down vote up
public void testRender_rendersVineCard() {
    final BaseTweetView view = createViewWithMocks(context, null);
    final Card sampleVineCard = TestFixtures.sampleValidVineCard();
    final Tweet tweetWithVineCard = TestFixtures.createTweetWithVineCard(
            TestFixtures.TEST_TWEET_ID, TestFixtures.TEST_USER,
            TestFixtures.TEST_STATUS, sampleVineCard);

    view.setTweet(tweetWithVineCard);

    assertEquals(TestFixtures.TEST_NAME, view.fullNameView.getText().toString());
    assertEquals(TestFixtures.TEST_FORMATTED_SCREEN_NAME, view.screenNameView.getText());
    assertEquals(TestFixtures.TEST_STATUS, view.contentView.getText().toString());
    assertEquals(View.VISIBLE, view.mediaContainer.getVisibility());
    assertEquals(View.VISIBLE, view.mediaBadgeView.getVisibility());
    assertEquals(View.VISIBLE, view.tweetMediaView.getVisibility());
}
 
Example #2
Source File: TwitterServiceImpl.java    From twittererer with Apache License 2.0 6 votes vote down vote up
public Observable<List<TimelineItem>> getTimelineItems() {
    return Observable.create(subscriber -> {
        Callback<List<Tweet>> callback = new Callback<List<Tweet>>() {
            @Override
            public void success(Result<List<Tweet>> result) {
                Log.i(TAG, "Got the tweets, buddy!");
                subscriber.onNext(TimelineConverter.fromTweets(result.data, DateTime.now()));
            }

            @Override
            public void failure(TwitterException e) {
                Log.e(TAG, e.getMessage(), e);
                subscriber.onError(e);
            }
        };

        getStatusesService().homeTimeline(null, null, null, null, null, null, null).enqueue(callback);
    });
}
 
Example #3
Source File: BaseTweetView.java    From twitter-kit-android with Apache License 2.0 6 votes vote down vote up
void setQuoteTweet(Tweet tweet) {
    quoteTweetView = null;
    quoteTweetHolder.removeAllViews();
    if (tweet != null && TweetUtils.showQuoteTweet(tweet)) {
        quoteTweetView = new QuoteTweetView(getContext());
        quoteTweetView.setStyle(primaryTextColor, secondaryTextColor, actionColor,
                actionHighlightColor, mediaBgColor, photoErrorResId);
        quoteTweetView.setTweet(tweet.quotedStatus);
        quoteTweetView.setTweetLinkClickListener(tweetLinkClickListener);
        quoteTweetView.setTweetMediaClickListener(tweetMediaClickListener);
        quoteTweetHolder.setVisibility(View.VISIBLE);
        quoteTweetHolder.addView(quoteTweetView);
    } else {
        quoteTweetHolder.setVisibility(View.GONE);
    }
}
 
Example #4
Source File: BaseTweetView.java    From twitter-kit-android with Apache License 2.0 6 votes vote down vote up
/**
 * Set the timestamp if data from the Tweet is available. If timestamp cannot be determined,
 * set the timestamp to an empty string to handle view recycling.
 */
private void setTimestamp(Tweet displayTweet) {
    final String formattedTimestamp;
    if (displayTweet != null && displayTweet.createdAt != null &&
            TweetDateUtils.isValidTimestamp(displayTweet.createdAt)) {
        final Long createdAtTimestamp
                = TweetDateUtils.apiTimeToLong(displayTweet.createdAt);
        final String timestamp = TweetDateUtils.getRelativeTimeString(getResources(),
                System.currentTimeMillis(),
                createdAtTimestamp);
        formattedTimestamp = TweetDateUtils.dotPrefix(timestamp);
    } else {
        formattedTimestamp = EMPTY_STRING;
    }

    timestampView.setText(formattedTimestamp);
}
 
Example #5
Source File: TweetActivity.java    From twitter-kit-android with Apache License 2.0 6 votes vote down vote up
/**
 * loadTweets wraps TweetUtils.loadTweets to use a callback that ensures each view is given
 * a known id to simplify UI automation testing.
 */
private void loadTweets(final List<Long> tweetIds, final ViewGroup container,
                        final List<Integer> viewIds) {
    TweetUtils.loadTweets(tweetIds, new Callback<List<Tweet>>() {
        @Override
        public void success(Result<List<Tweet>> result) {
            final Context context = getActivity();
            if (context == null) return;
            for (int i = 0; i < result.data.size(); i++) {
                final BaseTweetView tv = new CompactTweetView(context, result.data.get(i),
                        R.style.tw__TweetDarkWithActionsStyle);
                tv.setOnActionCallback(actionCallback);
                tv.setId(viewIds.get(i));
                container.addView(tv);
            }
        }

        @Override
        public void failure(TwitterException exception) {
            Log.e(TAG, "loadTweets failure " + tweetIds, exception);
        }
    });
}
 
Example #6
Source File: BaseTweetView.java    From twitter-kit-android with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the profile photo. If the profile photo url is available from the Tweet, sets the the
 * default avatar background and attempts to load the image. If the url is not available, just
 * sets the default avatar background. Setting the default background upfront handles view
 * recycling.
 */
void setProfilePhotoView(Tweet displayTweet) {
    final Picasso imageLoader = dependencyProvider.getImageLoader();

    if (imageLoader == null) return;

    final String url;
    if (displayTweet == null || displayTweet.user == null) {
        url = null;
    } else {
        url = UserUtils.getProfileImageUrlHttps(displayTweet.user,
                UserUtils.AvatarSize.REASONABLY_SMALL);
    }

    imageLoader.load(url).placeholder(avatarMediaBg).into(avatarView);
}
 
Example #7
Source File: TimelineObservable.java    From photosearcher with Apache License 2.0 6 votes vote down vote up
private void fetchPrevious(Long minPosition) {
    mTimeline.previous(minPosition, new Callback<TimelineResult<Tweet>>() {
        @Override
        public void success(Result<TimelineResult<Tweet>> result) {
            mTimelineStateHolder.setPreviousCursor(result.data.timelineCursor);
            if (mRefreshing) {
                mTweets.clear();
            }
            int position = mTweets.size();
            mTweets.addAll(result.data.items);

            mPreviousSubject.onNext(new DataSetInsertResult(position, result.data.items.size()));

            mTimelineStateHolder.finishTimelineRequest();
            mRefreshing = false;
        }

        @Override
        public void failure(TwitterException e) {
            mPreviousSubject.onError(e);

            mTimelineStateHolder.finishTimelineRequest();
            mRefreshing = false;
        }
    });
}
 
Example #8
Source File: TweetMediaView.java    From twitter-kit-android with Apache License 2.0 6 votes vote down vote up
public void setTweetMediaEntities(Tweet tweet, List<MediaEntity> mediaEntities) {
    if (tweet == null || mediaEntities == null || mediaEntities.isEmpty() ||
            mediaEntities.equals(this.mediaEntities)) {
        return;
    }

    this.tweet = tweet;
    this.mediaEntities = mediaEntities;

    clearImageViews();
    initializeImageViews(mediaEntities);

    internalRoundedCornersEnabled = TweetMediaUtils.isPhotoType(mediaEntities.get(0));

    requestLayout();
}
 
Example #9
Source File: TweetUploadService.java    From twitter-kit-android with Apache License 2.0 6 votes vote down vote up
void uploadTweetWithMedia(TwitterSession session, String text, String mediaId) {
    final TwitterApiClient client = dependencyProvider.getTwitterApiClient(session);

    client.getStatusesService().update(text, null, null, null, null, null, null, true, mediaId)
            .enqueue(
                    new Callback<Tweet>() {
                        @Override
                        public void success(Result<Tweet> result) {
                            sendSuccessBroadcast(result.data.getId());
                            stopSelf();
                        }

                        @Override
                        public void failure(TwitterException exception) {
                            fail(exception);
                        }
                    });
}
 
Example #10
Source File: AbstractTweetViewTest.java    From twitter-kit-android with Apache License 2.0 6 votes vote down vote up
public void testRender_rendersVineCard() {
    final AbstractTweetView view = createViewWithMocks(context, null);
    final Card sampleVineCard = TestFixtures.sampleValidVineCard();
    final Tweet tweetWithVineCard = TestFixtures.createTweetWithVineCard(
            TestFixtures.TEST_TWEET_ID, TestFixtures.TEST_USER,
            TestFixtures.TEST_STATUS, sampleVineCard);

    view.setTweet(tweetWithVineCard);

    assertEquals(TestFixtures.TEST_NAME, view.fullNameView.getText().toString());
    assertEquals(TestFixtures.TEST_FORMATTED_SCREEN_NAME, view.screenNameView.getText());
    assertEquals(TestFixtures.TEST_STATUS, view.contentView.getText().toString());
    assertEquals(View.VISIBLE, view.mediaContainer.getVisibility());
    assertEquals(View.VISIBLE, view.mediaBadgeView.getVisibility());
    assertEquals(View.VISIBLE, view.tweetMediaView.getVisibility());
}
 
Example #11
Source File: TweetTimelineRecyclerViewAdapterTest.java    From twitter-kit-android with Apache License 2.0 6 votes vote down vote up
public void testConstructor_withActionCallback() {
    final TimelineDelegate<Tweet> mockTimelineDelegate = mock(TestTimelineDelegate.class);
    final Callback<Tweet> mockCallback = mock(Callback.class);
    final TweetUi tweetUi = mock(TweetUi.class);
    recyclerViewAdapter = new TweetTimelineRecyclerViewAdapter(getContext(),
            mockTimelineDelegate, ANY_STYLE, mockCallback, tweetUi);
    // assert that
    // - developer callback wrapped in a ReplaceTweetCallback
    if (recyclerViewAdapter.actionCallback instanceof
            TweetTimelineRecyclerViewAdapter.ReplaceTweetCallback) {
        final TweetTimelineRecyclerViewAdapter.ReplaceTweetCallback replaceCallback
                = (TweetTimelineRecyclerViewAdapter.ReplaceTweetCallback)
                recyclerViewAdapter.actionCallback;
        assertEquals(mockTimelineDelegate, replaceCallback.delegate);
        assertEquals(mockCallback, replaceCallback.cb);
    } else {
        fail("Expected actionCallback to be wrapped in ReplaceTweetCallback");
    }
}
 
Example #12
Source File: AbstractTweetView.java    From twitter-kit-android with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the Tweet text. If the Tweet text is unavailable, resets to empty string.
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
private void setText(Tweet displayTweet) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        contentView.setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_NO);
    }
    final CharSequence tweetText = Utils.charSeqOrEmpty(getLinkifiedText(displayTweet));
    SpanClickHandler.enableClicksOnSpans(contentView);
    if (!TextUtils.isEmpty(tweetText)) {
        contentView.setText(tweetText);
        contentView.setVisibility(VISIBLE);
    } else {
        contentView.setText(EMPTY_STRING);
        contentView.setVisibility(GONE);
    }
}
 
Example #13
Source File: TestFixtures.java    From twitter-kit-android with Apache License 2.0 6 votes vote down vote up
static Tweet createMultiplePhotosTweet(int count, long id, User user, String text,
                                              String timestamp, String photoUrlHttps) {
    final ArrayList<MediaEntity> mediaEntities = new ArrayList<>();
    for (int x = 0; x < count; x++) {
        final MediaEntity photoEntity = new MediaEntity("", "", "", 0, 0, 0L, null, null,
                photoUrlHttps, createMediaEntitySizes(100, 100), 0L, null, "photo", null, "");
        mediaEntities.add(photoEntity);
    }
    final TweetEntities entities = new TweetEntities(null, null, mediaEntities, null, null);
    return new TweetBuilder()
            .setId(id)
            .setUser(user)
            .setText(text)
            .setCreatedAt(timestamp)
            .setEntities(entities)
            .setExtendedEntities(entities)
            .build();
}
 
Example #14
Source File: TweetMediaUtils.java    From twitter-kit-android with Apache License 2.0 6 votes vote down vote up
/**
 * This method gets the all the photos from the tweet, which are used to display inline
 *
 * @param tweet The Tweet
 * @return Photo entities of Tweet
 */
public static List<MediaEntity> getPhotoEntities(Tweet tweet) {
    final List<MediaEntity> photoEntities = new ArrayList<>();
    final TweetEntities extendedEntities = tweet.extendedEntities;

    if (extendedEntities != null && extendedEntities.media != null
            && extendedEntities.media.size() > 0) {
        for (int i = 0; i <= extendedEntities.media.size() - 1; i++) {
            final MediaEntity entity = extendedEntities.media.get(i);
            if (entity.type != null && isPhotoType(entity)) {
                photoEntities.add(entity);
            }
        }
        return photoEntities;
    }

    return photoEntities;
}
 
Example #15
Source File: TweetMediaUtilsTest.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetVideoEntity_nonVideoMedia() {
    final MediaEntity entity = TestFixtures.newMediaEntity(TEST_INDICES_START, TEST_INDICES_END,
            TEST_MEDIA_TYPE_PHOTO);
    final ArrayList<MediaEntity> media = new ArrayList<>();
    media.add(entity);
    final TweetEntities entities = new TweetEntities(null, null, media, null, null);
    final Tweet tweet = new TweetBuilder().setExtendedEntities(entities).build();

    assertNull(TweetMediaUtils.getVideoEntity(tweet));
}
 
Example #16
Source File: TweetView.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the verified check if the User is verified. If the User is not verified or if the
 * verification data is unavailable, remove the check.
 */
private void setVerifiedCheck(Tweet tweet) {
    if (tweet != null && tweet.user != null && tweet.user.verified) {
        fullNameView.setCompoundDrawablesWithIntrinsicBounds(0, 0,
                R.drawable.tw__ic_tweet_verified, 0);
    } else {
        fullNameView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
    }
}
 
Example #17
Source File: TweetTimelineRecyclerViewAdapter.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
@Override
public void success(Result<Tweet> result) {
    delegate.setItemById(result.data);
    if (cb != null) {
        cb.success(result);
    }
}
 
Example #18
Source File: TweetUtilsTest.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
public void testIsTweetResolvable_hasInvalidIdAndUserWithNullScreenName() {
    final Tweet tweet = new TweetBuilder()
            .setUser(
                    new UserBuilder()
                            .setId(1)
                            .setName(null)
                            .setScreenName(null)
                            .setVerified(false)
                            .build())
            .build();
    assertFalse(TweetUtils.isTweetResolvable(tweet));
}
 
Example #19
Source File: TweetTextUtils.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
/**
 * Calls the html unescaper and then the method to fix the entity indices errors caused by
 * emoji/supplementary characters.
 *
 * @param formattedTweetText The formatted tweet text that is to be populated
 * @param tweet The source Tweet
 */
static void format(FormattedTweetText formattedTweetText, Tweet tweet) {
    if (TextUtils.isEmpty(tweet.text)) return;

    final HtmlEntities.Unescaped u = HtmlEntities.HTML40.unescape(tweet.text);
    final StringBuilder result = new StringBuilder(u.unescaped);

    adjustIndicesForEscapedChars(formattedTweetText.urlEntities, u.indices);
    adjustIndicesForEscapedChars(formattedTweetText.mediaEntities, u.indices);
    adjustIndicesForEscapedChars(formattedTweetText.hashtagEntities, u.indices);
    adjustIndicesForEscapedChars(formattedTweetText.mentionEntities, u.indices);
    adjustIndicesForEscapedChars(formattedTweetText.symbolEntities, u.indices);
    adjustIndicesForSupplementaryChars(result, formattedTweetText);
    formattedTweetText.text = result.toString();
}
 
Example #20
Source File: TweetTimelineRecyclerViewAdapterTest.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
public void testItemCount_viaBuilder() {
    final Timeline<Tweet> fakeTimeline = new FakeTweetTimeline(ITEM_COUNT);
    final TweetTimelineRecyclerViewAdapter recyclerViewAdapter =
            new TweetTimelineRecyclerViewAdapter.Builder(getContext())
                    .setTimeline(fakeTimeline)
                    .setViewStyle(R.style.tw__TweetLightWithActionsStyle)
                    .build();
    assertEquals(recyclerViewAdapter.getItemCount(), ITEM_COUNT);
}
 
Example #21
Source File: BasicTimelineFilterTest.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilter() {
    final List<Tweet> tweets = new ArrayList<>();
    tweets.add(TEST_TWEET_1);
    tweets.add(TEST_TWEET_2);
    tweets.add(TEST_TWEET_3);

    final List<Tweet> filteredTweets = basicTimelineFilter.filter(tweets);

    assertNotNull(filteredTweets);
    assertEquals(2, filteredTweets.size());
    assertEquals(TEST_TWEET_2, filteredTweets.get(0));
    assertEquals(TEST_TWEET_3, filteredTweets.get(0));
}
 
Example #22
Source File: TweetMediaUtilsTest.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetVideoEntity_emptyMedia() {
    final TweetEntities entities = new TweetEntities(null, null, new ArrayList<>(),
            null, null);
    final Tweet tweet = new TweetBuilder().setExtendedEntities(entities).build();
    assertNull(TweetMediaUtils.getVideoEntity(tweet));
}
 
Example #23
Source File: TweetUtils.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
/**
 * Loads a single Tweet by id.
 * @param tweetId Tweet id
 * @param cb callback
 */
public static void loadTweet(final long tweetId, final Callback<Tweet> cb) {
    TweetUi.getInstance().getTweetRepository().loadTweet(tweetId,
            new LoggingCallback<Tweet>(cb, Twitter.getLogger()) {
                @Override
                public void success(Result<Tweet> result) {
                    if (cb != null) {
                        cb.success(result);
                    }
                }
            });
}
 
Example #24
Source File: TimelineConverterTest.java    From twittererer with Apache License 2.0 5 votes vote down vote up
@Test
public void fromTweetsMapAgeDays() {
    DateTime now = DateTime.now();
    DateTime fourDaysAgo = now.minusDays(4);
    List<Tweet> tweets = new ArrayList<>();
    tweets.add(createTweet(dtf.print(fourDaysAgo), null, null, null, null));

    List<TimelineItem> results = TimelineConverter.fromTweets(tweets, now);

    assertThat(results.get(0).getCreatedAt(), is(equalTo("4d")));
}
 
Example #25
Source File: SearchTimeline.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
/**
 * Loads Tweets with id less than (older than) maxId.
 * @param maxId maximum id of the Tweets to load (exclusive).
 * @param cb callback.
 */
@Override
public void previous(Long maxId, Callback<TimelineResult<Tweet>> cb) {
    // api quirk: search api provides results that are inclusive of the maxId iff
    // FILTER_RETWEETS is added to the query (which we currently always add), decrement the
    // maxId to get exclusive results
    createSearchRequest(null, decrementMaxId(maxId)).enqueue(new SearchCallback(cb));
}
 
Example #26
Source File: TweetTimelineListAdapter.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a CompactTweetView by default. May be overridden to provide another view for the
 * Tweet item. If Tweet actions are enabled, be sure to call setOnActionCallback(actionCallback)
 * on each new subclass of BaseTweetView to ensure proper success and failure handling
 * for Tweet actions (favorite, unfavorite).
 */
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View rowView = convertView;
    final Tweet tweet = getItem(position);
    if (rowView == null) {
        final BaseTweetView tv = new CompactTweetView(context, tweet, styleResId);
        tv.setOnActionCallback(actionCallback);
        rowView = tv;
    } else {
        ((BaseTweetView) rowView).setTweet(tweet);
    }
    return rowView;
}
 
Example #27
Source File: TweetTimelineListAdapterTest.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
public void testBuilder_withTimelineFilter() {
    final Timeline<Tweet> mockTimeline = mock(Timeline.class);
    final TimelineFilter mockTimelineFilter = mock(TimelineFilter.class);
    listAdapter = new TweetTimelineListAdapter.Builder(getContext())
            .setTimeline(mockTimeline)
            .setTimelineFilter(mockTimelineFilter)
            .build();

    assertTrue(listAdapter.delegate instanceof FilterTimelineDelegate);
}
 
Example #28
Source File: ResetTweetCallbackTest.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailure() {
    final Callback<Tweet> developerCallback = mock(Callback.class);
    final ResetTweetCallback resetCallback = new ResetTweetCallback(mockTweetView,
            mockTweetRepository, developerCallback);
    final TwitterException exception = mock(TwitterException.class);
    resetCallback.failure(exception);
    verify(developerCallback).failure(exception);
}
 
Example #29
Source File: TweetUtilsTest.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
public void testShowQuoteTweet_nullEntity() {
    final Tweet tweet = new TweetBuilder()
            .copy(TestFixtures.TEST_PHOTO_TWEET)
            .setQuotedStatus(TestFixtures.TEST_TWEET)
            .setEntities(null)
            .build();
    assertTrue(TweetUtils.showQuoteTweet(tweet));
}
 
Example #30
Source File: BaseTweetViewTest.java    From twitter-kit-android with Apache License 2.0 5 votes vote down vote up
public void testRender_passesCorrectTweetToActionBarView() {
    final BaseTweetView tweetView = createView(context, TestFixtures.TEST_RETWEET,
            R.style.tw__TweetActionsEnabled);
    final TweetActionBarView mockActionBarView = mock(TestTweetActionBarView.class);
    tweetView.tweetActionBarView = mockActionBarView;
    doNothing().when(mockActionBarView).setLike(any(Tweet.class));
    tweetView.render();
    // verify that the TweetActionBarView is set with the Tweet, not the inner retweeted Tweet
    final ArgumentCaptor<Tweet> tweetCaptor = ArgumentCaptor.forClass(Tweet.class);
    verify(mockActionBarView).setTweet(tweetCaptor.capture());
    assertEquals(TestFixtures.TEST_RETWEET.getId(), tweetCaptor.getValue().getId());
}