forked from Spikeysanju/Expenso
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
328 additions
and
36 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
app/src/main/java/dev/spikeysanju/expensetracker/services/exportcsv/Exportable.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package dev.spikeysanju.expensetracker.services.exportcsv | ||
|
||
import android.app.Activity | ||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Build | ||
import android.os.Environment | ||
import android.provider.DocumentsContract | ||
import android.provider.DocumentsProvider | ||
import android.provider.MediaStore | ||
import android.util.Log | ||
import androidx.activity.result.contract.ActivityResultContract | ||
import androidx.annotation.WorkerThread | ||
import androidx.core.content.FileProvider | ||
import androidx.core.net.toFile | ||
import androidx.core.net.toUri | ||
import com.opencsv.CSVWriter | ||
import com.opencsv.bean.CsvBindByName | ||
import com.opencsv.bean.StatefulBeanToCsvBuilder | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import dev.spikeysanju.expensetracker.model.Transaction | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.flow | ||
import kotlinx.coroutines.withContext | ||
import java.io.File | ||
import java.io.FileWriter | ||
import java.io.IOException | ||
import java.net.URI | ||
import javax.inject.Inject | ||
|
||
class CreateCsvContract : ActivityResultContract<String, Uri?>() { | ||
|
||
override fun createIntent(context: Context, input: String): Intent { | ||
return Intent(Intent.ACTION_CREATE_DOCUMENT).apply { | ||
addCategory(Intent.CATEGORY_OPENABLE) | ||
type = "application/csv" | ||
putExtra(Intent.EXTRA_TITLE, "$input.csv") | ||
} | ||
} | ||
|
||
override fun parseResult(resultCode: Int, intent: Intent?): Uri? { | ||
if (resultCode != Activity.RESULT_OK) { | ||
return null | ||
} | ||
return intent?.data | ||
} | ||
|
||
} | ||
|
||
data class TransactionsCSV( | ||
@CsvBindByName(column = "title") | ||
val title: String, | ||
@CsvBindByName(column = "amount") | ||
val amount: Double, | ||
@CsvBindByName(column = "transactionType") | ||
val transactionType: String, | ||
@CsvBindByName(column = "tag") | ||
val tag: String, | ||
@CsvBindByName(column = "date") | ||
val date: String, | ||
@CsvBindByName(column = "note") | ||
val note: String, | ||
@CsvBindByName(column = "createdAt") | ||
val createdAtDate: String | ||
) | ||
|
||
fun List<Transaction>.toCsv() = map { | ||
TransactionsCSV( | ||
title = it.title, | ||
amount = it.amount, | ||
transactionType = it.transactionType, | ||
tag = it.tag, | ||
date = it.date, | ||
note = it.note, | ||
createdAtDate = it.createdAtDateFormat, | ||
) | ||
} | ||
|
||
class ExportService( | ||
private val appContext: Context | ||
) { | ||
|
||
@WorkerThread | ||
fun <T> writeToCSV(csvFileUri: Uri, content: List<T>) = flow<String> { | ||
val fileDescriptor = appContext.contentResolver.openFileDescriptor(csvFileUri, "w") | ||
if (fileDescriptor != null) { | ||
fileDescriptor.use { | ||
val csvWriter = CSVWriter(FileWriter(it.fileDescriptor)) | ||
StatefulBeanToCsvBuilder<T>(csvWriter) | ||
.withSeparator(CSVWriter.DEFAULT_SEPARATOR) | ||
.build() | ||
.write(content) | ||
csvWriter.close() | ||
emit(csvFileUri.toString()) | ||
} | ||
} else { | ||
throw IllegalStateException("failed to read fileDescriptor") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
app/src/main/java/dev/spikeysanju/expensetracker/utils/viewState/ExportState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package dev.spikeysanju.expensetracker.utils.viewState | ||
|
||
sealed class ExportState { | ||
object Loading : ExportState() | ||
object Empty : ExportState() | ||
data class Success(val fileUri: String) : ExportState() | ||
data class Error(val exception: Throwable) : ExportState() | ||
} |
Oops, something went wrong.