First working sync (with hardcoded credentials and no concurrent modification checks)

This commit is contained in:
2024-11-07 19:43:56 +01:00
parent 106f721365
commit 6432045419
14 changed files with 229 additions and 120 deletions

View File

@@ -0,0 +1,48 @@
package it.danieleverducci.lunatracker.repository
import android.content.Context
import it.danieleverducci.lunatracker.entities.Logbook
import android.util.Log
import it.danieleverducci.lunatracker.entities.LunaEvent
import org.json.JSONArray
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
class FileLogbookRepository: LogbookRepository {
companion object {
val TAG = "FileLogbookRepository"
}
override fun loadLogbook(context: Context, listener: LogbookLoadedListener) {
val logbook = Logbook()
val file = File(context.getFilesDir(), "data.json")
try {
val json = FileInputStream(file).bufferedReader().use { it.readText() }
val ja = JSONArray(json)
for (i in 0 until ja.length()) {
val jo = ja.getJSONObject(i)
val evt = LunaEvent.fromJson(jo)
logbook.logs.add(evt)
}
} catch (e: FileNotFoundException) {
Log.d(TAG, "No logbook file found")
listener.onError(e.toString())
}
listener.onLogbookLoaded(logbook)
}
override fun saveLogbook(
context: Context,
logbook: Logbook,
listener: LogbookSavedListener
) {
val file = File(context.getFilesDir(), "data.json")
val ja = JSONArray()
for (l in logbook.logs) {
ja.put(l.toJson())
}
file.writeText(ja.toString())
listener.onLogbookSaved()
}
}

View File

@@ -0,0 +1,19 @@
package it.danieleverducci.lunatracker.repository
import android.content.Context
import it.danieleverducci.lunatracker.entities.Logbook
interface LogbookRepository {
fun loadLogbook(context: Context, listener: LogbookLoadedListener)
fun saveLogbook(context: Context, logbook: Logbook, listener: LogbookSavedListener)
}
interface LogbookLoadedListener {
fun onLogbookLoaded(logbook: Logbook)
fun onError(error: String)
}
interface LogbookSavedListener {
fun onLogbookSaved()
fun onError(error: String)
}

View File

@@ -0,0 +1,74 @@
package it.danieleverducci.lunatracker.repository
import android.content.Context
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 java.io.BufferedReader
import kotlin.io.bufferedReader
class WebDAVLogbookRepository(val webDavURL: String, val username: String, val password: String): LogbookRepository {
companion object {
val TAG = "LogbookRepository"
val FILE_NAME = "lunatracker_logbook.json"
}
val sardine: OkHttpSardine = OkHttpSardine()
init {
sardine.setCredentials(
username,
password
)
}
override fun loadLogbook(context: Context, listener: LogbookLoadedListener) {
Thread(Runnable {
try {
val inputStream = sardine.get("$webDavURL/$FILE_NAME")
val json = inputStream.bufferedReader().use(BufferedReader::readText)
val ja = JSONArray(json)
val logbook = Logbook()
for (i in 0 until ja.length()) {
val jo = ja.getJSONObject(i)
val evt = LunaEvent.fromJson(jo)
logbook.logs.add(evt)
}
listener.onLogbookLoaded(logbook)
} catch (e: SardineException) {
listener.onError(e.toString())
}
}).start()
}
override fun saveLogbook(context: Context, logbook: Logbook, listener: LogbookSavedListener) {
Thread(Runnable {
// Lock logbook on WebDAV to avoid concurrent changes
//sardine.lock(getUrl())
// Reload logbook from WebDAV
// Merge logbooks (based on time)
// Write logbook
// Unlock logbook on WebDAV
//sardine.unlock(getUrl())
val ja = JSONArray()
for (l in logbook.logs) {
ja.put(l.toJson())
}
try {
sardine.put(getUrl(), ja.toString().toByteArray())
listener.onLogbookSaved()
} catch (e: SardineException) {
listener.onError(e.toString())
}
}).start()
}
private fun getUrl(): String {
return "${TemporaryHardcodedCredentials.URL}/$FILE_NAME"
}
}