Fix crash in case of WebDAV misconfigurazion or non reachability, WIP settings screen

This commit is contained in:
2024-11-10 10:45:23 +01:00
parent 238e43509e
commit 8d218fbefe
10 changed files with 242 additions and 126 deletions

View File

@ -1,5 +1,6 @@
package it.danieleverducci.lunatracker
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.util.Log
@ -85,6 +86,18 @@ class MainActivity : AppCompatActivity() {
LunaEventType.DIAPERCHANGE_PEE
)
) }
findViewById<View>(R.id.button_no_connection_settings).setOnClickListener({
showSettings()
})
findViewById<View>(R.id.button_settings).setOnClickListener({
showSettings()
})
}
fun showSettings() {
val i = Intent(this, SettingsActivity::class.java)
startActivity(i)
}
fun showLogbook() {
@ -98,6 +111,9 @@ class MainActivity : AppCompatActivity() {
super.onStart()
// Update list dates
adapter.notifyDataSetChanged()
// Reload data
loadLogbook()
handler.postDelayed(updateListRunnable, 1000*30)
}
@ -165,7 +181,9 @@ class MainActivity : AppCompatActivity() {
override fun onError(error: String) {
runOnUiThread({
progressIndicator.visibility = View.INVISIBLE
Log.e(TAG, "Unable to load logbook. Create a new one.")
findViewById<View>(R.id.no_connection_screen).visibility = View.VISIBLE
Log.e(TAG, "Unable to load logbook: $error . Created a new one.")
logbook = Logbook()
showLogbook()
})
@ -185,6 +203,8 @@ class MainActivity : AppCompatActivity() {
Log.d(TAG, "Logbook saved")
runOnUiThread({
progressIndicator.visibility = View.INVISIBLE
Toast.makeText(this@MainActivity, R.string.toast_event_added, Toast.LENGTH_SHORT).show()
})
}
@ -192,12 +212,14 @@ class MainActivity : AppCompatActivity() {
Log.e(TAG, "ERROR: Logbook was NOT saved!")
runOnUiThread({
progressIndicator.visibility = View.INVISIBLE
Toast.makeText(this@MainActivity, R.string.toast_event_add_error, Toast.LENGTH_SHORT).show()
adapter.items.remove(event)
adapter.notifyDataSetChanged()
})
}
})
Toast.makeText(this, R.string.toast_event_added, Toast.LENGTH_SHORT).show()
}
}

View File

@ -2,12 +2,23 @@ package it.danieleverducci.lunatracker
import android.os.Bundle
import android.os.PersistableBundle
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
findViewById<View>(R.id.settings_save).setOnClickListener({
Toast.makeText(this, "TODO", Toast.LENGTH_SHORT).show()
})
findViewById<View>(R.id.settings_cancel).setOnClickListener({
finish()
})
}
}

View File

@ -9,6 +9,7 @@ import it.danieleverducci.lunatracker.entities.LunaEvent
import kotlinx.coroutines.Runnable
import org.json.JSONArray
import java.io.BufferedReader
import java.io.IOException
import kotlin.io.bufferedReader
class WebDAVLogbookRepository(val webDavURL: String, val username: String, val password: String): LogbookRepository {
@ -30,6 +31,7 @@ class WebDAVLogbookRepository(val webDavURL: String, val username: String, val p
try {
val inputStream = sardine.get("$webDavURL/$FILE_NAME")
val json = inputStream.bufferedReader().use(BufferedReader::readText)
inputStream.close()
val ja = JSONArray(json)
val logbook = Logbook()
for (i in 0 until ja.length()) {
@ -38,7 +40,7 @@ class WebDAVLogbookRepository(val webDavURL: String, val username: String, val p
logbook.logs.add(evt)
}
listener.onLogbookLoaded(logbook)
} catch (e: SardineException) {
} catch (e: IOException) {
listener.onError(e.toString())
}
}).start()