Fix "time for all" row on the "Set time" screen
This commit is contained in:
parent
bb49fb004c
commit
1913c06739
3 changed files with 58 additions and 13 deletions
|
@ -55,6 +55,7 @@
|
|||
tools:tint="@color/ctrl_active_light"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/time_for_all_title"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="@dimen/content_padding_standard"
|
||||
|
@ -62,9 +63,9 @@
|
|||
android:layout_weight="1"
|
||||
android:ellipsize="end"
|
||||
android:maxLines="1"
|
||||
android:text="@string/visible_time_for_all"
|
||||
android:textColor="?android:attr/textColorPrimary"
|
||||
android:textSize="@dimen/descr_text_size"/>
|
||||
android:textSize="@dimen/descr_text_size"
|
||||
tools:text="@string/visible_time_for_all"/>
|
||||
|
||||
<net.osmand.telegram.ui.views.TextViewEx
|
||||
android:id="@+id/time_for_all_value"
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
<resources>
|
||||
<string name="set_visible_time_for_all">Set visible time for all</string>
|
||||
<string name="hours_and_minutes_format">%1$d h %2$d m</string>
|
||||
<string name="minutes_format">%1$d m</string>
|
||||
<string name="hours_format">%1$d h</string>
|
||||
<string name="shared_string_install">Install</string>
|
||||
<string name="shared_string_share">Share</string>
|
||||
<string name="shared_string_back">Back</string>
|
||||
|
|
|
@ -14,6 +14,7 @@ import android.widget.TextView
|
|||
import android.widget.Toast
|
||||
import net.osmand.telegram.R
|
||||
import net.osmand.telegram.TelegramApplication
|
||||
import net.osmand.telegram.helpers.ShareLocationHelper
|
||||
import net.osmand.telegram.helpers.TelegramUiHelper
|
||||
import net.osmand.telegram.ui.SetTimeDialogFragment.SetTimeListAdapter.ChatViewHolder
|
||||
import org.drinkless.td.libcore.telegram.TdApi
|
||||
|
@ -28,6 +29,9 @@ class SetTimeDialogFragment : DialogFragment() {
|
|||
|
||||
private val adapter = SetTimeListAdapter()
|
||||
|
||||
private lateinit var timeForAllTitle: TextView
|
||||
private lateinit var timeForAllValue: TextView
|
||||
|
||||
private val chatIdsToDuration = HashMap<Long, Long>()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
|
@ -48,12 +52,15 @@ class SetTimeDialogFragment : DialogFragment() {
|
|||
findViewById<ImageView>(R.id.time_for_all_icon).setImageDrawable(
|
||||
app.uiUtils.getIcon(R.drawable.ic_action_time_span, R.color.ctrl_active_light)
|
||||
)
|
||||
findViewById<TextView>(R.id.time_for_all_value).text = "1 hour"
|
||||
timeForAllTitle = findViewById(R.id.time_for_all_title)
|
||||
timeForAllValue = findViewById(R.id.time_for_all_value)
|
||||
setOnClickListener {
|
||||
selectDuration()
|
||||
}
|
||||
}
|
||||
|
||||
updateTimeForAllRow()
|
||||
|
||||
view.findViewById<RecyclerView>(R.id.recycler_view).apply {
|
||||
layoutManager = LinearLayoutManager(context)
|
||||
adapter = this@SetTimeDialogFragment.adapter
|
||||
|
@ -100,21 +107,53 @@ class SetTimeDialogFragment : DialogFragment() {
|
|||
}
|
||||
}
|
||||
|
||||
private fun getTimeForAll(useDefValue: Boolean = false): Long {
|
||||
val returnVal = if (useDefValue) DEFAULT_VISIBLE_TIME_SECONDS else NO_VALUE
|
||||
val iterator = chatIdsToDuration.values.iterator()
|
||||
if (!iterator.hasNext()) {
|
||||
return returnVal
|
||||
}
|
||||
val first = iterator.next()
|
||||
while (iterator.hasNext()) {
|
||||
if (first != iterator.next()) {
|
||||
return returnVal
|
||||
}
|
||||
}
|
||||
return first
|
||||
}
|
||||
|
||||
private fun updateTimeForAllRow() {
|
||||
val timeForAll = getTimeForAll()
|
||||
if (timeForAll != NO_VALUE) {
|
||||
timeForAllTitle.text = getString(R.string.visible_time_for_all)
|
||||
timeForAllValue.visibility = View.VISIBLE
|
||||
timeForAllValue.text = formatDuration(timeForAll)
|
||||
} else {
|
||||
timeForAllTitle.text = getString(R.string.set_visible_time_for_all)
|
||||
timeForAllValue.visibility = View.GONE
|
||||
}
|
||||
}
|
||||
|
||||
private fun selectDuration(id: Long? = null) {
|
||||
val (defHours, defMinutes) = secondsToHoursAndMinutes(DEFAULT_VISIBLE_TIME_SECONDS)
|
||||
val timeForAll = getTimeForAll(true)
|
||||
val defSeconds = if (id == null) timeForAll else chatIdsToDuration[id] ?: timeForAll
|
||||
val (defHours, defMinutes) = secondsToHoursAndMinutes(defSeconds)
|
||||
TimePickerDialog(
|
||||
context,
|
||||
TimePickerDialog.OnTimeSetListener { _, hours, minutes ->
|
||||
val seconds = TimeUnit.HOURS.toSeconds(hours.toLong()) +
|
||||
TimeUnit.MINUTES.toSeconds(minutes.toLong())
|
||||
if (id != null) {
|
||||
chatIdsToDuration[id] = seconds
|
||||
} else {
|
||||
chatIdsToDuration.keys.forEach {
|
||||
chatIdsToDuration[it] = seconds
|
||||
if (seconds >= ShareLocationHelper.MIN_LOCATION_MESSAGE_LIVE_PERIOD_SEC) {
|
||||
if (id != null) {
|
||||
chatIdsToDuration[id] = seconds
|
||||
} else {
|
||||
chatIdsToDuration.keys.forEach {
|
||||
chatIdsToDuration[it] = seconds
|
||||
}
|
||||
}
|
||||
updateTimeForAllRow()
|
||||
adapter.notifyDataSetChanged()
|
||||
}
|
||||
adapter.notifyDataSetChanged()
|
||||
}, defHours, defMinutes, true
|
||||
).show()
|
||||
}
|
||||
|
@ -128,9 +167,9 @@ class SetTimeDialogFragment : DialogFragment() {
|
|||
private fun formatDuration(seconds: Long): String {
|
||||
val (hours, minutes) = secondsToHoursAndMinutes(seconds)
|
||||
return when {
|
||||
hours != 0 && minutes == 0 -> String.format("%d h", hours)
|
||||
hours == 0 && minutes != 0 -> String.format("%02d m", minutes)
|
||||
else -> String.format("%d h, %02d m", hours, minutes)
|
||||
hours != 0 && minutes == 0 -> getString(R.string.hours_format, hours)
|
||||
hours == 0 && minutes != 0 -> getString(R.string.minutes_format, minutes)
|
||||
else -> getString(R.string.hours_and_minutes_format, hours, minutes)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -189,6 +228,7 @@ class SetTimeDialogFragment : DialogFragment() {
|
|||
private const val TAG = "SetTimeDialogFragment"
|
||||
private const val CHATS_KEY = "chats_key"
|
||||
private const val DEFAULT_VISIBLE_TIME_SECONDS = 60 * 60L // 1 hour
|
||||
private const val NO_VALUE = -1L
|
||||
|
||||
fun showInstance(fm: FragmentManager, chatIds: Set<Long>): Boolean {
|
||||
return try {
|
||||
|
|
Loading…
Reference in a new issue