package com.sp365.calendar.components import android.graphics.Bitmap import android.graphics.Canvas import android.util.Log import androidx.compose.foundation.Image import androidx.compose.foundation.background import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxHeight import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.width import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.graphics.painter.BitmapPainter import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import com.caverock.androidsvg.SVG import com.sp365.calendar.IconProvider import com.sp365.calendar.model.EventNative // Hàm chuyển đổi SVG thành bitmap private fun svgToBitmap(svg: SVG, width: Int, height: Int): Bitmap { // Tạo bitmap để vẽ val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) svg.renderToCanvas(canvas) // Đảm bảo rằng svg được render vào canvas return bitmap } @Composable fun ItemList(job: EventNative) { Box { Column { Box( modifier = Modifier .fillMaxWidth() .height(52.dp) .background(color = Color(0xFFFFFFFF)) ) { Row( modifier = Modifier .fillMaxSize() .fillMaxHeight() .padding(start = 4.dp, end = 8.dp), verticalAlignment = Alignment.CenterVertically ) { Column( modifier = Modifier .width(70.dp) .padding(4.dp), ) { if (job.Duration == 24.0) { // Nếu Duration là 24, hiển thị "Cả ngày" Text( text = "Cả ngày", fontSize = 12.sp, fontWeight = FontWeight(400), lineHeight = 22.sp, color = Color(0xFF808080) ) } else { // Hiển thị giờ, chuyển đổi thời gian từ "HH:mm:ss" thành "HH:mm" val startTimeFormatted = job.startTime.split(":").take(2).joinToString(":") // Lấy giờ và phút Text( text = startTimeFormatted, fontSize = 12.sp, fontWeight = FontWeight(400), lineHeight = 22.sp, color = Color(0xFF808080) ) Spacer(modifier = Modifier.height(4.dp)) // Hiển thị Duration, chuyển đổi từ số giờ thành giờ và phút nếu cần val durationFormatted = if (job.Duration % 1 == 0.0) { "${job.Duration.toInt()}h" } else { val hours = job.Duration.toInt() val minutes = ((job.Duration - hours) * 60).toInt() "${hours}h ${minutes}m" } Text( text = durationFormatted, fontSize = 12.sp, fontWeight = FontWeight(400), lineHeight = 22.sp, color = Color(0xFF808080) ) } } Box( modifier = Modifier .width(40.dp) , contentAlignment = Alignment.Center ) { val svgIcon = when (job.priority) { 1 -> IconProvider.LOW 10 -> IconProvider.NORMAL 20 -> IconProvider.URGENT 30 -> IconProvider.EMERGENCY else -> null } val iconSize = if (job.priority == 10) 16.dp else 24.dp if (svgIcon != null) { val svg = try { SVG.getFromString(svgIcon) } catch (e: Exception) { Log.e("SVG Error", "Could not parse SVG: $svgIcon") return Row { } // Trả về nếu không thể phân tích SVG } // Chuyển đổi SVG thành bitmap val bitmap = svgToBitmap(svg, 24, 24) // Sử dụng BitmapPainter để hiển thị Image( painter = BitmapPainter(bitmap.asImageBitmap()), contentDescription = null, modifier = Modifier .size(iconSize) // Đặt kích thước cho hình ảnh .align(Alignment.Center) // Căn giữa hình ảnh ) } else { // Xử lý trường hợp icon không hợp lệ Log.w("Icon Warning", "Icon not found for: ${job.priority}") } } Box( modifier = Modifier .fillMaxWidth() .padding(start = 8.dp) ) { Text( text = job.title, fontSize = 13.sp, fontWeight = FontWeight(400), lineHeight = 22.sp, color = Color(0xFF808080) ) } } } Spacer(modifier = Modifier.height(2.dp)) } } }