Skip to content

Commit

Permalink
Merge pull request #351 from joeloewi7178/development
Browse files Browse the repository at this point in the history
  • Loading branch information
joeloewi7178 authored Feb 9, 2024
2 parents 028b957 + da17406 commit f85a42b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 39 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ android {

defaultConfig {
applicationId = "com.joeloewi.croissant"
versionCode = 56
versionCode = 57
versionName = "1.2.7"
targetSdk = 34

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.core.os.bundleOf
import androidx.hilt.navigation.compose.hiltViewModel
Expand Down Expand Up @@ -247,9 +246,7 @@ fun AttendanceWithGamesItem(
}
) {
DismissContent(
elevation = animateDpAsState(
HalfDp, label = ""
).value,
dismissDirection = { swipeToDismissState.dismissDirection },
attendanceWithGames = item,
onClickOneTimeAttend = remember {
{ attendance ->
Expand Down Expand Up @@ -319,20 +316,24 @@ private fun SwipeToDismissBackground(
}
}

@OptIn(ExperimentalFoundationApi::class)
@OptIn(ExperimentalFoundationApi::class, ExperimentalMaterial3Api::class)
@Composable
private fun DismissContent(
elevation: Dp,
dismissDirection: () -> SwipeToDismissBoxValue,
attendanceWithGames: () -> StableWrapper<AttendanceWithGames>,
onClickAttendance: (Attendance) -> Unit,
onClickOneTimeAttend: (Attendance) -> Unit
) {
ListItem(
modifier = Modifier
.shadow(elevation = elevation)
.composed {
remember {
clickable { onClickAttendance(attendanceWithGames().value.attendance) }
val elevation by animateDpAsState(
if (dismissDirection() != SwipeToDismissBoxValue.Settled) HalfDp else 0.dp,
label = ""
)

remember(elevation, attendanceWithGames().value.attendance.id) {
shadow(elevation).clickable { onClickAttendance(attendanceWithGames().value.attendance) }
}
},
supportingContent = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,62 @@ suspend fun <T : Any> runAndRetryWithExponentialBackOff(
initialDelay: Int = 500,
retryFactor: Float = 2f,
maxAttempts: Int = 5,
maxDelayMillis: Long = 5000,
maxDelayMillis: Long = 3000,
task: suspend () -> ApiResponse<T>,
): ApiResponse<T> = retryTask(
attempt = 1,
initialDelay = initialDelay,
retryFactor = retryFactor,
maxAttempts = maxAttempts,
maxDelayMillis = maxDelayMillis,
task = task
)

private tailrec suspend fun <T : Any> retryTask(
attempt: Int = 1,
initialDelay: Int,
retryFactor: Float,
maxAttempts: Int,
maxDelayMillis: Long,
task: suspend () -> ApiResponse<T>
): ApiResponse<T> {
var unsuccessfulAttempts = 0
var apiResponse: ApiResponse<T>
val isRetrying = attempt > 1

while (true) {
if (unsuccessfulAttempts > 1) {
val fullJitterExponentialBackOffDelay = Random.nextLong(
0,
min(
maxDelayMillis,
(initialDelay * retryFactor.pow(unsuccessfulAttempts)).toLong()
)
if (isRetrying) {
val fullJitterExponentialBackOffDelay = Random.nextLong(
0,
min(
maxDelayMillis,
(initialDelay * retryFactor.pow(attempt - 1)).toLong()
)
)

delay(fullJitterExponentialBackOffDelay)
}
delay(fullJitterExponentialBackOffDelay)
}

apiResponse = task()
when (apiResponse) {
is ApiResponse.Success -> break
is ApiResponse.Failure -> {
when (apiResponse) {
is ApiResponse.Failure.Error -> {
break
}
return when (val apiResponse = task()) {
is ApiResponse.Success -> apiResponse
is ApiResponse.Failure -> {
when (apiResponse) {
is ApiResponse.Failure.Error -> {
apiResponse
}

is ApiResponse.Failure.Exception -> {
if (unsuccessfulAttempts <= maxAttempts) {
unsuccessfulAttempts += 1
} else {
break
}
is ApiResponse.Failure.Exception -> {
if (attempt < maxAttempts) {
retryTask(
attempt = attempt + 1,
initialDelay = initialDelay,
retryFactor = retryFactor,
maxAttempts = maxAttempts,
maxDelayMillis = maxDelayMillis,
task = task
)
} else {
apiResponse
}
}
}
}
}

return apiResponse
}

0 comments on commit f85a42b

Please sign in to comment.