Skip to content
This repository has been archived by the owner on Nov 5, 2024. It is now read-only.

Commit

Permalink
Fix CSV imports
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Apr 29, 2022
1 parent d21063b commit 611cc5e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
18 changes: 8 additions & 10 deletions app/src/main/java/com/ivy/wallet/ui/csvimport/ImportViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
}
}
16 changes: 15 additions & 1 deletion app/src/main/java/com/ivy/wallet/utils/FileUtil.kt
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -81,4 +83,16 @@ private fun readFileContent(
}
return sb.toString()
}
}
}

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()

0 comments on commit 611cc5e

Please sign in to comment.