Skip to content
Merged
Changes from 1 commit
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 @@ -28,6 +28,7 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import be.scri.R
import java.time.LocalDate

/**
* A button component that reflects the state of a data download.
Expand Down Expand Up @@ -55,7 +56,13 @@ fun DownloadDataOptionComp(
when (downloadState) {
DownloadState.Ready -> DownloadState.Downloading
DownloadState.Downloading -> DownloadState.Completed
DownloadState.Completed -> DownloadState.Ready
DownloadState.Completed ->
if (isUpdateAvailable(PLACEBO_LOCAL_UPDATED_AT, PLACEBO_SERVER_UPDATED_AT)) {
DownloadState.Update
} else {
DownloadState.Completed
}
DownloadState.Update -> DownloadState.Downloading
}
},
enabled = true,
Expand Down Expand Up @@ -93,6 +100,7 @@ private fun DownloadButtonContent(
DownloadState.Ready -> "Download"
Copy link
Member

Choose a reason for hiding this comment

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

We'll need to have localized versions of these words :) Do you want to make in issue for this in Scribe-i18n, @catreedle?

DownloadState.Downloading -> "Downloading"
DownloadState.Completed -> "Up to Date"
DownloadState.Update -> "Update"
},
fontSize = 13.sp,
fontWeight = FontWeight.Medium,
Expand All @@ -110,7 +118,7 @@ private fun getBackgroundColor(
colorScheme: androidx.compose.material3.ColorScheme,
): Color =
when (downloadState) {
DownloadState.Ready, DownloadState.Downloading ->
DownloadState.Ready, DownloadState.Downloading, DownloadState.Update ->
if (isDarkTheme) colorScheme.tertiary else colorScheme.primary

DownloadState.Completed ->
Expand All @@ -123,7 +131,7 @@ private fun getTextColor(
colorScheme: androidx.compose.material3.ColorScheme,
): Color =
when (downloadState) {
DownloadState.Ready, DownloadState.Downloading ->
DownloadState.Ready, DownloadState.Downloading, DownloadState.Update ->
if (isDarkTheme) colorScheme.primary else Color.Black

DownloadState.Completed ->
Expand Down Expand Up @@ -172,14 +180,41 @@ private fun DownloadStateIcon(
tint = iconColor,
)
}

DownloadState.Update -> {
Icon(
painter = painterResource(id = R.drawable.clouddownload),
contentDescription = "Update",
modifier = Modifier.size(24.dp),
tint = iconColor,
)
}
}
}

/**
*
* @return true if server data is newer than local data
*/
private const val PLACEBO_SERVER_UPDATED_AT = "2025-01-10"
private const val PLACEBO_LOCAL_UPDATED_AT = "2025-01-01"

private fun isUpdateAvailable(
localUpdatedAt: String,
serverUpdatedAt: String,
): Boolean {
val localDate = LocalDate.parse(localUpdatedAt)
val serverDate = LocalDate.parse(serverUpdatedAt)

return serverDate.isAfter(localDate)
}

/**
* Represents the state of the download button.
*/
enum class DownloadState {
Ready,
Downloading,
Completed,
Update,
}
Loading