From 484a4698facfe61e3c2d9f63455f31d43d6035b9 Mon Sep 17 00:00:00 2001 From: Daniele Verducci Date: Mon, 18 Nov 2024 07:37:03 +0100 Subject: [PATCH] Using proper error screen --- .../lunatracker/MainActivity.kt | 54 +++++++++---------- .../repository/FileLogbookRepository.kt | 3 +- app/src/main/res/layout/activity_main.xml | 1 + 3 files changed, 26 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt index 4474daa..521a645 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt @@ -49,12 +49,6 @@ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - val localSettings = LocalSettingsRepository(this) - val webDavCredentials = localSettings.loadWebdavCredentials() - if (webDavCredentials == null) { - TODO("Not supported ATM (TODO: apply settings)") - } - handler = Handler(mainLooper) adapter = LunaEventRecyclerAdapter(this) @@ -214,46 +208,45 @@ class MainActivity : AppCompatActivity() { override fun onIOError(error: IOException) { runOnUiThread({ progressIndicator.visibility = View.INVISIBLE - Toast.makeText(this@MainActivity, getString(R.string.settings_network_error) + error.toString(), Toast.LENGTH_SHORT).show() + onRepoError(getString(R.string.settings_network_error) + error.toString()) }) } override fun onWebDAVError(error: SardineException) { runOnUiThread({ progressIndicator.visibility = View.INVISIBLE - if(error.toString().contains("401")) { - Toast.makeText(this@MainActivity, getString(R.string.settings_webdav_error_denied), Toast.LENGTH_SHORT).show() - } else { - Toast.makeText(this@MainActivity, getString(R.string.settings_webdav_error_generic) + error.toString(), Toast.LENGTH_SHORT).show() - } + onRepoError( + if(error.toString().contains("401")) { + getString(R.string.settings_webdav_error_denied) + } else { + getString(R.string.settings_webdav_error_generic) + error.toString() + } + ) }) } override fun onJSONError(error: JSONException) { runOnUiThread({ progressIndicator.visibility = View.INVISIBLE - Toast.makeText(this@MainActivity, getString(R.string.settings_json_error) + error.toString(), Toast.LENGTH_SHORT).show() + onRepoError(getString(R.string.settings_json_error) + error.toString()) }) } override fun onError(error: Exception) { runOnUiThread({ progressIndicator.visibility = View.INVISIBLE - Toast.makeText(this@MainActivity, getString(R.string.settings_generic_error) + error.toString(), Toast.LENGTH_SHORT).show() + onRepoError(getString(R.string.settings_generic_error) + error.toString()) }) } }) } - fun onRepoError(e: Exception){ + fun onRepoError(message: String){ runOnUiThread({ progressIndicator.visibility = View.INVISIBLE findViewById(R.id.no_connection_screen).visibility = View.VISIBLE - - Log.e(TAG, "Unable to load logbook: ${e.toString()} . Created a new one.") - logbook = Logbook() - showLogbook() + findViewById(R.id.no_connection_screen_message).text = message }) } @@ -279,7 +272,7 @@ class MainActivity : AppCompatActivity() { override fun onIOError(error: IOException) { runOnUiThread({ progressIndicator.visibility = View.INVISIBLE - Toast.makeText(this@MainActivity, getString(R.string.settings_network_error) + error.toString(), Toast.LENGTH_SHORT).show() + onRepoError(getString(R.string.settings_network_error) + error.toString()) onAddError(event, error.toString()) }) } @@ -287,20 +280,21 @@ class MainActivity : AppCompatActivity() { override fun onWebDAVError(error: SardineException) { runOnUiThread({ progressIndicator.visibility = View.INVISIBLE - if(error.toString().contains("401")) { - Toast.makeText(this@MainActivity, getString(R.string.settings_webdav_error_denied), Toast.LENGTH_SHORT).show() - onAddError(event, error.toString()) - } else { - Toast.makeText(this@MainActivity, getString(R.string.settings_webdav_error_generic) + error.toString(), Toast.LENGTH_SHORT).show() - onAddError(event, error.toString()) - } + onRepoError( + if(error.toString().contains("401")) { + getString(R.string.settings_webdav_error_denied) + } else { + getString(R.string.settings_webdav_error_generic) + error.toString() + } + ) + onAddError(event, error.toString()) }) } override fun onJSONError(error: JSONException) { runOnUiThread({ progressIndicator.visibility = View.INVISIBLE - Toast.makeText(this@MainActivity, getString(R.string.settings_json_error) + error.toString(), Toast.LENGTH_SHORT).show() + onRepoError(getString(R.string.settings_json_error) + error.toString()) onAddError(event, error.toString()) }) } @@ -308,7 +302,7 @@ class MainActivity : AppCompatActivity() { override fun onError(error: Exception) { runOnUiThread({ progressIndicator.visibility = View.INVISIBLE - Toast.makeText(this@MainActivity, getString(R.string.settings_generic_error) + error.toString(), Toast.LENGTH_SHORT).show() + onRepoError(getString(R.string.settings_generic_error) + error.toString()) onAddError(event, error.toString()) }) } @@ -316,7 +310,7 @@ class MainActivity : AppCompatActivity() { } private fun onAddError(event: LunaEvent, error: String) { - Log.e(TAG, "Logbook was NOT saved!") + Log.e(TAG, "Logbook was NOT saved! $error") runOnUiThread({ progressIndicator.visibility = View.INVISIBLE diff --git a/app/src/main/java/it/danieleverducci/lunatracker/repository/FileLogbookRepository.kt b/app/src/main/java/it/danieleverducci/lunatracker/repository/FileLogbookRepository.kt index 87b3444..f56a8d1 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/repository/FileLogbookRepository.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/repository/FileLogbookRepository.kt @@ -18,8 +18,7 @@ class FileLogbookRepository: LogbookRepository { try { listener.onLogbookLoaded(loadLogbook(context)) } catch (e: FileNotFoundException) { - Log.d(TAG, "No logbook file found") - listener.onIOError(e) + Log.d(TAG, "No logbook file found, will be created at first save") } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 9d178db..7245d7f 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -191,6 +191,7 @@ android:text="@string/no_connection"/>