Used data layer to save csv to disk.
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
package com.example.hpos.data
|
||||
|
||||
import com.example.hpos.data.datasource.LocalFileDataSource
|
||||
|
||||
class LocalFileRepository(private val localFileDataSource: LocalFileDataSource) {
|
||||
fun saveCsvToDisk(filepath: String, contents: ArrayList<Array<String>>) =
|
||||
localFileDataSource.saveCsvToDisk(filepath, contents)
|
||||
|
||||
fun saveTextToDisk(filepath: String, contents: String) =
|
||||
localFileDataSource.saveTextToDisk(filepath, contents)
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.example.hpos.data.datasource
|
||||
|
||||
import com.opencsv.CSVWriter
|
||||
import java.io.File
|
||||
import java.io.FileWriter
|
||||
|
||||
class LocalFileDataSource {
|
||||
|
||||
fun saveCsvToDisk(filepath: String, contents: ArrayList<Array<String>>){
|
||||
val writer = CSVWriter(FileWriter(filepath))
|
||||
writer.writeAll(contents) // data is adding to csv
|
||||
writer.close()
|
||||
}
|
||||
|
||||
fun saveTextToDisk(filepath: String, contents: String){
|
||||
val writer = FileWriter(File(filepath))
|
||||
|
||||
writer.append(contents)
|
||||
writer.flush()
|
||||
writer.close()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.example.hpos.domain
|
||||
|
||||
import android.util.Log
|
||||
import com.example.hpos.data.LocalFileRepository
|
||||
import com.example.hpos.data.constant.Constants
|
||||
import com.example.hpos.data.model.PatientData
|
||||
import com.example.hpos.data.model.TestRightCalculationData
|
||||
@@ -9,37 +10,38 @@ import java.io.File
|
||||
import java.io.FileWriter
|
||||
import java.util.Collections.sort
|
||||
|
||||
class SaveRawData {
|
||||
class SaveRawData(private val localFileRepository: LocalFileRepository) {
|
||||
|
||||
private val TAG = "saverawdata"
|
||||
|
||||
fun saveCsv(folderPath: String, fileName: String, matrix: ArrayList<ArrayList<Double>>) {
|
||||
// try {
|
||||
val fullPath = "$folderPath/$fileName"
|
||||
val writer = CSVWriter(FileWriter(fullPath))
|
||||
val fullPath = "$folderPath/$fileName"
|
||||
// val writer = CSVWriter(FileWriter(fullPath))
|
||||
|
||||
sort(matrix) { one: ArrayList<Double>, two: ArrayList<Double> ->
|
||||
one[0].compareTo(two[0])
|
||||
}
|
||||
sort(matrix) { one: ArrayList<Double>, two: ArrayList<Double> ->
|
||||
one[0].compareTo(two[0])
|
||||
}
|
||||
|
||||
val content = ArrayList<Array<String>>()
|
||||
content.add(arrayOf("NM", "CA"))
|
||||
val content = ArrayList<Array<String>>()
|
||||
content.add(arrayOf("NM", "CA"))
|
||||
|
||||
for (eachRow in matrix) {
|
||||
for (eachRow in matrix) {
|
||||
|
||||
if (eachRow[0] >= Constants.MIN_WAVELENGTH_RANGE_TO_RECORD && eachRow[0] < Constants.MAX_WAVELENGTH_RANGE_TO_RECORD + 1) {
|
||||
if (eachRow[0] >= Constants.MIN_WAVELENGTH_RANGE_TO_RECORD && eachRow[0] < Constants.MAX_WAVELENGTH_RANGE_TO_RECORD + 1) {
|
||||
// val rowContent = arrayOf(String.format("%.3f", eachRow[0]), String.format("%.3f", eachRow[1]))
|
||||
val rowContent =
|
||||
arrayOf(
|
||||
String.format("%.10f", eachRow[0]),
|
||||
String.format("%.10f", eachRow[1])
|
||||
)
|
||||
content.add(rowContent)
|
||||
}
|
||||
val rowContent =
|
||||
arrayOf(
|
||||
String.format("%.10f", eachRow[0]),
|
||||
String.format("%.10f", eachRow[1])
|
||||
)
|
||||
content.add(rowContent)
|
||||
}
|
||||
}
|
||||
|
||||
writer.writeAll(content) // data is adding to csv
|
||||
writer.close()
|
||||
localFileRepository.saveCsvToDisk(fullPath, content)
|
||||
// writer.writeAll(content) // data is adding to csv
|
||||
// writer.close()
|
||||
|
||||
// } catch (e: Exception) {
|
||||
// Log.e(TAG, e.toString())
|
||||
@@ -47,15 +49,18 @@ class SaveRawData {
|
||||
}
|
||||
|
||||
fun saveLog(folderPath: String, fileName: String, calculationData: TestRightCalculationData) {
|
||||
val fileObj = File(folderPath, fileName)
|
||||
val writer = FileWriter(fileObj)
|
||||
// val fileObj = File(folderPath, fileName)
|
||||
// val writer = FileWriter(fileObj)
|
||||
|
||||
writer.append(getLogStringFromObj(calculationData))
|
||||
writer.flush()
|
||||
writer.close()
|
||||
// writer.append(getLogStringFromObj(calculationData))
|
||||
// writer.flush()
|
||||
// writer.close()
|
||||
|
||||
val fullPath = folderPath + fileName
|
||||
localFileRepository.saveTextToDisk(fullPath, getLogStringFromObj(calculationData))
|
||||
}
|
||||
|
||||
fun getLogStringFromObj(calculationData: TestRightCalculationData): String {
|
||||
private fun getLogStringFromObj(calculationData: TestRightCalculationData): String {
|
||||
var outputString = "Test calculation logs ==>\n"
|
||||
outputString += "Absorbance one = ${
|
||||
String.format(
|
||||
@@ -98,12 +103,18 @@ class SaveRawData {
|
||||
calculationData: TestRightCalculationData,
|
||||
patientData: PatientData
|
||||
) {
|
||||
val fileObj = File(folderPath, fileName)
|
||||
val writer = FileWriter(fileObj)
|
||||
// val fileObj = File(folderPath, fileName)
|
||||
// val writer = FileWriter(fileObj)
|
||||
|
||||
writer.append(getLogStringFromObjWithPatientData(calculationData, patientData))
|
||||
writer.flush()
|
||||
writer.close()
|
||||
// writer.append(getLogStringFromObjWithPatientData(calculationData, patientData))
|
||||
// writer.flush()
|
||||
// writer.close()
|
||||
|
||||
val fullPath = folderPath + fileName
|
||||
localFileRepository.saveTextToDisk(
|
||||
fullPath,
|
||||
getLogStringFromObjWithPatientData(calculationData, patientData)
|
||||
)
|
||||
}
|
||||
|
||||
private fun getLogStringFromObjWithPatientData(
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package com.example.hpos.domain
|
||||
|
||||
import android.util.Log
|
||||
import com.example.hpos.data.LocalFileRepository
|
||||
import com.example.hpos.data.model.CalculationVariableForTest
|
||||
import com.opencsv.CSVWriter
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.FileWriter
|
||||
|
||||
class SaveRawDataTest {
|
||||
class SaveRawDataTest(private val localFileRepository: LocalFileRepository) {
|
||||
|
||||
private val TAG = "SaveRawDataTest"
|
||||
|
||||
@@ -15,7 +16,7 @@ class SaveRawDataTest {
|
||||
|
||||
// try {
|
||||
val fullPath = "$folderPath/$fileName"
|
||||
val writer = CSVWriter(FileWriter(fullPath))
|
||||
// val writer = CSVWriter(FileWriter(fullPath))
|
||||
val content = ArrayList<Array<String>>()
|
||||
|
||||
// Header
|
||||
@@ -35,27 +36,40 @@ class SaveRawDataTest {
|
||||
content.add(rowContent)
|
||||
}
|
||||
|
||||
writer.writeAll(content) // data is adding to csv
|
||||
writer.close()
|
||||
localFileRepository.saveCsvToDisk(fullPath, content)
|
||||
// writer.writeAll(content) // data is adding to csv
|
||||
// writer.close()
|
||||
// } catch (e: Exception){
|
||||
// Log.e(TAG, e.toString())
|
||||
// }
|
||||
}
|
||||
|
||||
fun saveLog(folderPath: String, fileName: String, isReference: Boolean, fullString: String) {
|
||||
val fileObj = File(folderPath, fileName)
|
||||
// val fileObj = File(folderPath, fileName)
|
||||
|
||||
// Log.d(TAG, "$folderPath --- $fileName")
|
||||
val writer = FileWriter(fileObj)
|
||||
// val writer = FileWriter(fileObj)
|
||||
|
||||
if (isReference)
|
||||
writer.append("\nREFERENCE OUTPUT\n")
|
||||
// if (isReference)
|
||||
// writer.append("\nREFERENCE OUTPUT\n")
|
||||
// else
|
||||
// writer.append("\nSAMPLE OUTPUT\n")
|
||||
//
|
||||
// writer.append(fullString)
|
||||
// writer.flush()
|
||||
// writer.close()
|
||||
|
||||
val filePath = folderPath + fileName
|
||||
var stringToWrite = ""
|
||||
stringToWrite += if (isReference)
|
||||
"\nREFERENCE OUTPUT\n"
|
||||
else
|
||||
writer.append("\nSAMPLE OUTPUT\n")
|
||||
"\nSAMPLE OUTPUT\n"
|
||||
|
||||
stringToWrite += fullString
|
||||
|
||||
localFileRepository.saveTextToDisk(filePath, stringToWrite)
|
||||
|
||||
writer.append(fullString)
|
||||
writer.flush()
|
||||
writer.close()
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.util.Log
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import com.example.hpos.data.DataHolder
|
||||
import com.example.hpos.data.LocalFileRepository
|
||||
import com.example.hpos.data.constant.Constants
|
||||
import com.example.hpos.data.model.*
|
||||
import com.example.hpos.domain.*
|
||||
@@ -16,7 +17,7 @@ import kotlin.collections.ArrayList
|
||||
import kotlin.math.log10
|
||||
import kotlin.math.pow
|
||||
|
||||
class TestRightViewModel : ViewModel() {
|
||||
class TestRightViewModel(private val saveRawData: SaveRawData, private val saveRawDataTest: SaveRawDataTest) : ViewModel() {
|
||||
|
||||
private val TAG = "TestRightViewModel"
|
||||
|
||||
@@ -257,13 +258,15 @@ class TestRightViewModel : ViewModel() {
|
||||
// try {
|
||||
var folderPath: String? = DataHolder.appFolderPath
|
||||
if (DataHolder.isAppFolderCreated) {
|
||||
SaveRawData().saveCsv(folderPath!!, filename, wavelengthToAbsorbance)
|
||||
// SaveRawData().saveCsv(folderPath!!, filename, wavelengthToAbsorbance)
|
||||
saveRawData.saveCsv(folderPath!!, filename, wavelengthToAbsorbance)
|
||||
} else {
|
||||
folderPath = MyUtils.createAppFolder(appContext)
|
||||
if (folderPath != null) {
|
||||
DataHolder.isAppFolderCreated = true
|
||||
DataHolder.appFolderPath = folderPath
|
||||
SaveRawData().saveCsv(folderPath, filename, wavelengthToAbsorbance)
|
||||
// SaveRawData().saveCsv(folderPath, filename, wavelengthToAbsorbance)
|
||||
saveRawData.saveCsv(folderPath, filename, wavelengthToAbsorbance)
|
||||
} else {
|
||||
// errorTriggered.postValue("Unable to save CSV, Please try again")
|
||||
}
|
||||
@@ -282,13 +285,15 @@ class TestRightViewModel : ViewModel() {
|
||||
// try {
|
||||
var folderPath: String? = DataHolder.appFolderPath
|
||||
if (DataHolder.isAppFolderCreated) {
|
||||
SaveRawDataTest().saveCsv(folderPath!!, filename, calculationVariableList)
|
||||
// SaveRawDataTest().saveCsv(folderPath!!, filename, calculationVariableList)
|
||||
saveRawDataTest.saveCsv(folderPath!!, filename, calculationVariableList)
|
||||
} else {
|
||||
folderPath = MyUtils.createAppFolder(appContext)
|
||||
if (folderPath != null) {
|
||||
DataHolder.isAppFolderCreated = true
|
||||
DataHolder.appFolderPath = folderPath
|
||||
SaveRawDataTest().saveCsv(folderPath, filename, calculationVariableList)
|
||||
// SaveRawDataTest().saveCsv(folderPath, filename, calculationVariableList)
|
||||
saveRawDataTest.saveCsv(folderPath, filename, calculationVariableList)
|
||||
} else {
|
||||
// errorTriggered.postValue("Unable to save CSV, Please try again")
|
||||
}
|
||||
@@ -309,13 +314,15 @@ class TestRightViewModel : ViewModel() {
|
||||
var folderPath: String? = DataHolder.appFolderPath
|
||||
|
||||
if (DataHolder.isAppFolderCreated) {
|
||||
SaveRawData().saveLog(folderPath!!, fileName, calculationData)
|
||||
// SaveRawData().saveLog(folderPath!!, fileName, calculationData)
|
||||
saveRawData.saveLog(folderPath!!, fileName, calculationData)
|
||||
} else {
|
||||
folderPath = MyUtils.createAppFolder(appContext)
|
||||
if (folderPath != null) {
|
||||
DataHolder.isAppFolderCreated = true
|
||||
DataHolder.appFolderPath = folderPath
|
||||
SaveRawData().saveLog(folderPath, fileName, calculationData)
|
||||
// SaveRawData().saveLog(folderPath, fileName, calculationData)
|
||||
saveRawData.saveLog(folderPath, fileName, calculationData)
|
||||
} else {
|
||||
// errorTriggered.postValue("Unable to save Log, Please try again")
|
||||
}
|
||||
@@ -335,7 +342,8 @@ class TestRightViewModel : ViewModel() {
|
||||
var folderPath: String? = DataHolder.appFolderPath
|
||||
|
||||
if (DataHolder.isAppFolderCreated) {
|
||||
SaveRawData().saveLogWithPatientData(
|
||||
// SaveRawData().saveLogWithPatientData(
|
||||
saveRawData.saveLogWithPatientData(
|
||||
folderPath!!,
|
||||
fileName,
|
||||
calculationData,
|
||||
@@ -346,7 +354,8 @@ class TestRightViewModel : ViewModel() {
|
||||
if (folderPath != null) {
|
||||
DataHolder.isAppFolderCreated = true
|
||||
DataHolder.appFolderPath = folderPath
|
||||
SaveRawData().saveLogWithPatientData(
|
||||
// SaveRawData().saveLogWithPatientData(
|
||||
saveRawData.saveLogWithPatientData(
|
||||
folderPath,
|
||||
fileName,
|
||||
calculationData,
|
||||
@@ -377,13 +386,15 @@ class TestRightViewModel : ViewModel() {
|
||||
|
||||
var folderPath: String? = DataHolder.appFolderPath
|
||||
if (DataHolder.isAppFolderCreated) {
|
||||
SaveRawDataTest().saveLog(folderPath!!, fileName, isReference, fullString)
|
||||
saveRawDataTest.saveLog(folderPath!!, fileName, isReference, fullString)
|
||||
// SaveRawDataTest().saveLog(folderPath!!, fileName, isReference, fullString)
|
||||
} else {
|
||||
folderPath = MyUtils.createAppFolder(appContext)
|
||||
if (folderPath != null) {
|
||||
DataHolder.isAppFolderCreated = true
|
||||
DataHolder.appFolderPath = folderPath
|
||||
SaveRawDataTest().saveLog(folderPath, fileName, isReference, fullString)
|
||||
saveRawDataTest.saveLog(folderPath, fileName, isReference, fullString)
|
||||
// SaveRawDataTest().saveLog(folderPath, fileName, isReference, fullString)
|
||||
} else {
|
||||
// errorTriggered.postValue("Unable to save Log, Please try again")
|
||||
}
|
||||
|
||||
@@ -3,6 +3,10 @@ package com.example.hpos.util
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import com.example.hpos.data.LocalFileRepository
|
||||
import com.example.hpos.data.datasource.LocalFileDataSource
|
||||
import com.example.hpos.domain.SaveRawData
|
||||
import com.example.hpos.domain.SaveRawDataTest
|
||||
import com.example.hpos.presentation.MainViewModel
|
||||
import com.example.hpos.presentation.testRight.TestRightViewModel
|
||||
|
||||
@@ -11,8 +15,10 @@ class MyViewModelFactory(private val context: Context) : ViewModelProvider.Facto
|
||||
override fun <T : ViewModel> create(modelClass: Class<T>): T {
|
||||
if (modelClass.isAssignableFrom(MainViewModel::class.java))
|
||||
return MainViewModel() as T
|
||||
else if (modelClass.isAssignableFrom(TestRightViewModel::class.java))
|
||||
return TestRightViewModel() as T
|
||||
else if (modelClass.isAssignableFrom(TestRightViewModel::class.java)) {
|
||||
val repository = LocalFileRepository(LocalFileDataSource())
|
||||
return TestRightViewModel(SaveRawData(repository), SaveRawDataTest(repository)) as T
|
||||
}
|
||||
else
|
||||
throw IllegalArgumentException("Unknown ViewModel class");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user