Custom.kt 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package eu.mjdev.desktop.extensions
  2. import androidx.compose.runtime.Composable
  3. import androidx.compose.runtime.collectAsState
  4. import androidx.compose.runtime.snapshots.SnapshotStateList
  5. import androidx.compose.ui.graphics.NativePaint
  6. import eu.mjdev.desktop.helpers.streams.ResourceStream
  7. import kotlinx.coroutines.delay
  8. import kotlinx.coroutines.flow.channelFlow
  9. import kotlinx.coroutines.launch
  10. import java.text.DateFormat
  11. import java.util.*
  12. object Custom {
  13. fun NativePaint.setMaskFilter(
  14. blurRadius: Float
  15. ) {
  16. this.maskFilter =
  17. org.jetbrains.skia.MaskFilter.makeBlur(org.jetbrains.skia.FilterBlurMode.NORMAL, blurRadius / 2, true)
  18. }
  19. val dateFlow
  20. @Composable
  21. get() = channelFlow {
  22. launch {
  23. var date = "1.1.1970"
  24. do {
  25. DateFormat.getDateInstance().format(Date()).also { t ->
  26. if (date != t) {
  27. date = t
  28. send(date)
  29. }
  30. }
  31. delay(5000L)
  32. } while (true)
  33. }
  34. }.collectAsState(initial = "1.1.1970")
  35. val timeFlow
  36. @Composable
  37. get() = channelFlow {
  38. launch {
  39. var time = "00:00:00"
  40. do {
  41. DateFormat.getTimeInstance().format(Date()).also { t ->
  42. if (time != t) {
  43. time = t
  44. send(time)
  45. }
  46. }
  47. delay(200L)
  48. } while (true)
  49. }
  50. }.collectAsState(initial = "00:00:00")
  51. // todo better solution
  52. fun <T> SnapshotStateList<T>.invalidate() = toList().also {
  53. clear()
  54. addAll(it)
  55. }
  56. fun loadKey(key: String): String = runCatching {
  57. ResourceStream("keys/$key.key").string
  58. }.getOrNull().orEmpty()
  59. }