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 "androidx.fragment:fragment-ktx:1.5.5"
|
||||||
|
|
||||||
|
implementation 'com.opencsv:opencsv:4.6'
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
package com.example.refactoredapp.data
|
package com.example.refactoredapp.data
|
||||||
|
|
||||||
|
import android.os.Environment
|
||||||
|
import com.example.refactoredapp.R
|
||||||
|
|
||||||
object DataHolder {
|
object DataHolder {
|
||||||
var isStoragePermissionGranted = false
|
var isStoragePermissionGranted = false
|
||||||
var isAppFolderCreated = false
|
var isAppFolderCreated = false
|
||||||
|
var appFolderPath = ""
|
||||||
// var isReferenceTaken = false
|
// 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.R
|
||||||
import com.example.refactoredapp.data.DataHolder
|
import com.example.refactoredapp.data.DataHolder
|
||||||
import com.example.refactoredapp.databinding.ActivitySplashBinding
|
import com.example.refactoredapp.databinding.ActivitySplashBinding
|
||||||
|
import com.example.refactoredapp.util.MyUtils
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -109,18 +110,10 @@ class SplashActivity : AppCompatActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun createAppFolder() {
|
private fun createAppFolder() {
|
||||||
val file = File(
|
val folderPath = MyUtils().createAppFolder(applicationContext)
|
||||||
Environment.getExternalStorageDirectory().absolutePath + "/" + resources.getString(R.string.app_name) + "/"
|
if (folderPath != null){
|
||||||
)
|
|
||||||
if (!file.exists()) {
|
|
||||||
if (!file.mkdirs()) {
|
|
||||||
Toast.makeText(this, "Problem creating folder", Toast.LENGTH_SHORT).show()
|
|
||||||
DataHolder.isAppFolderCreated = false
|
|
||||||
} else {
|
|
||||||
DataHolder.isAppFolderCreated = true
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
DataHolder.isAppFolderCreated = true
|
DataHolder.isAppFolderCreated = true
|
||||||
|
DataHolder.appFolderPath = folderPath
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,19 +1,26 @@
|
|||||||
package com.example.refactoredapp.presentation.testRight
|
package com.example.refactoredapp.presentation.testRight
|
||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.fragment.app.Fragment
|
import android.util.Log
|
||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.fragment.app.activityViewModels
|
import androidx.fragment.app.activityViewModels
|
||||||
import com.example.refactoredapp.R
|
import com.example.refactoredapp.R
|
||||||
|
import com.example.refactoredapp.data.DataHolder
|
||||||
import com.example.refactoredapp.databinding.FragmentTestRightResultsBinding
|
import com.example.refactoredapp.databinding.FragmentTestRightResultsBinding
|
||||||
|
import com.example.refactoredapp.util.MyUtils
|
||||||
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
class TestRightResults : Fragment() {
|
class TestRightResults : Fragment() {
|
||||||
|
|
||||||
private lateinit var binding: FragmentTestRightResultsBinding
|
private lateinit var binding: FragmentTestRightResultsBinding
|
||||||
private val viewModel: TestRightViewModel by activityViewModels()
|
private val viewModel: TestRightViewModel by activityViewModels()
|
||||||
|
|
||||||
|
private val TAG = "TestRightResults"
|
||||||
|
|
||||||
override fun onCreateView(
|
override fun onCreateView(
|
||||||
inflater: LayoutInflater, container: ViewGroup?,
|
inflater: LayoutInflater, container: ViewGroup?,
|
||||||
savedInstanceState: Bundle?
|
savedInstanceState: Bundle?
|
||||||
@@ -26,16 +33,53 @@ class TestRightResults : Fragment() {
|
|||||||
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
super.onViewCreated(view, savedInstanceState)
|
super.onViewCreated(view, savedInstanceState)
|
||||||
updateResults()
|
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() {
|
private fun updateResults() {
|
||||||
binding.tvName.text = getString(R.string.name_in_textview, viewModel.patientDetails.name)
|
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.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"){
|
if (viewModel.patientDetails.results == "Negative"){
|
||||||
// Setting color
|
// 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
|
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.R
|
||||||
|
import com.example.refactoredapp.data.DataHolder
|
||||||
import com.example.refactoredapp.data.model.DeviceType
|
import com.example.refactoredapp.data.model.DeviceType
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
class MyUtils {
|
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