Uploading files to storage and adding to pending queue.
This commit is contained in:
@@ -11,6 +11,8 @@ object DataHolder {
|
|||||||
|
|
||||||
val usbConnected = MutableLiveData(true)
|
val usbConnected = MutableLiveData(true)
|
||||||
|
|
||||||
|
var mobileUniqueId: String? = null
|
||||||
|
|
||||||
var isStoragePermissionGranted = false
|
var isStoragePermissionGranted = false
|
||||||
var isAppFolderCreated = false
|
var isAppFolderCreated = false
|
||||||
var appFolderPath = ""
|
var appFolderPath = ""
|
||||||
|
|||||||
4
app/src/main/java/com/example/hpos/data/FileUploader.kt
Normal file
4
app/src/main/java/com/example/hpos/data/FileUploader.kt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package com.example.hpos.data
|
||||||
|
|
||||||
|
class FileUploader {
|
||||||
|
}
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package com.example.hpos.data.model
|
|
||||||
|
|
||||||
data class PendingQueue(
|
|
||||||
val photoInDevicePath: String?,
|
|
||||||
val csvInDevicePath: String?,
|
|
||||||
val detailedCsvInDevicePath: String?,
|
|
||||||
val pdfReportInDevicePath: String?
|
|
||||||
)
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package com.example.hpos.data.model
|
||||||
|
|
||||||
|
import java.util.Date
|
||||||
|
|
||||||
|
data class PendingUploads(
|
||||||
|
val pendingId: String,
|
||||||
|
val mobileId: String,
|
||||||
|
val patientId: String,
|
||||||
|
val filePath: String,
|
||||||
|
val timeAdded: Date
|
||||||
|
)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.example.hpos.data
|
package com.example.hpos.data.model
|
||||||
|
|
||||||
sealed class Response<out R> {
|
sealed class Response<out R> {
|
||||||
data class Success<out T>(val data: T) : Response<T>()
|
data class Success<out T>(val data: T) : Response<T>()
|
||||||
@@ -1,18 +1,22 @@
|
|||||||
package com.example.hpos.data
|
package com.example.hpos.data.repository
|
||||||
|
|
||||||
|
import android.net.Uri
|
||||||
|
import com.example.hpos.data.model.Response
|
||||||
|
import com.example.hpos.data.model.PendingUploads
|
||||||
import com.example.hpos.data.model.patient.PatientDetails
|
import com.example.hpos.data.model.patient.PatientDetails
|
||||||
import com.example.hpos.data.model.test.TestDetails
|
import com.example.hpos.data.model.test.TestDetails
|
||||||
import com.google.firebase.firestore.FirebaseFirestore
|
import com.google.firebase.firestore.FirebaseFirestore
|
||||||
import com.google.firebase.firestore.Query
|
import com.google.firebase.firestore.Query
|
||||||
import com.google.firebase.firestore.ktx.firestore
|
import com.google.firebase.firestore.ktx.firestore
|
||||||
import com.google.firebase.ktx.Firebase
|
import com.google.firebase.ktx.Firebase
|
||||||
import com.google.firebase.storage.FirebaseStorage
|
import com.google.firebase.storage.ktx.storage
|
||||||
import kotlinx.coroutines.tasks.await
|
import kotlinx.coroutines.tasks.await
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
class DatabaseRepository : Repository {
|
class DatabaseRepository : Repository {
|
||||||
|
|
||||||
private val db: FirebaseFirestore = Firebase.firestore
|
private val db: FirebaseFirestore = Firebase.firestore
|
||||||
private val storage = FirebaseStorage.getInstance().reference
|
private val storage = Firebase.storage
|
||||||
|
|
||||||
suspend fun addPatientToDatabase(data: PatientDetails) : Response<String> {
|
suspend fun addPatientToDatabase(data: PatientDetails) : Response<String> {
|
||||||
return try {
|
return try {
|
||||||
@@ -79,9 +83,46 @@ class DatabaseRepository : Repository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
suspend fun uploadFileToStorage(id: String, filePath: String): Response<Boolean> {
|
||||||
|
try {
|
||||||
|
|
||||||
// suspend fun updateTestData(id: String, newTest: TestDetails, isUpdate): Boolean {
|
val file = Uri.fromFile(File(filePath))
|
||||||
//
|
val riversRef = storage.reference.child("$id/${file.lastPathSegment}")
|
||||||
// }
|
riversRef.putFile(file).await()
|
||||||
|
return Response.Success(true)
|
||||||
|
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
return Response.Error(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun addToPendingQueue(pendingUploads: PendingUploads): Response<Boolean> {
|
||||||
|
return try {
|
||||||
|
db.collection("pendingUploads")
|
||||||
|
.document(pendingUploads.pendingId)
|
||||||
|
.set(pendingUploads)
|
||||||
|
.await()
|
||||||
|
|
||||||
|
Response.Success(true)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
Response.Error(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun removeFromPendingQueue(pendingId: String): Response<Boolean> {
|
||||||
|
return try {
|
||||||
|
db.collection("pendingUploads")
|
||||||
|
.document(pendingId)
|
||||||
|
.delete()
|
||||||
|
.await()
|
||||||
|
|
||||||
|
Response.Success(true)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
Response.Error(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.example.hpos.data
|
package com.example.hpos.data.repository
|
||||||
|
|
||||||
import com.example.hpos.data.datasource.LocalFileDataSource
|
import com.example.hpos.data.datasource.LocalFileDataSource
|
||||||
|
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package com.example.hpos.data
|
package com.example.hpos.data.repository
|
||||||
|
|
||||||
interface Repository {
|
interface Repository {
|
||||||
// suspend fun addToDatabase(data: PatientDetails)
|
// suspend fun addToDatabase(data: PatientDetails)
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package com.example.hpos.domain
|
package com.example.hpos.domain
|
||||||
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import com.example.hpos.data.LocalFileRepository
|
import com.example.hpos.data.repository.LocalFileRepository
|
||||||
import com.example.hpos.data.constant.Constants
|
import com.example.hpos.data.constant.Constants
|
||||||
import com.example.hpos.data.model.test.TestDetails
|
import com.example.hpos.data.model.test.TestDetails
|
||||||
import com.example.hpos.data.model.test.TestRightCalculationData
|
import com.example.hpos.data.model.test.TestRightCalculationData
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package com.example.hpos.domain
|
package com.example.hpos.domain
|
||||||
|
|
||||||
import com.example.hpos.data.LocalFileRepository
|
import com.example.hpos.data.repository.LocalFileRepository
|
||||||
import com.example.hpos.data.model.CalculationVariableForTest
|
import com.example.hpos.data.model.CalculationVariableForTest
|
||||||
|
|
||||||
class SaveRawDataTest(private val localFileRepository: LocalFileRepository) {
|
class SaveRawDataTest(private val localFileRepository: LocalFileRepository) {
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ class SplashActivity : AppCompatActivity() {
|
|||||||
setContentView(binding.root)
|
setContentView(binding.root)
|
||||||
|
|
||||||
setupFirestoreCache()
|
setupFirestoreCache()
|
||||||
|
setupStaticConstants()
|
||||||
|
|
||||||
checkForStoragePermissions()
|
checkForStoragePermissions()
|
||||||
checkForLocationPermission()
|
checkForLocationPermission()
|
||||||
@@ -53,6 +54,10 @@ class SplashActivity : AppCompatActivity() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun setupStaticConstants() {
|
||||||
|
DataHolder.mobileUniqueId = Settings.Secure.getString(applicationContext.contentResolver, Settings.Secure.ANDROID_ID)
|
||||||
|
}
|
||||||
|
|
||||||
private fun setupFirestoreCache() {
|
private fun setupFirestoreCache() {
|
||||||
val settings = FirebaseFirestoreSettings.Builder()
|
val settings = FirebaseFirestoreSettings.Builder()
|
||||||
.setCacheSizeBytes(FirebaseFirestoreSettings.CACHE_SIZE_UNLIMITED)
|
.setCacheSizeBytes(FirebaseFirestoreSettings.CACHE_SIZE_UNLIMITED)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package com.example.hpos.presentation.dashboard
|
|||||||
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
|
import android.widget.Toast
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.drawerlayout.widget.DrawerLayout
|
import androidx.drawerlayout.widget.DrawerLayout
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
@@ -12,6 +13,7 @@ import androidx.navigation.ui.setupActionBarWithNavController
|
|||||||
import androidx.navigation.ui.setupWithNavController
|
import androidx.navigation.ui.setupWithNavController
|
||||||
import com.example.hpos.R
|
import com.example.hpos.R
|
||||||
import com.example.hpos.databinding.ActivityDashboardBinding
|
import com.example.hpos.databinding.ActivityDashboardBinding
|
||||||
|
import com.example.hpos.util.MyUtils
|
||||||
import com.example.hpos.util.MyViewModelFactory
|
import com.example.hpos.util.MyViewModelFactory
|
||||||
import com.google.android.material.navigation.NavigationView
|
import com.google.android.material.navigation.NavigationView
|
||||||
|
|
||||||
@@ -32,8 +34,7 @@ class DashboardActivity : AppCompatActivity() {
|
|||||||
val drawerLayout: DrawerLayout = binding.drawerLayout
|
val drawerLayout: DrawerLayout = binding.drawerLayout
|
||||||
val navView: NavigationView = binding.navView
|
val navView: NavigationView = binding.navView
|
||||||
val navController = findNavController(R.id.nav_host_fragment_content_dashboard)
|
val navController = findNavController(R.id.nav_host_fragment_content_dashboard)
|
||||||
// Passing each menu ID as a set of Ids because each
|
|
||||||
// menu should be considered as top level destinations.
|
|
||||||
appBarConfiguration = AppBarConfiguration(
|
appBarConfiguration = AppBarConfiguration(
|
||||||
setOf(
|
setOf(
|
||||||
R.id.nav_home, R.id.nav_profile, R.id.nav_settings
|
R.id.nav_home, R.id.nav_profile, R.id.nav_settings
|
||||||
@@ -42,9 +43,6 @@ class DashboardActivity : AppCompatActivity() {
|
|||||||
setupActionBarWithNavController(navController, appBarConfiguration)
|
setupActionBarWithNavController(navController, appBarConfiguration)
|
||||||
navView.setupWithNavController(navController)
|
navView.setupWithNavController(navController)
|
||||||
|
|
||||||
// val i = Intent(applicationContext, MainActivity::class.java)
|
|
||||||
// startActivity(i)
|
|
||||||
|
|
||||||
viewModel = ViewModelProvider(
|
viewModel = ViewModelProvider(
|
||||||
this,
|
this,
|
||||||
MyViewModelFactory()
|
MyViewModelFactory()
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
package com.example.hpos.presentation.dashboard
|
package com.example.hpos.presentation.dashboard
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import android.provider.Settings
|
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.example.hpos.BuildConfig
|
import com.example.hpos.BuildConfig
|
||||||
import com.example.hpos.data.DatabaseRepository
|
import com.example.hpos.data.DataHolder
|
||||||
import com.example.hpos.data.Response
|
import com.example.hpos.data.repository.DatabaseRepository
|
||||||
|
import com.example.hpos.data.model.Response
|
||||||
import com.example.hpos.data.model.AadharCard
|
import com.example.hpos.data.model.AadharCard
|
||||||
import com.example.hpos.data.model.Location
|
import com.example.hpos.data.model.Location
|
||||||
import com.example.hpos.data.model.patient.PatientDetails
|
import com.example.hpos.data.model.patient.PatientDetails
|
||||||
@@ -65,9 +64,8 @@ class DashboardViewModel() : ViewModel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun createTest(context: Context) {
|
fun createTest() {
|
||||||
val mTestId = MyUtils.generateTestIdForPatient(patientDetails.id)
|
val mTestId = MyUtils.generateTestIdForPatient(patientDetails.id)
|
||||||
val mMobileId = Settings.Secure.getString(context.contentResolver, Settings.Secure.ANDROID_ID)
|
|
||||||
|
|
||||||
testDetails = TestDetails(
|
testDetails = TestDetails(
|
||||||
testID = mTestId,
|
testID = mTestId,
|
||||||
@@ -81,7 +79,7 @@ class DashboardViewModel() : ViewModel() {
|
|||||||
csvPath = null,
|
csvPath = null,
|
||||||
logPath = null,
|
logPath = null,
|
||||||
deviceId = null,
|
deviceId = null,
|
||||||
mobileId = mMobileId,
|
mobileId = DataHolder.mobileUniqueId!!,
|
||||||
kitSerial = null,
|
kitSerial = null,
|
||||||
location = deviceLocation,
|
location = deviceLocation,
|
||||||
appVersion = BuildConfig.VERSION_CODE.toString(),
|
appVersion = BuildConfig.VERSION_CODE.toString(),
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import androidx.fragment.app.Fragment
|
|||||||
import androidx.fragment.app.activityViewModels
|
import androidx.fragment.app.activityViewModels
|
||||||
import androidx.navigation.fragment.findNavController
|
import androidx.navigation.fragment.findNavController
|
||||||
import com.example.hpos.R
|
import com.example.hpos.R
|
||||||
import com.example.hpos.data.Response
|
import com.example.hpos.data.model.Response
|
||||||
import com.example.hpos.data.model.patient.PatientDetails
|
import com.example.hpos.data.model.patient.PatientDetails
|
||||||
import com.example.hpos.databinding.FragmentRegisterThreeEnterPatientDetailsBinding
|
import com.example.hpos.databinding.FragmentRegisterThreeEnterPatientDetailsBinding
|
||||||
import com.example.hpos.presentation.dashboard.DashboardViewModel
|
import com.example.hpos.presentation.dashboard.DashboardViewModel
|
||||||
@@ -71,7 +71,7 @@ class RegisterThreeEnterPatientDetails : Fragment() {
|
|||||||
if (mPatient != null) {
|
if (mPatient != null) {
|
||||||
viewModel.patientDetails = mPatient
|
viewModel.patientDetails = mPatient
|
||||||
viewModel.pushPatientDetailsToDb()
|
viewModel.pushPatientDetailsToDb()
|
||||||
viewModel.createTest(requireContext())
|
viewModel.createTest()
|
||||||
moveToNextPage()
|
moveToNextPage()
|
||||||
} else {
|
} else {
|
||||||
Toast.makeText(requireContext(), "Fields can't be empty", Toast.LENGTH_SHORT).show()
|
Toast.makeText(requireContext(), "Fields can't be empty", Toast.LENGTH_SHORT).show()
|
||||||
|
|||||||
@@ -242,59 +242,26 @@ class TestRightExpSample : Fragment() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun saveDataLocally() {
|
private fun saveDataLocally() {
|
||||||
val patientName = viewModel.testDetails?.patientName
|
// OLD ------------------------------
|
||||||
|
// val patientName = viewModel.testDetails?.patientName
|
||||||
// if (patientName.length > 5){
|
// if (patientName.length > 5){
|
||||||
// patientName = patientName.substring(0, 5)
|
// patientName = patientName.substring(0, 5)
|
||||||
// }
|
// }
|
||||||
|
// val id = PreferenceUtility.generateId(requireContext())
|
||||||
|
|
||||||
val id = PreferenceUtility.generateId(requireContext())
|
|
||||||
|
|
||||||
// OLD ------------------------------
|
|
||||||
// val prefixCsv: String = if (DataHolder.selectedTestType == TestType.SICKLECERT)
|
// val prefixCsv: String = if (DataHolder.selectedTestType == TestType.SICKLECERT)
|
||||||
// "HPOSSC_${DataHolder.deviceSerialNumber}_${patientName}_"
|
// "HPOSSC_${DataHolder.deviceSerialNumber}_${patientName}_"
|
||||||
// else
|
// else
|
||||||
// "HPOSSF_${DataHolder.deviceSerialNumber}_${patientName}_"
|
// "HPOSSF_${DataHolder.deviceSerialNumber}_${patientName}_"
|
||||||
|
|
||||||
// NEW ------------------------------
|
|
||||||
val prefixCsv: String = if (DataHolder.selectedTestType == TestType.SICKLECERT)
|
|
||||||
"HPOSSC_${viewModel.testDetails?.testID}"
|
|
||||||
else
|
|
||||||
"HPOSSF_${viewModel.testDetails?.testID}"
|
|
||||||
|
|
||||||
// val prefixCsv = "HPOSSC_${DataHolder.deviceSerialNumber}_${patientName}_"
|
|
||||||
val fileExtensionCsv = ".csv"
|
|
||||||
|
|
||||||
// OLD ------------------------------
|
|
||||||
// val fileNameCsv = prefixCsv + id + fileExtensionCsv
|
// val fileNameCsv = prefixCsv + id + fileExtensionCsv
|
||||||
// NEW ------------------------------
|
|
||||||
val fileNameCsv = prefixCsv + fileExtensionCsv
|
|
||||||
|
|
||||||
saveCsv(fileNameCsv)
|
|
||||||
|
|
||||||
// OLD ------------------------------
|
|
||||||
// val prefixTxt = "log_${DataHolder.deviceSerialNumber}_${patientName}_"
|
|
||||||
// val fileExtensionTxt = ".txt"
|
|
||||||
// val fileNameTxt = prefixTxt + id + fileExtensionTxt
|
|
||||||
|
|
||||||
// NEW ------------------------------
|
// NEW ------------------------------
|
||||||
val prefixTxt = "log_${viewModel.testDetails?.testID}"
|
viewModel.saveCsv(requireContext().applicationContext, viewModel.getCSVFileName())
|
||||||
val fileExtensionTxt = ".txt"
|
viewModel.saveCsvForTesting(requireContext().applicationContext, viewModel.getDetailedCSVFileName())
|
||||||
val fileNameTxt = prefixTxt + fileExtensionTxt
|
|
||||||
|
|
||||||
saveLog(fileNameTxt)
|
viewModel.saveLogWithPatient(requireContext().applicationContext, viewModel.getLogFileName())
|
||||||
}
|
|
||||||
|
|
||||||
private fun saveCsv(fileName: String) {
|
|
||||||
Log.d(TAG, "saveCsv() called")
|
|
||||||
|
|
||||||
viewModel.saveCsv(requireContext().applicationContext, fileName)
|
|
||||||
viewModel.saveCsvForTesting(requireContext().applicationContext, "detailed_$fileName")
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun saveLog(fileName: String) {
|
|
||||||
Log.d(TAG, "saveLog() called")
|
|
||||||
viewModel.saveLogWithPatient(requireContext().applicationContext, fileName)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -17,6 +17,7 @@ import com.example.hpos.data.model.test.TestRightResultType
|
|||||||
import com.example.hpos.data.model.test.TestType
|
import com.example.hpos.data.model.test.TestType
|
||||||
import com.example.hpos.databinding.FragmentTestRightResultsBinding
|
import com.example.hpos.databinding.FragmentTestRightResultsBinding
|
||||||
import com.example.hpos.presentation.dashboard.DashboardActivity
|
import com.example.hpos.presentation.dashboard.DashboardActivity
|
||||||
|
import com.example.hpos.util.MyUtils
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
|
|
||||||
@@ -42,6 +43,15 @@ class TestRightResults : Fragment() {
|
|||||||
updateResults()
|
updateResults()
|
||||||
|
|
||||||
viewModel.addResultTestToDb()
|
viewModel.addResultTestToDb()
|
||||||
|
|
||||||
|
// This is to add files to the pending queue
|
||||||
|
viewModel.addFilesToQueue(requireContext())
|
||||||
|
|
||||||
|
// Uploading to cloud storage and deleting the from the pending queue when uploaded
|
||||||
|
// if (MyUtils.isInternetConnected()) {
|
||||||
|
// viewModel.addFilesToCloud(requireContext())
|
||||||
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun setupListeners() {
|
private fun setupListeners() {
|
||||||
|
|||||||
@@ -6,26 +6,36 @@ import androidx.lifecycle.MutableLiveData
|
|||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.viewModelScope
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.example.hpos.data.DataHolder
|
import com.example.hpos.data.DataHolder
|
||||||
import com.example.hpos.data.DatabaseRepository
|
import com.example.hpos.data.repository.DatabaseRepository
|
||||||
|
import com.example.hpos.data.model.Response
|
||||||
import com.example.hpos.data.constant.Constants
|
import com.example.hpos.data.constant.Constants
|
||||||
import com.example.hpos.data.model.CalculationVariableForTest
|
import com.example.hpos.data.model.CalculationVariableForTest
|
||||||
import com.example.hpos.data.model.ErrorMessage
|
import com.example.hpos.data.model.ErrorMessage
|
||||||
|
import com.example.hpos.data.model.PendingUploads
|
||||||
import com.example.hpos.data.model.test.Status
|
import com.example.hpos.data.model.test.Status
|
||||||
import com.example.hpos.data.model.test.TestRightCalculationData
|
import com.example.hpos.data.model.test.TestRightCalculationData
|
||||||
import com.example.hpos.data.model.test.TestRightDeviceConstants
|
import com.example.hpos.data.model.test.TestRightDeviceConstants
|
||||||
|
import com.example.hpos.data.model.test.TestType
|
||||||
import com.example.hpos.domain.ResultCalculationWithMaxImpl
|
import com.example.hpos.domain.ResultCalculationWithMaxImpl
|
||||||
import com.example.hpos.domain.SaveRawData
|
import com.example.hpos.domain.SaveRawData
|
||||||
import com.example.hpos.domain.SaveRawDataTest
|
import com.example.hpos.domain.SaveRawDataTest
|
||||||
import com.example.hpos.domain.SickleFindResultCaluculationWithMaxImpl
|
import com.example.hpos.domain.SickleFindResultCaluculationWithMaxImpl
|
||||||
import com.example.hpos.domain.TestRightResultCalculation
|
import com.example.hpos.domain.TestRightResultCalculation
|
||||||
import com.example.hpos.util.MyUtils
|
import com.example.hpos.util.MyUtils
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.GlobalScope
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
|
import java.util.Calendar
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
import kotlin.math.log10
|
import kotlin.math.log10
|
||||||
import kotlin.math.pow
|
import kotlin.math.pow
|
||||||
|
|
||||||
class TestRightViewModel(private val saveRawData: SaveRawData, private val saveRawDataTest: SaveRawDataTest, private val repository: DatabaseRepository) : ViewModel() {
|
class TestRightViewModel(
|
||||||
|
private val saveRawData: SaveRawData,
|
||||||
|
private val saveRawDataTest: SaveRawDataTest,
|
||||||
|
private val repository: DatabaseRepository
|
||||||
|
) : ViewModel() {
|
||||||
|
|
||||||
private val TAG = "TestRightViewModel"
|
private val TAG = "TestRightViewModel"
|
||||||
|
|
||||||
@@ -271,21 +281,21 @@ class TestRightViewModel(private val saveRawData: SaveRawData, private val saveR
|
|||||||
|
|
||||||
fun saveCsv(appContext: Context, filename: String) {
|
fun saveCsv(appContext: Context, filename: String) {
|
||||||
// try {
|
// try {
|
||||||
var folderPath: String? = DataHolder.appFolderPath
|
var folderPath: String? = DataHolder.appFolderPath
|
||||||
if (DataHolder.isAppFolderCreated) {
|
if (DataHolder.isAppFolderCreated) {
|
||||||
// SaveRawData().saveCsv(folderPath!!, filename, wavelengthToAbsorbance)
|
// SaveRawData().saveCsv(folderPath!!, filename, wavelengthToAbsorbance)
|
||||||
saveRawData.saveCsv(folderPath!!, filename, wavelengthToAbsorbance)
|
saveRawData.saveCsv(folderPath!!, filename, wavelengthToAbsorbance)
|
||||||
} else {
|
} else {
|
||||||
folderPath = MyUtils.createAppFolder(appContext)
|
folderPath = MyUtils.createAppFolder(appContext)
|
||||||
if (folderPath != null) {
|
if (folderPath != null) {
|
||||||
DataHolder.isAppFolderCreated = true
|
DataHolder.isAppFolderCreated = true
|
||||||
DataHolder.appFolderPath = folderPath
|
DataHolder.appFolderPath = folderPath
|
||||||
// SaveRawData().saveCsv(folderPath, filename, wavelengthToAbsorbance)
|
// SaveRawData().saveCsv(folderPath, filename, wavelengthToAbsorbance)
|
||||||
saveRawData.saveCsv(folderPath, filename, wavelengthToAbsorbance)
|
saveRawData.saveCsv(folderPath, filename, wavelengthToAbsorbance)
|
||||||
} else {
|
} else {
|
||||||
// errorTriggered.postValue("Unable to save CSV, Please try again")
|
// errorTriggered.postValue("Unable to save CSV, Please try again")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// } catch (e: java.io.FileNotFoundException) {
|
// } catch (e: java.io.FileNotFoundException) {
|
||||||
// errorTriggered.postValue(
|
// errorTriggered.postValue(
|
||||||
// ErrorMessage(
|
// ErrorMessage(
|
||||||
@@ -298,22 +308,22 @@ class TestRightViewModel(private val saveRawData: SaveRawData, private val saveR
|
|||||||
|
|
||||||
fun saveCsvForTesting(appContext: Context, filename: String) {
|
fun saveCsvForTesting(appContext: Context, filename: String) {
|
||||||
// try {
|
// try {
|
||||||
var folderPath: String? = DataHolder.appFolderPath
|
var folderPath: String? = DataHolder.appFolderPath
|
||||||
if (DataHolder.isAppFolderCreated) {
|
if (DataHolder.isAppFolderCreated) {
|
||||||
// SaveRawDataTest().saveCsv(folderPath!!, filename, calculationVariableList)
|
// SaveRawDataTest().saveCsv(folderPath!!, filename, calculationVariableList)
|
||||||
saveRawDataTest.saveCsv(folderPath!!, filename, calculationVariableList)
|
saveRawDataTest.saveCsv(folderPath!!, filename, calculationVariableList)
|
||||||
} else {
|
} else {
|
||||||
folderPath = MyUtils.createAppFolder(appContext)
|
folderPath = MyUtils.createAppFolder(appContext)
|
||||||
if (folderPath != null) {
|
if (folderPath != null) {
|
||||||
DataHolder.isAppFolderCreated = true
|
DataHolder.isAppFolderCreated = true
|
||||||
DataHolder.appFolderPath = folderPath
|
DataHolder.appFolderPath = folderPath
|
||||||
// SaveRawDataTest().saveCsv(folderPath, filename, calculationVariableList)
|
// SaveRawDataTest().saveCsv(folderPath, filename, calculationVariableList)
|
||||||
saveRawDataTest.saveCsv(folderPath, filename, calculationVariableList)
|
saveRawDataTest.saveCsv(folderPath, filename, calculationVariableList)
|
||||||
} else {
|
} else {
|
||||||
// errorTriggered.postValue("Unable to save CSV, Please try again")
|
// errorTriggered.postValue("Unable to save CSV, Please try again")
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
// } catch (e: java.io.FileNotFoundException) {
|
// } catch (e: java.io.FileNotFoundException) {
|
||||||
// errorTriggered.postValue(
|
// errorTriggered.postValue(
|
||||||
// ErrorMessage(
|
// ErrorMessage(
|
||||||
@@ -326,22 +336,22 @@ class TestRightViewModel(private val saveRawData: SaveRawData, private val saveR
|
|||||||
|
|
||||||
fun saveLog(appContext: Context, fileName: String) {
|
fun saveLog(appContext: Context, fileName: String) {
|
||||||
// try {
|
// try {
|
||||||
var folderPath: String? = DataHolder.appFolderPath
|
var folderPath: String? = DataHolder.appFolderPath
|
||||||
|
|
||||||
if (DataHolder.isAppFolderCreated) {
|
if (DataHolder.isAppFolderCreated) {
|
||||||
// SaveRawData().saveLog(folderPath!!, fileName, calculationData)
|
// SaveRawData().saveLog(folderPath!!, fileName, calculationData)
|
||||||
saveRawData.saveLog(folderPath!!, fileName, calculationData)
|
saveRawData.saveLog(folderPath!!, fileName, calculationData)
|
||||||
} else {
|
} else {
|
||||||
folderPath = MyUtils.createAppFolder(appContext)
|
folderPath = MyUtils.createAppFolder(appContext)
|
||||||
if (folderPath != null) {
|
if (folderPath != null) {
|
||||||
DataHolder.isAppFolderCreated = true
|
DataHolder.isAppFolderCreated = true
|
||||||
DataHolder.appFolderPath = folderPath
|
DataHolder.appFolderPath = folderPath
|
||||||
// SaveRawData().saveLog(folderPath, fileName, calculationData)
|
// SaveRawData().saveLog(folderPath, fileName, calculationData)
|
||||||
saveRawData.saveLog(folderPath, fileName, calculationData)
|
saveRawData.saveLog(folderPath, fileName, calculationData)
|
||||||
} else {
|
} else {
|
||||||
// errorTriggered.postValue("Unable to save Log, Please try again")
|
// errorTriggered.postValue("Unable to save Log, Please try again")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// } catch (e: java.io.FileNotFoundException) {
|
// } catch (e: java.io.FileNotFoundException) {
|
||||||
// errorTriggered.postValue(
|
// errorTriggered.postValue(
|
||||||
// ErrorMessage(
|
// ErrorMessage(
|
||||||
@@ -354,32 +364,32 @@ class TestRightViewModel(private val saveRawData: SaveRawData, private val saveR
|
|||||||
|
|
||||||
fun saveLogWithPatient(appContext: Context, fileName: String) {
|
fun saveLogWithPatient(appContext: Context, fileName: String) {
|
||||||
// try {
|
// try {
|
||||||
var folderPath: String? = DataHolder.appFolderPath
|
var folderPath: String? = DataHolder.appFolderPath
|
||||||
|
|
||||||
if (DataHolder.isAppFolderCreated) {
|
if (DataHolder.isAppFolderCreated) {
|
||||||
// SaveRawData().saveLogWithPatientData(
|
// SaveRawData().saveLogWithPatientData(
|
||||||
|
saveRawData.saveLogWithPatientData(
|
||||||
|
folderPath!!,
|
||||||
|
fileName,
|
||||||
|
calculationData,
|
||||||
|
testDetails
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
folderPath = MyUtils.createAppFolder(appContext)
|
||||||
|
if (folderPath != null) {
|
||||||
|
DataHolder.isAppFolderCreated = true
|
||||||
|
DataHolder.appFolderPath = folderPath
|
||||||
|
// SaveRawData().saveLogWithPatientData(
|
||||||
saveRawData.saveLogWithPatientData(
|
saveRawData.saveLogWithPatientData(
|
||||||
folderPath!!,
|
folderPath,
|
||||||
fileName,
|
fileName,
|
||||||
calculationData,
|
calculationData,
|
||||||
testDetails
|
testDetails
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
folderPath = MyUtils.createAppFolder(appContext)
|
|
||||||
if (folderPath != null) {
|
|
||||||
DataHolder.isAppFolderCreated = true
|
|
||||||
DataHolder.appFolderPath = folderPath
|
|
||||||
// SaveRawData().saveLogWithPatientData(
|
|
||||||
saveRawData.saveLogWithPatientData(
|
|
||||||
folderPath,
|
|
||||||
fileName,
|
|
||||||
calculationData,
|
|
||||||
testDetails
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
// errorTriggered.postValue("Unable to save Log, Please try again")
|
// errorTriggered.postValue("Unable to save Log, Please try again")
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// } catch (e: java.io.FileNotFoundException) {
|
// } catch (e: java.io.FileNotFoundException) {
|
||||||
// errorTriggered.postValue(
|
// errorTriggered.postValue(
|
||||||
// ErrorMessage(
|
// ErrorMessage(
|
||||||
@@ -416,16 +426,131 @@ class TestRightViewModel(private val saveRawData: SaveRawData, private val saveR
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scanner Fragment Related Members
|
fun getCSVFileName(): String {
|
||||||
|
val prefixCsv: String = if (DataHolder.selectedTestType == TestType.SICKLECERT)
|
||||||
|
"HPOSSC_${testDetails?.testID}"
|
||||||
|
else
|
||||||
|
"HPOSSF_${testDetails?.testID}"
|
||||||
|
|
||||||
// fun processEncodedScannedData(contents: String) {
|
val fileExtensionCsv = ".csv"
|
||||||
//
|
|
||||||
// }
|
return prefixCsv + fileExtensionCsv
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getDetailedCSVFileName(): String {
|
||||||
|
return "detailed_" + getCSVFileName()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLogFileName(): String {
|
||||||
|
val prefixTxt = "log_${testDetails?.testID}"
|
||||||
|
val fileExtensionTxt = ".txt"
|
||||||
|
return prefixTxt + fileExtensionTxt
|
||||||
|
}
|
||||||
|
|
||||||
fun addResultTestToDb() {
|
fun addResultTestToDb() {
|
||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
repository.addTestToDatabase(testDetails!!)
|
testDetails!!.uploadTime = Calendar.getInstance().time
|
||||||
|
repository.addTestToDatabase(testDetails)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun addFilesToQueue(context: Context) {
|
||||||
|
viewModelScope.launch {
|
||||||
|
val csvFilePath = MyUtils.getAppFolderPath(context) + getCSVFileName()
|
||||||
|
val pendingId = getCSVFileName().split(".")[0]
|
||||||
|
repository.addToPendingQueue(
|
||||||
|
PendingUploads(
|
||||||
|
pendingId,
|
||||||
|
DataHolder.mobileUniqueId!!,
|
||||||
|
testDetails!!.patientID,
|
||||||
|
csvFilePath,
|
||||||
|
Calendar.getInstance().time
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
viewModelScope.launch {
|
||||||
|
val detailedCsvFilePath = MyUtils.getAppFolderPath(context) + getDetailedCSVFileName()
|
||||||
|
val pendingId = getDetailedCSVFileName().split(".")[0]
|
||||||
|
repository.addToPendingQueue(
|
||||||
|
PendingUploads(
|
||||||
|
pendingId,
|
||||||
|
DataHolder.mobileUniqueId!!,
|
||||||
|
testDetails!!.patientID,
|
||||||
|
detailedCsvFilePath,
|
||||||
|
Calendar.getInstance().time
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
viewModelScope.launch {
|
||||||
|
val logTxtFilePath = MyUtils.getAppFolderPath(context) + getLogFileName()
|
||||||
|
val pendingId = getLogFileName().split(".")[0]
|
||||||
|
repository.addToPendingQueue(
|
||||||
|
PendingUploads(
|
||||||
|
pendingId,
|
||||||
|
DataHolder.mobileUniqueId!!,
|
||||||
|
testDetails!!.patientID,
|
||||||
|
logTxtFilePath,
|
||||||
|
Calendar.getInstance().time
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun addFilesToCloud(context: Context) {
|
||||||
|
GlobalScope.launch(Dispatchers.IO) {
|
||||||
|
val csvFilePath = MyUtils.getAppFolderPath(context) + getCSVFileName()
|
||||||
|
val fileName = getCSVFileName().split(".")[0]
|
||||||
|
val response = repository.uploadFileToStorage(testDetails!!.patientID, csvFilePath)
|
||||||
|
|
||||||
|
when (response) {
|
||||||
|
is Response.Success -> {
|
||||||
|
Log.d(TAG, "Uploaded $csvFilePath")
|
||||||
|
repository.removeFromPendingQueue(fileName)
|
||||||
|
}
|
||||||
|
|
||||||
|
is Response.Error -> {
|
||||||
|
Log.d(TAG, response.exception.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GlobalScope.launch(Dispatchers.IO) {
|
||||||
|
val detailedCsvFilePath =
|
||||||
|
MyUtils.getAppFolderPath(context) + getDetailedCSVFileName()
|
||||||
|
val fileName = getDetailedCSVFileName().split(".")[0]
|
||||||
|
val response =
|
||||||
|
repository.uploadFileToStorage(testDetails!!.patientID, detailedCsvFilePath)
|
||||||
|
|
||||||
|
when (response) {
|
||||||
|
is Response.Success -> {
|
||||||
|
Log.d(TAG, "Uploaded $detailedCsvFilePath")
|
||||||
|
repository.removeFromPendingQueue(fileName)
|
||||||
|
}
|
||||||
|
|
||||||
|
is Response.Error -> {
|
||||||
|
Log.d(TAG, response.exception.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GlobalScope.launch(Dispatchers.IO) {
|
||||||
|
val logTxtFilePath = MyUtils.getAppFolderPath(context) + getLogFileName()
|
||||||
|
val fileName = getLogFileName().split(".")[0]
|
||||||
|
val response =
|
||||||
|
repository.uploadFileToStorage(testDetails!!.patientID, logTxtFilePath)
|
||||||
|
|
||||||
|
when (response) {
|
||||||
|
is Response.Success -> {
|
||||||
|
Log.d(TAG, "Uploaded $logTxtFilePath")
|
||||||
|
repository.removeFromPendingQueue(fileName)
|
||||||
|
}
|
||||||
|
|
||||||
|
is Response.Error -> {
|
||||||
|
Log.d(TAG, response.exception.toString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,7 @@ import android.os.Environment
|
|||||||
import com.example.hpos.R
|
import com.example.hpos.R
|
||||||
import com.example.hpos.data.model.patient.PatientDetails
|
import com.example.hpos.data.model.patient.PatientDetails
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.io.IOException
|
||||||
import java.net.InetAddress
|
import java.net.InetAddress
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
@@ -14,7 +15,7 @@ import java.util.Locale
|
|||||||
object MyUtils {
|
object MyUtils {
|
||||||
|
|
||||||
fun createAppFolder(context: Context) : String? {
|
fun createAppFolder(context: Context) : String? {
|
||||||
val folderPath = Environment.getExternalStorageDirectory().absolutePath + "/" + context.resources.getString(R.string.app_name) + "/"
|
val folderPath = getAppFolderPath(context)
|
||||||
val file = File(folderPath)
|
val file = File(folderPath)
|
||||||
|
|
||||||
return if (!file.exists()) {
|
return if (!file.exists()) {
|
||||||
@@ -28,13 +29,22 @@ object MyUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getAppFolderPath(context: Context) : String {
|
||||||
|
return Environment.getExternalStorageDirectory().absolutePath + "/" + context.resources.getString(R.string.app_name) + "/"
|
||||||
|
}
|
||||||
|
|
||||||
fun isInternetConnected(): Boolean {
|
fun isInternetConnected(): Boolean {
|
||||||
return try {
|
val runtime = Runtime.getRuntime()
|
||||||
val ipAddr: InetAddress = InetAddress.getByName("google.com")
|
try {
|
||||||
ipAddr.equals("hpos")
|
val ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8")
|
||||||
} catch (e: Exception) {
|
val exitValue = ipProcess.waitFor()
|
||||||
false
|
return exitValue == 0
|
||||||
|
} catch (e: IOException) {
|
||||||
|
e.printStackTrace()
|
||||||
|
} catch (e: InterruptedException) {
|
||||||
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
fun generateTestIdForPatient(patientId: String): String {
|
fun generateTestIdForPatient(patientId: String): String {
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ package com.example.hpos.util
|
|||||||
|
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
import androidx.lifecycle.ViewModelProvider
|
import androidx.lifecycle.ViewModelProvider
|
||||||
import com.example.hpos.data.DatabaseRepository
|
import com.example.hpos.data.repository.DatabaseRepository
|
||||||
import com.example.hpos.data.LocalFileRepository
|
import com.example.hpos.data.repository.LocalFileRepository
|
||||||
import com.example.hpos.data.datasource.LocalFileDataSource
|
import com.example.hpos.data.datasource.LocalFileDataSource
|
||||||
import com.example.hpos.domain.SaveRawData
|
import com.example.hpos.domain.SaveRawData
|
||||||
import com.example.hpos.domain.SaveRawDataTest
|
import com.example.hpos.domain.SaveRawDataTest
|
||||||
|
|||||||
Reference in New Issue
Block a user