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

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

View File

@ -25,6 +25,10 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/settings_title"
android:theme="@style/Theme.LunaTracker"/>
</application>
</manifest>

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()

View File

@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="960" android:viewportWidth="960" android:width="24dp">
<path android:fillColor="@android:color/white" android:pathData="M198,682Q141,625 110.5,552.5Q80,480 80,400Q80,320 110.5,247.5Q141,175 198,118L246,166Q199,213 173.5,273.5Q148,334 148,400Q148,466 173.5,526.5Q199,587 246,634L198,682ZM290,590Q252,552 232,503Q212,454 212,400Q212,346 232,297Q252,248 290,210L338,258Q309,287 294.5,323.5Q280,360 280,400Q280,440 294.5,476.5Q309,513 338,542L290,590ZM440,840L440,492Q413,480 396.5,455Q380,430 380,400Q380,358 409,329Q438,300 480,300Q522,300 551,329Q580,358 580,400Q580,430 563.5,455Q547,480 520,492L520,840L440,840ZM670,590L622,542Q651,513 665.5,476.5Q680,440 680,400Q680,360 665.5,323.5Q651,287 622,258L670,210Q708,248 728,297Q748,346 748,400Q748,454 728,503Q708,552 670,590ZM762,682L714,634Q761,587 786.5,526.5Q812,466 812,400Q812,334 786.5,273.5Q761,213 714,166L762,118Q819,175 849.5,247.5Q880,320 880,400Q880,480 849.5,552.5Q819,625 762,682Z"/>
</vector>

View File

@ -0,0 +1,5 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:height="24dp" android:tint="#000000" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">
<path android:fillColor="@android:color/white" android:pathData="M19.14,12.94c0.04,-0.3 0.06,-0.61 0.06,-0.94c0,-0.32 -0.02,-0.64 -0.07,-0.94l2.03,-1.58c0.18,-0.14 0.23,-0.41 0.12,-0.61l-1.92,-3.32c-0.12,-0.22 -0.37,-0.29 -0.59,-0.22l-2.39,0.96c-0.5,-0.38 -1.03,-0.7 -1.62,-0.94L14.4,2.81c-0.04,-0.24 -0.24,-0.41 -0.48,-0.41h-3.84c-0.24,0 -0.43,0.17 -0.47,0.41L9.25,5.35C8.66,5.59 8.12,5.92 7.63,6.29L5.24,5.33c-0.22,-0.08 -0.47,0 -0.59,0.22L2.74,8.87C2.62,9.08 2.66,9.34 2.86,9.48l2.03,1.58C4.84,11.36 4.8,11.69 4.8,12s0.02,0.64 0.07,0.94l-2.03,1.58c-0.18,0.14 -0.23,0.41 -0.12,0.61l1.92,3.32c0.12,0.22 0.37,0.29 0.59,0.22l2.39,-0.96c0.5,0.38 1.03,0.7 1.62,0.94l0.36,2.54c0.05,0.24 0.24,0.41 0.48,0.41h3.84c0.24,0 0.44,-0.17 0.47,-0.41l0.36,-2.54c0.59,-0.24 1.13,-0.56 1.62,-0.94l2.39,0.96c0.22,0.08 0.47,0 0.59,-0.22l1.92,-3.32c0.12,-0.22 0.07,-0.47 -0.12,-0.61L19.14,12.94zM12,15.6c-1.98,0 -3.6,-1.62 -3.6,-3.6s1.62,-3.6 3.6,-3.6s3.6,1.62 3.6,3.6S13.98,15.6 12,15.6z"/>
</vector>

View File

