WIP Settings apply

This commit is contained in:
2024-11-16 09:13:20 +01:00
parent 24ee0500b3
commit afabb0c711
8 changed files with 231 additions and 34 deletions

View File

@@ -27,7 +27,7 @@ class FileLogbookRepository: LogbookRepository {
}
} catch (e: FileNotFoundException) {
Log.d(TAG, "No logbook file found")
listener.onError(e.toString())
listener.onIOError(e)
}
listener.onLogbookLoaded(logbook)
}

View File

@@ -8,10 +8,12 @@ class LocalSettingsRepository(val context: Context) {
companion object {
val SHARED_PREFS_FILE_NAME = "lunasettings"
val SHARED_PREFS_BB_CONTENT = "bbcontent"
val SHARED_PREFS_DATA_REPO = "webdav_url"
val SHARED_PREFS_DAV_URL = "webdav_url"
val SHARED_PREFS_DAV_USER = "webdav_user"
val SHARED_PREFS_DAV_PASS = "webdav_password"
}
enum class DATA_REPO {LOCAL_FILE, WEBDAV}
val sharedPreferences: SharedPreferences
init {
@@ -26,6 +28,27 @@ class LocalSettingsRepository(val context: Context) {
return sharedPreferences.getInt(SHARED_PREFS_BB_CONTENT, 1)
}
fun saveDataRepository(repo: DATA_REPO) {
val spe = sharedPreferences.edit()
spe.putString(
SHARED_PREFS_DATA_REPO,
when (repo) {
DATA_REPO.WEBDAV -> "webdav"
DATA_REPO.LOCAL_FILE -> "localfile"
}
)
spe.commit()
}
fun loadDataRepository(): DATA_REPO {
val repo = sharedPreferences.getString(SHARED_PREFS_DATA_REPO, null)
return when (repo) {
"webdav" -> DATA_REPO.WEBDAV
"localfile" -> DATA_REPO.LOCAL_FILE
else -> DATA_REPO.LOCAL_FILE
}
}
fun saveWebdavCredentials(url: String, username: String, password: String) {
val spe = sharedPreferences.edit()
spe.putString(SHARED_PREFS_DAV_URL, url)

View File

@@ -1,7 +1,10 @@
package it.danieleverducci.lunatracker.repository
import android.content.Context
import com.thegrizzlylabs.sardineandroid.impl.SardineException
import it.danieleverducci.lunatracker.entities.Logbook
import okio.IOException
import org.json.JSONException
interface LogbookRepository {
fun loadLogbook(context: Context, listener: LogbookLoadedListener)
@@ -10,7 +13,10 @@ interface LogbookRepository {
interface LogbookLoadedListener {
fun onLogbookLoaded(logbook: Logbook)
fun onError(error: String)
fun onIOError(error: IOException)
fun onWebDAVError(error: SardineException)
fun onJSONError(error: JSONException)
fun onError(error: Exception)
}
interface LogbookSavedListener {

View File

@@ -1,15 +1,17 @@
package it.danieleverducci.lunatracker.repository
import android.content.Context
import android.util.Log
import com.thegrizzlylabs.sardineandroid.impl.OkHttpSardine
import com.thegrizzlylabs.sardineandroid.impl.SardineException
import it.danieleverducci.lunatracker.TemporaryHardcodedCredentials
import it.danieleverducci.lunatracker.entities.Logbook
import it.danieleverducci.lunatracker.entities.LunaEvent
import kotlinx.coroutines.Runnable
import org.json.JSONArray
import org.json.JSONException
import java.io.BufferedReader
import java.io.IOException
import java.net.SocketTimeoutException
import kotlin.io.bufferedReader
class WebDAVLogbookRepository(val webDavURL: String, val username: String, val password: String): LogbookRepository {
@@ -40,8 +42,20 @@ class WebDAVLogbookRepository(val webDavURL: String, val username: String, val p
logbook.logs.add(evt)
}
listener.onLogbookLoaded(logbook)
} catch (e: SardineException) {
Log.e(TAG, e.toString())
listener.onWebDAVError(e)
} catch (e: IOException) {
listener.onError(e.toString())
Log.e(TAG, e.toString())
listener.onIOError(e)
} catch (e: SocketTimeoutException) {
Log.e(TAG, e.toString())
listener.onIOError(e)
} catch (e: JSONException) {
Log.e(TAG, e.toString())
listener.onJSONError(e)
} catch (e: Exception) {
listener.onError(e)
}
}).start()
}
@@ -71,6 +85,6 @@ class WebDAVLogbookRepository(val webDavURL: String, val username: String, val p
}
private fun getUrl(): String {
return "${TemporaryHardcodedCredentials.URL}/$FILE_NAME"
return "$webDavURL/$FILE_NAME"
}
}