-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
💄 Support file pasted item preview (#377)
- Loading branch information
1 parent
34cf44f
commit e740fbd
Showing
29 changed files
with
347 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
composeApp/src/commonMain/kotlin/com/clipevery/ui/clip/FilePreviewView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package com.clipevery.ui.clip | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.fillMaxHeight | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.foundation.layout.wrapContentWidth | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.material.Icon | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.text.font.FontFamily | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import com.clipevery.LocalKoinApplication | ||
import com.clipevery.clip.item.ClipFile | ||
import com.clipevery.dao.clip.ClipData | ||
import com.clipevery.i18n.GlobalCopywriter | ||
import com.clipevery.ui.base.file | ||
import com.clipevery.ui.base.folder | ||
import com.clipevery.utils.FileExtUtils.getExtImage | ||
import com.clipevery.utils.FileUtils | ||
|
||
@Composable | ||
fun FilePreviewView(clipData: ClipData) { | ||
clipData.getClipItem(ClipFile::class)?.let { | ||
val current = LocalKoinApplication.current | ||
val copywriter = current.koin.get<GlobalCopywriter>() | ||
val fileUtils = current.koin.get<FileUtils>() | ||
|
||
val imageBitmap: ImageBitmap? = remember(it.getExtension()) { | ||
getExtImage(it.getExtension()) | ||
} | ||
|
||
val isFile: Boolean = remember(it) { | ||
it.isFile | ||
} | ||
|
||
val fileSize = remember(it) { | ||
fileUtils.formatBytes(fileUtils.getFileSize(it.getFilePath())) | ||
} | ||
|
||
Row(modifier = Modifier.fillMaxSize() | ||
.padding(10.dp), | ||
horizontalArrangement = Arrangement.SpaceBetween | ||
) { | ||
Row { | ||
imageBitmap?.let { | ||
Image( | ||
modifier = Modifier.size(100.dp) | ||
.clip(RoundedCornerShape(5.dp)), | ||
bitmap = it, | ||
contentDescription = "fileType" | ||
) | ||
} ?: run { | ||
Icon( | ||
modifier = Modifier.size(100.dp), | ||
imageVector = if (isFile) file() else folder(), | ||
contentDescription = "fileType", | ||
tint = MaterialTheme.colors.onBackground | ||
) | ||
} | ||
|
||
Column( | ||
modifier = Modifier.fillMaxHeight() | ||
.wrapContentWidth() | ||
.padding(horizontal = 8.dp), | ||
verticalArrangement = Arrangement.Bottom | ||
) { | ||
Text( | ||
text = "${copywriter.getText("File_Name")}: ${fileUtils.getFileNameFromRelativePath(it.getFilePath())}", | ||
color = MaterialTheme.colors.onBackground, | ||
style = TextStyle( | ||
fontWeight = FontWeight.Light, | ||
color = MaterialTheme.colors.onBackground, | ||
fontSize = 10.sp | ||
) | ||
) | ||
|
||
Text( | ||
text = "${copywriter.getText("Size")}: $fileSize", | ||
color = MaterialTheme.colors.onBackground, | ||
style = TextStyle( | ||
fontWeight = FontWeight.Light, | ||
color = MaterialTheme.colors.onBackground, | ||
fontSize = 10.sp | ||
) | ||
) | ||
} | ||
} | ||
|
||
Row( | ||
modifier = Modifier.wrapContentWidth() | ||
.padding(end = 8.dp), | ||
horizontalArrangement = Arrangement.End, | ||
verticalAlignment = Alignment.CenterVertically | ||
) { | ||
Icon( | ||
file(), | ||
contentDescription = "File", | ||
modifier = Modifier.padding(3.dp).size(14.dp), | ||
tint = MaterialTheme.colors.onBackground | ||
) | ||
|
||
Text( | ||
text = copywriter.getText("File"), | ||
fontFamily = FontFamily.SansSerif, | ||
style = TextStyle( | ||
fontWeight = FontWeight.Light, | ||
color = MaterialTheme.colors.onBackground, | ||
fontSize = 10.sp | ||
) | ||
) | ||
} | ||
} | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
composeApp/src/commonMain/kotlin/com/clipevery/utils/FileExtUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.clipevery.utils | ||
|
||
import androidx.compose.ui.graphics.ImageBitmap | ||
|
||
object FileExtUtils { | ||
|
||
private val extImageMap = mapOf( | ||
Pair("pdf", "pdf"), | ||
Pair("doc", "word"), | ||
Pair("docx", "word"), | ||
Pair("ppt", "ppt"), | ||
Pair("pptx", "ppt"), | ||
Pair("xls", "excel"), | ||
Pair("xlsx", "excel"), | ||
Pair("pages", "pages"), | ||
Pair("key", "keynote"), | ||
Pair("numbers", "numbers") | ||
) | ||
|
||
fun getExtImage(ext: String): ImageBitmap? = | ||
extImageMap[ext]?.let { ResourceUtils.loadImageBitmap("file-ext/$it.png") } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
composeApp/src/commonMain/kotlin/com/clipevery/utils/ResourceUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.clipevery.utils | ||
|
||
import androidx.compose.ui.graphics.ImageBitmap | ||
import androidx.compose.ui.graphics.toComposeImageBitmap | ||
|
||
object ResourceUtils { | ||
|
||
fun loadImageBitmap(resourcePath: String): ImageBitmap { | ||
// Assuming `resourcePath` is a valid path for an image file within your resources directory. | ||
val image = org.jetbrains.skia.Image.makeFromEncoded( | ||
Thread.currentThread().contextClassLoader.getResourceAsStream(resourcePath) | ||
?.readBytes()!!) | ||
return image.toComposeImageBitmap() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.