-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotPaid.kt
71 lines (65 loc) · 2.16 KB
/
NotPaid.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2024 Stɑrry Shivɑm <[email protected]>
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*/
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.tooling.preview.Preview
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit
@Composable
fun EnsurePayment(
dueDate: String,
gracePeriod: Int,
content: @Composable () -> Unit
) {
val formatter = remember { DateTimeFormatter.ofPattern("yyyy-MM-dd") }
val parsedDueDate = remember(dueDate) { LocalDate.parse(dueDate, formatter) }
val currentDate = remember { LocalDate.now() }
// Calculate the number of days past the due date
val daysPastDue = remember(parsedDueDate, currentDate) {
ChronoUnit.DAYS.between(parsedDueDate, currentDate).toInt()
}
// Calculate opacity based on days past due date
val opacity = remember(daysPastDue, gracePeriod) {
when {
// Fully visible until due date
daysPastDue <= 0 -> 1f
// Fully invisible after grace period
daysPastDue >= gracePeriod -> 0f
// Gradually decrease opacity
else -> 1 - (daysPastDue / gracePeriod.toFloat())
}
}
Box(modifier = Modifier.graphicsLayer(alpha = opacity)) {
content()
}
}
@Preview(showBackground = true)
@Composable
private fun NeverGonnaPayYouUpPreview() {
EnsurePayment(
dueDate = "2024-08-24",
gracePeriod = 14
) {
Text("Meow >~<")
}
}