Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix budget slider responsiveness #10739

Merged
merged 4 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ private fun EditBudgetSection(
)
Slider(
modifier = Modifier.padding(top = 8.dp, bottom = 8.dp),
value = state.sliderValue,
value = state.totalBudget,
valueRange = state.budgetRangeMin..state.budgetRangeMax,
onValueChange = { onBudgetUpdated(it) },
onValueChangeFinished = { onBudgetChangeFinished() },
Expand Down Expand Up @@ -413,7 +413,6 @@ private fun CampaignBudgetScreenPreview() {
state = BlazeCampaignBudgetViewModel.BudgetUiState(
currencyCode = "USD",
totalBudget = 35f,
sliderValue = 35f,
budgetRangeMin = 5f,
budgetRangeMax = 35f,
dailySpending = "$5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize
import java.util.Date
import javax.inject.Inject
import kotlin.math.roundToInt
import kotlin.time.Duration.Companion.days

@HiltViewModel
Expand All @@ -38,7 +39,6 @@ class BlazeCampaignBudgetViewModel @Inject constructor(
BudgetUiState(
currencyCode = navArgs.budget.currencyCode,
totalBudget = navArgs.budget.totalBudget,
sliderValue = navArgs.budget.totalBudget,
budgetRangeMin = navArgs.budget.durationInDays * CAMPAIGN_MINIMUM_DAILY_SPEND,
budgetRangeMax = navArgs.budget.durationInDays * CAMPAIGN_MAXIMUM_DAILY_SPEND,
dailySpending = formatDailySpend(
Expand Down Expand Up @@ -106,14 +106,11 @@ class BlazeCampaignBudgetViewModel @Inject constructor(
}

fun onBudgetUpdated(sliderValue: Float) {
budgetUiState.update { it.copy(sliderValue = sliderValue) }
if (sliderValue.toInt().mod(budgetUiState.value.durationInDays) == 0) {
budgetUiState.update {
it.copy(
totalBudget = sliderValue,
dailySpending = formatDailySpend(sliderValue / it.durationInDays)
)
}
budgetUiState.update {
it.copy(
totalBudget = sliderValue,
dailySpending = formatDailySpend(sliderValue / it.durationInDays)
)
}
}

Expand All @@ -127,7 +124,6 @@ class BlazeCampaignBudgetViewModel @Inject constructor(
budgetRangeMax = duration * CAMPAIGN_MAXIMUM_DAILY_SPEND,
dailySpending = formatDailySpend(currentDailyExpend),
totalBudget = newTotalBudget,
sliderValue = newTotalBudget,
campaignDurationDates = getCampaignDurationDisplayDate(it.campaignStartDateMillis, duration)
)
}
Expand All @@ -148,6 +144,12 @@ class BlazeCampaignBudgetViewModel @Inject constructor(

fun onBudgetChangeFinished() {
fetchAdForecast()
val roundedBudgetToDurationMultiple =
(budgetUiState.value.totalBudget / budgetUiState.value.durationInDays).roundToInt() *
budgetUiState.value.durationInDays
budgetUiState.update {
it.copy(totalBudget = roundedBudgetToDurationMultiple.toFloat())
}
}

private fun fetchAdForecast() {
Expand Down Expand Up @@ -193,7 +195,6 @@ class BlazeCampaignBudgetViewModel @Inject constructor(
data class BudgetUiState(
val currencyCode: String,
val totalBudget: Float,
val sliderValue: Float,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new approach is simpler and doesn't require to have 2 different values, one for the budget and other for the slider position

val budgetRangeMin: Float,
val budgetRangeMax: Float,
val dailySpending: String,
Expand Down
Loading