Cleaner code

This commit is contained in:
Benoit Marty 2019-06-13 19:27:51 +02:00
parent 5f0d1d9536
commit b682f3e982
3 changed files with 67 additions and 53 deletions

View File

@ -18,16 +18,18 @@ package im.vector.riotredesign.core.files


import android.app.DownloadManager import android.app.DownloadManager
import android.content.Context import android.content.Context
import androidx.annotation.WorkerThread
import arrow.core.Try
import okio.Okio import okio.Okio
import timber.log.Timber import timber.log.Timber
import java.io.File import java.io.File


/** /**
* Save a string to a file with Okio * Save a string to a file with Okio
* @return true in case of success
*/ */
fun saveStringToFile(str: String, file: File): Boolean { @WorkerThread
return try { fun writeToFile(str: String, file: File): Try<Unit> {
return Try {
val sink = Okio.sink(file) val sink = Okio.sink(file)


val bufferedSink = Okio.buffer(sink) val bufferedSink = Okio.buffer(sink)
@ -36,14 +38,25 @@ fun saveStringToFile(str: String, file: File): Boolean {


bufferedSink.close() bufferedSink.close()
sink.close() sink.close()

true
} catch (e: Exception) {
Timber.e(e, "Error saving file")
false
} }
} }


/**
* Save a byte array to a file with Okio
*/
@WorkerThread
fun writeToFile(data: ByteArray, file: File): Try<Unit> {
return Try {
val sink = Okio.sink(file)

val bufferedSink = Okio.buffer(sink)

bufferedSink.write(data)

bufferedSink.close()
sink.close()
}
}


fun addEntryToDownloadManager(context: Context, fun addEntryToDownloadManager(context: Context,
file: File, file: File,

View File

@ -18,17 +18,16 @@ package im.vector.riotredesign.features.crypto.keys


import android.content.Context import android.content.Context
import android.os.Environment import android.os.Environment
import androidx.annotation.WorkerThread
import arrow.core.Try import arrow.core.Try
import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.MatrixCallback
import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.Session
import im.vector.matrix.android.internal.extensions.foldToCallback import im.vector.matrix.android.internal.extensions.foldToCallback
import im.vector.riotredesign.core.files.addEntryToDownloadManager import im.vector.riotredesign.core.files.addEntryToDownloadManager
import im.vector.riotredesign.core.files.writeToFile
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import okio.Okio
import java.io.File import java.io.File


class KeysExporter(private val session: Session) { class KeysExporter(private val session: Session) {
@ -42,7 +41,16 @@ class KeysExporter(private val session: Session) {
override fun onSuccess(data: ByteArray) { override fun onSuccess(data: ByteArray) {
GlobalScope.launch(Dispatchers.Main) { GlobalScope.launch(Dispatchers.Main) {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
copyToFile(context, data) Try {
val parentDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
val file = File(parentDir, "riotx-keys-" + System.currentTimeMillis() + ".txt")

writeToFile(data, file)

addEntryToDownloadManager(context, file, "text/plain")

file.absolutePath
}
} }
.foldToCallback(callback) .foldToCallback(callback)
} }
@ -53,25 +61,4 @@ class KeysExporter(private val session: Session) {
} }
}) })
} }

@WorkerThread
private fun copyToFile(context: Context, data: ByteArray): Try<String> {
return Try {
val parentDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
val file = File(parentDir, "riotx-keys-" + System.currentTimeMillis() + ".txt")

val sink = Okio.sink(file)

val bufferedSink = Okio.buffer(sink)

bufferedSink.write(data)

bufferedSink.close()
sink.close()

addEntryToDownloadManager(context, file, "text/plain")

file.absolutePath
}
}
} }

View File

@ -25,15 +25,20 @@ import androidx.appcompat.app.AlertDialog
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders import androidx.lifecycle.ViewModelProviders
import arrow.core.Try
import butterknife.BindView import butterknife.BindView
import butterknife.OnClick import butterknife.OnClick
import com.google.android.material.bottomsheet.BottomSheetDialog import com.google.android.material.bottomsheet.BottomSheetDialog
import im.vector.fragments.keysbackup.setup.KeysBackupSetupSharedViewModel import im.vector.fragments.keysbackup.setup.KeysBackupSetupSharedViewModel
import im.vector.riotredesign.R import im.vector.riotredesign.R
import im.vector.riotredesign.core.files.addEntryToDownloadManager import im.vector.riotredesign.core.files.addEntryToDownloadManager
import im.vector.riotredesign.core.files.saveStringToFile import im.vector.riotredesign.core.files.writeToFile
import im.vector.riotredesign.core.platform.VectorBaseFragment import im.vector.riotredesign.core.platform.VectorBaseFragment
import im.vector.riotredesign.core.utils.* import im.vector.riotredesign.core.utils.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File import java.io.File


class KeysBackupSetupStep3Fragment : VectorBaseFragment() { class KeysBackupSetupStep3Fragment : VectorBaseFragment() {
@ -157,30 +162,39 @@ class KeysBackupSetupStep3Fragment : VectorBaseFragment() {
} }


private fun exportRecoveryKeyToFile(data: String) { private fun exportRecoveryKeyToFile(data: String) {
val parentDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) GlobalScope.launch(Dispatchers.Main) {
val file = File(parentDir, "recovery-key-" + System.currentTimeMillis() + ".txt") withContext(Dispatchers.IO) {
Try {
val parentDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
val file = File(parentDir, "recovery-key-" + System.currentTimeMillis() + ".txt")


if (saveStringToFile(data, file)) { writeToFile(data, file)
addEntryToDownloadManager(requireContext(), file, "text/plain")


context?.let { addEntryToDownloadManager(requireContext(), file, "text/plain")
AlertDialog.Builder(it)
.setMessage(getString(R.string.recovery_key_export_saved_as_warning, file.absolutePath)) file.absolutePath
.setCancelable(false) }
.setPositiveButton(R.string.ok, null)
.show()
} }
.fold(
{ throwable ->
context?.let {
AlertDialog.Builder(it)
.setTitle(R.string.dialog_title_error)
.setMessage(throwable.localizedMessage)
}
},
{ path ->
viewModel.copyHasBeenMade = true


viewModel.copyHasBeenMade = true context?.let {
} else { AlertDialog.Builder(it)
context?.let { .setMessage(getString(R.string.recovery_key_export_saved_as_warning, path))
AlertDialog.Builder(it) }
.setTitle(R.string.dialog_title_error) }
.setMessage(getString(R.string.unknown_error)) )
.setCancelable(false) ?.setCancelable(false)
.setPositiveButton(R.string.ok, null) ?.setPositiveButton(R.string.ok, null)
.show() ?.show()
}
} }
} }