implemented donwload upload handling with username

This commit is contained in:
oxidiert
2025-09-23 16:49:59 +02:00
parent 5f5c766133
commit bf33501b69
6 changed files with 189 additions and 53 deletions

View File

@ -2,6 +2,12 @@ package com.dano.test1
import android.content.res.Configuration
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
@ -11,27 +17,112 @@ class MainActivity : AppCompatActivity() {
var isInQuestionnaire: Boolean = false
var isFirstQuestionnairePage: Boolean = false
private var progress: ProgressBar? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Opening Screen nur beim echten Start initialisieren.
// Durch configChanges wird onCreate bei Drehung NICHT erneut aufgerufen.
openingScreenHandler = HandlerOpeningScreen(this)
openingScreenHandler.init()
// Zeige sofort Login-Dialog; erst NACH erfolgreichem Login + Download initialisieren wir den Opening Screen
showLoginThenDownload()
}
// Wichtig: Bei Konfigurationsänderungen NICHT den Screen neu setzen.
// So bleibt man auf der aktuellen Frage/Seite.
private fun showLoginThenDownload() {
// UI mit Username/Passwort
val container = LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
setPadding(dp(20), dp(8), dp(20), 0)
}
val etUser = EditText(this).apply {
hint = "Username"
setSingleLine()
}
val etPass = EditText(this).apply {
hint = "Passwort"
setSingleLine()
inputType = android.text.InputType.TYPE_CLASS_TEXT or
android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD
}
container.addView(etUser)
container.addView(etPass)
val dialog = AlertDialog.Builder(this)
.setTitle("Login erforderlich")
.setView(container)
.setCancelable(false)
.setPositiveButton("Login") { _, _ ->
val user = etUser.text.toString().trim()
val pass = etPass.text.toString()
if (user.isEmpty() || pass.isEmpty()) {
Toast.makeText(this, "Bitte Username & Passwort eingeben", Toast.LENGTH_SHORT).show()
showLoginThenDownload()
return@setPositiveButton
}
showBusy(true)
// Login -> Token -> Auto-Download -> OpeningScreen init
LoginManager.loginUserWithCredentials(
context = this,
username = user,
password = pass,
onSuccess = { token ->
// optional speichern
TokenStore.save(this, user, token)
DatabaseDownloader.downloadAndReplaceDatabase(
context = this,
token = token
) { ok ->
showBusy(false)
if (!ok) {
Toast.makeText(this, "Download fehlgeschlagen", Toast.LENGTH_LONG).show()
}
// Jetzt erst die Start-UI hochziehen
openingScreenHandler = HandlerOpeningScreen(this)
openingScreenHandler.init()
}
},
onError = { msg ->
showBusy(false)
Toast.makeText(this, msg, Toast.LENGTH_LONG).show()
// erneut anbieten
showLoginThenDownload()
}
)
}
.setNegativeButton("Beenden") { _, _ ->
finishAffinity()
}
.create()
dialog.show()
}
private fun showBusy(show: Boolean) {
if (show) {
if (progress == null) {
progress = ProgressBar(this).apply {
isIndeterminate = true
// simpler Vollbild-Overlay
val content = window?.decorView as? android.view.ViewGroup
content?.addView(this, android.view.ViewGroup.LayoutParams(
android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT
))
this.x = (resources.displayMetrics.widthPixels / 2f) - width / 2f
this.y = (resources.displayMetrics.heightPixels / 2f) - height / 2f
}
}
progress?.visibility = View.VISIBLE
} else {
progress?.visibility = View.GONE
}
}
private fun dp(v: Int): Int = (v * resources.displayMetrics.density).toInt()
// Wichtig: Keine Neu-Initialisierung bei Rotation
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
// Kein setContentView(), kein openingScreenHandler.init() hier!
// Falls du Layout-Metriken bei Rotation neu berechnen willst, kannst du das gezielt hier tun.
}
/**
* Starts the given questionnaire and attaches it to this activity.
* @param questionnaire The questionnaire instance to start.
* @param languageID The language identifier for localization.
*/
fun startQuestionnaire(questionnaire: QuestionnaireBase<*>, languageID: String) {
isInQuestionnaire = true
isFirstQuestionnairePage = true
@ -39,23 +130,17 @@ class MainActivity : AppCompatActivity() {
questionnaire.startQuestionnaire()
}
/**
* Handle the back button press.
* If the openingScreenHandler can handle it, do not call super.
* Otherwise, call the default back press behavior.
*/
override fun onBackPressed() {
if (!openingScreenHandler.onBackPressed()) {
if (!::openingScreenHandler.isInitialized || !openingScreenHandler.onBackPressed()) {
super.onBackPressed()
}
}
/**
* Finish the questionnaire and return to the opening screen.
*/
fun finishQuestionnaire() {
isInQuestionnaire = false
isFirstQuestionnairePage = false
openingScreenHandler.init()
if (::openingScreenHandler.isInitialized) {
openingScreenHandler.init()
}
}
}