From 8d218fbefefa958956d246d99f088f40703556e4 Mon Sep 17 00:00:00 2001 From: Daniele Verducci Date: Sun, 10 Nov 2024 10:45:23 +0100 Subject: [PATCH] Fix crash in case of WebDAV misconfigurazion or non reachability, WIP settings screen --- app/src/main/AndroidManifest.xml | 4 + .../lunatracker/MainActivity.kt | 28 +- .../lunatracker/SettingsActivity.kt | 15 +- .../repository/WebDAVLogbookRepository.kt | 4 +- app/src/main/res/drawable/ic_connection.xml | 5 + app/src/main/res/drawable/ic_settings.xml | 5 + app/src/main/res/layout/activity_main.xml | 280 +++++++++++------- app/src/main/res/layout/activity_settings.xml | 18 +- app/src/main/res/values/colors.xml | 2 + app/src/main/res/values/strings.xml | 7 +- 10 files changed, 242 insertions(+), 126 deletions(-) create mode 100644 app/src/main/res/drawable/ic_connection.xml create mode 100644 app/src/main/res/drawable/ic_settings.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 16000e1..e14a93f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -25,6 +25,10 @@ + \ No newline at end of file diff --git a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt index fa6f1ab..8613502 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt @@ -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(R.id.button_no_connection_settings).setOnClickListener({ + showSettings() + }) + findViewById(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(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() } } diff --git a/app/src/main/java/it/danieleverducci/lunatracker/SettingsActivity.kt b/app/src/main/java/it/danieleverducci/lunatracker/SettingsActivity.kt index 013e5b0..a5ba166 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/SettingsActivity.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/SettingsActivity.kt @@ -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(R.id.settings_save).setOnClickListener({ + Toast.makeText(this, "TODO", Toast.LENGTH_SHORT).show() + }) + findViewById(R.id.settings_cancel).setOnClickListener({ + finish() + }) } } \ No newline at end of file diff --git a/app/src/main/java/it/danieleverducci/lunatracker/repository/WebDAVLogbookRepository.kt b/app/src/main/java/it/danieleverducci/lunatracker/repository/WebDAVLogbookRepository.kt index 60d464e..e115cc3 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/repository/WebDAVLogbookRepository.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/repository/WebDAVLogbookRepository.kt @@ -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() diff --git a/app/src/main/res/drawable/ic_connection.xml b/app/src/main/res/drawable/ic_connection.xml new file mode 100644 index 0000000..ab1815a --- /dev/null +++ b/app/src/main/res/drawable/ic_connection.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/drawable/ic_settings.xml b/app/src/main/res/drawable/ic_settings.xml new file mode 100644 index 0000000..6593f3a --- /dev/null +++ b/app/src/main/res/drawable/ic_settings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 58f03ce..c2faa5c 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,142 +1,200 @@ - - - - - + xmlns:app="http://schemas.android.com/apk/res-auto"> + android:layout_height="match_parent" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:orientation="vertical" + android:paddingTop="30dp" + android:paddingLeft="15dp" + android:paddingRight="15dp"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + android:indeterminate="true" + app:indicatorColor="@color/accent" + android:visibility="invisible"/> + + + android:layout_marginTop="10dp"/> + + + android:layout_height="match_parent" + android:orientation="vertical" + android:gravity="center" + android:background="@color/translucent" + android:clickable="true" + android:focusableInTouchMode="true" + android:focusable="true" + android:visibility="gone"> + + + android:textSize="30dp" + android:textColor="@color/accent" + android:text="@string/no_connection"/> + android:text="@string/no_connection_explain"/> - + android:layout_marginTop="20dp" + android:text="@string/no_connection_go_to_settings"/> - - - - - - - - - - - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_settings.xml b/app/src/main/res/layout/activity_settings.xml index 9be0ad1..7984823 100644 --- a/app/src/main/res/layout/activity_settings.xml +++ b/app/src/main/res/layout/activity_settings.xml @@ -13,7 +13,8 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/settings_title" - android:textSize="28sp"/> + android:textSize="28sp" + android:textColor="@color/accent"/> -