diff --git a/app/src/main/java/com/example/hpostesting/presentation/adapter/OfflineUserListAdapter.kt b/app/src/main/java/com/example/hpostesting/presentation/adapter/OfflineUserListAdapter.kt index 3e72898..82cb361 100644 --- a/app/src/main/java/com/example/hpostesting/presentation/adapter/OfflineUserListAdapter.kt +++ b/app/src/main/java/com/example/hpostesting/presentation/adapter/OfflineUserListAdapter.kt @@ -2,6 +2,7 @@ package com.example.hpostesting.presentation.adapter import android.annotation.SuppressLint import android.content.Context +import android.content.res.Resources import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -27,7 +28,9 @@ class OfflineUserListAdapter(private val view: View, private val batLevel: Int) RecyclerView.Adapter() { inner class OfflineUserListViewHolder(val binding: OfflineUserListViewBinding) : - RecyclerView.ViewHolder(binding.root) + RecyclerView.ViewHolder(binding.root) { + + } private val differCallback = object : DiffUtil.ItemCallback() { override fun areItemsTheSame( @@ -70,55 +73,35 @@ class OfflineUserListAdapter(private val view: View, private val batLevel: Int) }" userCard.setOnClickListener { if (batLevel < 40) { - Toast.makeText( - view.context, - context?.getString(R.string.low_battery_warning), - Toast.LENGTH_SHORT - ).show() - return@setOnClickListener - } - if (userList.testStatus != null) { - if (userList.testStatus!!) { - Toast.makeText( - view.context, - context?.getString(R.string.test_already_conducted), - Toast.LENGTH_SHORT - ).show() - } else { - if (userList.incubationTime != "") { - if (isBetween15And30Minutes(userList.incubationTime) < Constants.INCUBATION_TIME_MIN) { - Toast.makeText( - view.context, - context?.getString(R.string.incubation_not_completed), - Toast.LENGTH_SHORT - ).show() - } else if (isBetween15And30Minutes(userList.incubationTime) > Constants.INCUBATION_TIME_MAX) { - Toast.makeText( - view.context, - context?.getString(R.string.incubation_crossed_30_minutes), - Toast.LENGTH_SHORT - ).show() - } else { - DataHolder.selectedTest = UserData( - _id = userList._id, - bloodGroup = userList.bloodGroup, - incubationTime = userList.incubationTime - ) - view.findNavController() - .navigate(R.id.action_nav_home_to_mainActivity) - } + showWarningToast(R.string.low_battery_warning) + } else { + if (userList.testStatus != null) { + if (userList.testStatus!!) { + showWarningToast(R.string.test_already_conducted) } else { - Toast.makeText( - view.context, - context?.getString(R.string.incubation_not_started), - Toast.LENGTH_SHORT - ).show() + if (userList.incubationTime != "") { + val incubationTimeDiff = isBetween15And30Minutes(userList.incubationTime) + if (incubationTimeDiff < 15) { + showWarningToast(R.string.incubation_not_completed) + } else if (incubationTimeDiff > 30) { + showWarningToast(R.string.incubation_crossed_30_minutes) + } else { + DataHolder.selectedTest = UserData( + _id = userList._id, + bloodGroup = userList.bloodGroup, + incubationTime = userList.incubationTime + ) + view.findNavController() + .navigate(R.id.action_nav_home_to_mainActivity) + } + } else { + showWarningToast(R.string.incubation_not_started) + } } } } } - } } @@ -132,4 +115,25 @@ class OfflineUserListAdapter(private val view: View, private val batLevel: Int) return diffMillis / (60 * 1000) } + + + private fun showWarningToast(resId: Int) { + val contextToUse = view.context ?: return + + val message = try { + contextToUse.getString(resId) + } catch (e: Resources.NotFoundException) { + "Resource not found for ID: $resId" + } + + Toast.makeText( + contextToUse, + message, + Toast.LENGTH_SHORT + ).show() + } + + + + } \ No newline at end of file