Skip to content

Commit

Permalink
add slider to split record action
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Oct 26, 2024
1 parent fee6ed3 commit f45288b
Show file tree
Hide file tree
Showing 10 changed files with 114 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.example.util.simpletimetracker.feature_change_record.adapter

import com.example.util.simpletimetracker.feature_base_adapter.ViewHolderType
import com.example.util.simpletimetracker.feature_base_adapter.createRecyclerBindingAdapterDelegate
import com.example.util.simpletimetracker.feature_change_record.model.ChangeRecordActionsBlock
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordSliderViewData as ViewData
import com.example.util.simpletimetracker.feature_change_record.databinding.ChangeRecordSliderItemBinding as Binding

fun createChangeRecordSliderAdapterDelegate(
onValueChanged: (ViewData, Float) -> Unit,
) = createRecyclerBindingAdapterDelegate<ViewData, Binding>(
Binding::inflate,
) { binding, item, _ ->

with(binding) {
item as ViewData
// To avoid illegal state exception.
val actualMax = item.max.coerceAtLeast(item.min + 1)

root.tag = item.block
if (item.min != sliderChangeRecordItem.valueFrom) {
sliderChangeRecordItem.valueFrom = item.min
}
if (item.max != sliderChangeRecordItem.valueTo) {
sliderChangeRecordItem.valueTo = actualMax
}
if (item.value != sliderChangeRecordItem.value) {
sliderChangeRecordItem.value = item.value
}
sliderChangeRecordItem.addOnChangeListener { _, value, fromUser ->
if (fromUser) onValueChanged(item, value)
}
}
}

