diff --git a/app/src/main/java/de/leongeorgi/pretixinfo/MainActivity.kt b/app/src/main/java/de/leongeorgi/pretixinfo/MainActivity.kt index 3acda1f..b3c277a 100644 --- a/app/src/main/java/de/leongeorgi/pretixinfo/MainActivity.kt +++ b/app/src/main/java/de/leongeorgi/pretixinfo/MainActivity.kt @@ -2,26 +2,78 @@ package de.leongeorgi.pretixinfo import android.Manifest import android.content.Intent +import android.graphics.Color import android.os.Bundle import android.util.Log +import android.view.Menu import android.view.ViewGroup.LayoutParams.MATCH_PARENT import android.view.ViewManager +import android.widget.Button +import android.widget.TextView 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.readValue import com.google.zxing.BarcodeFormat import com.google.zxing.Result import me.dm7.barcodescanner.zxing.ZXingScannerView import org.jetbrains.anko.* import org.jetbrains.anko.custom.ankoView +import org.jetbrains.anko.sdk27.coroutines.onClick import java.io.BufferedReader +import java.io.File import java.io.InputStreamReader class MainActivity : PermissionActivity(), ZXingScannerView.ResultHandler { - private val ui = MainActivityUI() + private val ui = MainActivityUI(this) 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 = emptySet() + + val paidList: Set + get() { + if (dirtyList) { + cachedPaidList = try { + jacksonObjectMapper().readValue(paidFile) + } catch (e: MismatchedInputException) { + emptySet() + } + dirtyList = false + } + return cachedPaidList + } + + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply { @@ -114,8 +166,63 @@ 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) { - createAlert(position.item.name) { + createAlertWithSecret(position.item.name, position.secret) { verticalLayout { textView("Größe: ${position.variation?.name}") textView("Preis: ${"%.2f".format(position.price)} €") @@ -127,7 +234,7 @@ class MainActivity : PermissionActivity(), ZXingScannerView.ResultHandler { private fun createTicketAlert(event: Event, position: Position) = with(event) { val inclusiveTextile = position.addons.find { it.item.category.name == "Inklusivtextil" } - createAlert(position.item.name) { + createAlertWithSecret(position.item.name, position.secret) { verticalLayout { textView("Anzeigename: ${position.answers.first { it.question.question == "Anzeigename" }.answer}") textView("Hochschule: ${position.answers.first { it.question.question == "Von welcher Hochschule kommst du?" }.answer}") @@ -160,14 +267,12 @@ class MainActivity : PermissionActivity(), ZXingScannerView.ResultHandler { if (requestCode == CHOOSE_FILE_ID) { assert(data != null) { "The selected file is null" } - val t = toast("Parsing JSON…") val reader = BufferedReader(InputStreamReader(contentResolver.openInputStream(data?.data))) try { val eventData = jacksonObjectMapper().readValue(reader, Data::class.java) dataLoaded(eventData) - t.cancel() + toast("File imported successfully.") } catch (_: JsonParseException) { - t.cancel() toast("File content is not valid JSON.") finish() } @@ -178,14 +283,21 @@ class MainActivity : PermissionActivity(), ZXingScannerView.ResultHandler { companion object { private const val CHOOSE_FILE_ID = 1337 + private val GREEN = Color.parseColor("#43A047") + private val YELLOW = Color.parseColor("#F9A825") } } -class MainActivityUI : AnkoComponent { +class MainActivityUI(val mainActivity: MainActivity) : AnkoComponent { lateinit var scannerView: ZXingScannerView override fun createView(ui: AnkoContext) = with(ui) { verticalLayout { + button("Bezahlte Secrets exportieren") { + onClick { + share(mainActivity.paidList.joinToString(",\n")) + } + } scannerView = ankoView({ ZXingScannerView(it) }, 0) { } lparams(height = MATCH_PARENT) }