Skip to content

Commit

Permalink
Optimized the share as pdf feature
Browse files Browse the repository at this point in the history
  • Loading branch information
aritra-tech committed Aug 3, 2023
1 parent 73cc617 commit e96eadd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.aritra.notify.components.actions.ShareOption
import com.aritra.notify.data.models.Note
import com.aritra.notify.ui.screens.notes.editNoteScreen.EditScreenViewModel
import com.aritra.notify.utils.shareAsImage
import com.aritra.notify.utils.shareAsPdf
import com.aritra.notify.utils.shareNoteAsText
import java.util.Date

Expand Down Expand Up @@ -112,6 +113,14 @@ fun EditNoteTopBar(
showSheet = false
}
)
Spacer(modifier = Modifier.height(15.dp))
ShareOption(
text = stringResource(R.string.share_as_pdf),
onClick = {
shareAsPdf(view, "Notify")
showSheet = false
}
)
}
}
}
Expand Down
33 changes: 13 additions & 20 deletions app/src/main/java/com/aritra/notify/utils/ShareUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import android.view.View
import androidx.core.content.FileProvider
import java.io.File
import java.io.FileOutputStream
import java.io.IOException

fun shareNoteAsText(context: Context, title: String, description: String) {

val shareMsg = "Title: $title\nNote: $description"
Expand Down Expand Up @@ -59,9 +57,9 @@ private fun saveBitmapToCache(context: Context, bitmap: Bitmap): Uri {
}
fun shareAsPdf(view: View, pdfFileName: String) {
val bitmap = createBitmapFromView(view, view.width, view.height)
if (bitmap != null) {
bitmap?.let {
val pdfFile = saveBitmapAsPdf(view.context, bitmap, pdfFileName)
if (pdfFile != null) {
pdfFile?.let {
val uri = FileProvider.getUriForFile(view.context, "${view.context.packageName}.provider", pdfFile)
val shareIntent = Intent(Intent.ACTION_SEND).apply {
type = "application/pdf"
Expand All @@ -72,28 +70,23 @@ fun shareAsPdf(view: View, pdfFileName: String) {
}
}
private fun saveBitmapAsPdf(context: Context, bitmap: Bitmap, pdfFileName: String): File? {
val pdfDir = File(context.cacheDir, "pdfs")
pdfDir.mkdirs()
val pdfDir = File(context.cacheDir, "pdfs").apply { mkdirs() }
val pdfFile = File(pdfDir, "$pdfFileName.pdf")

try {
pdfFile.createNewFile()
val outputStream = FileOutputStream(pdfFile)
val document = PdfDocument()
pdfFile.createNewFile()
val outputStream = FileOutputStream(pdfFile)
PdfDocument().apply {
val pageInfo = PdfDocument.PageInfo.Builder(bitmap.width, bitmap.height, 1).create()
val page = document.startPage(pageInfo)
val page = startPage(pageInfo)
val canvas = page.canvas
val paint = Paint()
paint.color = Color.parseColor("#FFFFFF")
val paint = Paint().apply { color = Color.WHITE }
canvas.drawPaint(paint)
canvas.drawBitmap(bitmap, 0f, 0f, null)
document.finishPage(page)
document.writeTo(outputStream)
finishPage(page)
writeTo(outputStream)
outputStream.close()
document.close()
return pdfFile
} catch (e: IOException) {
e.printStackTrace()
close()
}
return null

return pdfFile
}

0 comments on commit e96eadd

Please sign in to comment.