2019-08-22 20:30:08 +02:00
|
|
|
/* Copyright 2019 Tusky Contributors
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
2019-12-19 19:09:40 +01:00
|
|
|
package com.keylesspalace.tusky.components.compose.view
|
2019-08-22 20:30:08 +02:00
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.util.AttributeSet
|
2021-03-07 19:04:22 +01:00
|
|
|
import android.view.LayoutInflater
|
2019-08-22 20:30:08 +02:00
|
|
|
import android.widget.LinearLayout
|
|
|
|
import com.keylesspalace.tusky.R
|
|
|
|
import com.keylesspalace.tusky.adapter.PreviewPollOptionsAdapter
|
2021-03-07 19:04:22 +01:00
|
|
|
import com.keylesspalace.tusky.databinding.ViewPollPreviewBinding
|
2019-08-22 20:30:08 +02:00
|
|
|
import com.keylesspalace.tusky.entity.NewPoll
|
|
|
|
|
|
|
|
class PollPreviewView @JvmOverloads constructor(
|
2021-06-28 21:13:24 +02:00
|
|
|
context: Context?,
|
|
|
|
attrs: AttributeSet? = null,
|
|
|
|
defStyleAttr: Int = 0
|
|
|
|
) :
|
|
|
|
LinearLayout(context, attrs, defStyleAttr) {
|
2019-08-22 20:30:08 +02:00
|
|
|
|
2021-03-07 19:04:22 +01:00
|
|
|
private val adapter = PreviewPollOptionsAdapter()
|
2019-08-22 20:30:08 +02:00
|
|
|
|
2021-03-07 19:04:22 +01:00
|
|
|
private val binding = ViewPollPreviewBinding.inflate(LayoutInflater.from(context), this)
|
2019-08-22 20:30:08 +02:00
|
|
|
|
2021-03-07 19:04:22 +01:00
|
|
|
init {
|
2019-08-22 20:30:08 +02:00
|
|
|
orientation = VERTICAL
|
|
|
|
|
|
|
|
setBackgroundResource(R.drawable.card_frame)
|
|
|
|
|
|
|
|
val padding = resources.getDimensionPixelSize(R.dimen.poll_preview_padding)
|
|
|
|
|
|
|
|
setPadding(padding, padding, padding, padding)
|
|
|
|
|
2021-03-07 19:04:22 +01:00
|
|
|
binding.pollPreviewOptions.adapter = adapter
|
2019-08-22 20:30:08 +02:00
|
|
|
}
|
|
|
|
|
2021-06-28 21:13:24 +02:00
|
|
|
fun setPoll(poll: NewPoll) {
|
2019-08-22 20:30:08 +02:00
|
|
|
adapter.update(poll.options, poll.multiple)
|
|
|
|
|
|
|
|
val pollDurationId = resources.getIntArray(R.array.poll_duration_values).indexOfLast {
|
|
|
|
it <= poll.expiresIn
|
|
|
|
}
|
2021-03-07 19:04:22 +01:00
|
|
|
binding.pollDurationPreview.text = resources.getStringArray(R.array.poll_duration_names)[pollDurationId]
|
2019-08-22 20:30:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
override fun setOnClickListener(l: OnClickListener?) {
|
|
|
|
super.setOnClickListener(l)
|
|
|
|
adapter.setOnClickListener(l)
|
|
|
|
}
|
2021-06-28 21:13:24 +02:00
|
|
|
}
|