added offline mode and improved coachcode system
This commit is contained in:
@ -27,29 +27,37 @@ class HandlerClientCoachCode(
|
||||
this.question = question
|
||||
|
||||
val clientCodeField = layout.findViewById<EditText>(R.id.client_code)
|
||||
val coachCodeField = layout.findViewById<EditText>(R.id.coach_code)
|
||||
val coachCodeField = layout.findViewById<EditText>(R.id.coach_code)
|
||||
val questionTextView = layout.findViewById<TextView>(R.id.question)
|
||||
val titleTextView = layout.findViewById<TextView>(R.id.textView)
|
||||
val titleTextView = layout.findViewById<TextView>(R.id.textView)
|
||||
|
||||
questionTextView.text = question.question?.let { LanguageManager.getText(languageID, it) } ?: ""
|
||||
|
||||
setTextSizePercentOfScreenHeight(titleTextView, 0.03f)
|
||||
setTextSizePercentOfScreenHeight(questionTextView,0.03f)
|
||||
setTextSizePercentOfScreenHeight(clientCodeField, 0.025f)
|
||||
setTextSizePercentOfScreenHeight(coachCodeField, 0.025f)
|
||||
setTextSizePercentOfScreenHeight(titleTextView, 0.03f)
|
||||
setTextSizePercentOfScreenHeight(questionTextView, 0.03f)
|
||||
setTextSizePercentOfScreenHeight(clientCodeField, 0.025f)
|
||||
setTextSizePercentOfScreenHeight(coachCodeField, 0.025f)
|
||||
|
||||
// *** WICHTIG: Nur den ERFOLGREICH GELADENEN Code verwenden ***
|
||||
// Client-Code: nur verwenden, wenn bereits geladen
|
||||
val loadedClientCode = GlobalValues.LOADED_CLIENT_CODE
|
||||
if (!loadedClientCode.isNullOrBlank()) {
|
||||
clientCodeField.setText(loadedClientCode)
|
||||
clientCodeField.isEnabled = false
|
||||
} else {
|
||||
// Nichts ist geladen → Feld bleibt leer und editierbar
|
||||
clientCodeField.setText("")
|
||||
clientCodeField.isEnabled = true
|
||||
}
|
||||
|
||||
coachCodeField.setText(answers["coach_code"] as? String ?: "")
|
||||
// === NEU: Coach-Code immer aus dem Login (TokenStore) setzen und sperren ===
|
||||
val coachFromLogin = TokenStore.getUsername(layout.context)
|
||||
if (!coachFromLogin.isNullOrBlank()) {
|
||||
coachCodeField.setText(coachFromLogin)
|
||||
lockCoachField(coachCodeField) // optisch & technisch gesperrt
|
||||
} else {
|
||||
// Falls (theoretisch) kein Login-Username vorhanden ist, verhalten wie bisher
|
||||
coachCodeField.setText(answers["coach_code"] as? String ?: "")
|
||||
coachCodeField.isEnabled = true
|
||||
}
|
||||
|
||||
layout.findViewById<Button>(R.id.Qnext).setOnClickListener {
|
||||
onNextClicked(clientCodeField, coachCodeField)
|
||||
@ -67,7 +75,6 @@ class HandlerClientCoachCode(
|
||||
}
|
||||
|
||||
private fun onNextClicked(clientCodeField: EditText, coachCodeField: EditText) {
|
||||
// 1) Ohne vorher geladenen Client NICHT weiter
|
||||
val loadedClientCode = GlobalValues.LOADED_CLIENT_CODE
|
||||
|
||||
if (!validate()) {
|
||||
@ -77,7 +84,8 @@ class HandlerClientCoachCode(
|
||||
}
|
||||
|
||||
val clientCode = clientCodeField.text.toString()
|
||||
val coachCode = coachCodeField.text.toString()
|
||||
// Erzwinge Coach-Code aus Login (falls vorhanden)
|
||||
val coachCode = TokenStore.getUsername(layout.context) ?: coachCodeField.text.toString()
|
||||
|
||||
// Prüfen, ob die DB-Datei vor dem Zugriff existiert
|
||||
val dbPath = layout.context.getDatabasePath("questionnaire_database")
|
||||
@ -87,7 +95,6 @@ class HandlerClientCoachCode(
|
||||
val existingClient = MyApp.database.clientDao().getClientByCode(clientCode)
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
// Wenn Feld editierbar war und Code bereits existiert → Hinweis
|
||||
if (existingClient != null && clientCodeField.isEnabled) {
|
||||
val message = LanguageManager.getText(languageID, "client_code_exists")
|
||||
showToast(message)
|
||||
@ -108,25 +115,44 @@ class HandlerClientCoachCode(
|
||||
|
||||
private fun onPreviousClicked(clientCodeField: EditText, coachCodeField: EditText) {
|
||||
val clientCode = clientCodeField.text.toString()
|
||||
val coachCode = coachCodeField.text.toString()
|
||||
val coachCode = TokenStore.getUsername(layout.context) ?: coachCodeField.text.toString()
|
||||
saveAnswers(clientCode, coachCode)
|
||||
goToPreviousQuestion()
|
||||
}
|
||||
|
||||
override fun validate(): Boolean {
|
||||
val clientCode = layout.findViewById<EditText>(R.id.client_code).text
|
||||
val coachCode = layout.findViewById<EditText>(R.id.coach_code).text
|
||||
return clientCode.isNotBlank() && coachCode.isNotBlank()
|
||||
// Validierung nimmt den (ggf. gesperrten) Text – passt
|
||||
val coachText = layout.findViewById<EditText>(R.id.coach_code).text
|
||||
return clientCode.isNotBlank() && coachText.isNotBlank()
|
||||
}
|
||||
|
||||
private fun saveAnswers(clientCode: String, coachCode: String) {
|
||||
// Optional: LAST_CLIENT_CODE kann gesetzt bleiben; maßgeblich ist LOADED_CLIENT_CODE
|
||||
GlobalValues.LAST_CLIENT_CODE = clientCode
|
||||
answers["client_code"] = clientCode
|
||||
answers["coach_code"] = coachCode
|
||||
// Speichere garantierten Coach-Code aus Login bevorzugt
|
||||
val loginCoach = TokenStore.getUsername(layout.context)
|
||||
answers["coach_code"] = loginCoach ?: coachCode
|
||||
}
|
||||
|
||||
override fun saveAnswer() {
|
||||
// Not used
|
||||
}
|
||||
|
||||
// --- Helfer zum Sperren inkl. optischer Markierung (wie im Opening Screen) ---
|
||||
private fun lockCoachField(field: EditText) {
|
||||
field.isFocusable = false
|
||||
field.isFocusableInTouchMode = false
|
||||
field.isCursorVisible = false
|
||||
field.keyListener = null
|
||||
field.isLongClickable = false
|
||||
field.isClickable = false
|
||||
field.setBackgroundResource(R.drawable.bg_field_locked)
|
||||
field.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_lock_24, 0)
|
||||
field.compoundDrawablePadding = dp(8)
|
||||
field.alpha = 0.95f
|
||||
}
|
||||
|
||||
private fun dp(v: Int): Int =
|
||||
(v * layout.resources.displayMetrics.density).toInt()
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@ import android.widget.ProgressBar
|
||||
import android.widget.Toast
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import java.io.File
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
|
||||
@ -23,15 +24,33 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
private var progress: ProgressBar? = null
|
||||
|
||||
// LIVE: Network-Callback (wird in onResume registriert, in onPause deregistriert)
|
||||
// LIVE: Network-Callback (optional für Statusleiste)
|
||||
private var netCb: ConnectivityManager.NetworkCallback? = null
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
// Login-Dialog -> Login -> Auto-Download -> OpeningScreen
|
||||
|
||||
// === Offline-Start ermöglichen ===
|
||||
// Bedingung: gespeicherter User/Token UND lokale DB vorhanden -> direkt OpeningScreen
|
||||
val hasCreds = !TokenStore.getUsername(this).isNullOrBlank() && !TokenStore.getToken(this).isNullOrBlank()
|
||||
val hasDb = hasLocalDb()
|
||||
if (hasCreds && hasDb) {
|
||||
openingScreenHandler = HandlerOpeningScreen(this)
|
||||
openingScreenHandler.init()
|
||||
return
|
||||
}
|
||||
|
||||
// Sonst: Login-Dialog -> Login -> DB (einmalig) laden -> OpeningScreen
|
||||
showLoginThenDownload()
|
||||
}
|
||||
|
||||
/** Prüft, ob die lokale DB-Datei vorhanden ist. */
|
||||
private fun hasLocalDb(): Boolean {
|
||||
val dbFile = getDatabasePath("questionnaire_database")
|
||||
return dbFile != null && dbFile.exists() && dbFile.length() > 0
|
||||
}
|
||||
|
||||
/** Zeigt den Login-Dialog an, führt Login aus und lädt danach einmalig die DB. */
|
||||
private fun showLoginThenDownload() {
|
||||
val container = LinearLayout(this).apply {
|
||||
orientation = LinearLayout.VERTICAL
|
||||
@ -47,7 +66,8 @@ class MainActivity : AppCompatActivity() {
|
||||
inputType = android.text.InputType.TYPE_CLASS_TEXT or
|
||||
android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD
|
||||
}
|
||||
container.addView(etUser); container.addView(etPass)
|
||||
container.addView(etUser)
|
||||
container.addView(etPass)
|
||||
|
||||
val dialog = AlertDialog.Builder(this)
|
||||
.setTitle("Login erforderlich")
|
||||
@ -67,18 +87,29 @@ class MainActivity : AppCompatActivity() {
|
||||
username = user,
|
||||
password = pass,
|
||||
onSuccess = { token ->
|
||||
// Token & Login-Timestamp werden in TokenStore gespeichert (siehe LoginManager/TokenStore).
|
||||
// Nach erfolgreichem Login: einmalig komplette DB ziehen
|
||||
DatabaseDownloader.downloadAndReplaceDatabase(
|
||||
context = this,
|
||||
token = token
|
||||
) { ok ->
|
||||
showBusy(false)
|
||||
if (!ok) {
|
||||
Toast.makeText(this, "Download fehlgeschlagen", Toast.LENGTH_LONG).show()
|
||||
|
||||
// Wenn Download fehlgeschlagen ist, aber evtl. schon eine DB lokal liegt,
|
||||
// lassen wir den Nutzer trotzdem weiterarbeiten (Offline).
|
||||
if (!ok && !hasLocalDb()) {
|
||||
Toast.makeText(this, "Download fehlgeschlagen – keine lokale Datenbank vorhanden", Toast.LENGTH_LONG).show()
|
||||
// Zurück zum Login, damit man es erneut probieren kann
|
||||
showLoginThenDownload()
|
||||
return@downloadAndReplaceDatabase
|
||||
}
|
||||
|
||||
if (!ok) {
|
||||
Toast.makeText(this, "Download fehlgeschlagen – arbeite offline mit vorhandener DB", Toast.LENGTH_LONG).show()
|
||||
}
|
||||
|
||||
// Opening-Screen starten
|
||||
openingScreenHandler = HandlerOpeningScreen(this)
|
||||
openingScreenHandler.init()
|
||||
// Ersten Status sofort anzeigen
|
||||
openingScreenHandler.refreshHeaderStatusLive()
|
||||
}
|
||||
},
|
||||
@ -111,11 +142,10 @@ class MainActivity : AppCompatActivity() {
|
||||
|
||||
private fun dp(v: Int): Int = (v * resources.displayMetrics.density).toInt()
|
||||
|
||||
// --- LIVE NETZSTATUS ---
|
||||
// --- LIVE NETZSTATUS (optional, für deine Status-Leiste) ---
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
registerNetworkCallback()
|
||||
// Falls die Startseite schon steht: Status jetzt gleich ziehen
|
||||
if (::openingScreenHandler.isInitialized && !isInQuestionnaire) {
|
||||
openingScreenHandler.refreshHeaderStatusLive()
|
||||
}
|
||||
@ -127,7 +157,7 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun registerNetworkCallback() {
|
||||
if (netCb != null) return // schon aktiv
|
||||
if (netCb != null) return
|
||||
val cm = getSystemService(ConnectivityManager::class.java)
|
||||
val req = NetworkRequest.Builder()
|
||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||
@ -147,7 +177,6 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
}
|
||||
// Optional: auch auf „nicht validiert“ reagieren
|
||||
override fun onCapabilitiesChanged(network: Network, caps: NetworkCapabilities) {
|
||||
runOnUiThread {
|
||||
if (::openingScreenHandler.isInitialized && !isInQuestionnaire) {
|
||||
|
||||
Reference in New Issue
Block a user