refactoring hemocube fragment and add tests

This commit is contained in:
Pritimay Sarkar
2024-01-25 12:19:40 +05:30
4 changed files with 3 additions and 50 deletions

View File

@@ -76,24 +76,19 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
"APK UPLOAD ${result.data}", "APK UPLOAD ${result.data}",
Toast.LENGTH_SHORT Toast.LENGTH_SHORT
).show() ).show()
// The APK URL LiveData will be updated automatically
} }
is Result.Error -> { is Result.Error -> {
result.exception.let { message -> result.exception.let { message ->
Toast.makeText(this, "An error occurred On Updating App: $message", Toast.LENGTH_LONG) Toast.makeText(this, "An error occurred On Updating App: $message", Toast.LENGTH_LONG)
.show() .show()
} }
} }
is Result.Loading -> { is Result.Loading -> {
} }
else -> { else -> {
} }
} }
} }
@@ -124,12 +119,8 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
} }
private fun initiateUpdate(responseBody: String) { private fun initiateUpdate(responseBody: String) {
// Extract the URL from the ResponseBody
val apkUrl = responseBody val apkUrl = responseBody
// Check if the extracted APK URL is valid
if (!isValidHttpUrl(apkUrl)) { if (!isValidHttpUrl(apkUrl)) {
// Handle the case where the URL is not valid
return return
} }
@@ -146,15 +137,9 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
// val filter = IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE) // val filter = IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
// registerReceiver(downloadReceiver, filter) // registerReceiver(downloadReceiver, filter)
} }
// Function to check if a URL has a valid HTTP/HTTPS scheme
// Function to extract the APK URL from the ResponseBody
private fun extractApkUrl(responseBody: ResponseBody): String { private fun extractApkUrl(responseBody: ResponseBody): String {
// Assuming your ResponseBody contains the APK URL as a string
return responseBody.string() return responseBody.string()
} }
// Function to check if a URL has a valid HTTP/HTTPS scheme
private fun isValidHttpUrl(url: String): Boolean { private fun isValidHttpUrl(url: String): Boolean {
return url.startsWith("http://") || url.startsWith("https://") return url.startsWith("http://") || url.startsWith("https://")
} }
@@ -162,7 +147,6 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
override fun onReceive(context: Context?, intent: Intent?) { override fun onReceive(context: Context?, intent: Intent?) {
val id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1) val id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
if (id == downloadId) { if (id == downloadId) {
// Install the downloaded APK
installApk() installApk()
} }
} }
@@ -198,9 +182,6 @@ class DashboardActivity : AppCompatActivity(), IDataCollector {
// unregisterReceiver(downloadReceiver) // unregisterReceiver(downloadReceiver)
} }
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()

View File

@@ -237,34 +237,6 @@ class HomeFragment : Fragment() {
} }
} }
hemoCubeViewModel.deviceUpdate.observe(viewLifecycleOwner) { response ->
when (response) {
is Result.Success -> {
Toast.makeText(
requireContext(), response.data.toString(), Toast.LENGTH_SHORT
).show()
}
is Result.Error -> {
response.exception.let { message ->
Toast.makeText(
activity,
"An error occurred check update: $message",
Toast.LENGTH_LONG
)
.show()
}
}
is Result.Loading -> {
}
else -> {}
}
}
hemoCubeViewModel.uploadLogs.observe(viewLifecycleOwner) { response -> hemoCubeViewModel.uploadLogs.observe(viewLifecycleOwner) { response ->
when (response) { when (response) {
is Result.Success -> { is Result.Success -> {

View File

@@ -149,11 +149,11 @@ class DeviceProvisionFragment : Fragment() {
private fun handleUsbData() { private fun handleUsbData() {
when { when {
resultData.contains("SNE") -> { resultData.contains("SNE") -> {
val pattern = Regex("HCV-\\d{3}-\\d{4}") val pattern = Regex("HPP1-\\d{4}")
val matchResult = pattern.find(resultData) val matchResult = pattern.find(resultData)
val hardwareId = matchResult?.value val hardwareId = matchResult?.value
if (hardwareId.toString().length == 12) { if (hardwareId.toString().length == 9) {
with(sharedPreferences.edit()) { with(sharedPreferences.edit()) {
putString(Constants.DEVICE_ID, hardwareId) putString(Constants.DEVICE_ID, hardwareId)
apply() apply()

View File

@@ -92,7 +92,7 @@
app:cornerRadius="16dp" app:cornerRadius="16dp"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_calibration" /> app:layout_constraintTop_toBottomOf="@id/btn_deviceInfo" />
<com.google.android.material.button.MaterialButton <com.google.android.material.button.MaterialButton
android:id="@+id/btn_deviceInfo" android:id="@+id/btn_deviceInfo"