csv
This commit is contained in:
@@ -1,4 +0,0 @@
|
||||
<changelist name="Uncommitted_changes_before_Checkout_at_04-12-2023_02_50_pm_[Changes]" date="1701681655205" recycled="true" deleted="true">
|
||||
<option name="PATH" value="$PROJECT_DIR$/.idea/shelf/Uncommitted_changes_before_Checkout_at_04-12-2023_02_50_pm_[Changes]/shelved.patch" />
|
||||
<option name="DESCRIPTION" value="Uncommitted changes before Checkout at 04-12-2023 02:50 pm [Changes]" />
|
||||
</changelist>
|
||||
@@ -91,10 +91,12 @@ dependencies {
|
||||
implementation 'com.github.bumptech.glide:glide:4.13.2'
|
||||
annotationProcessor 'com.github.bumptech.glide:compiler:4.13.2'
|
||||
|
||||
implementation 'com.opencsv:opencsv:5.5'
|
||||
|
||||
//Room
|
||||
implementation "androidx.room:room-ktx:2.6.0"
|
||||
implementation "androidx.room:room-runtime:2.6.0"
|
||||
kapt ("androidx.room:room-compiler:2.6.0")
|
||||
implementation "androidx.room:room-ktx:2.6.1"
|
||||
implementation "androidx.room:room-runtime:2.6.1"
|
||||
kapt ("androidx.room:room-compiler:2.6.1")
|
||||
|
||||
// Barcode scanner
|
||||
implementation 'com.journeyapps:zxing-android-embedded:4.3.0'
|
||||
|
||||
@@ -7,7 +7,9 @@
|
||||
android:required="false" />
|
||||
|
||||
<uses-permission android:name="android.permission.CAMERA" />
|
||||
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
|
||||
@@ -43,7 +45,7 @@
|
||||
</activity>
|
||||
<activity
|
||||
android:name="com.journeyapps.barcodescanner.CaptureActivity"
|
||||
android:screenOrientation="portrait"
|
||||
android:screenOrientation="fullSensor"
|
||||
android:stateNotNeeded="true"
|
||||
tools:replace="android:screenOrientation" />
|
||||
</application>
|
||||
|
||||
@@ -5,7 +5,7 @@ import androidx.room.RoomDatabase
|
||||
import androidx.room.TypeConverters
|
||||
import com.example.hposregistration.data.user.UserData
|
||||
|
||||
@Database(entities = [UserData::class], version = 1, exportSchema = false)
|
||||
@Database(entities = [UserData::class], version = 3, exportSchema = false)
|
||||
abstract class MyDatabase : RoomDatabase() {
|
||||
abstract fun userDao(): UserDao
|
||||
}
|
||||
@@ -20,4 +20,7 @@ interface UserDao {
|
||||
|
||||
@Query("DELETE FROM user_table WHERE _id = :id")
|
||||
suspend fun deleteById(id: String)
|
||||
|
||||
@Query("UPDATE user_table SET isCSVCreated = :newValue WHERE _id = :id")
|
||||
suspend fun updateCSVFieldById(id: String, newValue: Boolean)
|
||||
}
|
||||
@@ -30,4 +30,8 @@ class MainRepository @Inject constructor(
|
||||
return userFireBaseDao.addUserToDatabase(userData)
|
||||
}
|
||||
|
||||
suspend fun updateCSVFieldById(id: String, isCSVCreated: Boolean) {
|
||||
userDao.updateCSVFieldById(id, isCSVCreated)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
package com.example.hpostesting.data.datasource
|
||||
|
||||
import android.os.Environment
|
||||
import com.example.hposregistration.data.user.UserData
|
||||
|
||||
import com.opencsv.CSVWriter
|
||||
import java.io.File
|
||||
import java.io.FileWriter
|
||||
import java.io.IOException
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
|
||||
fun exportDataToCSV(
|
||||
fileName: String, dataList: List<UserData>
|
||||
): Boolean {
|
||||
try {
|
||||
val formattedFileName = fileName.replace(Regex("[^a-zA-Z0-9.-]"), "_") // Replace special characters with underscores
|
||||
val filePath = File(getExternalStorageDirectory(), formattedFileName)
|
||||
val writer = FileWriter(filePath)
|
||||
val csvWriter = CSVWriter(writer)
|
||||
// Write CSV header
|
||||
val header = arrayOf(
|
||||
"_id",
|
||||
"name",
|
||||
"bloodGroup",
|
||||
"birthYear", // Include other fields from the data class
|
||||
"state",
|
||||
"abhaId",
|
||||
"userImageURL",
|
||||
"testStatus", // Assuming testStatus is a boolean field
|
||||
"gender",
|
||||
"appVersion",
|
||||
"phoneNumber",
|
||||
"careOf",
|
||||
"maritalStatus",
|
||||
"category",
|
||||
"caste",
|
||||
"subCaste",
|
||||
"house",
|
||||
"city",
|
||||
"district",
|
||||
"state",
|
||||
"pinCode",
|
||||
"isUnderMedication",
|
||||
"isUnderTransfusion",
|
||||
"sickleCellHistory",
|
||||
"userImageURL",
|
||||
"registeredBy",
|
||||
"createdAt",
|
||||
"registrationCenterName"
|
||||
|
||||
)
|
||||
csvWriter.writeNext(header)
|
||||
|
||||
// Write data rows
|
||||
for (data in dataList) {
|
||||
val row = arrayOf(
|
||||
data._id,
|
||||
data.name,
|
||||
data.bloodGroup,
|
||||
data.birthYear ?: "", // Replace with the actual birthYear field
|
||||
data.state,
|
||||
data.abhaId,
|
||||
data.userImageURL,
|
||||
data.testStatus?.toString() ?: "",
|
||||
data.gender,
|
||||
data.appVersion ?: "",
|
||||
data.phoneNumber,
|
||||
data.careOf,
|
||||
data.maritalStatus,
|
||||
data.category,
|
||||
data.caste,
|
||||
data.subCaste ?: "",
|
||||
data.house,
|
||||
data.city,
|
||||
data.district,
|
||||
data.state,
|
||||
data.pinCode,
|
||||
data.isUnderMedication?.toString() ?: "",
|
||||
data.isUnderTransfusion?.toString() ?: "",
|
||||
data.sickleCellHistory ?: "",
|
||||
data.userImageURL,
|
||||
data.registeredBy ?: "",
|
||||
data.createdAt ?: "",
|
||||
data.registrationCenterName ?: ""
|
||||
|
||||
)
|
||||
csvWriter.writeNext(row)
|
||||
}
|
||||
writer.close()
|
||||
return true
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
private fun getExternalStorageDirectory(): File {
|
||||
val folder = File(Environment.getExternalStorageDirectory(), "HPOSRegistration")
|
||||
|
||||
if (!folder.exists()) {
|
||||
folder.mkdirs()
|
||||
}
|
||||
|
||||
return folder
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -32,7 +32,9 @@ data class UserData(
|
||||
var registeredBy: String? = "",
|
||||
var createdAt: String? = "",
|
||||
var registrationCenterName: String? = "",
|
||||
var testStatus: Boolean = false
|
||||
var testStatus: Boolean = false,
|
||||
var isCSVCreated: Boolean = false,
|
||||
var localFlag: Boolean = false,
|
||||
){
|
||||
enum class Gender {
|
||||
MALE,
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.content.Context
|
||||
import androidx.room.Room
|
||||
import com.example.hposregistration.dao.MyDatabase
|
||||
import com.example.hposregistration.dao.UserDao
|
||||
import com.example.hpostesting.data.datasource.LocalFileDataSource
|
||||
import dagger.Module
|
||||
import dagger.Provides
|
||||
import dagger.hilt.InstallIn
|
||||
@@ -41,4 +42,10 @@ object AppModule {
|
||||
fun provideContext(application: Application): Context {
|
||||
return application.applicationContext
|
||||
}
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
fun provideLocalFileDataSource(): LocalFileDataSource {
|
||||
return LocalFileDataSource()
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.example.hposregistration.network
|
||||
|
||||
import androidx.room.Query
|
||||
import com.example.hposregistration.data.Response
|
||||
import com.example.hposregistration.data.user.UserData
|
||||
import com.google.firebase.firestore.FirebaseFirestore
|
||||
@@ -24,6 +25,8 @@ class UserDao {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// suspend fun getPatientList(): List<UserData> {
|
||||
// return patientCollection.get().await().toObjects(UserData::class.java)
|
||||
// }
|
||||
|
||||
@@ -5,11 +5,13 @@ import android.content.Context
|
||||
import android.content.DialogInterface
|
||||
import android.content.SharedPreferences
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Button
|
||||
import android.widget.Toast
|
||||
import androidx.fragment.app.activityViewModels
|
||||
import androidx.fragment.app.viewModels
|
||||
import androidx.navigation.fragment.findNavController
|
||||
import com.example.hposregistration.adapters.UserListOfflineAdapter
|
||||
@@ -30,7 +32,7 @@ class UserListFragment : BaseFragment() {
|
||||
private var _binding: FragmentUserListBinding? = null
|
||||
private val binding get() = _binding!!
|
||||
|
||||
private val viewModel by viewModels<RegistrationViewModel>()
|
||||
private val viewModel:RegistrationViewModel by activityViewModels()
|
||||
|
||||
private var isOnline = false
|
||||
|
||||
@@ -81,6 +83,10 @@ class UserListFragment : BaseFragment() {
|
||||
showUploadDialog(requireContext())
|
||||
}
|
||||
|
||||
binding.btnSaveLocal.setOnClickListener {
|
||||
showDownloadDialog(requireContext())
|
||||
}
|
||||
|
||||
binding.btnsettings.setOnClickListener{
|
||||
findNavController().navigate(R.id.action_userListFragment_to_languageSettingsFragment)
|
||||
|
||||
@@ -174,6 +180,25 @@ class UserListFragment : BaseFragment() {
|
||||
adapter.startListening()
|
||||
}
|
||||
|
||||
|
||||
private fun showDownloadDialog(context: Context) {
|
||||
val builder = AlertDialog.Builder(context)
|
||||
builder.setTitle(R.string.download_db_registration_title)
|
||||
builder.setMessage(R.string.download_db_registration_message)
|
||||
|
||||
builder.setPositiveButton(R.string.downloadcsv) { dialog, _ ->
|
||||
downloadLocalDBData(dialog)
|
||||
}
|
||||
|
||||
builder.setNegativeButton(R.string.cancel) { dialog, _ ->
|
||||
dialog.dismiss()
|
||||
}
|
||||
|
||||
val dialog = builder.create()
|
||||
dialog.show()
|
||||
}
|
||||
|
||||
|
||||
private fun fetchFromLocalDB() {
|
||||
binding.uploadData.visibility = View.GONE
|
||||
viewModel.allUserData.observe(viewLifecycleOwner) { userDataList ->
|
||||
@@ -196,6 +221,40 @@ class UserListFragment : BaseFragment() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun downloadLocalDBData(dialog: DialogInterface) {
|
||||
viewModel.allUserData.observe(viewLifecycleOwner) { userDataList ->
|
||||
try {
|
||||
val downloadList = mutableListOf<UserData>()
|
||||
|
||||
userDataList.forEach { userData ->
|
||||
// Log details for debugging
|
||||
Log.d("DownloadDebug", "userData: $userData, isCSVCreated: ${userData.isCSVCreated}")
|
||||
|
||||
if (userData.isCSVCreated) {
|
||||
downloadList.add(userData)
|
||||
}
|
||||
}
|
||||
// Log the size of downloadList for debugging
|
||||
Log.d("DownloadDebug", "downloadList size: ${downloadList.size}")
|
||||
|
||||
if (downloadList.isNotEmpty()) {
|
||||
// Call ViewModel function to create CSV with filtered data
|
||||
viewModel.createCSV(downloadList, requireContext())
|
||||
Toast.makeText(requireContext(), "CSV file downloaded successfully", Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
Toast.makeText(requireContext(), "No data to download", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
Toast.makeText(requireContext(), "Error downloading CSV file", Toast.LENGTH_SHORT).show()
|
||||
} finally {
|
||||
dialog.dismiss()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun deteleIncompleteRegistration(userDataList: List<UserData>) {
|
||||
userDataList.forEach {
|
||||
if (it.userImageURL.isEmpty()) {
|
||||
|
||||
@@ -10,14 +10,21 @@ import com.example.hposregistration.data.MainRepository
|
||||
import com.example.hposregistration.data.NetworkStatusLiveData
|
||||
import com.example.hposregistration.data.Response
|
||||
import com.example.hposregistration.data.user.UserData
|
||||
import com.example.hposregistration.dao.UserDao
|
||||
import com.example.hpostesting.data.datasource.LocalFileDataSource
|
||||
import com.google.firebase.storage.FirebaseStorage
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.launch
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
class RegistrationViewModel @Inject constructor(
|
||||
private val mainRepository: MainRepository,
|
||||
private val userdao: UserDao,
|
||||
private val localFileDataSource: LocalFileDataSource,
|
||||
context: Context
|
||||
) : ViewModel() {
|
||||
|
||||
@@ -73,4 +80,24 @@ class RegistrationViewModel @Inject constructor(
|
||||
fun deleteById(userId: String) = viewModelScope.launch {
|
||||
mainRepository.deleteById(id = userId)
|
||||
}
|
||||
|
||||
fun createCSV(userData: List<UserData>, appContext: Context) =
|
||||
viewModelScope.launch {
|
||||
val fileName = "${getCurrentDate()}.csv"
|
||||
if (localFileDataSource.exportDataToCSV(fileName, userData)) {
|
||||
userData.forEach { data ->
|
||||
data.localFlag = true
|
||||
mainRepository.updateCSVFieldById(
|
||||
data._id,
|
||||
true
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getCurrentDate(): String {
|
||||
val dateFormat = SimpleDateFormat("dd-MM-yy", Locale.getDefault())
|
||||
val currentDate = Date()
|
||||
return dateFormat.format(currentDate)
|
||||
}
|
||||
}
|
||||
5
app/src/main/res/drawable/downloads.xml
Normal file
5
app/src/main/res/drawable/downloads.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<vector android:height="24dp" android:tint="@color/primary"
|
||||
android:viewportHeight="24" android:viewportWidth="24"
|
||||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<path android:fillColor="@color/primary" android:pathData="M19,9h-4V3H9v6H5l7,7 7,-7zM5,18v2h14v-2H5z"/>
|
||||
</vector>
|
||||
@@ -80,6 +80,15 @@
|
||||
app:layout_constraintTop_toBottomOf="@+id/tv_title"
|
||||
tools:listitem="@layout/user_item_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"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
<com.google.android.material.floatingactionbutton.FloatingActionButton
|
||||
android:id="@+id/btnRegisterUser"
|
||||
android:layout_width="wrap_content"
|
||||
|
||||
@@ -144,7 +144,10 @@
|
||||
<string name="scanned_aadhar_id">You have scanned the Aadhar ID, need to enter Aadhar ID number you have scanned and Abha ID manually</string>
|
||||
<string name="scanned_abha_id">You have scanned the Abha ID</string>
|
||||
<string name="scanned_aadharid">You have scanned the Aadhar ID</string>
|
||||
|
||||
<string name="downloadcsv">Download CSV</string>
|
||||
<string name="download_db_registration_title">Download DB Tests</string>
|
||||
<string name="download_db_registration_message">Do you want to download the local DB tests in CSV format?</string>
|
||||
<string name="upload_success_message">Upload done successfully</string>
|
||||
<string-array name="category">
|
||||
<item>NT-A</item>
|
||||
<item>NT-B</item>
|
||||
|
||||
Reference in New Issue
Block a user