From 3600a30834244e74299d7905342dc0392397f33a Mon Sep 17 00:00:00 2001 From: inventory69 Date: Sat, 20 Dec 2025 01:19:41 +0100 Subject: [PATCH] Fix: Separate connection test from sync - Add testConnection() method that only tests connectivity - Connection test no longer syncs notes automatically - Only 'Sync Now' button triggers actual synchronization --- .../dettmer/simplenotes/SettingsActivity.kt | 4 +- .../simplenotes/sync/WebDavSyncService.kt | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/android/app/src/main/java/dev/dettmer/simplenotes/SettingsActivity.kt b/android/app/src/main/java/dev/dettmer/simplenotes/SettingsActivity.kt index 5148b90..55fbf81 100644 --- a/android/app/src/main/java/dev/dettmer/simplenotes/SettingsActivity.kt +++ b/android/app/src/main/java/dev/dettmer/simplenotes/SettingsActivity.kt @@ -97,10 +97,10 @@ class SettingsActivity : AppCompatActivity() { try { showToast("Teste Verbindung...") val syncService = WebDavSyncService(this@SettingsActivity) - val result = syncService.syncNotes() + val result = syncService.testConnection() if (result.isSuccess) { - showToast("Verbindung erfolgreich! ${result.syncedCount} Notizen synchronisiert") + showToast("Verbindung erfolgreich!") } else { showToast("Verbindung fehlgeschlagen: ${result.errorMessage}") } diff --git a/android/app/src/main/java/dev/dettmer/simplenotes/sync/WebDavSyncService.kt b/android/app/src/main/java/dev/dettmer/simplenotes/sync/WebDavSyncService.kt index 09e94cf..8b667a5 100644 --- a/android/app/src/main/java/dev/dettmer/simplenotes/sync/WebDavSyncService.kt +++ b/android/app/src/main/java/dev/dettmer/simplenotes/sync/WebDavSyncService.kt @@ -28,6 +28,52 @@ class WebDavSyncService(private val context: Context) { return prefs.getString(Constants.KEY_SERVER_URL, null) } + suspend fun testConnection(): SyncResult = withContext(Dispatchers.IO) { + return@withContext try { + val sardine = getSardine() ?: return@withContext SyncResult( + isSuccess = false, + errorMessage = "Server-Zugangsdaten nicht konfiguriert" + ) + + val serverUrl = getServerUrl() ?: return@withContext SyncResult( + isSuccess = false, + errorMessage = "Server-URL nicht konfiguriert" + ) + + // Only test if directory exists or can be created + val exists = sardine.exists(serverUrl) + if (!exists) { + sardine.createDirectory(serverUrl) + } + + SyncResult( + isSuccess = true, + syncedCount = 0, + errorMessage = null + ) + + } catch (e: Exception) { + SyncResult( + isSuccess = false, + errorMessage = when (e) { + is java.net.UnknownHostException -> "Server nicht erreichbar" + is java.net.SocketTimeoutException -> "Verbindungs-Timeout" + is javax.net.ssl.SSLException -> "SSL-Fehler" + is com.thegrizzlylabs.sardineandroid.impl.SardineException -> { + when (e.statusCode) { + 401 -> "Authentifizierung fehlgeschlagen" + 403 -> "Zugriff verweigert" + 404 -> "Server-Pfad nicht gefunden" + 500 -> "Server-Fehler" + else -> "HTTP-Fehler: ${e.statusCode}" + } + } + else -> e.message ?: "Unbekannter Fehler" + } + ) + } + } + suspend fun syncNotes(): SyncResult = withContext(Dispatchers.IO) { return@withContext try { val sardine = getSardine() ?: return@withContext SyncResult(