Sync button, prevented concurrent sync

This commit is contained in:
Daniele Verducci 2024-11-10 11:08:03 +01:00
parent 8d218fbefe
commit 2b94b56350
4 changed files with 46 additions and 2 deletions

View File

@ -35,6 +35,7 @@ class MainActivity : AppCompatActivity() {
lateinit var progressIndicator: LinearProgressIndicator lateinit var progressIndicator: LinearProgressIndicator
lateinit var recyclerView: RecyclerView lateinit var recyclerView: RecyclerView
lateinit var handler: Handler lateinit var handler: Handler
var savingEvent = false
val updateListRunnable: Runnable = Runnable { val updateListRunnable: Runnable = Runnable {
loadLogbook() loadLogbook()
handler.postDelayed(updateListRunnable, 1000*60) handler.postDelayed(updateListRunnable, 1000*60)
@ -92,6 +93,12 @@ class MainActivity : AppCompatActivity() {
findViewById<View>(R.id.button_settings).setOnClickListener({ findViewById<View>(R.id.button_settings).setOnClickListener({
showSettings() showSettings()
}) })
findViewById<View>(R.id.button_no_connection_retry).setOnClickListener({
loadLogbook()
})
findViewById<View>(R.id.button_sync).setOnClickListener({
loadLogbook()
})
} }
fun showSettings() { fun showSettings() {
@ -167,12 +174,16 @@ class MainActivity : AppCompatActivity() {
} }
fun loadLogbook() { fun loadLogbook() {
if (savingEvent)
return
// Load data // Load data
progressIndicator.visibility = View.VISIBLE progressIndicator.visibility = View.VISIBLE
logbookRepo.loadLogbook(this, object: LogbookLoadedListener{ logbookRepo.loadLogbook(this, object: LogbookLoadedListener{
override fun onLogbookLoaded(lb: Logbook) { override fun onLogbookLoaded(lb: Logbook) {
runOnUiThread({ runOnUiThread({
progressIndicator.visibility = View.INVISIBLE progressIndicator.visibility = View.INVISIBLE
findViewById<View>(R.id.no_connection_screen).visibility = View.GONE
logbook = lb logbook = lb
showLogbook() showLogbook()
}) })
@ -192,6 +203,7 @@ class MainActivity : AppCompatActivity() {
} }
fun logEvent(event: LunaEvent) { fun logEvent(event: LunaEvent) {
savingEvent = true
adapter.items.add(0, event) adapter.items.add(0, event)
adapter.notifyItemInserted(0) adapter.notifyItemInserted(0)
recyclerView.smoothScrollToPosition(0) recyclerView.smoothScrollToPosition(0)
@ -205,6 +217,7 @@ class MainActivity : AppCompatActivity() {
progressIndicator.visibility = View.INVISIBLE progressIndicator.visibility = View.INVISIBLE
Toast.makeText(this@MainActivity, R.string.toast_event_added, Toast.LENGTH_SHORT).show() Toast.makeText(this@MainActivity, R.string.toast_event_added, Toast.LENGTH_SHORT).show()
savingEvent = false
}) })
} }
@ -216,6 +229,7 @@ class MainActivity : AppCompatActivity() {
Toast.makeText(this@MainActivity, R.string.toast_event_add_error, Toast.LENGTH_SHORT).show() Toast.makeText(this@MainActivity, R.string.toast_event_add_error, Toast.LENGTH_SHORT).show()
adapter.items.remove(event) adapter.items.remove(event)
adapter.notifyDataSetChanged() adapter.notifyDataSetChanged()
savingEvent = false
}) })
} }

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="M12,4L12,1L8,5l4,4L12,6c3.31,0 6,2.69 6,6 0,1.01 -0.25,1.97 -0.7,2.8l1.46,1.46C19.54,15.03 20,13.57 20,12c0,-4.42 -3.58,-8 -8,-8zM12,18c-3.31,0 -6,-2.69 -6,-6 0,-1.01 0.25,-1.97 0.7,-2.8L5.24,7.74C4.46,8.97 4,10.43 4,12c0,4.42 3.58,8 8,8v3l4,-4 -4,-4v3z"/>
</vector>

View File

@ -151,10 +151,19 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:padding="10dp" android:padding="10dp"
android:layout_gravity="right" android:layout_gravity="end"
android:src="@drawable/ic_settings" android:src="@drawable/ic_settings"
app:tint="@color/grey"/> app:tint="@color/grey"/>
<ImageView
android:id="@+id/button_sync"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="start"
android:src="@drawable/ic_sync"
app:tint="@color/grey"/>
<LinearLayout <LinearLayout
android:id="@+id/no_connection_screen" android:id="@+id/no_connection_screen"
android:layout_width="match_parent" android:layout_width="match_parent"
@ -188,12 +197,27 @@
android:gravity="center_horizontal" android:gravity="center_horizontal"
android:text="@string/no_connection_explain"/> android:text="@string/no_connection_explain"/>
<Button
android:id="@+id/button_no_connection_retry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:drawableLeft="@drawable/ic_sync"
android:drawableTint="@color/white"
android:drawablePadding="10dp"
android:text="@string/no_connection_retry"
android:textColor="@color/white"/>
<Button <Button
android:id="@+id/button_no_connection_settings" android:id="@+id/button_no_connection_settings"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="20dp" android:layout_marginTop="20dp"
android:text="@string/no_connection_go_to_settings"/> android:drawableLeft="@drawable/ic_settings"
android:drawableTint="@color/white"
android:drawablePadding="10dp"
android:text="@string/no_connection_go_to_settings"
android:textColor="@color/white"/>
</LinearLayout> </LinearLayout>

View File

@ -41,6 +41,7 @@
<string name="no_connection">Nessuna connessione</string> <string name="no_connection">Nessuna connessione</string>
<string name="no_connection_explain">Impossibile raggiungere il servizio WebDAV</string> <string name="no_connection_explain">Impossibile raggiungere il servizio WebDAV</string>
<string name="no_connection_go_to_settings">Impostazioni</string> <string name="no_connection_go_to_settings">Impostazioni</string>
<string name="no_connection_retry">Riprova</string>
<string name="settings_title">Impostazioni</string> <string name="settings_title">Impostazioni</string>
<string name="settings_storage">Scegli dove l\'app salva i dati</string> <string name="settings_storage">Scegli dove l\'app salva i dati</string>