From 611cc5e2026282f1071dfd720bb3d19ad11753c8 Mon Sep 17 00:00:00 2001 From: Iliyan Germanov Date: Fri, 29 Apr 2022 16:32:41 +0300 Subject: [PATCH] Fix CSV imports --- .../ivy/wallet/ui/csvimport/ImportViewModel.kt | 18 ++++++++---------- .../main/java/com/ivy/wallet/utils/FileUtil.kt | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/ivy/wallet/ui/csvimport/ImportViewModel.kt b/app/src/main/java/com/ivy/wallet/ui/csvimport/ImportViewModel.kt index 5cec1d883a..4d278ea971 100644 --- a/app/src/main/java/com/ivy/wallet/ui/csvimport/ImportViewModel.kt +++ b/app/src/main/java/com/ivy/wallet/ui/csvimport/ImportViewModel.kt @@ -16,10 +16,7 @@ import com.ivy.wallet.domain.deprecated.logic.zip.ExportZipLogic import com.ivy.wallet.ui.Import import com.ivy.wallet.ui.IvyWalletCtx import com.ivy.wallet.ui.onboarding.viewmodel.OnboardingViewModel -import com.ivy.wallet.utils.TestIdlingResource -import com.ivy.wallet.utils.asLiveData -import com.ivy.wallet.utils.ioThread -import com.ivy.wallet.utils.uiThread +import com.ivy.wallet.utils.* import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.launch import timber.log.Timber @@ -79,7 +76,7 @@ class ImportViewModel @Inject constructor( _importStep.value = ImportStep.LOADING - _importResult.value = if (hasCSVExtension(fileUri)) + _importResult.value = if (hasCSVExtension(context, fileUri)) restoreCSVFile(fileUri = fileUri, importType = importType) else { exportZipLogic.import( @@ -196,10 +193,11 @@ class ImportViewModel @Inject constructor( _importStep.value = ImportStep.IMPORT_FROM } - private fun hasCSVExtension(fileUri: Uri): Boolean { - var ex = fileUri.toString() - ex = ex.substring(ex.lastIndexOf(".")) - - return ex.equals(".csv", ignoreCase = true) + private suspend fun hasCSVExtension( + context: Context, + fileUri: Uri + ): Boolean = ioThread { + val fileName = context.getFileName(fileUri) + fileName?.endsWith(suffix = ".csv", ignoreCase = true) ?: false } } \ No newline at end of file diff --git a/app/src/main/java/com/ivy/wallet/utils/FileUtil.kt b/app/src/main/java/com/ivy/wallet/utils/FileUtil.kt index 1cfe4ce093..0243acd8d5 100644 --- a/app/src/main/java/com/ivy/wallet/utils/FileUtil.kt +++ b/app/src/main/java/com/ivy/wallet/utils/FileUtil.kt @@ -1,8 +1,10 @@ package com.ivy.wallet.utils +import android.content.ContentResolver import android.content.Context import android.net.Uri import android.os.Environment +import android.provider.OpenableColumns import java.io.* import java.nio.charset.Charset @@ -81,4 +83,16 @@ private fun readFileContent( } return sb.toString() } -} \ No newline at end of file +} + +fun Context.getFileName(uri: Uri): String? = when (uri.scheme) { + ContentResolver.SCHEME_CONTENT -> getContentFileName(uri) + else -> uri.path?.let(::File)?.name +} + +private fun Context.getContentFileName(uri: Uri): String? = runCatching { + contentResolver.query(uri, null, null, null, null)?.use { cursor -> + cursor.moveToFirst() + return@use cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME).let(cursor::getString) + } +}.getOrNull() \ No newline at end of file