SubwayTooter-Android-App/app/src/main/java/jp/juggler/subwaytooter/ViewHolderHeaderProfile.kt

573 lines
17 KiB
Kotlin
Raw Normal View History

package jp.juggler.subwaytooter
import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.*
import jp.juggler.emoji.EmojiMap201709
import jp.juggler.subwaytooter.action.Action_Follow
import jp.juggler.subwaytooter.action.Action_User
import jp.juggler.subwaytooter.api.MisskeyAccountDetailMap
import jp.juggler.subwaytooter.api.entity.TootAccount
import jp.juggler.subwaytooter.api.entity.TootAccountRef
import jp.juggler.subwaytooter.api.entity.TootStatus
2018-11-18 03:38:52 +01:00
import jp.juggler.subwaytooter.span.EmojiImageSpan
import jp.juggler.subwaytooter.table.AcctColor
import jp.juggler.subwaytooter.table.UserRelation
import jp.juggler.subwaytooter.util.DecodeOptions
import jp.juggler.subwaytooter.util.NetworkEmojiInvalidator
import jp.juggler.subwaytooter.util.startMargin
import jp.juggler.subwaytooter.view.MyLinkMovementMethod
import jp.juggler.subwaytooter.view.MyNetworkImageView
import jp.juggler.subwaytooter.view.MyTextView
2019-01-18 16:33:53 +01:00
import jp.juggler.util.*
2018-11-18 03:38:52 +01:00
import org.jetbrains.anko.textColor
internal class ViewHolderHeaderProfile(
activity : ActMain,
viewRoot : View
) : ViewHolderHeaderBase(activity, viewRoot), View.OnClickListener, View.OnLongClickListener {
private val ivBackground : MyNetworkImageView
private val tvCreated : TextView
private val tvLastStatusAt : TextView
private val ivAvatar : MyNetworkImageView
private val tvDisplayName : TextView
private val tvAcct : TextView
private val btnFollowing : Button
private val btnFollowers : Button
private val btnStatusCount : Button
private val tvNote : TextView
private val tvMisskeyExtra : TextView
private val btnFollow : ImageButton
private val ivFollowedBy : ImageView
private val llProfile : View
private val tvRemoteProfileWarning : TextView
private val name_invalidator : NetworkEmojiInvalidator
private val note_invalidator : NetworkEmojiInvalidator
private val llFields : LinearLayout
private var whoRef : TootAccountRef? = null
private var movedRef : TootAccountRef? = null
private val llMoved : View
private val tvMoved : TextView
private val ivMoved : MyNetworkImageView
private val tvMovedName : TextView
private val tvMovedAcct : TextView
private val btnMoved : ImageButton
private val ivMovedBy : ImageView
private val moved_caption_invalidator : NetworkEmojiInvalidator
private val moved_name_invalidator : NetworkEmojiInvalidator
private val density : Float
2018-11-18 03:38:52 +01:00
private val btnMore : ImageButton
init {
ivBackground = viewRoot.findViewById(R.id.ivBackground)
llProfile = viewRoot.findViewById(R.id.llProfile)
tvCreated = viewRoot.findViewById(R.id.tvCreated)
tvLastStatusAt= viewRoot.findViewById(R.id.tvLastStatusAt)
ivAvatar = viewRoot.findViewById(R.id.ivAvatar)
tvDisplayName = viewRoot.findViewById(R.id.tvDisplayName)
tvAcct = viewRoot.findViewById(R.id.tvAcct)
btnFollowing = viewRoot.findViewById(R.id.btnFollowing)
btnFollowers = viewRoot.findViewById(R.id.btnFollowers)
btnStatusCount = viewRoot.findViewById(R.id.btnStatusCount)
tvNote = viewRoot.findViewById(R.id.tvNote)
2019-01-18 16:33:53 +01:00
tvMisskeyExtra = viewRoot.findViewById(R.id.tvMisskeyExtra)
2018-11-18 03:38:52 +01:00
btnMore = viewRoot.findViewById(R.id.btnMore)
btnFollow = viewRoot.findViewById(R.id.btnFollow)
ivFollowedBy = viewRoot.findViewById(R.id.ivFollowedBy)
tvRemoteProfileWarning = viewRoot.findViewById(R.id.tvRemoteProfileWarning)
llMoved = viewRoot.findViewById(R.id.llMoved)
tvMoved = viewRoot.findViewById(R.id.tvMoved)
ivMoved = viewRoot.findViewById(R.id.ivMoved)
tvMovedName = viewRoot.findViewById(R.id.tvMovedName)
tvMovedAcct = viewRoot.findViewById(R.id.tvMovedAcct)
btnMoved = viewRoot.findViewById(R.id.btnMoved)
ivMovedBy = viewRoot.findViewById(R.id.ivMovedBy)
llFields = viewRoot.findViewById(R.id.llFields)
density = tvDisplayName.resources.displayMetrics.density
ivBackground.setOnClickListener(this)
btnFollowing.setOnClickListener(this)
btnFollowers.setOnClickListener(this)
btnStatusCount.setOnClickListener(this)
btnMore.setOnClickListener(this)
btnFollow.setOnClickListener(this)
tvRemoteProfileWarning.setOnClickListener(this)
btnMoved.setOnClickListener(this)
llMoved.setOnClickListener(this)
btnMoved.setOnLongClickListener(this)
btnFollow.setOnLongClickListener(this)
tvNote.movementMethod = MyLinkMovementMethod
name_invalidator = NetworkEmojiInvalidator(activity.handler, tvDisplayName)
note_invalidator = NetworkEmojiInvalidator(activity.handler, tvNote)
moved_caption_invalidator = NetworkEmojiInvalidator(activity.handler, tvMoved)
moved_name_invalidator = NetworkEmojiInvalidator(activity.handler, tvMovedName)
ivBackground.measureProfileBg = true
}
override fun showColor() {
2019-01-18 16:33:53 +01:00
llProfile.setBackgroundColor(
when(val c = column.column_bg_color) {
0 -> getAttributeColor(activity, R.attr.colorProfileBackgroundMask)
else -> - 0x40000000 or (0x00ffffff and c)
}
)
}
2018-11-18 03:38:52 +01:00
private var contentColor = 0
override fun bindData(column : Column) {
super.bindData(column)
2018-11-18 03:38:52 +01:00
var f : Float
2018-11-18 03:38:52 +01:00
f = activity.timeline_font_size_sp
if(! f.isNaN()) {
tvMovedName.textSize = f
tvMoved.textSize = f
}
2018-11-18 03:38:52 +01:00
f = activity.acct_font_size_sp
if(! f.isNaN()) {
tvMovedAcct.textSize = f
tvCreated.textSize = f
tvLastStatusAt.textSize = f
}
val contentColor = column.getContentColor()
2018-11-18 03:38:52 +01:00
this.contentColor = contentColor
tvMoved.textColor = contentColor
tvMovedName.textColor = contentColor
tvDisplayName.textColor = contentColor
tvNote.textColor = contentColor
tvRemoteProfileWarning.textColor = contentColor
btnStatusCount.textColor = contentColor
btnFollowing.textColor = contentColor
btnFollowers.textColor = contentColor
2018-12-01 00:02:18 +01:00
setIconDrawableId(
activity,
btnMore,
R.drawable.ic_more,
color = contentColor,
alphaMultiplier = Styler.boost_alpha
)
2018-11-18 03:38:52 +01:00
val acctColor = column.getAcctColor()
2018-11-18 03:38:52 +01:00
tvCreated.textColor = acctColor
tvMovedAcct.textColor = acctColor
tvLastStatusAt.textColor = acctColor
2018-11-18 03:38:52 +01:00
val whoRef = column.who_account
this.whoRef = whoRef
2018-05-18 19:08:46 +02:00
val who = whoRef?.get()
2018-11-18 03:38:52 +01:00
// Misskeyの場合はNote中のUserエンティティと /api/users/show の情報量がかなり異なる
val whoDetail = if(who == null) {
null
} else {
MisskeyAccountDetailMap.get(access_info, who.id)
}
showColor()
llMoved.visibility = View.GONE
tvMoved.visibility = View.GONE
llFields.visibility = View.GONE
llFields.removeAllViews()
if(who == null) {
tvCreated.text = ""
vg(tvLastStatusAt,false)
ivBackground.setImageDrawable(null)
ivAvatar.setImageDrawable(null)
tvAcct.text = "@"
tvDisplayName.text = ""
name_invalidator.register(null)
tvNote.text = ""
2019-01-18 16:33:53 +01:00
tvMisskeyExtra.text = ""
note_invalidator.register(null)
btnStatusCount.text = activity.getString(R.string.statuses) + "\n" + "?"
btnFollowing.text = activity.getString(R.string.following) + "\n" + "?"
btnFollowers.text = activity.getString(R.string.followers) + "\n" + "?"
btnFollow.setImageDrawable(null)
tvRemoteProfileWarning.visibility = View.GONE
} else {
2018-11-18 03:38:52 +01:00
tvCreated.text =
TootStatus.formatTime(tvCreated.context, (whoDetail ?: who).time_created_at, true)
if( vg( tvLastStatusAt,who.last_status_at > 0L)){
tvLastStatusAt.text = activity.getString(R.string.last_active)+": "+TootStatus.formatTime(activity,who.last_status_at,true)
}
ivBackground.setImageUrl(
activity.pref,
0f,
access_info.supplyBaseUrl(who.header_static)
)
ivAvatar.setImageUrl(
activity.pref,
Styler.calcIconRound(ivAvatar.layoutParams),
access_info.supplyBaseUrl(who.avatar_static),
access_info.supplyBaseUrl(who.avatar)
)
val name = whoDetail?.decodeDisplayName(activity) ?: whoRef.decoded_display_name
tvDisplayName.text = name
name_invalidator.register(name)
tvRemoteProfileWarning.visibility =
if(column.access_info.isRemoteUser(who)) View.VISIBLE else View.GONE
2019-01-18 16:33:53 +01:00
fun SpannableStringBuilder.appendSpan(text : String, span : Any) {
val start = length
append(text)
setSpan(
span,
start,
length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
2019-01-18 16:33:53 +01:00
tvAcct.text = SpannableStringBuilder().apply {
append("@")
2019-01-18 16:33:53 +01:00
append(access_info.getFullAcct(who))
2019-01-18 16:33:53 +01:00
if(whoDetail?.locked ?: who.locked) {
append(" ")
val info = EmojiMap201709.sShortNameToImageId["lock"]
if(info != null) {
appendSpan("locked", EmojiImageSpan(activity, info.image_id))
2019-01-18 16:33:53 +01:00
} else {
append("locked")
}
}
2019-01-18 16:33:53 +01:00
if(who.bot) {
append(" ")
val info = EmojiMap201709.sShortNameToImageId["robot_face"]
if(info != null) {
appendSpan("bot", EmojiImageSpan(activity, info.image_id))
2019-01-18 16:33:53 +01:00
} else {
append("bot")
}
}
}
val note = whoRef.decoded_note
tvNote.text = note
note_invalidator.register(note)
2019-01-18 16:33:53 +01:00
tvMisskeyExtra.text = SpannableStringBuilder().apply {
var s = whoDetail?.location
2019-01-18 16:33:53 +01:00
if(s?.isNotEmpty() == true) {
if(isNotEmpty()) append('\n')
appendSpan(
activity.getString(R.string.location),
EmojiImageSpan(
2019-01-18 16:33:53 +01:00
activity,
R.drawable.ic_location,
useColorShader = true
)
)
append(' ')
append(s)
}
s = whoDetail?.birthday
2019-01-18 16:33:53 +01:00
if(s?.isNotEmpty() == true) {
if(isNotEmpty()) append('\n')
appendSpan(
activity.getString(R.string.birthday),
EmojiImageSpan(
activity,
R.drawable.ic_cake,
useColorShader = true
2019-01-18 16:33:53 +01:00
)
)
append(' ')
append(s)
}
}
2019-01-18 16:33:53 +01:00
vg(tvMisskeyExtra, tvMisskeyExtra.text.isNotEmpty())
btnStatusCount.text = activity.getString(R.string.statuses) + "\n" +
(whoDetail?.statuses_count ?: who.statuses_count)
btnFollowing.text = activity.getString(R.string.following) + "\n" +
(whoDetail?.following_count ?: who.following_count)
btnFollowers.text = activity.getString(R.string.followers) + "\n" +
(whoDetail?.followers_count ?: who.followers_count)
val relation = UserRelation.load(access_info.db_id, who.id)
2019-01-18 16:33:53 +01:00
Styler.setFollowIcon(
activity,
btnFollow,
ivFollowedBy,
relation,
who,
contentColor,
alphaMultiplier = Styler.boost_alpha
)
showMoved(who, who.movedRef)
val fields = whoDetail?.fields ?: who.fields
if(fields != null) {
llFields.visibility = View.VISIBLE
// fieldsのnameにはカスタム絵文字が適用されない
val nameDecodeOptions = DecodeOptions(
context = activity,
decodeEmoji = true,
linkHelper = access_info,
short = true,
emojiMapProfile = who.profile_emojis
)
2019-01-18 16:33:53 +01:00
// valueはMisskeyならMFM、MastodonならHTML
val valueDecodeOptions = DecodeOptions(
context = activity,
decodeEmoji = true,
linkHelper = access_info,
short = true,
emojiMapCustom = who.custom_emojis,
emojiMapProfile = who.profile_emojis
)
2018-08-22 03:05:54 +02:00
val nameTypeface = ActMain.timeline_font_bold
val valueTypeface = ActMain.timeline_font
for(item in fields) {
//
val nameView = MyTextView(activity)
val nameLp = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
val nameText = nameDecodeOptions.decodeEmoji(item.name)
val nameInvalidator = NetworkEmojiInvalidator(activity.handler, nameView)
nameInvalidator.register(nameText)
nameLp.topMargin = (density * 6f).toInt()
nameView.layoutParams = nameLp
nameView.text = nameText
2018-11-18 03:38:52 +01:00
nameView.setTextColor(contentColor)
nameView.typeface = nameTypeface
nameView.movementMethod = MyLinkMovementMethod
llFields.addView(nameView)
// 値の方はHTMLエンコードされている
val valueView = MyTextView(activity)
val valueLp = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
val valueText = valueDecodeOptions.decodeHTML(item.value)
if(item.verified_at > 0L) {
valueText.append('\n')
val start = valueText.length
valueText.append(activity.getString(R.string.verified_at))
valueText.append(": ")
valueText.append(TootStatus.formatTime(activity, item.verified_at, false))
val end = valueText.length
valueText.setSpan(
ForegroundColorSpan(Color.BLACK or 0x7fbc99)
, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
}
val valueInvalidator = NetworkEmojiInvalidator(activity.handler, valueView)
valueInvalidator.register(valueText)
valueLp.startMargin = (density * 32f).toInt()
valueView.layoutParams = valueLp
valueView.text = valueText
2018-11-18 03:38:52 +01:00
valueView.setTextColor(contentColor)
valueView.typeface = valueTypeface
valueView.movementMethod = MyLinkMovementMethod
if(item.verified_at > 0L) {
valueView.setBackgroundColor(0x337fbc99)
}
llFields.addView(valueView)
}
}
}
}
private fun showMoved(who : TootAccount, movedRef : TootAccountRef?) {
if(movedRef == null) return
this.movedRef = movedRef
2018-05-18 19:08:46 +02:00
val moved = movedRef.get()
llMoved.visibility = View.VISIBLE
tvMoved.visibility = View.VISIBLE
2018-12-01 00:02:18 +01:00
val caption = who.decodeDisplayName(activity)
.intoStringResource(activity, R.string.account_moved_to)
tvMoved.text = caption
moved_caption_invalidator.register(caption)
ivMoved.layoutParams.width = activity.avatarIconSize
ivMoved.setImageUrl(
activity.pref,
Styler.calcIconRound(ivMoved.layoutParams),
access_info.supplyBaseUrl(moved.avatar_static)
)
tvMovedName.text = movedRef.decoded_display_name
moved_name_invalidator.register(movedRef.decoded_display_name)
setAcct(tvMovedAcct, access_info.getFullAcct(moved), moved.acct)
val relation = UserRelation.load(access_info.db_id, moved.id)
2019-01-18 16:33:53 +01:00
Styler.setFollowIcon(
activity,
btnMoved,
ivMovedBy,
relation,
moved,
contentColor,
alphaMultiplier = Styler.boost_alpha
)
}
private fun setAcct(tv : TextView, acctLong : String, acctShort : String) {
val ac = AcctColor.load(acctLong)
tv.text = when {
AcctColor.hasNickname(ac) -> ac.nickname
2018-11-18 03:38:52 +01:00
Pref.bpShortAcctLocalUser(App1.pref) -> "@$acctShort"
else -> acctLong
}
tv.textColor = ac.color_fg.notZero() ?: column.getAcctColor()
tv.setBackgroundColor(ac.color_bg) // may 0
tv.setPaddingRelative(activity.acct_pad_lr, 0, activity.acct_pad_lr, 0)
}
override fun onClick(v : View) {
when(v.id) {
2018-05-18 19:08:46 +02:00
R.id.ivBackground, R.id.tvRemoteProfileWarning -> whoRef?.get()?.url?.let { url ->
App1.openCustomTab(activity, url)
}
R.id.btnFollowing -> {
2019-08-23 01:49:20 +02:00
column.profile_tab = ProfileTab.Following
activity.app_state.saveColumnList()
column.startLoading()
}
R.id.btnFollowers -> {
2019-08-23 01:49:20 +02:00
column.profile_tab = ProfileTab.Followers
activity.app_state.saveColumnList()
column.startLoading()
}
R.id.btnStatusCount -> {
2019-08-23 01:49:20 +02:00
column.profile_tab = ProfileTab.Status
activity.app_state.saveColumnList()
column.startLoading()
}
R.id.btnMore -> whoRef?.let { whoRef ->
2019-01-18 16:33:53 +01:00
DlgContextMenu(activity, column, whoRef, null, null, null).show()
}
R.id.btnFollow -> whoRef?.let { whoRef ->
2019-01-18 16:33:53 +01:00
DlgContextMenu(activity, column, whoRef, null, null, null).show()
}
R.id.btnMoved -> movedRef?.let { movedRef ->
2019-01-18 16:33:53 +01:00
DlgContextMenu(activity, column, movedRef, null, null, null).show()
}
R.id.llMoved -> movedRef?.let { movedRef ->
2018-01-11 10:31:25 +01:00
if(access_info.isPseudo) {
2019-01-18 16:33:53 +01:00
DlgContextMenu(activity, column, movedRef, null, null, null).show()
2018-01-11 10:31:25 +01:00
} else {
Action_User.profileLocal(
activity,
activity.nextPosition(column),
access_info,
2018-05-18 19:08:46 +02:00
movedRef.get()
)
2018-01-11 10:31:25 +01:00
}
}
}
}
override fun onLongClick(v : View) : Boolean {
when(v.id) {
R.id.btnFollow -> {
Action_Follow.followFromAnotherAccount(
activity,
activity.nextPosition(column),
access_info,
2018-05-18 19:08:46 +02:00
whoRef?.get()
)
return true
}
R.id.btnMoved -> {
Action_Follow.followFromAnotherAccount(
activity,
activity.nextPosition(column),
access_info,
2018-05-18 19:08:46 +02:00
movedRef?.get()
)
return true
}
}
return false
}
override fun onViewRecycled() {
}
2018-11-18 03:38:52 +01:00
// fun updateRelativeTime() {
// val who = whoRef?.get()
// if(who != null) {
// tvCreated.text = TootStatus.formatTime(tvCreated.context, who.time_created_at, true)
// }
// }
override fun getAccount() : TootAccountRef? = whoRef
}