Seperated function of creating folder & implemented methods to save csv. [working]
This commit is contained in:
@@ -53,4 +53,6 @@ dependencies {
|
||||
|
||||
implementation "androidx.fragment:fragment-ktx:1.5.5"
|
||||
|
||||
implementation 'com.opencsv:opencsv:4.6'
|
||||
|
||||
}
|
||||
@@ -1,8 +1,11 @@
|
||||
package com.example.refactoredapp.data
|
||||
|
||||
import android.os.Environment
|
||||
import com.example.refactoredapp.R
|
||||
|
||||
object DataHolder {
|
||||
var isStoragePermissionGranted = false
|
||||
var isAppFolderCreated = false
|
||||
|
||||
var appFolderPath = ""
|
||||
// var isReferenceTaken = false
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.example.refactoredapp.domain
|
||||
|
||||
import android.util.Log
|
||||
import com.opencsv.CSVWriter
|
||||
import java.io.FileWriter
|
||||
|
||||
class SaveRawData {
|
||||
|
||||
fun saveCsv(folderPath: String, fileName: String, matrix: ArrayList<ArrayList<Double>>) {
|
||||
|
||||
val fullPath = "$folderPath/$fileName"
|
||||
val writer = CSVWriter(FileWriter(fullPath))
|
||||
val content = ArrayList<Array<String>>()
|
||||
|
||||
for (eachRow in matrix){
|
||||
val rowContent = arrayOf(eachRow[0].toString(), eachRow[1].toString())
|
||||
content.add(rowContent)
|
||||
}
|
||||
|
||||
// Log.d("TestRight", " --- --- --- --- --- PRINTING ALL THE CONTENTSSSS --- --- --- --- --- ")
|
||||
// for (row in content){
|
||||
// for (col in row){
|
||||
// Log.d("TestRight", col)
|
||||
// }
|
||||
// }
|
||||
|
||||
writer.writeAll(content) // data is adding to csv
|
||||
writer.close()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import androidx.core.content.ContextCompat
|
||||
import com.example.refactoredapp.R
|
||||
import com.example.refactoredapp.data.DataHolder
|
||||
import com.example.refactoredapp.databinding.ActivitySplashBinding
|
||||
import com.example.refactoredapp.util.MyUtils
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
@@ -109,18 +110,10 @@ class SplashActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
private fun createAppFolder() {
|
||||
val file = File(
|
||||
Environment.getExternalStorageDirectory().absolutePath + "/" + resources.getString(R.string.app_name) + "/"
|
||||
)
|
||||
if (!file.exists()) {
|
||||
if (!file.mkdirs()) {
|
||||
Toast.makeText(this, "Problem creating folder", Toast.LENGTH_SHORT).show()
|
||||
DataHolder.isAppFolderCreated = false
|
||||
} else {
|
||||
DataHolder.isAppFolderCreated = true
|
||||
}
|
||||
} else {
|
||||
val folderPath = MyUtils().createAppFolder(applicationContext)
|
||||
if (folderPath != null){
|
||||
DataHolder.isAppFolderCreated = true
|
||||
DataHolder.appFolderPath = folderPath
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,26 @@
|
||||
package com.example.refactoredapp.presentation.testRight
|
||||
|
||||
import android.os.Bundle
|
||||
import androidx.fragment.app.Fragment
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import com.example.refactoredapp.R
|
||||
import com.example.refactoredapp.data.DataHolder
|
||||
import com.example.refactoredapp.databinding.FragmentTestRightResultsBinding
|
||||
import com.example.refactoredapp.util.MyUtils
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.*
|
||||
|
||||
class TestRightResults : Fragment() {
|
||||
|
||||
private lateinit var binding: FragmentTestRightResultsBinding
|
||||
private val viewModel: TestRightViewModel by activityViewModels()
|
||||
|
||||
private val TAG = "TestRightResults"
|
||||
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater, container: ViewGroup?,
|
||||
savedInstanceState: Bundle?
|
||||
@@ -26,16 +33,53 @@ class TestRightResults : Fragment() {
|
||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||
super.onViewCreated(view, savedInstanceState)
|
||||
updateResults()
|
||||
saveCsv()
|
||||
}
|
||||
|
||||
private fun saveCsv() {
|
||||
Log.d(TAG, "saveCsv() called")
|
||||
|
||||
viewModel.calculateDataForCSV()
|
||||
|
||||
// val fileName = "surya.csv"
|
||||
val sdfDate = SimpleDateFormat("ddMMyyyy HHmmss")
|
||||
val fileName: String = sdfDate.format(Date()) + ".csv"
|
||||
|
||||
if (!DataHolder.isAppFolderCreated){
|
||||
val folderPath = MyUtils().createAppFolder(requireContext().applicationContext)
|
||||
if (folderPath != null){
|
||||
DataHolder.isAppFolderCreated = true
|
||||
DataHolder.appFolderPath = folderPath
|
||||
viewModel.saveRawData(DataHolder.appFolderPath, fileName)
|
||||
}
|
||||
} else {
|
||||
viewModel.saveRawData(DataHolder.appFolderPath, fileName)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private fun updateResults() {
|
||||
binding.tvName.text = getString(R.string.name_in_textview, viewModel.patientDetails.name)
|
||||
binding.tvAge.text = getString(R.string.age_in_textview, viewModel.patientDetails.age.toString())
|
||||
binding.tvGender.text = getString(R.string.age_in_textview, viewModel.patientDetails.gender)
|
||||
binding.tvGender.text = getString(R.string.gender_in_textview, viewModel.patientDetails.gender)
|
||||
|
||||
binding.tvResultValue.text = getString(R.string.age_in_textview, viewModel.patientDetails.results)
|
||||
binding.tvResultValue.text = viewModel.patientDetails.results.toString()
|
||||
if (viewModel.patientDetails.results == "Negative"){
|
||||
// Setting color
|
||||
}
|
||||
|
||||
// Log.d(TAG, "\n\n\n\nFor intensity Reference array size = ${viewModel.intensityReferenceArray.size}")
|
||||
// for (each in viewModel.intensityReferenceArray){
|
||||
// Log.d(TAG, "${each[0]} -> ${each[1]}")
|
||||
// }
|
||||
//
|
||||
// Log.d(TAG, "\n\n\n\nFor intensity Reference array size = ${viewModel.intensitySampleArray.size}")
|
||||
// for (each in viewModel.intensitySampleArray){
|
||||
// Log.d(TAG, "${each[0]} -> ${each[1]}")
|
||||
// }
|
||||
// Log.d(TAG,"")
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,12 @@
|
||||
package com.example.refactoredapp.util
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Environment
|
||||
import android.widget.Toast
|
||||
import com.example.refactoredapp.R
|
||||
import com.example.refactoredapp.data.DataHolder
|
||||
import com.example.refactoredapp.data.model.DeviceType
|
||||
import java.io.File
|
||||
|
||||
class MyUtils {
|
||||
|
||||
@@ -13,4 +18,26 @@ class MyUtils {
|
||||
}
|
||||
}
|
||||
|
||||
fun createAppFolder(context: Context) : String? {
|
||||
val folderPath = Environment.getExternalStorageDirectory().absolutePath + "/" + context.resources.getString(R.string.app_name) + "/"
|
||||
// DataHolder.appFolderPath = folderPath
|
||||
val file = File(folderPath)
|
||||
|
||||
return if (!file.exists()) {
|
||||
if (!file.mkdirs()) {
|
||||
// DataHolder.isAppFolderCreated = false
|
||||
// false
|
||||
null
|
||||
} else {
|
||||
// DataHolder.isAppFolderCreated = true
|
||||
// true
|
||||
folderPath
|
||||
}
|
||||
} else {
|
||||
// DataHolder.isAppFolderCreated = true
|
||||
// true
|
||||
folderPath
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user