2014-07-03 07:48:39 +02:00
package org.mariotaku.twidere.view.holder ;
import android.content.Context ;
2014-12-03 11:27:11 +01:00
import android.database.Cursor ;
2014-12-13 16:14:47 +01:00
import android.support.annotation.NonNull ;
import android.support.annotation.Nullable ;
2014-12-03 11:27:11 +01:00
import android.support.v7.widget.CardView ;
2014-11-21 04:00:46 +01:00
import android.support.v7.widget.RecyclerView ;
2014-07-03 07:48:39 +02:00
import android.view.View ;
2014-11-21 04:00:46 +01:00
import android.view.View.OnClickListener ;
2014-07-03 07:48:39 +02:00
import android.widget.ImageView ;
import android.widget.TextView ;
import org.mariotaku.twidere.R ;
2015-01-06 16:46:07 +01:00
import org.mariotaku.twidere.adapter.iface.ContentCardClickListener ;
2014-11-21 04:00:46 +01:00
import org.mariotaku.twidere.adapter.iface.IStatusesAdapter ;
import org.mariotaku.twidere.model.ParcelableMedia ;
import org.mariotaku.twidere.model.ParcelableStatus ;
2014-12-03 11:27:11 +01:00
import org.mariotaku.twidere.model.ParcelableStatus.CursorIndices ;
2014-12-12 14:53:18 +01:00
import org.mariotaku.twidere.util.AsyncTwitterWrapper ;
2014-11-21 04:00:46 +01:00
import org.mariotaku.twidere.util.ImageLoaderWrapper ;
2014-12-10 13:15:09 +01:00
import org.mariotaku.twidere.util.ImageLoadingHandler ;
2015-01-03 16:16:50 +01:00
import org.mariotaku.twidere.util.UserColorNameUtils ;
2014-07-03 07:48:39 +02:00
import org.mariotaku.twidere.util.Utils ;
2014-12-24 05:39:21 +01:00
import org.mariotaku.twidere.view.CardMediaContainer ;
import org.mariotaku.twidere.view.ShapedImageView ;
2014-07-03 07:48:39 +02:00
import org.mariotaku.twidere.view.ShortTimeView ;
2014-11-21 04:00:46 +01:00
import java.util.Locale ;
2014-12-13 16:14:47 +01:00
import twitter4j.TranslationResult ;
2014-11-22 15:12:54 +01:00
import static org.mariotaku.twidere.util.Utils.getUserTypeIconRes ;
2014-11-21 04:00:46 +01:00
/ * *
* Created by mariotaku on 14 / 11 / 19 .
* /
2014-12-04 15:31:29 +01:00
public class StatusViewHolder extends RecyclerView . ViewHolder implements OnClickListener {
2014-11-21 04:00:46 +01:00
2014-12-04 15:31:29 +01:00
private final IStatusesAdapter < ? > adapter ;
2014-11-21 04:00:46 +01:00
private final ImageView retweetProfileImageView ;
2014-12-24 05:39:21 +01:00
private final ShapedImageView profileImageView ;
2014-11-21 04:00:46 +01:00
private final ImageView profileTypeView ;
private final TextView textView ;
private final TextView nameView , screenNameView ;
private final TextView replyRetweetView ;
private final ShortTimeView timeView ;
2014-12-24 05:39:21 +01:00
private final CardMediaContainer mediaPreviewContainer ;
2014-11-21 04:00:46 +01:00
private final TextView replyCountView , retweetCountView , favoriteCountView ;
2015-01-06 16:46:07 +01:00
private StatusClickListener statusClickListener ;
2014-12-10 13:15:09 +01:00
public StatusViewHolder ( View itemView ) {
this ( null , itemView ) ;
}
2014-12-04 15:31:29 +01:00
public StatusViewHolder ( IStatusesAdapter < ? > adapter , View itemView ) {
2014-11-21 04:00:46 +01:00
super ( itemView ) ;
this . adapter = adapter ;
2014-12-24 05:39:21 +01:00
profileImageView = ( ShapedImageView ) itemView . findViewById ( R . id . profile_image ) ;
2014-11-21 04:00:46 +01:00
profileTypeView = ( ImageView ) itemView . findViewById ( R . id . profile_type ) ;
textView = ( TextView ) itemView . findViewById ( R . id . text ) ;
nameView = ( TextView ) itemView . findViewById ( R . id . name ) ;
screenNameView = ( TextView ) itemView . findViewById ( R . id . screen_name ) ;
retweetProfileImageView = ( ImageView ) itemView . findViewById ( R . id . retweet_profile_image ) ;
replyRetweetView = ( TextView ) itemView . findViewById ( R . id . reply_retweet_status ) ;
timeView = ( ShortTimeView ) itemView . findViewById ( R . id . time ) ;
2014-12-24 05:39:21 +01:00
mediaPreviewContainer = ( CardMediaContainer ) itemView . findViewById ( R . id . media_preview_container ) ;
2014-11-21 04:00:46 +01:00
replyCountView = ( TextView ) itemView . findViewById ( R . id . reply_count ) ;
retweetCountView = ( TextView ) itemView . findViewById ( R . id . retweet_count ) ;
favoriteCountView = ( TextView ) itemView . findViewById ( R . id . favorite_count ) ;
2014-12-03 11:27:11 +01:00
//TODO
// profileImageView.setSelectorColor(ThemeUtils.getUserHighlightColor(itemView.getContext()));
2014-12-10 13:15:09 +01:00
}
2015-01-04 16:12:10 +01:00
public void setOnClickListeners ( ) {
2015-01-06 16:46:07 +01:00
setStatusClickListener ( adapter ) ;
2015-01-04 16:12:10 +01:00
}
2015-01-06 16:46:07 +01:00
public static interface StatusClickListener extends ContentCardClickListener {
void onUserProfileClick ( StatusViewHolder holder , int position ) ;
void onStatusClick ( StatusViewHolder holder , int position ) ;
}
2015-01-04 16:12:10 +01:00
2015-01-06 16:46:07 +01:00
public void setStatusClickListener ( StatusClickListener listener ) {
statusClickListener = listener ;
itemView . findViewById ( R . id . item_content ) . setOnClickListener ( this ) ;
itemView . findViewById ( R . id . item_menu ) . setOnClickListener ( this ) ;
itemView . setOnClickListener ( this ) ;
profileImageView . setOnClickListener ( this ) ;
mediaPreviewContainer . setOnClickListener ( this ) ;
replyCountView . setOnClickListener ( this ) ;
retweetCountView . setOnClickListener ( this ) ;
favoriteCountView . setOnClickListener ( this ) ;
2014-11-21 04:00:46 +01:00
}
2014-12-24 05:39:21 +01:00
public void setupViewOptions ( ) {
2015-01-03 16:16:50 +01:00
setTextSize ( adapter . getTextSize ( ) ) ;
}
public void setTextSize ( final float textSize ) {
2014-12-24 05:39:21 +01:00
nameView . setTextSize ( textSize ) ;
textView . setTextSize ( textSize ) ;
screenNameView . setTextSize ( textSize * 0 . 85f ) ;
timeView . setTextSize ( textSize * 0 . 85f ) ;
replyRetweetView . setTextSize ( textSize * 0 . 75f ) ;
replyCountView . setTextSize ( textSize ) ;
replyCountView . setTextSize ( textSize ) ;
favoriteCountView . setTextSize ( textSize ) ;
}
2014-12-10 13:15:09 +01:00
public void displayStatus ( final ParcelableStatus status ) {
displayStatus ( adapter . getContext ( ) , adapter . getImageLoader ( ) ,
2015-01-01 11:38:34 +01:00
adapter . getImageLoadingHandler ( ) , adapter . getTwitterWrapper ( ) ,
adapter . getProfileImageStyle ( ) , adapter . getMediaPreviewStyle ( ) , status , null ) ;
2014-12-13 16:14:47 +01:00
}
public void displayStatus ( final Context context , final ImageLoaderWrapper loader ,
final ImageLoadingHandler handler , final AsyncTwitterWrapper twitter ,
2015-01-01 11:38:34 +01:00
final int profileImageStyle , final int mediaPreviewStyle ,
2014-12-13 16:14:47 +01:00
@NonNull final ParcelableStatus status ,
@Nullable final TranslationResult translation ) {
2014-11-21 04:00:46 +01:00
final ParcelableMedia [ ] media = status . media ;
if ( status . retweet_id > 0 ) {
2014-12-13 16:14:47 +01:00
replyRetweetView . setText ( context . getString ( R . string . name_retweeted , status . retweeted_by_name ) ) ;
2014-11-21 04:00:46 +01:00
replyRetweetView . setVisibility ( View . VISIBLE ) ;
2014-11-22 15:12:54 +01:00
retweetProfileImageView . setVisibility ( View . GONE ) ;
} else if ( status . in_reply_to_status_id > 0 & & status . in_reply_to_user_id > 0 ) {
2014-11-21 04:00:46 +01:00
replyRetweetView . setText ( context . getString ( R . string . in_reply_to_name , status . in_reply_to_name ) ) ;
replyRetweetView . setVisibility ( View . VISIBLE ) ;
retweetProfileImageView . setVisibility ( View . GONE ) ;
} else {
replyRetweetView . setText ( null ) ;
replyRetweetView . setVisibility ( View . GONE ) ;
replyRetweetView . setCompoundDrawablesWithIntrinsicBounds ( 0 , 0 , 0 , 0 ) ;
retweetProfileImageView . setVisibility ( View . GONE ) ;
}
2014-11-22 15:12:54 +01:00
final int typeIconRes = getUserTypeIconRes ( status . user_is_verified , status . user_is_protected ) ;
if ( typeIconRes ! = 0 ) {
profileTypeView . setImageResource ( typeIconRes ) ;
profileTypeView . setVisibility ( View . VISIBLE ) ;
2014-11-21 04:00:46 +01:00
} else {
profileTypeView . setImageDrawable ( null ) ;
2014-11-22 15:12:54 +01:00
profileTypeView . setVisibility ( View . GONE ) ;
2014-11-21 04:00:46 +01:00
}
nameView . setText ( status . user_name ) ;
screenNameView . setText ( " @ " + status . user_screen_name ) ;
timeView . setTime ( status . timestamp ) ;
2015-01-03 16:16:50 +01:00
final int userColor = UserColorNameUtils . getUserColor ( context , status . user_id ) ;
2014-12-03 11:27:11 +01:00
profileImageView . setBorderColor ( userColor ) ;
2015-01-01 11:38:34 +01:00
profileImageView . setStyle ( profileImageStyle ) ;
2014-11-21 04:00:46 +01:00
loader . displayProfileImage ( profileImageView , status . user_profile_image_url ) ;
2015-01-01 11:38:34 +01:00
mediaPreviewContainer . setStyle ( mediaPreviewStyle ) ;
2014-12-24 05:39:21 +01:00
mediaPreviewContainer . displayMedia ( media , loader , status . account_id , null , handler ) ;
2014-12-13 16:14:47 +01:00
if ( translation ! = null ) {
textView . setText ( translation . getText ( ) ) ;
} else {
textView . setText ( status . text_unescaped ) ;
}
2014-11-21 04:00:46 +01:00
if ( status . reply_count > 0 ) {
replyCountView . setText ( Utils . getLocalizedNumber ( Locale . getDefault ( ) , status . reply_count ) ) ;
} else {
replyCountView . setText ( null ) ;
}
2014-12-13 16:14:47 +01:00
final long retweet_count ;
if ( twitter . isDestroyingStatus ( status . account_id , status . my_retweet_id ) ) {
retweetCountView . setActivated ( false ) ;
retweet_count = Math . max ( 0 , status . favorite_count - 1 ) ;
2014-11-21 04:00:46 +01:00
} else {
2014-12-13 16:14:47 +01:00
final boolean creatingRetweet = twitter . isCreatingRetweet ( status . account_id , status . id ) ;
retweetCountView . setActivated ( creatingRetweet | | Utils . isMyRetweet ( status ) ) ;
retweet_count = status . retweet_count + ( creatingRetweet ? 1 : 0 ) ;
2014-11-21 04:00:46 +01:00
}
2014-12-13 16:14:47 +01:00
if ( retweet_count > 0 ) {
retweetCountView . setText ( Utils . getLocalizedNumber ( Locale . getDefault ( ) , retweet_count ) ) ;
2014-11-21 04:00:46 +01:00
} else {
2014-12-13 16:14:47 +01:00
retweetCountView . setText ( null ) ;
2014-11-21 04:00:46 +01:00
}
retweetCountView . setEnabled ( ! status . user_is_protected ) ;
2014-12-13 16:14:47 +01:00
final long favorite_count ;
2014-12-12 14:53:18 +01:00
if ( twitter . isDestroyingFavorite ( status . account_id , status . id ) ) {
favoriteCountView . setActivated ( false ) ;
2014-12-13 16:14:47 +01:00
favorite_count = Math . max ( 0 , status . favorite_count - 1 ) ;
2014-12-12 14:53:18 +01:00
} else {
2014-12-13 16:14:47 +01:00
final boolean creatingFavorite = twitter . isCreatingFavorite ( status . account_id , status . id ) ;
favoriteCountView . setActivated ( creatingFavorite | | status . is_favorite ) ;
favorite_count = status . favorite_count + ( creatingFavorite ? 1 : 0 ) ;
}
if ( favorite_count > 0 ) {
favoriteCountView . setText ( Utils . getLocalizedNumber ( Locale . getDefault ( ) , favorite_count ) ) ;
} else {
favoriteCountView . setText ( null ) ;
2014-12-12 14:53:18 +01:00
}
2014-11-21 04:00:46 +01:00
}
2014-12-03 11:27:11 +01:00
public void displayStatus ( Cursor cursor , CursorIndices indices ) {
final ImageLoaderWrapper loader = adapter . getImageLoader ( ) ;
2014-12-12 14:53:18 +01:00
final AsyncTwitterWrapper twitter = adapter . getTwitterWrapper ( ) ;
2014-12-03 11:27:11 +01:00
final Context context = adapter . getContext ( ) ;
2014-12-13 16:14:47 +01:00
final long reply_count = cursor . getLong ( indices . reply_count ) ;
final long retweet_count ;
final long favorite_count ;
2014-12-03 11:27:11 +01:00
final long account_id = cursor . getLong ( indices . account_id ) ;
final long timestamp = cursor . getLong ( indices . status_timestamp ) ;
final long user_id = cursor . getLong ( indices . user_id ) ;
2014-12-12 14:53:18 +01:00
final long status_id = cursor . getLong ( indices . status_id ) ;
2014-12-03 11:27:11 +01:00
final long retweet_id = cursor . getLong ( indices . retweet_id ) ;
final long my_retweet_id = cursor . getLong ( indices . my_retweet_id ) ;
final long retweeted_by_id = cursor . getLong ( indices . retweeted_by_user_id ) ;
final long in_reply_to_status_id = cursor . getLong ( indices . in_reply_to_status_id ) ;
final long in_reply_to_user_id = cursor . getLong ( indices . in_reply_to_user_id ) ;
final boolean user_is_protected = cursor . getInt ( indices . is_protected ) = = 1 ;
final String user_name = cursor . getString ( indices . user_name ) ;
final String user_screen_name = cursor . getString ( indices . user_screen_name ) ;
final String user_profile_image_url = cursor . getString ( indices . user_profile_image_url ) ;
final String retweeted_by_name = cursor . getString ( indices . retweeted_by_user_name ) ;
final String in_reply_to_name = cursor . getString ( indices . in_reply_to_user_name ) ;
final ParcelableMedia [ ] media = ParcelableMedia . fromJSONString ( cursor . getString ( indices . media ) ) ;
if ( retweet_id > 0 ) {
2014-12-13 16:14:47 +01:00
replyRetweetView . setText ( context . getString ( R . string . name_retweeted , retweeted_by_name ) ) ;
2014-12-03 11:27:11 +01:00
replyRetweetView . setVisibility ( View . VISIBLE ) ;
retweetProfileImageView . setVisibility ( View . GONE ) ;
} else if ( in_reply_to_status_id > 0 & & in_reply_to_user_id > 0 ) {
replyRetweetView . setText ( context . getString ( R . string . in_reply_to_name , in_reply_to_name ) ) ;
replyRetweetView . setVisibility ( View . VISIBLE ) ;
retweetProfileImageView . setVisibility ( View . GONE ) ;
} else {
replyRetweetView . setText ( null ) ;
replyRetweetView . setVisibility ( View . GONE ) ;
replyRetweetView . setCompoundDrawablesWithIntrinsicBounds ( 0 , 0 , 0 , 0 ) ;
retweetProfileImageView . setVisibility ( View . GONE ) ;
}
final int typeIconRes = getUserTypeIconRes ( cursor . getInt ( indices . is_verified ) = = 1 ,
user_is_protected ) ;
if ( typeIconRes ! = 0 ) {
profileTypeView . setImageResource ( typeIconRes ) ;
profileTypeView . setVisibility ( View . VISIBLE ) ;
} else {
profileTypeView . setImageDrawable ( null ) ;
profileTypeView . setVisibility ( View . GONE ) ;
}
nameView . setText ( user_name ) ;
screenNameView . setText ( " @ " + user_screen_name ) ;
timeView . setTime ( timestamp ) ;
2015-01-03 16:16:50 +01:00
final int userColor = UserColorNameUtils . getUserColor ( context , user_id ) ;
2014-12-03 11:27:11 +01:00
profileImageView . setBorderColor ( userColor ) ;
2015-01-01 11:38:34 +01:00
profileImageView . setStyle ( adapter . getProfileImageStyle ( ) ) ;
2014-12-03 11:27:11 +01:00
loader . displayProfileImage ( profileImageView , user_profile_image_url ) ;
2014-12-24 05:39:21 +01:00
final String text_unescaped = cursor . getString ( indices . text_unescaped ) ;
2015-01-01 11:38:34 +01:00
mediaPreviewContainer . setStyle ( adapter . getMediaPreviewStyle ( ) ) ;
2014-12-24 05:39:21 +01:00
mediaPreviewContainer . displayMedia ( media , loader , account_id , null ,
adapter . getImageLoadingHandler ( ) ) ;
textView . setText ( text_unescaped ) ;
2014-12-03 11:27:11 +01:00
if ( reply_count > 0 ) {
replyCountView . setText ( Utils . getLocalizedNumber ( Locale . getDefault ( ) , reply_count ) ) ;
} else {
replyCountView . setText ( null ) ;
}
2014-12-13 16:14:47 +01:00
if ( twitter . isDestroyingStatus ( account_id , my_retweet_id ) ) {
retweetCountView . setActivated ( false ) ;
retweet_count = Math . max ( 0 , cursor . getLong ( indices . retweet_count ) - 1 ) ;
} else {
final boolean creatingRetweet = twitter . isCreatingRetweet ( account_id , status_id ) ;
retweetCountView . setActivated ( creatingRetweet | | Utils . isMyRetweet ( account_id ,
retweeted_by_id , my_retweet_id ) ) ;
retweet_count = cursor . getLong ( indices . retweet_count ) + ( creatingRetweet ? 1 : 0 ) ;
}
2014-12-03 11:27:11 +01:00
if ( retweet_count > 0 ) {
retweetCountView . setText ( Utils . getLocalizedNumber ( Locale . getDefault ( ) , retweet_count ) ) ;
} else {
retweetCountView . setText ( null ) ;
}
retweetCountView . setEnabled ( ! user_is_protected ) ;
2014-12-13 16:14:47 +01:00
2014-12-03 11:27:11 +01:00
favoriteCountView . setActivated ( cursor . getInt ( indices . is_favorite ) = = 1 ) ;
2014-12-12 14:53:18 +01:00
if ( twitter . isDestroyingFavorite ( account_id , status_id ) ) {
favoriteCountView . setActivated ( false ) ;
2014-12-13 16:14:47 +01:00
favorite_count = Math . max ( 0 , cursor . getLong ( indices . favorite_count ) - 1 ) ;
2014-12-12 14:53:18 +01:00
} else {
2014-12-13 16:14:47 +01:00
final boolean creatingFavorite = twitter . isCreatingFavorite ( account_id , status_id ) ;
favoriteCountView . setActivated ( creatingFavorite | | cursor . getInt ( indices . is_favorite ) = = 1 ) ;
favorite_count = cursor . getLong ( indices . favorite_count ) + ( creatingFavorite ? 1 : 0 ) ;
}
if ( favorite_count > 0 ) {
favoriteCountView . setText ( Utils . getLocalizedNumber ( Locale . getDefault ( ) , favorite_count ) ) ;
} else {
favoriteCountView . setText ( null ) ;
2014-12-12 14:53:18 +01:00
}
2014-12-03 11:27:11 +01:00
}
public CardView getCardView ( ) {
return ( CardView ) itemView . findViewById ( R . id . card ) ;
}
2014-12-24 05:39:21 +01:00
public ShapedImageView getProfileImageView ( ) {
2014-12-03 11:27:11 +01:00
return profileImageView ;
}
2014-12-07 16:13:07 +01:00
public ImageView getProfileTypeView ( ) {
return profileTypeView ;
}
2014-12-24 05:39:21 +01:00
public void displaySampleStatus ( ) {
nameView . setText ( " User " ) ;
screenNameView . setText ( " @user " ) ;
timeView . setTime ( System . currentTimeMillis ( ) ) ;
textView . setText ( " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam faucibus quis purus ac malesuada. Duis id vulputate magna, a eleifend amet. " ) ;
mediaPreviewContainer . displayMedia ( R . drawable . profile_image_nyan_sakamoto ,
R . drawable . profile_image_nyan_sakamoto_santa ) ;
}
2014-12-03 11:27:11 +01:00
2014-11-21 04:00:46 +01:00
@Override
public void onClick ( View v ) {
2015-01-06 16:46:07 +01:00
if ( statusClickListener = = null ) return ;
2014-12-03 11:27:11 +01:00
final int position = getPosition ( ) ;
2014-11-21 04:00:46 +01:00
switch ( v . getId ( ) ) {
case R . id . item_content : {
2015-01-06 16:46:07 +01:00
statusClickListener . onStatusClick ( this , position ) ;
2014-11-21 04:00:46 +01:00
break ;
}
2014-12-10 13:15:09 +01:00
case R . id . item_menu : {
2015-01-06 16:46:07 +01:00
statusClickListener . onItemMenuClick ( this , position ) ;
2014-11-21 04:00:46 +01:00
break ;
}
case R . id . profile_image : {
2015-01-06 16:46:07 +01:00
statusClickListener . onUserProfileClick ( this , position ) ;
2014-11-21 04:00:46 +01:00
break ;
}
2014-12-10 13:15:09 +01:00
case R . id . reply_count :
case R . id . retweet_count :
case R . id . favorite_count : {
2015-01-06 16:46:07 +01:00
statusClickListener . onItemActionClick ( this , v . getId ( ) , position ) ;
2014-11-21 04:00:46 +01:00
break ;
}
}
}
2014-07-03 07:48:39 +02:00
}