Compare commits

..

No commits in common. "master" and "v1.0.0" have entirely different histories.

View file

@ -2,78 +2,26 @@ package de.leongeorgi.pretixinfo
import android.Manifest import android.Manifest
import android.content.Intent import android.content.Intent
import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.util.Log import android.util.Log
import android.view.Menu
import android.view.ViewGroup.LayoutParams.MATCH_PARENT import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewManager import android.view.ViewManager
import android.widget.Button
import android.widget.TextView
import com.fasterxml.jackson.core.JsonParseException import com.fasterxml.jackson.core.JsonParseException
import com.fasterxml.jackson.databind.exc.MismatchedInputException
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.google.zxing.BarcodeFormat import com.google.zxing.BarcodeFormat
import com.google.zxing.Result import com.google.zxing.Result
import me.dm7.barcodescanner.zxing.ZXingScannerView import me.dm7.barcodescanner.zxing.ZXingScannerView
import org.jetbrains.anko.* import org.jetbrains.anko.*
import org.jetbrains.anko.custom.ankoView import org.jetbrains.anko.custom.ankoView
import org.jetbrains.anko.sdk27.coroutines.onClick
import java.io.BufferedReader import java.io.BufferedReader
import java.io.File
import java.io.InputStreamReader import java.io.InputStreamReader
class MainActivity : PermissionActivity(), ZXingScannerView.ResultHandler { class MainActivity : PermissionActivity(), ZXingScannerView.ResultHandler {
private val ui = MainActivityUI(this) private val ui = MainActivityUI()
private var eventData: Data? = null private var eventData: Data? = null
private val paidFile by lazy {
File(filesDir, "paidSecrets.json").apply {
if (!exists()) {
val created = createNewFile()
if (created) {
toast("created paidSecrets.json")
} else {
toast("paidSecrets.json could not be created")
}
} else {
toast("found paidSecrets.json")
}
}
}
private fun addPaidSecret(secret: String) {
val newList = paidList + secret
dirtyList = true
jacksonObjectMapper().writeValue(paidFile, newList)
}
private fun removePaidSecret(secret: String) {
val newList = paidList - secret
dirtyList = true
jacksonObjectMapper().writeValue(paidFile, newList)
}
private var dirtyList: Boolean = true
private var cachedPaidList: Set<String> = emptySet()
val paidList: Set<String>
get() {
if (dirtyList) {
cachedPaidList = try {
jacksonObjectMapper().readValue(paidFile)
} catch (e: MismatchedInputException) {
emptySet()
}
dirtyList = false
}
return cachedPaidList
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply { val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
@ -166,63 +114,8 @@ class MainActivity : PermissionActivity(), ZXingScannerView.ResultHandler {
} }
} }
private fun createAlertWithSecret(
title: String,
secret: String,
init: ViewManager.() -> Unit
) = alert {
lateinit var titleTextView: TextView
lateinit var payButton: Button
fun isPaid() = secret in paidList
fun updateContent() {
titleTextView.apply {
text = "$title (${if (isPaid()) "bezahlt" else "ausstehend"})"
textColor = if (isPaid()) GREEN else YELLOW
}
payButton.text = if (isPaid()) "Auf \"ausstehend\" setzen" else "Auf \"bezahlt\" setzen"
}
customTitle {
titleTextView = textView {
textAppearance = android.R.style.TextAppearance_Material_Headline
horizontalPadding = dip(16)
topPadding = dip(8)
}
}
customView {
verticalLayout {
init(this)
payButton = button("Set paid") {
onClick {
if (isPaid()) {
alert("Wirklich auf \"ausstehend\" setzen?") {
yesButton {
removePaidSecret(secret)
updateContent()
}
noButton { }
}.show()
} else {
addPaidSecret(secret)
updateContent()
}
}
}
}
}
updateContent()
okButton {
it.cancel()
}
onCancelled {
ui.scannerView.resumeCameraPreview(this@MainActivity)
}
}
private fun createTextileAlert(event: Event, position: Position) = with(event) { private fun createTextileAlert(event: Event, position: Position) = with(event) {
createAlertWithSecret(position.item.name, position.secret) { createAlert(position.item.name) {
verticalLayout { verticalLayout {
textView("Größe: ${position.variation?.name}") textView("Größe: ${position.variation?.name}")
textView("Preis: ${"%.2f".format(position.price)}") textView("Preis: ${"%.2f".format(position.price)}")
@ -234,7 +127,7 @@ class MainActivity : PermissionActivity(), ZXingScannerView.ResultHandler {
private fun createTicketAlert(event: Event, position: Position) = with(event) { private fun createTicketAlert(event: Event, position: Position) = with(event) {
val inclusiveTextile = position.addons.find { it.item.category.name == "Inklusivtextil" } val inclusiveTextile = position.addons.find { it.item.category.name == "Inklusivtextil" }
createAlertWithSecret(position.item.name, position.secret) { createAlert(position.item.name) {
verticalLayout { verticalLayout {
textView("Anzeigename: ${position.answers.first { it.question.question == "Anzeigename" }.answer}") textView("Anzeigename: ${position.answers.first { it.question.question == "Anzeigename" }.answer}")
textView("Hochschule: ${position.answers.first { it.question.question == "Von welcher Hochschule kommst du?" }.answer}") textView("Hochschule: ${position.answers.first { it.question.question == "Von welcher Hochschule kommst du?" }.answer}")
@ -267,12 +160,14 @@ class MainActivity : PermissionActivity(), ZXingScannerView.ResultHandler {
if (requestCode == CHOOSE_FILE_ID) { if (requestCode == CHOOSE_FILE_ID) {
assert(data != null) { "The selected file is null" } assert(data != null) { "The selected file is null" }
val t = toast("Parsing JSON…")
val reader = BufferedReader(InputStreamReader(contentResolver.openInputStream(data?.data))) val reader = BufferedReader(InputStreamReader(contentResolver.openInputStream(data?.data)))
try { try {
val eventData = jacksonObjectMapper().readValue<Data>(reader, Data::class.java) val eventData = jacksonObjectMapper().readValue<Data>(reader, Data::class.java)
dataLoaded(eventData) dataLoaded(eventData)
toast("File imported successfully.") t.cancel()
} catch (_: JsonParseException) { } catch (_: JsonParseException) {
t.cancel()
toast("File content is not valid JSON.") toast("File content is not valid JSON.")
finish() finish()
} }
@ -283,21 +178,14 @@ class MainActivity : PermissionActivity(), ZXingScannerView.ResultHandler {
companion object { companion object {
private const val CHOOSE_FILE_ID = 1337 private const val CHOOSE_FILE_ID = 1337
private val GREEN = Color.parseColor("#43A047")
private val YELLOW = Color.parseColor("#F9A825")
} }
} }
class MainActivityUI(val mainActivity: MainActivity) : AnkoComponent<MainActivity> { class MainActivityUI : AnkoComponent<MainActivity> {
lateinit var scannerView: ZXingScannerView lateinit var scannerView: ZXingScannerView
override fun createView(ui: AnkoContext<MainActivity>) = with(ui) { override fun createView(ui: AnkoContext<MainActivity>) = with(ui) {
verticalLayout { verticalLayout {
button("Bezahlte Secrets exportieren") {
onClick {
share(mainActivity.paidList.joinToString(",\n"))
}
}
scannerView = ankoView({ ZXingScannerView(it) }, 0) { } scannerView = ankoView({ ZXingScannerView(it) }, 0) { }
lparams(height = MATCH_PARENT) lparams(height = MATCH_PARENT)
} }