Adds import object to a CSVDescriptor

csv
Laurent 6 years ago
parent b85059ba2e
commit fa61d76b71
  1. 27
      app/src/main/java/net/pokeranalytics/android/ui/activity/ImportActivity.kt
  2. 8
      app/src/main/java/net/pokeranalytics/android/ui/fragment/ImportFragment.kt
  3. 3
      app/src/main/java/net/pokeranalytics/android/util/csv/CSVDescriptor.kt
  4. 17
      app/src/main/java/net/pokeranalytics/android/util/csv/CSVImporter.kt

@ -61,7 +61,7 @@ class ImportActivity : PokerAnalyticsActivity() {
val fis = contentResolver.openInputStream(fileURI)
Timber.d("Load fragment data with: $fis")
fis?.let {
fragment.setData(it)
fragment.setData(it, fileURI)
}
fragmentTransaction.add(R.id.container, fragment)
@ -69,31 +69,6 @@ class ImportActivity : PokerAnalyticsActivity() {
}
// override fun onNewIntent(intent: Intent?) {
// super.onNewIntent(intent)
//
// Timber.d("++++++ data = ${intent?.data}")
//
// setIntent(intent)
// intent?.let {
//
// when (intent.action) {
// "android.intent.action.VIEW" -> { // import
// val data = it.data
// if (data != null) {
// this.requestImportConfirmation(data)
// } else {
// throw PAIllegalStateException("URI null on import")
// }
// }
// else -> {
// Timber.w("Intent ${intent.action} unmanaged")
// }
// }
// }
//
// }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

@ -1,5 +1,6 @@
package net.pokeranalytics.android.ui.fragment
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
@ -26,7 +27,7 @@ class ImportFragment : RealmFragment(), ImportDelegate {
val coroutineContext: CoroutineContext
get() = Dispatchers.Main
private lateinit var filePath: String
private var filePath: String? = null
private lateinit var inputStream: InputStream
private lateinit var importer: CSVImporter
@ -34,8 +35,9 @@ class ImportFragment : RealmFragment(), ImportDelegate {
this.filePath = path
}
fun setData(inputStream: InputStream) {
fun setData(inputStream: InputStream, uri: Uri) {
this.inputStream = inputStream
this.filePath = uri.path
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@ -72,7 +74,7 @@ class ImportFragment : RealmFragment(), ImportDelegate {
var errorMessage: String? = null
this.importer = CSVImporter(inputStream)
this.importer = CSVImporter(this.inputStream, this.filePath)
this.importer.delegate = this
GlobalScope.launch(coroutineContext) {

@ -5,6 +5,7 @@ import io.realm.kotlin.deleteFromRealm
import net.pokeranalytics.android.model.interfaces.Identifiable
import net.pokeranalytics.android.model.interfaces.ObjectIdentifier
import net.pokeranalytics.android.model.realm.CustomField
import net.pokeranalytics.android.model.realm.Import
import net.pokeranalytics.android.model.realm.Session
import net.pokeranalytics.android.util.extensions.findById
import org.apache.commons.csv.CSVRecord
@ -93,6 +94,8 @@ abstract class CSVDescriptor(var source: DataSource, vararg elements: CSVField)
*/
protected var fieldMapping: MutableMap<CSVField, Int> = mutableMapOf()
lateinit var import: Import
init {
if (elements.isNotEmpty()) {
this.fields = elements.toMutableList()

@ -3,6 +3,7 @@ package net.pokeranalytics.android.util.csv
import android.os.Handler
import android.os.Looper
import io.realm.Realm
import net.pokeranalytics.android.model.realm.Import
import org.apache.commons.csv.CSVFormat
import org.apache.commons.csv.CSVParser
import org.apache.commons.csv.CSVRecord
@ -25,7 +26,7 @@ interface ImportDelegate {
* When finding a descriptor, the CSVImporter then continue to parse the file and delegates the parsing of each row
* to the CSVDescriptor
*/
open class CSVImporter(istream: InputStream) {
open class CSVImporter(istream: InputStream, filePath: String? = null) {
/**
* The object being notified of the import progress
@ -50,7 +51,7 @@ open class CSVImporter(istream: InputStream) {
/**
* The path of the CSV file
*/
private var path: String? = null
private var path: String? = filePath
/**
* The InputStream containing a file content
*/
@ -107,13 +108,18 @@ open class CSVImporter(istream: InputStream) {
realm.beginTransaction()
val import = realm.copyToRealm(Import())
this.path?.let {
import.fileName = it
}
parser.forEachIndexed { index, record ->
// Timber.d("line $index")
this.notifyDelegate()
if (this.currentDescriptor == null) { // find descriptor
this.currentDescriptor = this.findDescriptor(record, realm)
this.currentDescriptor = this.findDescriptor(realm, record, import)
if (this.currentDescriptor == null) {
@ -176,13 +182,12 @@ open class CSVImporter(istream: InputStream) {
/**
* Search for a descriptor in the list of managed formats
*/
private fun findDescriptor(record: CSVRecord, realm: Realm): CSVDescriptor? {
private fun findDescriptor(realm: Realm, record: CSVRecord, import: Import): CSVDescriptor? {
val allCSVDescriptors = ProductCSVDescriptors.all
Timber.d(allCSVDescriptors.toString())
allCSVDescriptors.forEach { descriptor ->
Timber.d("descriptor = $descriptor, record = $record")
if (descriptor.matches(record)) {
descriptor.import = import
descriptor.mapCustomField(record, realm)
this.currentDescriptor = descriptor
Timber.d("Identified source: ${descriptor.source}")

Loading…
Cancel
Save