Fixes crash with backups

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

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

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

Loading…
Cancel
Save