OFFLINE FEATURE ADDED- with csv file data downloading format added
This commit is contained in:
5
app/src/debug/res/drawable/downloads.xml
Normal file
5
app/src/debug/res/drawable/downloads.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<vector android:height="24dp" android:tint="#000000"
|
||||
android:viewportHeight="24" android:viewportWidth="24"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="@android:color/white" android:pathData="M12,4c4.41,0 8,3.59 8,8s-3.59,8 -8,8s-8,-3.59 -8,-8S7.59,4 12,4M12,2C6.48,2 2,6.48 2,12c0,5.52 4.48,10 10,10c5.52,0 10,-4.48 10,-10C22,6.48 17.52,2 12,2L12,2zM13,12l0,-4h-2l0,4H8l4,4l4,-4H13z"/>
|
||||
</vector>
|
||||
50
app/src/main/java/com/example/hpostesting/data/CsvWriter.kt
Normal file
50
app/src/main/java/com/example/hpostesting/data/CsvWriter.kt
Normal file
@@ -0,0 +1,50 @@
|
||||
package com.example.hpostesting.data
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Environment
|
||||
import java.io.File
|
||||
import java.io.FileWriter
|
||||
import java.io.IOException
|
||||
|
||||
class CsvWriter(private val context: Context) {
|
||||
|
||||
fun writeCsv(fileName: String, data: List<Array<String>>): Boolean {
|
||||
try {
|
||||
val filePath = File(getExternalStorageDirectory(), fileName)
|
||||
val writer = FileWriter(filePath)
|
||||
|
||||
// Write header row
|
||||
val header = arrayOf(
|
||||
"ID",
|
||||
"Blood Group",
|
||||
"Birth Year",
|
||||
"Classification Result",
|
||||
"Test Time",
|
||||
"User Image URL",
|
||||
"Name"
|
||||
)
|
||||
writer.write(header.joinToString(",") + "\n")
|
||||
|
||||
// Write data rows
|
||||
for (line in data) {
|
||||
writer.write(line.joinToString(",") + "\n")
|
||||
}
|
||||
|
||||
writer.close()
|
||||
return true
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private fun getExternalStorageDirectory(): File {
|
||||
val folder = File(Environment.getExternalStorageDirectory(), "HposFolder")
|
||||
|
||||
if (!folder.exists()) {
|
||||
folder.mkdirs()
|
||||
}
|
||||
|
||||
return folder
|
||||
}
|
||||
}
|
||||
@@ -97,7 +97,7 @@ class OfflineUserListAdapter(private val view: View, private val batLevel: Int)
|
||||
DataHolder.selectedTest = UserData(
|
||||
_id = userList._id,
|
||||
bloodGroup = userList.bloodGroup,
|
||||
incubationTime = userList.incubationTime
|
||||
incubationTime = userList.incubationTime,
|
||||
)
|
||||
view.findNavController()
|
||||
.navigate(R.id.action_nav_home_to_mainActivity)
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.example.hpostesting.presentation.dashboard
|
||||
import android.os.Bundle
|
||||
import android.view.Menu
|
||||
import android.widget.Toast
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.drawerlayout.widget.DrawerLayout
|
||||
import androidx.navigation.findNavController
|
||||
@@ -10,6 +11,7 @@ import androidx.navigation.ui.AppBarConfiguration
|
||||
import androidx.navigation.ui.navigateUp
|
||||
import androidx.navigation.ui.setupActionBarWithNavController
|
||||
import androidx.navigation.ui.setupWithNavController
|
||||
import com.example.hpostesting.presentation.hemocube.HemoCubeViewModel
|
||||
import com.google.android.material.navigation.NavigationView
|
||||
import com.google.firebase.appdistribution.FirebaseAppDistribution
|
||||
import com.google.firebase.appdistribution.FirebaseAppDistributionException
|
||||
@@ -26,7 +28,7 @@ class DashboardActivity : AppCompatActivity() {
|
||||
|
||||
private lateinit var appBarConfiguration: AppBarConfiguration
|
||||
private lateinit var binding: ActivityDashboardBinding
|
||||
|
||||
private val viewModel: HemoCubeViewModel by viewModels()
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
@@ -60,6 +62,8 @@ class DashboardActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ class HomeFragment : Fragment() {
|
||||
private lateinit var rvAdapter: UserListAdapter
|
||||
private var batLevel: Int = 0 // Initialize with a default value, or obtain the actual battery level
|
||||
private lateinit var adapter: OfflineUserListAdapter
|
||||
|
||||
private val homeViewModel: HemoCubeViewModel by activityViewModels()
|
||||
private lateinit var sharedPreference: SharedPreferences
|
||||
override fun onCreateView(
|
||||
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
|
||||
@@ -121,6 +121,10 @@ class HomeFragment : Fragment() {
|
||||
binding.uploadData.setOnClickListener {
|
||||
showUploadDialog(requireContext())
|
||||
}
|
||||
|
||||
binding.btnSaveLocal.setOnClickListener {
|
||||
downloadCsv()
|
||||
}
|
||||
binding.btnNewKit.setOnClickListener {
|
||||
with(sharedPreference.edit()) {
|
||||
putString(Constants.KIT_NUMBER, "")
|
||||
@@ -376,4 +380,22 @@ class HomeFragment : Fragment() {
|
||||
super.onDestroyView()
|
||||
_binding = null
|
||||
}
|
||||
|
||||
|
||||
|
||||
private fun downloadCsv() {
|
||||
context?.let { context ->
|
||||
val success = homeViewModel.getLocalUserDataForCsv(context)
|
||||
if (success) {
|
||||
// Provide feedback to the user if needed
|
||||
Toast.makeText(context, "CSV file downloaded successfully", Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
// Handle the case where CSV file generation failed
|
||||
Toast.makeText(context, "Failed to download CSV file", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.example.hpostesting.presentation.dashboard.ui.home
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
|
||||
class HomeViewModel : ViewModel() {
|
||||
|
||||
private val _text = MutableLiveData<String>().apply {
|
||||
value = "This is home Fragment"
|
||||
}
|
||||
val text: LiveData<String> = _text
|
||||
}
|
||||
@@ -373,6 +373,9 @@ class HemoCubeFragment : Fragment() {
|
||||
led1Buffer = led1BufferForDevice
|
||||
led2Buffer = led2BufferForDevice
|
||||
appVersion = version
|
||||
this.name = testDetails?.name.toString()
|
||||
this.birthYear = testDetails?.birthYear.toString()
|
||||
this.userImageURL = testDetails?.userImageURL.toString()
|
||||
this.led1Sample = led1Sample
|
||||
this.led2Sample = led2Sample
|
||||
this.led1Average = led1Average
|
||||
|
||||
@@ -7,6 +7,7 @@ import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.example.hpostesting.data.CsvWriter
|
||||
import com.example.hpostesting.data.DataHolder
|
||||
import com.example.hpostesting.data.NetworkStatusLiveData
|
||||
import com.example.hpostesting.data.constant.Constants
|
||||
@@ -102,6 +103,9 @@ class HemoCubeViewModel @Inject constructor(
|
||||
testDetails?.deviceRatio = DataHolder.hemoCubeTestData?.deviceRatio
|
||||
testDetails?.calculatedRatio = DataHolder.hemoCubeTestData?.calculatedRatio
|
||||
testDetails?.coefficients = DataHolder.hemoCubeTestData?.coefficients
|
||||
testDetails?.name = DataHolder.hemoCubeTestData?.name.toString()
|
||||
testDetails?.birthYear = DataHolder.hemoCubeTestData?.birthYear.toString()
|
||||
testDetails?.userImageURL = DataHolder.hemoCubeTestData?.userImageURL.toString()
|
||||
testDetails?.classificationResult = DataHolder.hemoCubeTestData?.classificationResult!!
|
||||
}
|
||||
|
||||
@@ -187,6 +191,41 @@ class HemoCubeViewModel @Inject constructor(
|
||||
hemoCubeDao.insertAll(userData)
|
||||
}
|
||||
|
||||
|
||||
fun getLocalUserDataForCsv(context: Context): Boolean {
|
||||
val localUserDataLiveData: LiveData<List<HemoCubeTestData>> = hemoCubeDao.getAll()
|
||||
|
||||
// Observe the LiveData to get the actual data when available
|
||||
localUserDataLiveData.observeForever { localUserData ->
|
||||
localUserData?.let {
|
||||
val csvData = mutableListOf<Array<String>>()
|
||||
|
||||
it.forEach { userData ->
|
||||
csvData.add(
|
||||
arrayOf(
|
||||
userData._id,
|
||||
userData.name,
|
||||
userData.bloodGroup,
|
||||
userData.birthYear,
|
||||
userData.classificationResult,
|
||||
userData.testTime.toString(),
|
||||
userData.userImageURL
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
val csvWriter = CsvWriter(context)
|
||||
csvWriter.writeCsv("userData.csv", csvData)
|
||||
|
||||
// Remove the observer to avoid leaks
|
||||
localUserDataLiveData.removeObserver {}
|
||||
}
|
||||
}
|
||||
|
||||
return true // Assuming success, you might want to modify this based on your actual logic
|
||||
}
|
||||
|
||||
|
||||
fun deleteById(userId: String) = viewModelScope.launch {
|
||||
hemoCubeDao.deleteById(id = userId)
|
||||
}
|
||||
|
||||
@@ -100,6 +100,16 @@
|
||||
app:layout_constraintTop_toBottomOf="@+id/btn_submit"
|
||||
tools:listitem="@layout/offline_user_list_view" />
|
||||
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/btnSaveLocal"
|
||||
android:layout_width="30dp"
|
||||
android:layout_height="30dp"
|
||||
android:layout_marginEnd="10dp"
|
||||
android:src="@drawable/downloads"
|
||||
app:layout_constraintBottom_toBottomOf="@+id/rv_order_offline"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
|
||||
@@ -32,7 +32,7 @@
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:text="Name: MohamedKaif" />
|
||||
tools:text="Name: Mariya" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/bloodGroup"
|
||||
@@ -47,7 +47,7 @@
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toBottomOf="@id/userID"
|
||||
tools:text="UserID: MohamedKaif" />
|
||||
tools:text="UserID: Mariya" />
|
||||
<TextView
|
||||
android:id="@+id/teststatus"
|
||||
android:layout_width="0dp"
|
||||
|
||||
Reference in New Issue
Block a user