rename class NicoEnquete to TootPolls
This commit is contained in:
parent
9e1f390285
commit
ed4daf950f
|
@ -723,11 +723,11 @@ class ActPost : AppCompatActivity(),
|
|||
val src_enquete = base_status.enquete
|
||||
val src_items = src_enquete?.items
|
||||
if(src_items != null) {
|
||||
if(src_enquete.pollType == PollType.FriendsNico && src_enquete.type != NicoEnquete.TYPE_ENQUETE) {
|
||||
if(src_enquete.pollType == TootPollsType.FriendsNico && src_enquete.type != TootPolls.TYPE_ENQUETE) {
|
||||
// フレニコAPIのアンケート結果は再編集の対象外
|
||||
} else {
|
||||
spEnquete.setSelection(
|
||||
if(src_enquete.pollType == PollType.FriendsNico) {
|
||||
if(src_enquete.pollType == TootPollsType.FriendsNico) {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
|
@ -2241,7 +2241,7 @@ class ActPost : AppCompatActivity(),
|
|||
when(spEnquete.selectedItemPosition) {
|
||||
1 -> {
|
||||
copyEnqueteText()
|
||||
post_helper.poll_type = PollType.Mastodon
|
||||
post_helper.poll_type = TootPollsType.Mastodon
|
||||
post_helper.poll_expire_seconds = getExpireSeconds()
|
||||
post_helper.poll_hide_totals = cbHideTotals.isChecked
|
||||
post_helper.poll_multiple_choice = cbMultipleChoice.isChecked
|
||||
|
@ -2249,7 +2249,7 @@ class ActPost : AppCompatActivity(),
|
|||
|
||||
2 -> {
|
||||
copyEnqueteText()
|
||||
post_helper.poll_type = PollType.FriendsNico
|
||||
post_helper.poll_type = TootPollsType.FriendsNico
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -140,19 +140,19 @@ class ActText : AppCompatActivity(), View.OnClickListener {
|
|||
|
||||
// friends.nico の場合は本文に投票の選択肢が含まれるので
|
||||
// アプリ側での文字列化は不要
|
||||
PollType.FriendsNico -> return
|
||||
TootPollsType.FriendsNico -> return
|
||||
|
||||
// MastodonとMisskeyは投票の選択肢が本文に含まれないので
|
||||
// アプリ側で文字列化する
|
||||
|
||||
PollType.Mastodon -> when {
|
||||
TootPollsType.Mastodon -> when {
|
||||
enquete.expired -> false
|
||||
now >= enquete.expired_at -> false
|
||||
enquete.myVoted != null -> false
|
||||
else -> true
|
||||
}
|
||||
|
||||
PollType.Misskey -> enquete.myVoted == null
|
||||
TootPollsType.Misskey -> enquete.myVoted == null
|
||||
}
|
||||
|
||||
sb.addAfterLine("\n")
|
||||
|
@ -162,7 +162,7 @@ class ActText : AppCompatActivity(), View.OnClickListener {
|
|||
}
|
||||
|
||||
when(enquete.pollType) {
|
||||
PollType.Mastodon -> encodePollFooterMastodon(sb, context, enquete)
|
||||
TootPollsType.Mastodon -> encodePollFooterMastodon(sb, context, enquete)
|
||||
|
||||
else->{}
|
||||
}
|
||||
|
@ -171,14 +171,14 @@ class ActText : AppCompatActivity(), View.OnClickListener {
|
|||
private fun encodePollChoice(
|
||||
sb : StringBuilder,
|
||||
context : Context,
|
||||
enquete : NicoEnquete,
|
||||
enquete : TootPolls,
|
||||
canVote : Boolean,
|
||||
i : Int,
|
||||
item : NicoEnquete.Choice
|
||||
item : TootPollsChoice
|
||||
) {
|
||||
|
||||
val text = when(enquete.pollType) {
|
||||
PollType.Misskey -> {
|
||||
TootPollsType.Misskey -> {
|
||||
val sb2 = StringBuilder().append(item.decoded_text)
|
||||
if(enquete.myVoted != null) {
|
||||
sb2.append(" / ")
|
||||
|
@ -188,11 +188,11 @@ class ActText : AppCompatActivity(), View.OnClickListener {
|
|||
sb2
|
||||
}
|
||||
|
||||
PollType.FriendsNico -> {
|
||||
TootPollsType.FriendsNico -> {
|
||||
item.decoded_text
|
||||
}
|
||||
|
||||
PollType.Mastodon -> if(canVote) {
|
||||
TootPollsType.Mastodon -> if(canVote) {
|
||||
item.decoded_text
|
||||
} else {
|
||||
val sb2 = StringBuilder().append(item.decoded_text)
|
||||
|
@ -215,7 +215,7 @@ class ActText : AppCompatActivity(), View.OnClickListener {
|
|||
private fun encodePollFooterMastodon(
|
||||
sb : StringBuilder,
|
||||
context : Context,
|
||||
enquete : NicoEnquete
|
||||
enquete : TootPolls
|
||||
) {
|
||||
val line = StringBuilder()
|
||||
|
||||
|
|
|
@ -10,13 +10,11 @@ import android.text.Spannable
|
|||
import android.text.SpannableString
|
||||
import android.text.SpannableStringBuilder
|
||||
import android.text.TextUtils
|
||||
import android.util.LayoutDirection
|
||||
import android.util.TypedValue
|
||||
import android.view.Gravity
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.*
|
||||
import androidx.core.view.ViewCompat
|
||||
import com.google.android.flexbox.FlexWrap
|
||||
import com.google.android.flexbox.FlexboxLayout
|
||||
import com.google.android.flexbox.JustifyContent
|
||||
|
@ -1210,7 +1208,7 @@ internal class ItemViewHolder(
|
|||
// ニコフレのアンケートの表示
|
||||
val enquete = status.enquete
|
||||
if(enquete != null) {
|
||||
if(enquete.pollType == PollType.FriendsNico && enquete.type != NicoEnquete.TYPE_ENQUETE) {
|
||||
if(enquete.pollType == TootPollsType.FriendsNico && enquete.type != TootPolls.TYPE_ENQUETE) {
|
||||
// フレニコの投票の結果表示は普通にテキストを表示するだけでよい
|
||||
} else {
|
||||
|
||||
|
@ -2518,25 +2516,25 @@ internal class ItemViewHolder(
|
|||
})
|
||||
}
|
||||
|
||||
private fun showEnqueteItems(status : TootStatus, enquete : NicoEnquete) {
|
||||
private fun showEnqueteItems(status : TootStatus, enquete : TootPolls) {
|
||||
val items = enquete.items ?: return
|
||||
|
||||
val now = System.currentTimeMillis()
|
||||
|
||||
val canVote = when(enquete.pollType) {
|
||||
PollType.Mastodon -> when {
|
||||
TootPollsType.Mastodon -> when {
|
||||
enquete.expired -> false
|
||||
now >= enquete.expired_at -> false
|
||||
enquete.myVoted != null -> false
|
||||
else -> true
|
||||
}
|
||||
|
||||
PollType.FriendsNico -> {
|
||||
val remain = enquete.time_start + NicoEnquete.ENQUETE_EXPIRE - now
|
||||
TootPollsType.FriendsNico -> {
|
||||
val remain = enquete.time_start + TootPolls.ENQUETE_EXPIRE - now
|
||||
enquete.myVoted == null && remain > 0L
|
||||
}
|
||||
|
||||
PollType.Misskey -> enquete.myVoted == null
|
||||
TootPollsType.Misskey -> enquete.myVoted == null
|
||||
}
|
||||
|
||||
items.forEachIndexed { index, choice ->
|
||||
|
@ -2544,25 +2542,25 @@ internal class ItemViewHolder(
|
|||
}
|
||||
|
||||
when(enquete.pollType) {
|
||||
PollType.Mastodon -> makeEnqueteFooterMastodon(status, enquete, canVote)
|
||||
TootPollsType.Mastodon -> makeEnqueteFooterMastodon(status, enquete, canVote)
|
||||
|
||||
PollType.FriendsNico -> makeEnqueteFooterFriendsNico(enquete)
|
||||
TootPollsType.FriendsNico -> makeEnqueteFooterFriendsNico(enquete)
|
||||
|
||||
PollType.Misskey -> {
|
||||
TootPollsType.Misskey -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun makeEnqueteChoiceView(
|
||||
status : TootStatus,
|
||||
enquete : NicoEnquete,
|
||||
enquete : TootPolls,
|
||||
canVote : Boolean,
|
||||
i : Int,
|
||||
item : NicoEnquete.Choice
|
||||
item : TootPollsChoice
|
||||
) {
|
||||
|
||||
val text = when(enquete.pollType) {
|
||||
PollType.Misskey -> {
|
||||
TootPollsType.Misskey -> {
|
||||
val sb = SpannableStringBuilder()
|
||||
.append(item.decoded_text)
|
||||
|
||||
|
@ -2574,11 +2572,11 @@ internal class ItemViewHolder(
|
|||
sb
|
||||
}
|
||||
|
||||
PollType.FriendsNico -> {
|
||||
TootPollsType.FriendsNico -> {
|
||||
item.decoded_text
|
||||
}
|
||||
|
||||
PollType.Mastodon -> if(canVote) {
|
||||
TootPollsType.Mastodon -> if(canVote) {
|
||||
item.decoded_text
|
||||
} else {
|
||||
val sb = SpannableStringBuilder()
|
||||
|
@ -2617,7 +2615,7 @@ internal class ItemViewHolder(
|
|||
b.padding = (activity.density * 3f + 0.5f).toInt()
|
||||
|
||||
val ratio = when(enquete.pollType){
|
||||
PollType.Mastodon ->{
|
||||
TootPollsType.Mastodon ->{
|
||||
val votesCount = enquete.votes_count ?:0
|
||||
val max = enquete.maxVotesCount ?:0
|
||||
if( max > 0 && votesCount > 0 ){
|
||||
|
@ -2687,19 +2685,19 @@ internal class ItemViewHolder(
|
|||
}
|
||||
}
|
||||
|
||||
private fun makeEnqueteFooterFriendsNico(enquete : NicoEnquete) {
|
||||
private fun makeEnqueteFooterFriendsNico(enquete : TootPolls) {
|
||||
val density = activity.density
|
||||
val height = (0.5f + 6 * density).toInt()
|
||||
val view = EnqueteTimerView(activity)
|
||||
view.layoutParams =
|
||||
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, height)
|
||||
view.setParams(enquete.time_start, NicoEnquete.ENQUETE_EXPIRE)
|
||||
view.setParams(enquete.time_start, TootPolls.ENQUETE_EXPIRE)
|
||||
llExtra.addView(view)
|
||||
}
|
||||
|
||||
private fun makeEnqueteFooterMastodon(
|
||||
status : TootStatus,
|
||||
enquete : NicoEnquete,
|
||||
enquete : TootPolls,
|
||||
canVote : Boolean
|
||||
) {
|
||||
|
||||
|
@ -2765,7 +2763,7 @@ internal class ItemViewHolder(
|
|||
|
||||
private fun onClickEnqueteChoice(
|
||||
status : TootStatus,
|
||||
enquete : NicoEnquete,
|
||||
enquete : TootPolls,
|
||||
context : Context,
|
||||
accessInfo : SavedAccount,
|
||||
idx : Int
|
||||
|
@ -2778,19 +2776,19 @@ internal class ItemViewHolder(
|
|||
val now = System.currentTimeMillis()
|
||||
|
||||
when(enquete.pollType) {
|
||||
PollType.Misskey -> {
|
||||
TootPollsType.Misskey -> {
|
||||
// Misskeyのアンケートには期限がない?
|
||||
}
|
||||
|
||||
PollType.FriendsNico -> {
|
||||
val remain = enquete.time_start + NicoEnquete.ENQUETE_EXPIRE - now
|
||||
TootPollsType.FriendsNico -> {
|
||||
val remain = enquete.time_start + TootPolls.ENQUETE_EXPIRE - now
|
||||
if(remain <= 0L) {
|
||||
showToast(context, false, R.string.enquete_was_end)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
PollType.Mastodon -> {
|
||||
TootPollsType.Mastodon -> {
|
||||
if(enquete.expired || now >= enquete.expired_at) {
|
||||
showToast(context, false, R.string.enquete_was_end)
|
||||
return
|
||||
|
@ -2800,14 +2798,14 @@ internal class ItemViewHolder(
|
|||
|
||||
TootTaskRunner(context).run(accessInfo, object : TootTask {
|
||||
override fun background(client : TootApiClient) = when(enquete.pollType) {
|
||||
PollType.Misskey -> client.request(
|
||||
TootPollsType.Misskey -> client.request(
|
||||
"/api/notes/polls/vote",
|
||||
accessInfo.putMisskeyApiToken(JSONObject())
|
||||
.put("noteId", enquete.status_id.toString())
|
||||
.put("choice", idx)
|
||||
.toPostRequestBuilder()
|
||||
)
|
||||
PollType.Mastodon -> client.request(
|
||||
TootPollsType.Mastodon -> client.request(
|
||||
"/api/v1/polls/${enquete.pollId}/votes",
|
||||
JSONObject()
|
||||
.put(
|
||||
|
@ -2818,7 +2816,7 @@ internal class ItemViewHolder(
|
|||
)
|
||||
.toPostRequestBuilder()
|
||||
)
|
||||
PollType.FriendsNico -> client.request(
|
||||
TootPollsType.FriendsNico -> client.request(
|
||||
"/api/v1/votes/${enquete.status_id}",
|
||||
JSONObject()
|
||||
.put("item_index", idx.toString())
|
||||
|
@ -2832,20 +2830,20 @@ internal class ItemViewHolder(
|
|||
val data = result.jsonObject
|
||||
if(data != null) {
|
||||
when(enquete.pollType) {
|
||||
PollType.Misskey -> if(enquete.increaseVote(activity, idx, true)) {
|
||||
TootPollsType.Misskey -> if(enquete.increaseVote(activity, idx, true)) {
|
||||
showToast(context, false, R.string.enquete_voted)
|
||||
|
||||
// 1個だけ開閉するのではなく、例えば通知TLにある複数の要素をまとめて開閉するなどある
|
||||
list_adapter.notifyChange(reason = "onClickEnqueteChoice", reset = true)
|
||||
}
|
||||
|
||||
PollType.Mastodon -> {
|
||||
val newPoll = NicoEnquete.parse(
|
||||
TootPollsType.Mastodon -> {
|
||||
val newPoll = TootPolls.parse(
|
||||
TootParser(activity, accessInfo),
|
||||
status,
|
||||
status.media_attachments,
|
||||
data,
|
||||
PollType.Mastodon
|
||||
TootPollsType.Mastodon
|
||||
)
|
||||
if(newPoll != null) {
|
||||
status.enquete = newPoll
|
||||
|
@ -2859,7 +2857,7 @@ internal class ItemViewHolder(
|
|||
}
|
||||
}
|
||||
|
||||
PollType.FriendsNico -> {
|
||||
TootPollsType.FriendsNico -> {
|
||||
val message = data.parseString("message") ?: "?"
|
||||
val valid = data.optBoolean("valid")
|
||||
if(valid) {
|
||||
|
@ -2879,7 +2877,7 @@ internal class ItemViewHolder(
|
|||
|
||||
private fun sendMultiple(
|
||||
status : TootStatus,
|
||||
enquete : NicoEnquete,
|
||||
enquete : TootPolls,
|
||||
context : Context,
|
||||
accessInfo : SavedAccount
|
||||
) {
|
||||
|
@ -2891,7 +2889,7 @@ internal class ItemViewHolder(
|
|||
|
||||
TootTaskRunner(context).run(accessInfo, object : TootTask {
|
||||
|
||||
var newPoll : NicoEnquete? = null
|
||||
var newPoll : TootPolls? = null
|
||||
|
||||
override fun background(client : TootApiClient) : TootApiResult? {
|
||||
return client.request(
|
||||
|
@ -2906,12 +2904,12 @@ internal class ItemViewHolder(
|
|||
)?.also { result ->
|
||||
val data = result.jsonObject
|
||||
if(data != null) {
|
||||
newPoll = NicoEnquete.parse(
|
||||
newPoll = TootPolls.parse(
|
||||
TootParser(activity, accessInfo),
|
||||
status,
|
||||
status.media_attachments,
|
||||
data,
|
||||
PollType.Mastodon
|
||||
TootPollsType.Mastodon
|
||||
)
|
||||
if(newPoll == null) result.setError("response parse error")
|
||||
}
|
||||
|
|
|
@ -11,18 +11,26 @@ import org.json.JSONArray
|
|||
import org.json.JSONObject
|
||||
import java.util.regex.Pattern
|
||||
|
||||
enum class PollType {
|
||||
Mastodon, // Mastodon 2.8's poll
|
||||
Misskey, // Misskey's poll
|
||||
FriendsNico, // friends.nico
|
||||
enum class TootPollsType {
|
||||
Mastodon, // Mastodon 2.8
|
||||
Misskey, // Misskey
|
||||
FriendsNico, // friends.nico API
|
||||
}
|
||||
|
||||
class NicoEnquete(
|
||||
class TootPollsChoice(
|
||||
val text : String,
|
||||
val decoded_text : Spannable,
|
||||
var isVoted : Boolean = false, // misskey
|
||||
var votes : Int? = 0, // misskey
|
||||
var checked : Boolean = false // Mastodon
|
||||
)
|
||||
|
||||
class TootPolls private constructor(
|
||||
parser : TootParser,
|
||||
status : TootStatus,
|
||||
list_attachment : ArrayList<TootAttachmentLike>?,
|
||||
src : JSONObject,
|
||||
val pollType : PollType
|
||||
val pollType : TootPollsType
|
||||
) {
|
||||
|
||||
// one of enquete,enquete_result
|
||||
|
@ -33,13 +41,13 @@ class NicoEnquete(
|
|||
val decoded_question : Spannable // 表示用にデコードしてしまうのでNonNullになる
|
||||
|
||||
// array of text with emoji
|
||||
val items : ArrayList<Choice>?
|
||||
val items : ArrayList<TootPollsChoice>?
|
||||
|
||||
// 結果の数値 // null or array of number
|
||||
var ratios : MutableList<Float>? = null
|
||||
|
||||
// 結果の数値のテキスト // null or array of string
|
||||
var ratios_text : MutableList<String>? = null
|
||||
private var ratios_text : MutableList<String>? = null
|
||||
|
||||
var myVoted : Int? = null
|
||||
|
||||
|
@ -60,132 +68,134 @@ class NicoEnquete(
|
|||
this.time_start = status.time_created_at
|
||||
this.status_id = status.id
|
||||
|
||||
if(parser.serviceType == ServiceType.MISSKEY) {
|
||||
|
||||
this.items = parseChoiceListMisskey(
|
||||
when(pollType) {
|
||||
TootPollsType.Misskey -> {
|
||||
|
||||
src.optJSONArray("choices")
|
||||
)
|
||||
|
||||
val votesList = ArrayList<Int>()
|
||||
var votesMax = 1
|
||||
items?.forEachIndexed { index, choice ->
|
||||
if(choice.isVoted) this.myVoted = index
|
||||
val votes = choice.votes ?: 0
|
||||
votesList.add(votes)
|
||||
if(votes > votesMax) votesMax = votes
|
||||
}
|
||||
|
||||
if(votesList.isNotEmpty()) {
|
||||
this.ratios = votesList.map { (it.toFloat() / votesMax.toFloat()) }.toMutableList()
|
||||
this.ratios_text =
|
||||
votesList.map { parser.context.getString(R.string.vote_count_text, it) }
|
||||
.toMutableList()
|
||||
} else {
|
||||
this.ratios = null
|
||||
this.ratios_text = null
|
||||
}
|
||||
|
||||
this.type = NicoEnquete.TYPE_ENQUETE
|
||||
|
||||
this.question = status.content
|
||||
this.decoded_question = DecodeOptions(
|
||||
parser.context,
|
||||
parser.linkHelper,
|
||||
short = true,
|
||||
decodeEmoji = true,
|
||||
attachmentList = list_attachment,
|
||||
linkTag = status,
|
||||
emojiMapCustom = status.custom_emojis,
|
||||
emojiMapProfile = status.profile_emojis
|
||||
).decodeHTML(this.question ?: "?")
|
||||
|
||||
} else if(pollType == PollType.Mastodon) {
|
||||
this.type = "enquete"
|
||||
|
||||
this.question = status.content
|
||||
this.decoded_question = DecodeOptions(
|
||||
parser.context,
|
||||
parser.linkHelper,
|
||||
short = true,
|
||||
decodeEmoji = true,
|
||||
attachmentList = list_attachment,
|
||||
linkTag = status,
|
||||
emojiMapCustom = status.custom_emojis,
|
||||
emojiMapProfile = status.profile_emojis
|
||||
).decodeHTML(this.question ?: "?")
|
||||
|
||||
this.items = parseChoiceListMastodon(
|
||||
parser.context,
|
||||
status,
|
||||
src.optJSONArray("options")?.toObjectList()
|
||||
)
|
||||
|
||||
this.pollId = EntityId.mayNull(src.parseString("id"))
|
||||
this.expired_at = TootStatus.parseTime(src.parseString("expires_at")).notZero() ?: Long.MAX_VALUE
|
||||
this.expired = src.optBoolean("expired", false)
|
||||
this.multiple = src.optBoolean("multiple", false)
|
||||
this.votes_count = src.parseInt("votes_count")
|
||||
this.myVoted = if(src.optBoolean("voted", false)) 1 else null
|
||||
|
||||
when {
|
||||
this.items == null -> maxVotesCount = null
|
||||
this.multiple -> {
|
||||
var max :Int? = null
|
||||
for( item in items){
|
||||
val v = item.votes
|
||||
if( v != null && (max == null || v > max) ) max =v
|
||||
|
||||
}
|
||||
maxVotesCount = max
|
||||
this.items = parseChoiceListMisskey(
|
||||
|
||||
src.optJSONArray("choices")
|
||||
)
|
||||
|
||||
val votesList = ArrayList<Int>()
|
||||
var votesMax = 1
|
||||
items?.forEachIndexed { index, choice ->
|
||||
if(choice.isVoted) this.myVoted = index
|
||||
val votes = choice.votes ?: 0
|
||||
votesList.add(votes)
|
||||
if(votes > votesMax) votesMax = votes
|
||||
}
|
||||
else -> {
|
||||
var sum :Int?= null
|
||||
for( item in items){
|
||||
val v = item.votes
|
||||
if( v != null ) sum = (sum?:0) + v
|
||||
}
|
||||
maxVotesCount = sum
|
||||
|
||||
if(votesList.isNotEmpty()) {
|
||||
this.ratios =
|
||||
votesList.map { (it.toFloat() / votesMax.toFloat()) }.toMutableList()
|
||||
this.ratios_text =
|
||||
votesList.map { parser.context.getString(R.string.vote_count_text, it) }
|
||||
.toMutableList()
|
||||
} else {
|
||||
this.ratios = null
|
||||
this.ratios_text = null
|
||||
}
|
||||
|
||||
this.type = TYPE_ENQUETE
|
||||
|
||||
this.question = status.content
|
||||
this.decoded_question = DecodeOptions(
|
||||
parser.context,
|
||||
parser.linkHelper,
|
||||
short = true,
|
||||
decodeEmoji = true,
|
||||
attachmentList = list_attachment,
|
||||
linkTag = status,
|
||||
emojiMapCustom = status.custom_emojis,
|
||||
emojiMapProfile = status.profile_emojis
|
||||
).decodeHTML(this.question ?: "?")
|
||||
|
||||
}
|
||||
|
||||
} else {
|
||||
this.type = src.parseString("type")
|
||||
|
||||
this.question = src.parseString("question")
|
||||
this.decoded_question = DecodeOptions(
|
||||
parser.context,
|
||||
parser.linkHelper,
|
||||
short = true,
|
||||
decodeEmoji = true,
|
||||
attachmentList = list_attachment,
|
||||
linkTag = status,
|
||||
emojiMapCustom = status.custom_emojis,
|
||||
emojiMapProfile = status.profile_emojis
|
||||
).decodeHTML(this.question ?: "?")
|
||||
TootPollsType.Mastodon -> {
|
||||
this.type = "enquete"
|
||||
|
||||
this.question = status.content
|
||||
this.decoded_question = DecodeOptions(
|
||||
parser.context,
|
||||
parser.linkHelper,
|
||||
short = true,
|
||||
decodeEmoji = true,
|
||||
attachmentList = list_attachment,
|
||||
linkTag = status,
|
||||
emojiMapCustom = status.custom_emojis,
|
||||
emojiMapProfile = status.profile_emojis
|
||||
).decodeHTML(this.question ?: "?")
|
||||
|
||||
this.items = parseChoiceListMastodon(
|
||||
parser.context,
|
||||
status,
|
||||
src.optJSONArray("options")?.toObjectList()
|
||||
)
|
||||
|
||||
this.pollId = EntityId.mayNull(src.parseString("id"))
|
||||
this.expired_at =
|
||||
TootStatus.parseTime(src.parseString("expires_at")).notZero() ?: Long.MAX_VALUE
|
||||
this.expired = src.optBoolean("expired", false)
|
||||
this.multiple = src.optBoolean("multiple", false)
|
||||
this.votes_count = src.parseInt("votes_count")
|
||||
this.myVoted = if(src.optBoolean("voted", false)) 1 else null
|
||||
|
||||
when {
|
||||
this.items == null -> maxVotesCount = null
|
||||
|
||||
this.multiple -> {
|
||||
var max : Int? = null
|
||||
for(item in items) {
|
||||
val v = item.votes
|
||||
if(v != null && (max == null || v > max)) max = v
|
||||
|
||||
}
|
||||
maxVotesCount = max
|
||||
}
|
||||
|
||||
else -> {
|
||||
var sum : Int? = null
|
||||
for(item in items) {
|
||||
val v = item.votes
|
||||
if(v != null) sum = (sum ?: 0) + v
|
||||
}
|
||||
maxVotesCount = sum
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
this.items = parseChoiceListFriendsNico(
|
||||
parser.context,
|
||||
status,
|
||||
src.parseStringArrayList("items")
|
||||
)
|
||||
|
||||
this.ratios = src.parseFloatArrayList("ratios")
|
||||
this.ratios_text = src.parseStringArrayList("ratios_text")
|
||||
TootPollsType.FriendsNico -> {
|
||||
this.type = src.parseString("type")
|
||||
|
||||
this.question = src.parseString("question")
|
||||
this.decoded_question = DecodeOptions(
|
||||
parser.context,
|
||||
parser.linkHelper,
|
||||
short = true,
|
||||
decodeEmoji = true,
|
||||
attachmentList = list_attachment,
|
||||
linkTag = status,
|
||||
emojiMapCustom = status.custom_emojis,
|
||||
emojiMapProfile = status.profile_emojis
|
||||
).decodeHTML(this.question ?: "?")
|
||||
|
||||
this.items = parseChoiceListFriendsNico(
|
||||
parser.context,
|
||||
status,
|
||||
src.parseStringArrayList("items")
|
||||
)
|
||||
|
||||
this.ratios = src.parseFloatArrayList("ratios")
|
||||
this.ratios_text = src.parseStringArrayList("ratios_text")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Choice(
|
||||
val text : String,
|
||||
val decoded_text : Spannable,
|
||||
var isVoted : Boolean = false, // misskey
|
||||
var votes : Int? = 0, // misskey
|
||||
var checked : Boolean = false // Mastodon
|
||||
)
|
||||
|
||||
companion object {
|
||||
internal val log = LogCategory("NicoEnquete")
|
||||
internal val log = LogCategory("TootPolls")
|
||||
|
||||
const val ENQUETE_EXPIRE = 30000L
|
||||
|
||||
|
@ -202,11 +212,11 @@ class NicoEnquete(
|
|||
status : TootStatus,
|
||||
list_attachment : ArrayList<TootAttachmentLike>?,
|
||||
src : JSONObject?,
|
||||
pollType : PollType
|
||||
) : NicoEnquete? {
|
||||
pollType : TootPollsType
|
||||
) : TootPolls? {
|
||||
src ?: return null
|
||||
return try {
|
||||
NicoEnquete(
|
||||
TootPolls(
|
||||
parser,
|
||||
status,
|
||||
list_attachment,
|
||||
|
@ -223,10 +233,10 @@ class NicoEnquete(
|
|||
context : Context,
|
||||
status : TootStatus,
|
||||
objectArray : ArrayList<JSONObject>?
|
||||
) : ArrayList<Choice>? {
|
||||
) : ArrayList<TootPollsChoice>? {
|
||||
if(objectArray != null) {
|
||||
val size = objectArray.size
|
||||
val items = ArrayList<Choice>(size)
|
||||
val items = ArrayList<TootPollsChoice>(size)
|
||||
val options = DecodeOptions(
|
||||
context,
|
||||
emojiMapCustom = status.custom_emojis,
|
||||
|
@ -240,7 +250,7 @@ class NicoEnquete(
|
|||
val decoded_text = options.decodeHTML(text)
|
||||
|
||||
items.add(
|
||||
Choice(
|
||||
TootPollsChoice(
|
||||
text,
|
||||
decoded_text,
|
||||
votes = o.parseInt("votes_count") // may null
|
||||
|
@ -256,10 +266,10 @@ class NicoEnquete(
|
|||
context : Context,
|
||||
status : TootStatus,
|
||||
stringArray : ArrayList<String>?
|
||||
) : ArrayList<Choice>? {
|
||||
) : ArrayList<TootPollsChoice>? {
|
||||
if(stringArray != null) {
|
||||
val size = stringArray.size
|
||||
val items = ArrayList<Choice>(size)
|
||||
val items = ArrayList<TootPollsChoice>(size)
|
||||
val options = DecodeOptions(
|
||||
context,
|
||||
emojiMapCustom = status.custom_emojis,
|
||||
|
@ -272,7 +282,12 @@ class NicoEnquete(
|
|||
.replaceAll(" ")
|
||||
val decoded_text = options.decodeHTML(text)
|
||||
|
||||
items.add(Choice(text, decoded_text))
|
||||
items.add(
|
||||
TootPollsChoice(
|
||||
text,
|
||||
decoded_text
|
||||
)
|
||||
)
|
||||
}
|
||||
if(items.isNotEmpty()) return items
|
||||
}
|
||||
|
@ -281,9 +296,9 @@ class NicoEnquete(
|
|||
|
||||
private fun parseChoiceListMisskey(
|
||||
choices : JSONArray?
|
||||
) : ArrayList<Choice>? {
|
||||
) : ArrayList<TootPollsChoice>? {
|
||||
if(choices != null) {
|
||||
val items = ArrayList<Choice>()
|
||||
val items = ArrayList<TootPollsChoice>()
|
||||
for(i in 0 until choices.length()) {
|
||||
val src = choices.optJSONObject(i)
|
||||
|
||||
|
@ -292,7 +307,7 @@ class NicoEnquete(
|
|||
.replaceAll(" ")
|
||||
val decoded_text = SpannableString(text) // misskey ではマークダウン不可で絵文字もない
|
||||
|
||||
val dst = Choice(
|
||||
val dst = TootPollsChoice(
|
||||
text = text,
|
||||
decoded_text = decoded_text,
|
||||
// 配列インデクスと同じだった id = EntityId.mayNull(src.parseLong("id")),
|
|
@ -136,7 +136,7 @@ class TootStatus(parser : TootParser, src : JSONObject) : TimelineItem() {
|
|||
|
||||
var conversation_main : Boolean = false
|
||||
|
||||
var enquete : NicoEnquete? = null
|
||||
var enquete : TootPolls? = null
|
||||
|
||||
//
|
||||
var replies_count : Long? = null
|
||||
|
@ -319,12 +319,12 @@ class TootStatus(parser : TootParser, src : JSONObject) : TimelineItem() {
|
|||
}
|
||||
|
||||
// contentを読んだ後にアンケートのデコード
|
||||
this.enquete = NicoEnquete.parse(
|
||||
this.enquete = TootPolls.parse(
|
||||
parser,
|
||||
this,
|
||||
media_attachments,
|
||||
src.optJSONObject("poll"),
|
||||
PollType.Misskey
|
||||
TootPollsType.Misskey
|
||||
)
|
||||
|
||||
this.reactionCounts = parseReactionCounts(src.optJSONObject("reactionCounts"))
|
||||
|
@ -493,21 +493,21 @@ class TootStatus(parser : TootParser, src : JSONObject) : TimelineItem() {
|
|||
this.enquete = try {
|
||||
sv = src.parseString("enquete") ?: ""
|
||||
if(sv.isNotEmpty()) {
|
||||
NicoEnquete.parse(
|
||||
TootPolls.parse(
|
||||
parser,
|
||||
this,
|
||||
media_attachments,
|
||||
sv.toJsonObject(),
|
||||
PollType.FriendsNico
|
||||
TootPollsType.FriendsNico
|
||||
)
|
||||
} else {
|
||||
val ov = src.optJSONObject("poll")
|
||||
NicoEnquete.parse(
|
||||
TootPolls.parse(
|
||||
parser,
|
||||
this,
|
||||
media_attachments,
|
||||
ov,
|
||||
PollType.Mastodon
|
||||
TootPollsType.Mastodon
|
||||
)
|
||||
}
|
||||
} catch(ex : Throwable) {
|
||||
|
|
|
@ -71,7 +71,7 @@ class PostHelper(
|
|||
var in_reply_to_id : EntityId? = null
|
||||
var attachment_list : ArrayList<PostAttachment>? = null
|
||||
var enquete_items : ArrayList<String>? = null
|
||||
var poll_type : PollType? = null
|
||||
var poll_type : TootPollsType? = null
|
||||
var poll_expire_seconds = 0
|
||||
var poll_hide_totals = false
|
||||
var poll_multiple_choice = false
|
||||
|
@ -123,11 +123,10 @@ class PostHelper(
|
|||
|
||||
if(enquete_items?.isNotEmpty() == true) {
|
||||
|
||||
val choice_max_chars = if(isMisskey) {
|
||||
15
|
||||
} else when(poll_type) {
|
||||
PollType.Mastodon -> 25
|
||||
else -> 15
|
||||
val choice_max_chars = when {
|
||||
isMisskey -> 15
|
||||
poll_type == TootPollsType.FriendsNico -> 15
|
||||
else -> 25 // TootPollsType.Mastodon
|
||||
}
|
||||
|
||||
for(n in 0 until enquete_items.size) {
|
||||
|
@ -496,7 +495,7 @@ class PostHelper(
|
|||
}
|
||||
|
||||
if(enquete_items?.isNotEmpty() == true) {
|
||||
if(poll_type == PollType.Mastodon) {
|
||||
if(poll_type == TootPollsType.Mastodon) {
|
||||
json.put("poll", JSONObject().apply {
|
||||
put("multiple", poll_multiple_choice)
|
||||
put("hide_totals", poll_hide_totals)
|
||||
|
|
Loading…
Reference in New Issue