Big commit.
Managed to do upload and download right. No Race conditions.
This commit is contained in:
@ -9,6 +9,7 @@ import android.widget.*
|
||||
import kotlinx.coroutines.*
|
||||
import org.json.JSONArray
|
||||
import android.util.Log
|
||||
import java.io.File
|
||||
|
||||
|
||||
var INTEGRATION_INDEX_POINTS: Int? = null
|
||||
@ -35,6 +36,7 @@ class HandlerOpeningScreen(private val activity: MainActivity) {
|
||||
activity.setContentView(R.layout.opening_screen)
|
||||
|
||||
bindViews()
|
||||
|
||||
loadQuestionnaireOrder()
|
||||
createQuestionnaireButtons()
|
||||
restorePreviousClientCode()
|
||||
@ -46,11 +48,22 @@ class HandlerOpeningScreen(private val activity: MainActivity) {
|
||||
setupUploadButton()
|
||||
setupDownloadButton()
|
||||
|
||||
val dbPath = "/data/data/com.dano.test1/databases/questionnaire_database"
|
||||
val pathExists = File(dbPath).exists()
|
||||
if (pathExists) {
|
||||
updateMainButtonsState(true)
|
||||
}
|
||||
else{
|
||||
updateMainButtonsState(false)
|
||||
}
|
||||
|
||||
if (!editText.text.isNullOrBlank()) {
|
||||
buttonLoad.performClick()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private fun bindViews() {
|
||||
editText = activity.findViewById(R.id.editText)
|
||||
spinner = activity.findViewById(R.id.string_spinner1)
|
||||
@ -183,13 +196,12 @@ class HandlerOpeningScreen(private val activity: MainActivity) {
|
||||
}
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
updateMainButtonsState(true) // Datenbank vorhanden -> Buttons aktivieren
|
||||
handleNormalLoad(clientCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
private suspend fun handleNormalLoad(clientCode: String) {
|
||||
val completedIds = withContext(Dispatchers.IO) {
|
||||
MyApp.database.completedQuestionnaireDao().getCompletedQuestionnairesForClient(clientCode)
|
||||
@ -559,6 +571,22 @@ class HandlerOpeningScreen(private val activity: MainActivity) {
|
||||
}
|
||||
}
|
||||
|
||||
// --- Füge diese Funktion in deine Klasse ein ---
|
||||
private fun isDatabasePopulated(): Boolean {
|
||||
return try {
|
||||
// Wir prüfen, ob die Datenbank mindestens eine nicht-interne Tabelle enthält.
|
||||
// Das ist robust gegenüber verschiedenen Tabellennamen.
|
||||
val db = MyApp.database.openHelper.readableDatabase
|
||||
val cursor = db.query(
|
||||
"SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' AND name != 'room_master_table'"
|
||||
)
|
||||
cursor.use { it.count > 0 }
|
||||
} catch (e: Exception) {
|
||||
// Falls etwas schiefgeht (z.B. DB noch nicht vorhanden), gilt: nicht vorhanden
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
private fun setupDownloadButton() {
|
||||
downloadButton.text = "Download"
|
||||
downloadButton.setOnClickListener {
|
||||
@ -568,7 +596,15 @@ class HandlerOpeningScreen(private val activity: MainActivity) {
|
||||
|
||||
Toast.makeText(activity, "Datenbank wird heruntergeladen...", Toast.LENGTH_SHORT).show()
|
||||
DatabaseDownloader.downloadAndReplaceDatabase(activity)
|
||||
updateMainButtonsState(true)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateMainButtonsState(isDatabaseAvailable: Boolean) {
|
||||
val buttons = listOf(buttonLoad, saveButton, editButton)
|
||||
buttons.forEach { button ->
|
||||
button.isEnabled = isDatabaseAvailable
|
||||
button.alpha = if (isDatabaseAvailable) 1.0f else 0.5f
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user