Implemented transition from local to webdav logbooks and error management
This commit is contained in:
@@ -70,6 +70,18 @@ class FileLogbookRepository: LogbookRepository {
|
||||
context: Context,
|
||||
listener: LogbookListObtainedListener
|
||||
) {
|
||||
listener.onLogbookListObtained(listLogbooks(context))
|
||||
}
|
||||
|
||||
fun getAllLogbooks(context: Context): List<Logbook> {
|
||||
val logbooks = arrayListOf<Logbook>()
|
||||
for (logbookName in listLogbooks(context)) {
|
||||
logbooks.add(loadLogbook(context, logbookName))
|
||||
}
|
||||
return logbooks
|
||||
}
|
||||
|
||||
private fun listLogbooks(context: Context): ArrayList<String> {
|
||||
val logbooksFileNames = context.getFilesDir().list(object: FilenameFilter {
|
||||
override fun accept(dir: File?, name: String?): Boolean {
|
||||
if (name == null)
|
||||
@@ -81,8 +93,7 @@ class FileLogbookRepository: LogbookRepository {
|
||||
})
|
||||
|
||||
if (logbooksFileNames == null || logbooksFileNames.isEmpty()) {
|
||||
listener.onLogbookListObtained(arrayListOf())
|
||||
return
|
||||
return arrayListOf()
|
||||
}
|
||||
|
||||
val logbooksNames = arrayListOf<String>()
|
||||
@@ -91,7 +102,7 @@ class FileLogbookRepository: LogbookRepository {
|
||||
it.replace("${FILE_NAME_START}_", "").replace(FILE_NAME_START, "").replace(FILE_NAME_END, "")
|
||||
)
|
||||
}
|
||||
listener.onLogbookListObtained(logbooksNames)
|
||||
return logbooksNames
|
||||
}
|
||||
|
||||
private fun getFileName(name: String): String {
|
||||
|
||||
@@ -137,47 +137,46 @@ class WebDAVLogbookRepository(val webDavURL: String, val username: String, val p
|
||||
|
||||
/**
|
||||
* Connect to server and check if a logbook already exists.
|
||||
* If it does not exist, try to upload the local one (or create a new one).
|
||||
* If it does not exist, try to upload the local one.
|
||||
* @return error, or null if no error
|
||||
*/
|
||||
fun createLogbook(context: Context, name: String, listener: LogbookCreatedListener) {
|
||||
Thread(Runnable {
|
||||
try {
|
||||
loadLogbook(name)
|
||||
listener.onLogbookCreated()
|
||||
} catch (e: SardineException) {
|
||||
if (e.toString().contains("404")) {
|
||||
// Connection successful, but no existing save. Upload the local one.
|
||||
try {
|
||||
val flr = FileLogbookRepository()
|
||||
val logbook = flr.loadLogbook(context, name)
|
||||
saveLogbook(context, logbook)
|
||||
Log.d(TAG, "Local logbook file found, uploaded")
|
||||
listener.onLogbookCreated()
|
||||
} catch (e: FileNotFoundException) {
|
||||
Log.d(TAG, "No local logbook file found, uploading empty file")
|
||||
saveLogbook(context, Logbook(name))
|
||||
listener.onLogbookCreated()
|
||||
} catch (e: SardineException) {
|
||||
Log.e(TAG, "Unable to upload logbook: $e")
|
||||
listener.onWebDAVError(e)
|
||||
}
|
||||
} else {
|
||||
Log.e(TAG, e.toString())
|
||||
listener.onWebDAVError(e)
|
||||
fun uploadLogbookIfNotExists(context: Context, name: String): String? {
|
||||
val flr = FileLogbookRepository()
|
||||
try {
|
||||
loadLogbook(name)
|
||||
Log.d(TAG, "Logbook file $name already exist on the webDav share: will not overwrite it")
|
||||
return null
|
||||
} catch (e: SardineException) {
|
||||
if (e.toString().contains("404")) {
|
||||
// Connection successful, but logbook does not exist. Upload the local one.
|
||||
try {
|
||||
val logbook = flr.loadLogbook(context, name)
|
||||
saveLogbook(context, logbook)
|
||||
Log.d(TAG, "Local logbook file $name found, uploaded")
|
||||
return null
|
||||
} catch (e: FileNotFoundException) {
|
||||
Log.e(TAG, "No local logbook file found, this should not happen!")
|
||||
return "No local logbook file found, app is in inconsistent state, please delete and reinstall it"
|
||||
} catch (e: SardineException) {
|
||||
Log.e(TAG, "Unable to upload logbook: $e")
|
||||
return e.toString()
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
} else {
|
||||
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)
|
||||
return e.toString()
|
||||
}
|
||||
}).start()
|
||||
} catch (e: IOException) {
|
||||
Log.e(TAG, e.toString())
|
||||
return e.toString()
|
||||
} catch (e: SocketTimeoutException) {
|
||||
Log.e(TAG, e.toString())
|
||||
return e.toString()
|
||||
} catch (e: JSONException) {
|
||||
Log.e(TAG, e.toString())
|
||||
return e.toString()
|
||||
} catch (e: Exception) {
|
||||
return e.toString()
|
||||
}
|
||||
}
|
||||
|
||||
private fun getUrl(name: String): String {
|
||||
@@ -185,13 +184,4 @@ class WebDAVLogbookRepository(val webDavURL: String, val username: String, val p
|
||||
Log.d(TAG, fileName)
|
||||
return "$webDavURL/$fileName"
|
||||
}
|
||||
|
||||
|
||||
interface LogbookCreatedListener {
|
||||
fun onLogbookCreated()
|
||||
fun onIOError(error: okio.IOException)
|
||||
fun onWebDAVError(error: SardineException)
|
||||
fun onJSONError(error: JSONException)
|
||||
fun onError(error: Exception)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user