data class ChangeRecordSliderViewData(
val block: ChangeRecordActionsBlock,
val min: Float,
val max: Float,
val value: Float,
) : ViewHolderType {

override fun getUniqueId(): Long = block.ordinal.toLong()

override fun isValidType(other: ViewHolderType): Boolean =
other is ViewData
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.example.util.simpletimetracker.feature_change_record.model
enum class ChangeRecordActionsBlock {
SplitTimePreview,
SplitTimeAdjustment,
SplitSlider,
SplitButton,

AdjustTimePreview,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.example.util.simpletimetracker.feature_change_record.adapter.createCh
import com.example.util.simpletimetracker.feature_change_record.adapter.createChangeRecordChangePreviewAdapterDelegate
import com.example.util.simpletimetracker.feature_change_record.adapter.createChangeRecordCommentAdapterDelegate
import com.example.util.simpletimetracker.feature_change_record.adapter.createChangeRecordCommentFieldAdapterDelegate
import com.example.util.simpletimetracker.feature_change_record.adapter.createChangeRecordSliderAdapterDelegate
import com.example.util.simpletimetracker.feature_change_record.adapter.createChangeRecordTimeAdjustmentAdapterDelegate
import com.example.util.simpletimetracker.feature_change_record.adapter.createChangeRecordTimeDoublePreviewAdapterDelegate
import com.example.util.simpletimetracker.feature_change_record.adapter.createChangeRecordTimePreviewAdapterDelegate
Expand Down Expand Up @@ -114,6 +115,7 @@ class ChangeRecordCore(
),
createChangeRecordTimeAdjustmentAdapterDelegate(viewModel::onTimeAdjustmentClick),
createChangeRecordButtonAdapterDelegate(viewModel::onItemButtonClick),
createChangeRecordSliderAdapterDelegate(viewModel::onSliderValueChanged)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRe
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordChangePreviewViewData
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordTimeDoublePreviewViewData
import com.example.util.simpletimetracker.feature_change_record.model.ChangeRecordActionsBlock
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import javax.inject.Inject

Expand All @@ -22,6 +23,7 @@ class ChangeRecordActionsDelegateImpl @Inject constructor(
val timeChangeAdjustmentState get() = delegateHolder.adjustDelegate.timeChangeAdjustmentState

private var parent: ChangeRecordActionsDelegate.Parent? = null
private var updateJob: Job? = null

fun attach(parent: ChangeRecordActionsDelegate.Parent) {
this.parent = parent
Expand All @@ -36,8 +38,11 @@ class ChangeRecordActionsDelegateImpl @Inject constructor(
}

fun updateData() {
delegateHolder.delegatesList.forEach { delegate ->
delegateScope.launch { delegate.updateViewData() }
updateJob?.cancel()
updateJob = delegateScope.launch {
delegateHolder.delegatesList.forEach { delegate ->
launch { delegate.updateViewData() }
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import com.example.util.simpletimetracker.feature_base_adapter.hint.HintViewData
import com.example.util.simpletimetracker.feature_change_record.R
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordButtonViewData
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordChangePreviewViewData
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordSliderViewData
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordTimeAdjustmentViewData
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordTimePreviewViewData
import com.example.util.simpletimetracker.feature_change_record.interactor.ChangeRecordViewDataInteractor
import com.example.util.simpletimetracker.feature_change_record.mapper.ChangeRecordViewDataMapper
import com.example.util.simpletimetracker.feature_change_record.model.ChangeRecordActionsBlock
import com.example.util.simpletimetracker.feature_change_record.model.ChangeRecordDateTimeFieldsState
import com.example.util.simpletimetracker.feature_change_record.viewData.ChangeRecordPreview
import kotlinx.coroutines.coroutineScope
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import kotlinx.coroutines.ensureActive

class ChangeRecordActionsSplitDelegate @Inject constructor(
private val resourceRepo: ResourceRepo,
Expand All @@ -36,8 +40,11 @@ class ChangeRecordActionsSplitDelegate @Inject constructor(
}

override suspend fun updateViewData() {
viewData = loadViewData()
parent?.update()
coroutineScope {
viewData = loadViewData()
ensureActive()
parent?.update()
}
}

suspend fun onSplitClickDelegate() {
Expand Down Expand Up @@ -76,6 +83,12 @@ class ChangeRecordActionsSplitDelegate @Inject constructor(
block = ChangeRecordActionsBlock.SplitTimeAdjustment,
items = loadTimeSplitAdjustmentItems(),
)
result += ChangeRecordSliderViewData(
block = ChangeRecordActionsBlock.SplitSlider,
min = 0f,
max = TimeUnit.MILLISECONDS.toSeconds(newTimeEnded - newTimeStarted).toFloat(),
value = TimeUnit.MILLISECONDS.toSeconds(newTimeSplit - newTimeStarted).toFloat(),
)
val previewData = loadSplitPreviewViewData(
newTypeId = newTypeId,
newTimeStarted = newTimeStarted,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.example.util.simpletimetracker.feature_change_record.R
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordButtonViewData
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordChangePreviewViewData
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordCommentViewData
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordSliderViewData
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordTimeAdjustmentViewData
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordTimeDoublePreviewViewData
import com.example.util.simpletimetracker.feature_change_record.adapter.ChangeRecordTimePreviewViewData
Expand Down Expand Up @@ -460,6 +461,16 @@ abstract class ChangeRecordBaseViewModel(
}
}

fun onSliderValueChanged(viewData: ChangeRecordSliderViewData, value: Float) {
when (viewData.block) {
ChangeRecordActionsBlock.SplitSlider ->
onSliderSplitValueChanged(value)
else -> {
// Do nothing.
}
}
}

fun onAdjustTimeStartedItemClick(viewData: TimeAdjustmentView.ViewData) {
onAdjustTimeItemClick(TimeAdjustmentState.TIME_STARTED, viewData)
}
Expand Down Expand Up @@ -501,23 +512,26 @@ abstract class ChangeRecordBaseViewModel(
}

private fun onAdjustTimeSplitItemClick(viewData: TimeAdjustmentView.ViewData) {
viewModelScope.launch {
when (viewData) {
is TimeAdjustmentView.ViewData.Now -> {
newTimeSplit = System.currentTimeMillis()
onTimeSplitChanged()
}
is TimeAdjustmentView.ViewData.Zero -> {
// Do nothing, shouldn't be there.
}
is TimeAdjustmentView.ViewData.Adjust -> {
newTimeSplit += TimeUnit.MINUTES.toMillis(viewData.value)
onTimeSplitChanged()
}
when (viewData) {
is TimeAdjustmentView.ViewData.Now -> {
newTimeSplit = System.currentTimeMillis()
onTimeSplitChanged()
}
is TimeAdjustmentView.ViewData.Zero -> {
// Do nothing, shouldn't be there.
}
is TimeAdjustmentView.ViewData.Adjust -> {
newTimeSplit += TimeUnit.MINUTES.toMillis(viewData.value)
onTimeSplitChanged()
}
}
}

private fun onSliderSplitValueChanged(value: Float) {
newTimeSplit = newTimeStarted + TimeUnit.SECONDS.toMillis(value.toLong())
onTimeSplitChanged()
}

private fun onRecordChangeButtonClick(
onProceed: suspend () -> Unit,
checkTypeSelected: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.slider.Slider xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/sliderChangeRecordItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelBehavior="gone"
tools:value="33"
tools:valueFrom="0"
tools:valueTo="100" />

Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ class ColorSelectionDialogFragment : BaseBottomSheetFragment<Binding>() {

override fun initUx() = with(binding) {
sliderColorSelectionHue.addOnChangeListener { _, value, fromUser ->
if (fromUser) {
viewModel.onHueChanged(value)
}
if (fromUser) viewModel.onHueChanged(value)
}
viewColorSelectionView.setListener(
object : ColorSelectionView.ColorSelectedListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="8dp"
android:layout_marginEnd="8dp"
android:valueFrom="0"
android:valueTo="360"
app:labelBehavior="gone"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
android:paddingHorizontal="2dp"
android:text=":"
android:textColor="?appTextPrimaryColor"
android:textSize="32sp"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/tvPomodoroTimerSeconds"
app:layout_constraintStart_toEndOf="@id/tvPomodoroTimerMinutes"
Expand All @@ -149,7 +149,7 @@
android:fontFamily="sans-serif-condensed"
android:includeFontPadding="false"
android:textColor="?appTextPrimaryColor"
android:textSize="32sp"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/tvPomodoroTimerMinutesDelimiter"
Expand Down

0 comments on commit f45288b

Please sign in to comment.