@ -1,142 +1,200 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
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">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title"
android:textSize="30dp"
android:gravity="center_horizontal"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/log_an_event"
android:gravity="center_horizontal"/>
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
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">
<TextView
android:id="@+id/button_bottle"
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title"
android:textSize="30dp"
android:gravity="center_horizontal"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/log_an_event"
android:gravity="center_horizontal"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/button_bottle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_margin="10dp"
android:background="@drawable/button_background"
android:gravity="center_horizontal"
android:textSize="50sp"
android:text="@string/event_bottle_type"/>
<TextView
android:id="@+id/button_scale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="10dp"
android:background="@drawable/button_background"
android:gravity="center_horizontal"
android:textSize="50sp"
android:text="@string/event_scale_type"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/button_nipple_left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/button_background"
android:gravity="center_horizontal"
android:textSize="30sp"
android:text="🤱⬅️"/>
<TextView
android:id="@+id/button_nipple_both"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/button_background"
android:gravity="center_horizontal"
android:textSize="30sp"
android:text="🤱↔️"/>
<TextView
android:id="@+id/button_nipple_right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/button_background"
android:gravity="center_horizontal"
android:textSize="30sp"
android:text="🤱➡️️"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/button_change_poo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/button_background"
android:gravity="center_horizontal"
android:textSize="30sp"
android:text="🚼 💩"/>
<TextView
android:id="@+id/button_change_pee"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/button_background"
android:gravity="center_horizontal"
android:textSize="30sp"
android:text="🚼 💧"/>
</LinearLayout>
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/progress_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_margin="10dp"
android:background="@drawable/button_background"
android:gravity="center_horizontal"
android:textSize="50sp"
android:text="@string/event_bottle_type"/>
android:indeterminate="true"
app:indicatorColor="@color/accent"
android:visibility="invisible"/>
<TextView
android:id="@+id/button_scale"
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Diario di bordo"
android:textColor="@color/accent"
android:textStyle="bold"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_events"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:background="@drawable/button_background"
android:gravity="center_horizontal"
android:textSize="50sp"
android:text="@string/event_scale_type"/>
android:layout_marginTop="10dp"/>
</LinearLayout>
<ImageView
android:id="@+id/button_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="right"
android:src="@drawable/ic_settings"
app:tint="@color/grey"/>
<LinearLayout
android:id="@+id/no_connection_screen"
android:layout_width="match_parent"
android:layout_height="wrap_content">
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">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/ic_connection"
app:tint="@color/accent"/>
<TextView
android:id="@+id/button_nipple_left"
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/button_background"
android:gravity="center_horizontal"
android:textSize="30sp"
android:text="🤱⬅️"/>
android:textSize="30dp"
android:textColor="@color/accent"
android:text="@string/no_connection"/>
<TextView
android:id="@+id/button_nipple_both"
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/button_background"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
android:textSize="30sp"
android:text="🤱↔️"/>
android:text="@string/no_connection_explain"/>
<TextView
android:id="@+id/button_nipple_right"
android:layout_width="0dp"
<Button
android:id="@+id/button_no_connection_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/button_background"
android:gravity="center_horizontal"
android:textSize="30sp"
android:text="🤱➡️️"/>
android:layout_marginTop="20dp"
android:text="@string/no_connection_go_to_settings"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/button_change_poo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/button_background"
android:gravity="center_horizontal"
android:textSize="30sp"
android:text="🚼 💩"/>
<TextView
android:id="@+id/button_change_pee"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="@drawable/button_background"
android:gravity="center_horizontal"
android:textSize="30sp"
android:text="🚼 💧"/>
</LinearLayout>
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/progress_indicator"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:indeterminate="true"
app:indicatorColor="@color/accent"
android:visibility="invisible"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Diario di bordo"
android:textColor="@color/accent"
android:textStyle="bold"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list_events"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="10dp"/>
</LinearLayout>
</FrameLayout>

View File

@ -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"/>
<TextView
android:layout_width="match_parent"
@ -101,13 +102,6 @@
android:layout_marginTop="20dp"
android:orientation="horizontal">
<Button
android:id="@+id/settings_save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@android:string/ok"/>
<Button
android:id="@+id/settings_cancel"
android:layout_width="0dp"
@ -115,6 +109,14 @@
android:layout_weight="1"
android:text="@android:string/cancel"/>
<Button
android:id="@+id/settings_save"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="@color/accent"
android:text="@android:string/ok"/>
</LinearLayout>
</RadioGroup>

View File

@ -3,4 +3,6 @@
<color name="accent">#FFE68F</color>
<color name="black">#FF000000</color>
<color name="white">#FFFFFFFF</color>
<color name="translucent">#c000</color>
<color name="grey">#ccc</color>
</resources>

View File

@ -28,7 +28,8 @@
<string name="event_diaperchange_pee_desc">Cambio (con pipì)</string>
<string name="event_unknown_desc"></string>
<string name="toast_event_added">Evento aggiunto!</string>
<string name="toast_event_added">Evento aggiunto</string>
<string name="toast_event_add_error">Impossibile aggiungere l\'evento</string>
<string name="toast_integer_error">Valore non valido. Inserire un numero intero.</string>
<string name="now">adesso</string>
@ -37,6 +38,10 @@
<string name="minute_ago">min</string>
<string name="minutes_ago">min</string>
<string name="no_connection">Nessuna connessione</string>
<string name="no_connection_explain">Impossibile raggiungere il servizio WebDAV</string>
<string name="no_connection_go_to_settings">Impostazioni</string>
<string name="settings_title">Impostazioni</string>
<string name="settings_storage">Scegli dove l\'app salva i dati</string>
<string name="settings_storage_local">Sul dispositivo</string>