Fixes crash with backups

realmasync
Laurent 2 years ago
parent 98c2434365
commit e4aa792542
  1. 16
      app/src/main/java/net/pokeranalytics/android/api/BackupApi.kt
  2. 12
      app/src/main/java/net/pokeranalytics/android/util/BackupOperator.kt

@ -3,7 +3,7 @@ package net.pokeranalytics.android.api
import android.content.Context
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.async
import net.pokeranalytics.android.util.extensions.isNetworkAvailable
import okhttp3.MediaType
import okhttp3.MultipartBody
@ -40,7 +40,7 @@ object BackupApi {
val service = BackupService()
// curl -F recipient=laurent@staxriver.com -F file=@test.txt https://www.pokeranalytics.net/backup/send
fun backupFile(context: Context, mail: String, fileName: String, fileContent: String): Boolean {
suspend fun backupFile(context: Context, mail: String, fileName: String, fileContent: String): Boolean {
val filePart = MultipartBody.Part.createFormData(
"file",
@ -51,12 +51,20 @@ object BackupApi {
val mailPart = MultipartBody.Part.createFormData("recipient", mail)
return if (context.isNetworkAvailable()) {
CoroutineScope(context = Dispatchers.IO).launch {
var success = false
val job = CoroutineScope(context = Dispatchers.IO).async {
success = try {
val response = service.backupApi.postFile(mailPart, filePart).execute()
Timber.d("response code = ${response.code()}")
Timber.d("success = ${response.isSuccessful}")
}
true
} catch (e: Exception) {
Timber.d("!!! backup failed: ${e.message}")
false
}
}
job.await()
return success
} else {
false
}

@ -3,6 +3,9 @@ package net.pokeranalytics.android.util
import android.content.Context
import io.realm.Realm
import io.realm.RealmResults
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.pokeranalytics.android.api.BackupApi
import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.model.realm.Transaction
@ -54,8 +57,10 @@ class BackupOperator(var context: Context) {
val csv = ProductCSVDescriptors.pokerAnalyticsAndroid6Sessions.toCSV(sessions)
val fileName = "sessions_${Date().dateTimeFileFormatted}.csv"
CoroutineScope(context = Dispatchers.IO).launch {
if (BackupApi.backupFile(context, email, fileName, csv)) {
this.sessionsChanged = false
sessionsChanged = false
}
}
}
@ -68,8 +73,11 @@ class BackupOperator(var context: Context) {
val transactions = this.realm.where(Transaction::class.java).findAll().sort("date")
val csv = ProductCSVDescriptors.pokerAnalyticsAndroidTransactions.toCSV(transactions)
val fileName = "transactions_${Date().dateTimeFileFormatted}.csv"
CoroutineScope(context = Dispatchers.IO).launch {
if (BackupApi.backupFile(context, email, fileName, csv)) {
this.transactionsChanged = false
transactionsChanged = false
}
}
}

Loading…
Cancel
Save