2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.res.ColorStateList
|
|
|
|
import android.graphics.Color
|
|
|
|
import android.graphics.PorterDuff
|
2018-08-20 19:37:42 +02:00
|
|
|
import android.graphics.drawable.*
|
2018-01-04 19:52:25 +01:00
|
|
|
import android.graphics.drawable.shapes.RectShape
|
|
|
|
import android.os.Build
|
|
|
|
import android.support.v4.content.ContextCompat
|
|
|
|
import android.text.SpannableStringBuilder
|
|
|
|
import android.text.Spanned
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
|
|
|
import android.widget.ImageButton
|
|
|
|
import android.widget.ImageView
|
|
|
|
import jp.juggler.subwaytooter.api.entity.TootAccount
|
2018-08-20 19:37:42 +02:00
|
|
|
import jp.juggler.subwaytooter.api.entity.TootVisibility
|
2018-01-04 19:52:25 +01:00
|
|
|
import jp.juggler.subwaytooter.span.EmojiImageSpan
|
2018-08-20 19:37:42 +02:00
|
|
|
import jp.juggler.subwaytooter.table.UserRelation
|
|
|
|
import java.util.*
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
object Styler {
|
|
|
|
|
|
|
|
fun getAttributeColor(context : Context, attrId : Int) : Int {
|
|
|
|
val theme = context.theme
|
|
|
|
val a = theme.obtainStyledAttributes(intArrayOf(attrId))
|
|
|
|
val color = a.getColor(0, Color.BLACK)
|
|
|
|
a.recycle()
|
|
|
|
return color
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getAttributeResourceId(context : Context, attrId : Int) : Int {
|
|
|
|
val theme = context.theme
|
|
|
|
val a = theme.obtainStyledAttributes(intArrayOf(attrId))
|
|
|
|
val resourceId = a.getResourceId(0, 0)
|
|
|
|
a.recycle()
|
|
|
|
if(resourceId == 0)
|
2018-01-17 18:39:16 +01:00
|
|
|
throw RuntimeException(
|
|
|
|
String.format(
|
|
|
|
Locale.JAPAN,
|
|
|
|
"attr not defined.attr_id=0x%x",
|
|
|
|
attrId
|
|
|
|
)
|
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
return resourceId
|
|
|
|
}
|
|
|
|
|
|
|
|
fun getAttributeDrawable(context : Context, attrId : Int) : Drawable {
|
|
|
|
val drawableId = getAttributeResourceId(context, attrId)
|
2018-01-19 10:27:35 +01:00
|
|
|
val d = ContextCompat.getDrawable(context, drawableId)
|
|
|
|
return d ?: throw RuntimeException(
|
|
|
|
String.format(
|
|
|
|
Locale.JAPAN,
|
|
|
|
"getDrawable failed. drawableId=0x%x",
|
|
|
|
drawableId
|
2018-01-17 18:39:16 +01:00
|
|
|
)
|
2018-01-19 10:27:35 +01:00
|
|
|
)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 20:29:00 +01:00
|
|
|
private fun setIconDrawableId(
|
|
|
|
context : Context,
|
|
|
|
imageView : ImageView,
|
|
|
|
drawableId : Int,
|
|
|
|
color:Int? = null
|
|
|
|
) {
|
|
|
|
if( color == null) {
|
|
|
|
// ImageViewにアイコンを設定する。デフォルトの色
|
|
|
|
imageView.setImageResource(drawableId)
|
|
|
|
}else{
|
|
|
|
// ImageViewにアイコンを設定する。色を変えてしまう
|
|
|
|
val d = ContextCompat.getDrawable(context,drawableId) ?: return
|
|
|
|
d.mutate() // 色指定が他のアイコンに影響しないようにする
|
|
|
|
d.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
|
|
|
|
imageView.setImageDrawable(d)
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
2018-10-30 20:29:00 +01:00
|
|
|
fun setIconAttr(
|
2018-01-17 18:39:16 +01:00
|
|
|
context : Context,
|
|
|
|
imageView : ImageView,
|
2018-10-30 20:29:00 +01:00
|
|
|
iconAttrId : Int,
|
|
|
|
color:Int? = null
|
2018-01-17 18:39:16 +01:00
|
|
|
) {
|
2018-10-30 20:29:00 +01:00
|
|
|
setIconDrawableId(context,imageView,getAttributeResourceId(context, iconAttrId),color)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
2018-08-20 19:37:42 +02:00
|
|
|
fun getVisibilityIconAttr(isMisskeyData:Boolean ,visibility : TootVisibility):Int {
|
|
|
|
val isMisskey = when(Pref.ipVisibilityStyle(App1.pref)){
|
|
|
|
Pref.VS_MASTODON -> false
|
|
|
|
Pref.VS_MISSKEY ->true
|
|
|
|
else-> isMisskeyData
|
|
|
|
}
|
|
|
|
return when{
|
|
|
|
isMisskey -> when(visibility) {
|
|
|
|
TootVisibility.Public -> R.attr.ic_public
|
|
|
|
TootVisibility.UnlistedHome -> R.attr.btn_home
|
|
|
|
TootVisibility.PrivateFollowers -> R.attr.ic_lock_open
|
|
|
|
TootVisibility.DirectSpecified -> R.attr.ic_mail
|
|
|
|
TootVisibility.DirectPrivate -> R.attr.ic_lock
|
|
|
|
TootVisibility.WebSetting -> R.attr.ic_question
|
2018-11-16 00:43:10 +01:00
|
|
|
|
|
|
|
TootVisibility.LocalPublic-> R.attr.ic_local_ltl
|
|
|
|
TootVisibility.LocalHome-> R.attr.ic_local_home
|
|
|
|
TootVisibility.LocalFollowers-> R.attr.ic_local_lock_open
|
|
|
|
|
2018-08-20 19:37:42 +02:00
|
|
|
}
|
|
|
|
else-> when(visibility) {
|
|
|
|
TootVisibility.Public -> R.attr.ic_public
|
|
|
|
TootVisibility.UnlistedHome -> R.attr.ic_lock_open
|
|
|
|
TootVisibility.PrivateFollowers -> R.attr.ic_lock
|
|
|
|
TootVisibility.DirectSpecified -> R.attr.ic_mail
|
|
|
|
TootVisibility.DirectPrivate -> R.attr.ic_mail
|
|
|
|
TootVisibility.WebSetting -> R.attr.ic_question
|
2018-11-16 00:43:10 +01:00
|
|
|
|
|
|
|
TootVisibility.LocalPublic-> R.attr.ic_local_ltl
|
|
|
|
TootVisibility.LocalHome-> R.attr.ic_local_lock_open
|
|
|
|
TootVisibility.LocalFollowers-> R.attr.ic_local_lock
|
|
|
|
|
2018-08-20 19:37:42 +02:00
|
|
|
}
|
2018-01-11 10:31:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 19:37:42 +02:00
|
|
|
fun getVisibilityIcon(context : Context, isMisskeyData:Boolean ,visibility : TootVisibility) : Int {
|
|
|
|
return getAttributeResourceId(context, getVisibilityIconAttr(isMisskeyData,visibility))
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
2018-08-20 19:37:42 +02:00
|
|
|
fun getVisibilityString(context : Context,isMisskeyData:Boolean ,visibility : TootVisibility):String {
|
|
|
|
val isMisskey = when(Pref.ipVisibilityStyle(App1.pref)){
|
|
|
|
Pref.VS_MASTODON -> false
|
|
|
|
Pref.VS_MISSKEY ->true
|
|
|
|
else-> isMisskeyData
|
|
|
|
}
|
2018-11-16 00:43:10 +01:00
|
|
|
return context.getString(when{
|
2018-08-20 19:37:42 +02:00
|
|
|
isMisskey -> when(visibility) {
|
2018-11-16 00:43:10 +01:00
|
|
|
TootVisibility.Public ->R.string.visibility_public
|
|
|
|
TootVisibility.UnlistedHome -> R.string.visibility_home
|
|
|
|
TootVisibility.PrivateFollowers -> R.string.visibility_followers
|
|
|
|
TootVisibility.DirectSpecified -> R.string.visibility_direct
|
|
|
|
TootVisibility.DirectPrivate -> R.string.visibility_private
|
|
|
|
TootVisibility.WebSetting -> R.string.visibility_web_setting
|
|
|
|
|
|
|
|
TootVisibility.LocalPublic-> R.string.visibility_local_public
|
|
|
|
TootVisibility.LocalHome-> R.string.visibility_local_home
|
|
|
|
TootVisibility.LocalFollowers-> R.string.visibility_local_followers
|
2018-08-20 19:37:42 +02:00
|
|
|
}
|
|
|
|
else-> when(visibility) {
|
2018-11-16 00:43:10 +01:00
|
|
|
TootVisibility.Public -> R.string.visibility_public
|
|
|
|
TootVisibility.UnlistedHome -> R.string.visibility_unlisted
|
|
|
|
TootVisibility.PrivateFollowers -> R.string.visibility_followers
|
|
|
|
TootVisibility.DirectSpecified -> R.string.visibility_direct
|
|
|
|
TootVisibility.DirectPrivate -> R.string.visibility_direct
|
|
|
|
TootVisibility.WebSetting -> R.string.visibility_web_setting
|
|
|
|
|
|
|
|
TootVisibility.LocalPublic-> R.string.visibility_local_public
|
|
|
|
TootVisibility.LocalHome-> R.string.visibility_local_unlisted
|
|
|
|
TootVisibility.LocalFollowers-> R.string.visibility_local_followers
|
2018-08-20 19:37:42 +02:00
|
|
|
}
|
2018-11-16 00:43:10 +01:00
|
|
|
})
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// アイコン付きの装飾テキストを返す
|
2018-08-20 19:37:42 +02:00
|
|
|
fun getVisibilityCaption(context : Context, isMisskeyData:Boolean ,visibility : TootVisibility) : CharSequence {
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-08-20 19:37:42 +02:00
|
|
|
val icon_id = getVisibilityIcon(context, isMisskeyData,visibility)
|
|
|
|
val sv = getVisibilityString(context, isMisskeyData,visibility)
|
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
val sb = SpannableStringBuilder()
|
2018-08-20 19:37:42 +02:00
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
// アイコン部分
|
|
|
|
val start = sb.length
|
2018-08-20 19:37:42 +02:00
|
|
|
sb.append(" ")
|
2018-01-04 19:52:25 +01:00
|
|
|
val end = sb.length
|
|
|
|
sb.setSpan(EmojiImageSpan(context, icon_id), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
|
|
|
|
|
|
|
|
// 文字列部分
|
|
|
|
sb.append(' ')
|
2018-08-20 19:37:42 +02:00
|
|
|
sb.append(sv)
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
return sb
|
|
|
|
}
|
|
|
|
|
|
|
|
fun setFollowIcon(
|
|
|
|
context : Context
|
|
|
|
, ibFollow : ImageButton
|
|
|
|
, ivDot : ImageView
|
|
|
|
, relation : UserRelation
|
|
|
|
, who : TootAccount
|
|
|
|
) {
|
|
|
|
|
|
|
|
// 被フォロー状態
|
2018-10-30 20:29:00 +01:00
|
|
|
when {
|
|
|
|
|
|
|
|
relation.blocked_by -> {
|
|
|
|
ivDot.visibility = View.VISIBLE
|
|
|
|
setIconDrawableId(context, ivDot, R.drawable.ic_blocked_by,
|
|
|
|
color=Styler.getAttributeColor(context, R.attr.colorRegexFilterError)
|
|
|
|
)
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
|
2018-10-30 20:29:00 +01:00
|
|
|
relation.requested_by -> {
|
|
|
|
ivDot.visibility = View.VISIBLE
|
|
|
|
setIconDrawableId(context, ivDot, R.drawable.ic_requested_by,
|
|
|
|
color=Styler.getAttributeColor(context, R.attr.colorRegexFilterError)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
relation.followed_by-> {
|
|
|
|
ivDot.visibility = View.VISIBLE
|
|
|
|
setIconAttr(context, ivDot, R.attr.ic_followed_by)
|
|
|
|
|
|
|
|
// 被フォローリクエスト状態の時に followed_by が 真と偽の両方がありえるようなので
|
|
|
|
// Relationshipだけを見ても被フォローリクエスト状態は分からないっぽい
|
|
|
|
// 仕方ないので馬鹿正直に「 followed_byが真ならバッジをつける」しかできない
|
|
|
|
}
|
|
|
|
|
|
|
|
else -> {
|
|
|
|
ivDot.visibility = View.GONE
|
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// フォローボタン
|
|
|
|
// follow button
|
|
|
|
val color_attr : Int
|
|
|
|
val icon_attr : Int
|
2018-08-19 06:19:33 +02:00
|
|
|
val contentDescription : String
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
when {
|
|
|
|
relation.blocking -> {
|
|
|
|
icon_attr = R.attr.ic_block
|
|
|
|
color_attr = R.attr.colorImageButton
|
2018-08-19 06:19:33 +02:00
|
|
|
contentDescription = context.getString(R.string.follow)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
relation.muting -> {
|
|
|
|
icon_attr = R.attr.ic_mute
|
|
|
|
color_attr = R.attr.colorImageButton
|
2018-08-19 06:19:33 +02:00
|
|
|
contentDescription = context.getString(R.string.follow)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
relation.getFollowing(who) -> {
|
|
|
|
icon_attr = R.attr.ic_follow_cross
|
|
|
|
color_attr = R.attr.colorImageButtonAccent
|
2018-08-19 06:19:33 +02:00
|
|
|
contentDescription = context.getString(R.string.unfollow)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
relation.getRequested(who) -> {
|
|
|
|
icon_attr = R.attr.ic_follow_wait
|
|
|
|
color_attr = R.attr.colorRegexFilterError
|
2018-08-19 06:19:33 +02:00
|
|
|
contentDescription = context.getString(R.string.unfollow)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
else -> {
|
|
|
|
icon_attr = R.attr.ic_follow_plus
|
|
|
|
color_attr = R.attr.colorImageButton
|
2018-08-19 06:19:33 +02:00
|
|
|
contentDescription = context.getString(R.string.follow)
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-30 20:29:00 +01:00
|
|
|
setIconAttr(context, ibFollow, icon_attr, color =Styler.getAttributeColor(context, color_attr) )
|
2018-08-19 06:19:33 +02:00
|
|
|
ibFollow.contentDescription = contentDescription
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 色を指定してRippleDrawableを生成する
|
|
|
|
fun getAdaptiveRippleDrawable(normalColor : Int, pressedColor : Int) : Drawable {
|
|
|
|
return if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
|
|
RippleDrawable(
|
|
|
|
ColorStateList.valueOf(pressedColor), getRectShape(normalColor), null
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
getStateListDrawable(normalColor, pressedColor)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 色を指定してRectShapeを生成する
|
|
|
|
private fun getRectShape(color : Int) : Drawable {
|
|
|
|
val r = RectShape()
|
|
|
|
val shapeDrawable = ShapeDrawable(r)
|
|
|
|
shapeDrawable.paint.color = color
|
|
|
|
return shapeDrawable
|
|
|
|
}
|
|
|
|
|
|
|
|
// 後方互換用にボタン背景Drawableを生成する
|
|
|
|
private fun getStateListDrawable(normalColor : Int, pressedColor : Int) : StateListDrawable {
|
|
|
|
val states = StateListDrawable()
|
|
|
|
states.addState(intArrayOf(android.R.attr.state_pressed), ColorDrawable(pressedColor))
|
|
|
|
states.addState(intArrayOf(android.R.attr.state_focused), ColorDrawable(pressedColor))
|
|
|
|
states.addState(intArrayOf(android.R.attr.state_activated), ColorDrawable(pressedColor))
|
|
|
|
states.addState(intArrayOf(), ColorDrawable(normalColor))
|
|
|
|
return states
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun getHorizontalPadding(v : View, delta_dp : Float) : Int {
|
|
|
|
val form_width_max = 420f
|
|
|
|
val dm = v.resources.displayMetrics
|
|
|
|
val screen_w = dm.widthPixels
|
|
|
|
val content_w = (0.5f + form_width_max * dm.density).toInt()
|
|
|
|
val pad_lr = (screen_w - content_w) / 2
|
|
|
|
return (if(pad_lr < 0) 0 else pad_lr) + (0.5f + delta_dp * dm.density).toInt()
|
|
|
|
}
|
|
|
|
|
|
|
|
fun fixHorizontalPadding(v : View) {
|
|
|
|
val pad_lr = getHorizontalPadding(v, 12f)
|
|
|
|
val pad_t = v.paddingTop
|
|
|
|
val pad_b = v.paddingBottom
|
|
|
|
v.setPaddingRelative(pad_lr, pad_t, pad_lr, pad_b)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun fixHorizontalPadding2(v : View) {
|
|
|
|
val pad_lr = getHorizontalPadding(v, 0f)
|
|
|
|
val pad_t = v.paddingTop
|
|
|
|
val pad_b = v.paddingBottom
|
|
|
|
v.setPaddingRelative(pad_lr, pad_t, pad_lr, pad_b)
|
|
|
|
}
|
|
|
|
|
|
|
|
fun fixHorizontalMargin(v : View) {
|
|
|
|
val pad_lr = getHorizontalPadding(v, 0f)
|
|
|
|
val lp = v.layoutParams
|
|
|
|
if(lp is ViewGroup.MarginLayoutParams) {
|
|
|
|
lp.leftMargin = pad_lr
|
|
|
|
lp.rightMargin = pad_lr
|
|
|
|
}
|
|
|
|
}
|
2018-01-18 08:53:32 +01:00
|
|
|
|
|
|
|
// ActMainの初期化時に更新される
|
2018-01-19 10:27:35 +01:00
|
|
|
var round_ratio : Float = 0.33f * 0.5f
|
2018-01-18 08:53:32 +01:00
|
|
|
|
2018-01-19 10:27:35 +01:00
|
|
|
fun calcIconRound(wh : Int) = wh.toFloat() * round_ratio
|
|
|
|
|
|
|
|
fun calcIconRound(lp : ViewGroup.LayoutParams) =
|
|
|
|
Math.min(lp.width, lp.height).toFloat() * round_ratio
|
2018-01-18 08:53:32 +01:00
|
|
|
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|
2018-04-21 01:16:44 +02:00
|
|
|
|
|
|
|
fun SpannableStringBuilder.appendColorShadeIcon(
|
|
|
|
context:Context,
|
|
|
|
drawable_id:Int,
|
2018-10-28 14:08:10 +01:00
|
|
|
text:String,
|
|
|
|
color : Int? = null
|
2018-08-21 03:53:52 +02:00
|
|
|
):SpannableStringBuilder{
|
2018-04-21 01:16:44 +02:00
|
|
|
val start = this.length
|
|
|
|
this.append(text)
|
|
|
|
val end = this.length
|
|
|
|
this.setSpan(
|
2018-10-28 14:08:10 +01:00
|
|
|
EmojiImageSpan(context, drawable_id,useColorShader=true,color=color),
|
2018-04-21 01:16:44 +02:00
|
|
|
start,
|
|
|
|
end,
|
|
|
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
|
|
|
|
)
|
2018-08-21 03:53:52 +02:00
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
fun SpannableStringBuilder.appendDrawableIcon(
|
|
|
|
context:Context,
|
|
|
|
drawable_id:Int,
|
|
|
|
text:String
|
|
|
|
):SpannableStringBuilder{
|
|
|
|
val start = this.length
|
|
|
|
this.append(text)
|
|
|
|
val end = this.length
|
|
|
|
this.setSpan(
|
|
|
|
EmojiImageSpan(context, drawable_id),
|
|
|
|
start,
|
|
|
|
end,
|
|
|
|
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
|
|
|
|
)
|
|
|
|
return this
|
2018-04-21 01:16:44 +02:00
|
|
|
}
|