parent
b5769173b7
commit
4583b5e12a
@ -0,0 +1,75 @@ |
||||
package net.pokeranalytics.android.util |
||||
|
||||
import android.content.Context |
||||
import androidx.work.Worker |
||||
import androidx.work.WorkerParameters |
||||
import io.realm.Realm |
||||
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 |
||||
import net.pokeranalytics.android.util.csv.DataType |
||||
import net.pokeranalytics.android.util.csv.ProductCSVDescriptors |
||||
import net.pokeranalytics.android.util.extensions.dateTimeFileFormatted |
||||
import timber.log.Timber |
||||
import java.util.* |
||||
|
||||
|
||||
class BackupTask(var context: Context, var params: WorkerParameters) : Worker(context, params) { |
||||
|
||||
enum class ParamKeys(val value: String) { |
||||
DATA("title"), |
||||
} |
||||
|
||||
override fun doWork(): Result { |
||||
|
||||
val data = params.inputData |
||||
|
||||
val dataTypeInt = data.getInt(ParamKeys.DATA.value, 0) |
||||
val dataType = DataType.values()[dataTypeInt] |
||||
|
||||
Preferences.getBackupEmail(context)?.let { email -> |
||||
when(dataType) { |
||||
DataType.SESSION -> { |
||||
backupSessions(email) |
||||
} |
||||
DataType.TRANSACTION -> { |
||||
backupTransactions(email) |
||||
} |
||||
} |
||||
} |
||||
|
||||
return Result.success() |
||||
} |
||||
|
||||
private fun backupSessions(email: String) { |
||||
|
||||
Timber.d(">>>> backup sessions") |
||||
val realm = Realm.getDefaultInstance() |
||||
val sessions = realm.where(Session::class.java).findAll().sort("startDate") |
||||
val csv = ProductCSVDescriptors.pokerAnalyticsAndroid6Sessions.toCSV(sessions) |
||||
val fileName = "sessions_${Date().dateTimeFileFormatted}.csv" |
||||
|
||||
CoroutineScope(context = Dispatchers.IO).launch { |
||||
BackupApi.backupFile(context, email, fileName, csv) |
||||
} |
||||
realm.close() |
||||
} |
||||
|
||||
private fun backupTransactions(email: String) { |
||||
|
||||
Timber.d(">>>> backup transactions") |
||||
val realm = Realm.getDefaultInstance() |
||||
val transactions = 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 { |
||||
BackupApi.backupFile(context, email, fileName, csv) |
||||
} |
||||
realm.close() |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue