Füge Funktion zum Exportieren der Liste als PDF-Datei hinzu
parent
d6d44277e4
commit
e5659e8908
@ -0,0 +1,18 @@
|
||||
package com.cyb3rko.techniklogger.table
|
||||
|
||||
import com.itextpdf.text.Element
|
||||
import com.itextpdf.text.Font
|
||||
import com.itextpdf.text.Phrase
|
||||
import com.itextpdf.text.pdf.PdfPCell
|
||||
|
||||
class ColumnHeaderCell(title: String) : PdfPCell() {
|
||||
init {
|
||||
setPhrase(Phrase(
|
||||
title,
|
||||
Font(Font.FontFamily.HELVETICA, 14f, Font.BOLD)
|
||||
))
|
||||
setPadding(8f)
|
||||
verticalAlignment = Element.ALIGN_MIDDLE
|
||||
horizontalAlignment = Element.ALIGN_CENTER
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,103 @@
|
||||
package com.cyb3rko.techniklogger.table
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Environment
|
||||
import android.util.Log
|
||||
import com.cyb3rko.techniklogger.recycler.ProjectViewState
|
||||
import com.itextpdf.text.*
|
||||
import com.itextpdf.text.pdf.PdfPTable
|
||||
import com.itextpdf.text.pdf.PdfWriter
|
||||
import com.parse.ParseObject
|
||||
import com.parse.ParseQuery
|
||||
import es.dmoral.toasty.Toasty
|
||||
import java.io.File
|
||||
import java.io.FileNotFoundException
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
class ExportBuilder(val myContext: Context, data: MutableList<ProjectViewState.ProjectEntry>) : Document(PageSize.A4.rotate()) {
|
||||
private val destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString() + "/"
|
||||
private val directory = File(destination)
|
||||
private val data = data.reversed()
|
||||
private val table = PdfPTable(4)
|
||||
private val techniker = mutableListOf<List<String>>()
|
||||
private val time = SimpleDateFormat("dd.MM.yyyy, HH:mm", Locale.GERMANY).format(Date())
|
||||
|
||||
init {
|
||||
if (!directory.exists()) {
|
||||
directory.mkdirs()
|
||||
}
|
||||
|
||||
try {
|
||||
val file = File(destination, "Arbeitseinsätze Technik - ${time.split(",")[0]}.pdf")
|
||||
file.createNewFile()
|
||||
val fOut = FileOutputStream(file, false)
|
||||
PdfWriter.getInstance(this, fOut)
|
||||
} catch (e: DocumentException) {
|
||||
e.printStackTrace()
|
||||
Log.e("TechnikLogger.Export", e.toString())
|
||||
} catch (e: FileNotFoundException) {
|
||||
e.printStackTrace()
|
||||
Log.e("TechnikLogger.Export", e.toString())
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
this.setMargins(0f, 0f, 36f, 36f)
|
||||
table.totalWidth = PageSize.A4.rotate().width
|
||||
|
||||
try {
|
||||
this.open()
|
||||
table.addCell(TimeCell(time))
|
||||
table.addCell(HeaderCell())
|
||||
val columnHeaderTexts = listOf("Datum", "Veranstaltung", "Techniker", "Dauer insgesamt (h)")
|
||||
repeat(4) {
|
||||
table.addCell(ColumnHeaderCell(columnHeaderTexts[it]))
|
||||
}
|
||||
fetchTechniker(0)
|
||||
} catch (e: DocumentException) {
|
||||
e.printStackTrace()
|
||||
Log.e("TechnikLogger.Export", e.toString())
|
||||
}
|
||||
}
|
||||
|
||||
private fun fetchTechniker(index: Int) {
|
||||
if (index < data.size) {
|
||||
val query = ParseQuery.getQuery<ParseObject>("Teilnahme")
|
||||
query.whereEqualTo("an", ParseObject.createWithoutData("Einsatz", data[index].childKey))
|
||||
query.include("von")
|
||||
query.findInBackground { objects, e ->
|
||||
if (e == null) {
|
||||
val tempList = mutableListOf<String>()
|
||||
var aktuellerTechniker: ParseObject
|
||||
objects.forEach {
|
||||
aktuellerTechniker = it.getParseObject("von")!!
|
||||
tempList.add(aktuellerTechniker.getString("name")!!)
|
||||
}
|
||||
techniker.add(tempList.sorted())
|
||||
fetchTechniker(index + 1)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
export()
|
||||
}
|
||||
}
|
||||
|
||||
private fun export() {
|
||||
data.forEachIndexed { index, projectEntry ->
|
||||
val information = listOf(
|
||||
SimpleDateFormat("dd.MM.yyyy", Locale.GERMANY).format(projectEntry.date),
|
||||
projectEntry.text,
|
||||
techniker[index].joinToString("\n"),
|
||||
projectEntry.duration)
|
||||
information.forEachIndexed { index2, string ->
|
||||
table.addCell(NormalCell(string, index2 != 2))
|
||||
}
|
||||
}
|
||||
this.add(table)
|
||||
this.close()
|
||||
Toasty.success(myContext, "Liste in Downloads gespeichert").show()
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package com.cyb3rko.techniklogger.table
|
||||
|
||||
import com.itextpdf.text.BaseColor
|
||||
import com.itextpdf.text.Element
|
||||
import com.itextpdf.text.Font
|
||||
import com.itextpdf.text.Phrase
|
||||
import com.itextpdf.text.pdf.PdfPCell
|
||||
|
||||
class HeaderCell : PdfPCell() {
|
||||
init {
|
||||
setPhrase(Phrase(
|
||||
"\nAutomatisch erstellt über die Technik-Logger App\n(Entwickler: Niko Diamadis)\n ",
|
||||
Font(Font.FontFamily.HELVETICA, 12f, Font.BOLD)
|
||||
))
|
||||
borderWidth = 0.2f
|
||||
borderColor = BaseColor.GRAY
|
||||
colspan = 4
|
||||
rowspan = 3
|
||||
verticalAlignment = Element.ALIGN_MIDDLE
|
||||
horizontalAlignment = Element.ALIGN_CENTER
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package com.cyb3rko.techniklogger.table
|
||||
|
||||
import com.itextpdf.text.Element
|
||||
import com.itextpdf.text.Font
|
||||
import com.itextpdf.text.Phrase
|
||||
import com.itextpdf.text.pdf.PdfPCell
|
||||
|
||||
class NormalCell(title: String, centered: Boolean) : PdfPCell() {
|
||||
init {
|
||||
setPhrase(Phrase(
|
||||
title,
|
||||
Font(Font.FontFamily.HELVETICA, 13f)
|
||||
))
|
||||
setPadding(6f)
|
||||
verticalAlignment = Element.ALIGN_MIDDLE
|
||||
if (centered) horizontalAlignment = Element.ALIGN_CENTER
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package com.cyb3rko.techniklogger.table
|
||||
|
||||
import com.itextpdf.text.BaseColor
|
||||
import com.itextpdf.text.Element
|
||||
import com.itextpdf.text.Font
|
||||
import com.itextpdf.text.Phrase
|
||||
import com.itextpdf.text.pdf.PdfPCell
|
||||
|
||||
class TimeCell(time: String) : PdfPCell() {
|
||||
init {
|
||||
setPhrase(Phrase(
|
||||
"Erstellung: $time Uhr",
|
||||
Font(Font.FontFamily.HELVETICA, 12f, Font.BOLD)
|
||||
))
|
||||
setPadding(8f)
|
||||
borderWidth = 0.2f
|
||||
borderColor = BaseColor.GRAY
|
||||
colspan = 4
|
||||
rowspan = 3
|
||||
verticalAlignment = Element.ALIGN_MIDDLE
|
||||
horizontalAlignment = Element.ALIGN_CENTER
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 3.7 KiB |
@ -1,3 +1,6 @@
|
||||
<resources>
|
||||
<dimen name="fab_margin">16dp</dimen>
|
||||
|
||||
<dimen name="first_fab">60dp</dimen>
|
||||
<dimen name="second_fab">120dp</dimen>
|
||||
</resources>
|
||||
Loading…
Reference in New Issue