From 83c8295e7cb99c33743a8ce3d2d0dc5bc9229044 Mon Sep 17 00:00:00 2001 From: chaneylc Date: Tue, 28 Sep 2021 12:38:00 -0500 Subject: [PATCH 1/3] Wrapped ui calls within timer tasks with isAdded to ensure fragment is attached to activity. --- .../fragments/ExperimentListFragment.kt | 14 ++++++++------ .../prospector/fragments/NewSampleFragment.kt | 16 +++++++++------- .../prospector/fragments/SampleListFragment.kt | 11 +++++++---- .../prospector/fragments/ScanListFragment.kt | 16 +++++++++------- 4 files changed, 33 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/org/phenoapps/prospector/fragments/ExperimentListFragment.kt b/app/src/main/java/org/phenoapps/prospector/fragments/ExperimentListFragment.kt index b5d7d20..58c721d 100644 --- a/app/src/main/java/org/phenoapps/prospector/fragments/ExperimentListFragment.kt +++ b/app/src/main/java/org/phenoapps/prospector/fragments/ExperimentListFragment.kt @@ -228,14 +228,16 @@ class ExperimentListFragment : Fragment(), CoroutineScope by MainScope() { activity?.runOnUiThread { - with(mBinding?.experimentToolbar) { + if (isAdded) { + with(mBinding?.experimentToolbar) { - this?.menu?.findItem(R.id.action_connection) - ?.setIcon( - if (sDeviceViewModel.isConnected()) R.drawable.ic_vector_link - else R.drawable.ic_vector_difference_ab - ) + this?.menu?.findItem(R.id.action_connection) + ?.setIcon( + if (sDeviceViewModel.isConnected()) R.drawable.ic_vector_link + else R.drawable.ic_vector_difference_ab + ) + } } } } diff --git a/app/src/main/java/org/phenoapps/prospector/fragments/NewSampleFragment.kt b/app/src/main/java/org/phenoapps/prospector/fragments/NewSampleFragment.kt index abc9448..6023dce 100644 --- a/app/src/main/java/org/phenoapps/prospector/fragments/NewSampleFragment.kt +++ b/app/src/main/java/org/phenoapps/prospector/fragments/NewSampleFragment.kt @@ -245,13 +245,15 @@ class NewSampleFragment : Fragment(), CoroutineScope by MainScope() { activity?.runOnUiThread { - with (activity as? MainActivity) { - mBinding?.fragNewSampleToolbar?.menu?.findItem(R.id.action_connection) - ?.setIcon(if (this?.sDeviceViewModel?.isConnected() == true){ - attachDeviceButtonPressListener() - R.drawable.ic_vector_link - } - else R.drawable.ic_vector_difference_ab) + if (isAdded) { + with (activity as? MainActivity) { + mBinding?.fragNewSampleToolbar?.menu?.findItem(R.id.action_connection) + ?.setIcon(if (this?.sDeviceViewModel?.isConnected() == true){ + attachDeviceButtonPressListener() + R.drawable.ic_vector_link + } + else R.drawable.ic_vector_difference_ab) + } } } } diff --git a/app/src/main/java/org/phenoapps/prospector/fragments/SampleListFragment.kt b/app/src/main/java/org/phenoapps/prospector/fragments/SampleListFragment.kt index 6c02104..560c496 100644 --- a/app/src/main/java/org/phenoapps/prospector/fragments/SampleListFragment.kt +++ b/app/src/main/java/org/phenoapps/prospector/fragments/SampleListFragment.kt @@ -5,6 +5,7 @@ import ALPHA_DESC import CONVERT_TO_WAVELENGTHS import DATE_ASC import DATE_DESC +import android.content.Context import android.os.Bundle import android.os.Handler import android.os.Looper @@ -404,12 +405,14 @@ class SampleListFragment : Fragment(), CoroutineScope by MainScope(), activity?.runOnUiThread { - with(mBinding?.samplesToolbar) { + if (isAdded) { + with(mBinding?.samplesToolbar) { - this?.menu?.findItem(R.id.action_connection) - ?.setIcon(if (sDeviceViewModel.isConnected()) R.drawable.ic_vector_link - else R.drawable.ic_vector_difference_ab) + this?.menu?.findItem(R.id.action_connection) + ?.setIcon(if (sDeviceViewModel.isConnected()) R.drawable.ic_vector_link + else R.drawable.ic_vector_difference_ab) + } } } } diff --git a/app/src/main/java/org/phenoapps/prospector/fragments/ScanListFragment.kt b/app/src/main/java/org/phenoapps/prospector/fragments/ScanListFragment.kt index 67afcb5..8fc939d 100644 --- a/app/src/main/java/org/phenoapps/prospector/fragments/ScanListFragment.kt +++ b/app/src/main/java/org/phenoapps/prospector/fragments/ScanListFragment.kt @@ -544,15 +544,17 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli activity?.runOnUiThread { - with(mBinding?.titleToolbar) { + if (isAdded) { + with(mBinding?.titleToolbar) { - this?.menu?.findItem(R.id.action_connection) - ?.setIcon(if (sDeviceViewModel.isConnected()) { - attachDeviceButtonPressListener() - R.drawable.ic_vector_link - } - else R.drawable.ic_vector_difference_ab) + this?.menu?.findItem(R.id.action_connection) + ?.setIcon(if (sDeviceViewModel.isConnected()) { + attachDeviceButtonPressListener() + R.drawable.ic_vector_link + } + else R.drawable.ic_vector_difference_ab) + } } } } From 1fd14b4eec8d8d79b496232d9fbd34291828c9b7 Mon Sep 17 00:00:00 2001 From: chaneylc Date: Tue, 28 Sep 2021 17:04:13 -0500 Subject: [PATCH 2/3] various fixes --- .../data/viewmodels/DeviceViewModel.kt | 11 +- .../fragments/BarcodeSearchFragment.kt | 7 +- .../fragments/ConnectInstructionsFragment.kt | 2 - .../fragments/ExperimentListFragment.kt | 32 +++- .../prospector/fragments/NewSampleFragment.kt | 57 ++++--- .../fragments/SampleListFragment.kt | 58 +++++-- .../prospector/fragments/ScanListFragment.kt | 149 +++++++++++------- 7 files changed, 204 insertions(+), 112 deletions(-) diff --git a/app/src/main/java/org/phenoapps/prospector/data/viewmodels/DeviceViewModel.kt b/app/src/main/java/org/phenoapps/prospector/data/viewmodels/DeviceViewModel.kt index 005be0b..9088c55 100644 --- a/app/src/main/java/org/phenoapps/prospector/data/viewmodels/DeviceViewModel.kt +++ b/app/src/main/java/org/phenoapps/prospector/data/viewmodels/DeviceViewModel.kt @@ -141,7 +141,7 @@ class DeviceViewModel @Inject constructor() : ViewModel() { // } // } - fun scan(context: Context) = liveData> { + fun scan(context: Context) = liveData { with (manager(context)) { @@ -157,13 +157,12 @@ class DeviceViewModel @Inject constructor() : ViewModel() { } - private suspend fun scan(ledFrames: Int, bulbFrames: Int) = withContext(sDeviceScope.coroutineContext) { + private val mFrames = ArrayList() + private suspend fun scan(ledFrames: Int, bulbFrames: Int) = withContext>(sDeviceScope.coroutineContext) { - val frames = ArrayList() + sDevice?.Scan(ledFrames, bulbFrames, mFrames) - sDevice?.Scan(ledFrames, bulbFrames, frames) - - return@withContext frames + return@withContext mFrames } diff --git a/app/src/main/java/org/phenoapps/prospector/fragments/BarcodeSearchFragment.kt b/app/src/main/java/org/phenoapps/prospector/fragments/BarcodeSearchFragment.kt index 23ec5c9..508b8f3 100644 --- a/app/src/main/java/org/phenoapps/prospector/fragments/BarcodeSearchFragment.kt +++ b/app/src/main/java/org/phenoapps/prospector/fragments/BarcodeSearchFragment.kt @@ -99,9 +99,10 @@ class BarcodeSearchFragment : Fragment() { mSamples.find { it.name == result.text.toString() }?.name?.let { name -> - findNavController().navigate(BarcodeSearchFragmentDirections - .actionToScanList(mExpId, name)) - + if (findNavController().currentDestination?.id == R.id.barcode_search_fragment) { + findNavController().navigate(BarcodeSearchFragmentDirections + .actionToScanList(mExpId, name)) + } } } else { diff --git a/app/src/main/java/org/phenoapps/prospector/fragments/ConnectInstructionsFragment.kt b/app/src/main/java/org/phenoapps/prospector/fragments/ConnectInstructionsFragment.kt index 53825a6..36f316d 100644 --- a/app/src/main/java/org/phenoapps/prospector/fragments/ConnectInstructionsFragment.kt +++ b/app/src/main/java/org/phenoapps/prospector/fragments/ConnectInstructionsFragment.kt @@ -7,8 +7,6 @@ import android.view.ViewGroup import androidx.appcompat.view.ContextThemeWrapper import androidx.databinding.DataBindingUtil import androidx.fragment.app.Fragment -import dagger.hilt.android.AndroidEntryPoint -import dagger.hilt.android.WithFragmentBindings import org.phenoapps.prospector.R import org.phenoapps.prospector.databinding.FragmentConnectInstructionsBinding diff --git a/app/src/main/java/org/phenoapps/prospector/fragments/ExperimentListFragment.kt b/app/src/main/java/org/phenoapps/prospector/fragments/ExperimentListFragment.kt index 58c721d..f00552f 100644 --- a/app/src/main/java/org/phenoapps/prospector/fragments/ExperimentListFragment.kt +++ b/app/src/main/java/org/phenoapps/prospector/fragments/ExperimentListFragment.kt @@ -7,7 +7,6 @@ import DATE_DESC import android.os.Bundle import android.view.* import android.widget.Toast -import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AlertDialog import androidx.appcompat.view.ContextThemeWrapper import androidx.databinding.DataBindingUtil @@ -26,7 +25,6 @@ import kotlinx.coroutines.launch import org.phenoapps.prospector.R import org.phenoapps.prospector.activities.MainActivity import org.phenoapps.prospector.adapter.ExperimentAdapter -import org.phenoapps.prospector.adapter.SampleAdapter import org.phenoapps.prospector.data.viewmodels.DeviceViewModel import org.phenoapps.prospector.data.viewmodels.ExperimentViewModel import org.phenoapps.prospector.databinding.FragmentExperimentListBinding @@ -54,14 +52,18 @@ class ExperimentListFragment : Fragment(), CoroutineScope by MainScope() { */ private val sOnNewExpClick = View.OnClickListener { - findNavController().navigate(ExperimentListFragmentDirections + if (findNavController().currentDestination?.id == R.id.experiment_list_fragment) { + findNavController().navigate(ExperimentListFragmentDirections .actionToNewExperiment()) + } } private var mBinding: FragmentExperimentListBinding? = null private var mSortState = ALPHA_ASC + private var mTimer: Timer? = null + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { val contextThemeWrapper = ContextThemeWrapper(activity, R.style.AppTheme) @@ -76,6 +78,8 @@ class ExperimentListFragment : Fragment(), CoroutineScope by MainScope() { ui.setupToolbar() + startTimer() + setupButtons() updateUi() @@ -125,8 +129,10 @@ class ExperimentListFragment : Fragment(), CoroutineScope by MainScope() { } else { - findNavController().navigate(ExperimentListFragmentDirections - .actionToConnectInstructions()) + if (findNavController().currentDestination?.id == R.id.experiment_list_fragment) { + findNavController().navigate(ExperimentListFragmentDirections + .actionToConnectInstructions()) + } (activity as? MainActivity)?.startDeviceConnection() } @@ -220,6 +226,9 @@ class ExperimentListFragment : Fragment(), CoroutineScope by MainScope() { mBinding?.recyclerView?.adapter?.notifyItemRangeChanged(0, it.size) }) + } + + private fun startTimer() { //use the activity view model to access the current connection status val check = object : TimerTask() { @@ -243,11 +252,18 @@ class ExperimentListFragment : Fragment(), CoroutineScope by MainScope() { } } - Timer().cancel() + mTimer = Timer() + + mTimer?.scheduleAtFixedRate(check, 0, 1500) + } + + override fun onDestroy() { + super.onDestroy() - Timer().purge() + mTimer?.cancel() - Timer().scheduleAtFixedRate(check, 0, 1500) + mTimer?.purge() + mTimer = null } } \ No newline at end of file diff --git a/app/src/main/java/org/phenoapps/prospector/fragments/NewSampleFragment.kt b/app/src/main/java/org/phenoapps/prospector/fragments/NewSampleFragment.kt index 6023dce..300426a 100644 --- a/app/src/main/java/org/phenoapps/prospector/fragments/NewSampleFragment.kt +++ b/app/src/main/java/org/phenoapps/prospector/fragments/NewSampleFragment.kt @@ -24,7 +24,6 @@ import org.phenoapps.prospector.data.models.Sample import org.phenoapps.prospector.data.viewmodels.SampleViewModel import org.phenoapps.prospector.databinding.FragmentNewSampleBinding import org.phenoapps.prospector.utils.Dialogs -import org.phenoapps.prospector.utils.SnackbarQueue import org.phenoapps.prospector.utils.observeOnce import java.lang.IllegalStateException import java.util.* @@ -49,6 +48,8 @@ class NewSampleFragment : Fragment(), CoroutineScope by MainScope() { arguments?.getString("note") } + private var mTimer: Timer? = null + private val sOnBarcodeScanClick = View.OnClickListener { setFragmentResultListener("BarcodeResult") { _, bundle -> @@ -59,9 +60,10 @@ class NewSampleFragment : Fragment(), CoroutineScope by MainScope() { } - findNavController().navigate(NewSampleFragmentDirections + if (findNavController().currentDestination?.id == R.id.new_sample_fragment) { + findNavController().navigate(NewSampleFragmentDirections .actionToBarcodeScanner()) - + } } @@ -96,6 +98,8 @@ class NewSampleFragment : Fragment(), CoroutineScope by MainScope() { setupButtons() + startTimer() + binding.setupToolbar() //possible arguments sent if the sample is being edited @@ -213,9 +217,11 @@ class NewSampleFragment : Fragment(), CoroutineScope by MainScope() { if (it) { - findNavController() - .navigate(NewSampleFragmentDirections - .actionToScanList(mExpId, name)) + if (findNavController().currentDestination?.id == R.id.new_sample_fragment) { + findNavController() + .navigate(NewSampleFragmentDirections + .actionToScanList(mExpId, name)) + } } else { @@ -236,7 +242,7 @@ class NewSampleFragment : Fragment(), CoroutineScope by MainScope() { } } - private fun startObservers() { + private fun startTimer() { //use the activity view model to access the current connection status val check = object : TimerTask() { @@ -259,11 +265,12 @@ class NewSampleFragment : Fragment(), CoroutineScope by MainScope() { } } - Timer().cancel() + mTimer = Timer() - Timer().purge() + mTimer?.scheduleAtFixedRate(check, 0, 1500) + } - Timer().scheduleAtFixedRate(check, 0, 1500) + private fun startObservers() { //set the title header sViewModel.experiments.observe(viewLifecycleOwner, { experiments -> @@ -299,9 +306,11 @@ class NewSampleFragment : Fragment(), CoroutineScope by MainScope() { sDeviceViewModel.reset() } else { - findNavController().navigate( - NewSampleFragmentDirections - .actionToConnectInstructions()) + if (findNavController().currentDestination?.id == R.id.new_sample_fragment) { + findNavController().navigate( + NewSampleFragmentDirections + .actionToConnectInstructions()) + } this?.startDeviceConnection() } @@ -334,13 +343,15 @@ class NewSampleFragment : Fragment(), CoroutineScope by MainScope() { (act as MainActivity).notify("$newSampleString $name") - if (startScan) { + if (findNavController().currentDestination?.id == R.id.new_sample_fragment) { + if (startScan) { - findNavController().navigate(NewSampleFragmentDirections - .actionToScanList(mExpId, name, startScan)) + findNavController().navigate(NewSampleFragmentDirections + .actionToScanList(mExpId, name, startScan)) - } else findNavController().navigate(NewSampleFragmentDirections - .actionToScanList(mExpId, name)) + } else findNavController().navigate(NewSampleFragmentDirections + .actionToScanList(mExpId, name)) + } } } } @@ -371,4 +382,14 @@ class NewSampleFragment : Fragment(), CoroutineScope by MainScope() { mBinding?.onBarcodeScanClick = sOnBarcodeScanClick } + + override fun onDestroy() { + super.onDestroy() + + mTimer?.cancel() + + mTimer?.purge() + + mTimer = null + } } \ No newline at end of file diff --git a/app/src/main/java/org/phenoapps/prospector/fragments/SampleListFragment.kt b/app/src/main/java/org/phenoapps/prospector/fragments/SampleListFragment.kt index 560c496..7cfbd95 100644 --- a/app/src/main/java/org/phenoapps/prospector/fragments/SampleListFragment.kt +++ b/app/src/main/java/org/phenoapps/prospector/fragments/SampleListFragment.kt @@ -5,7 +5,6 @@ import ALPHA_DESC import CONVERT_TO_WAVELENGTHS import DATE_ASC import DATE_DESC -import android.content.Context import android.os.Bundle import android.os.Handler import android.os.Looper @@ -69,11 +68,14 @@ class SampleListFragment : Fragment(), CoroutineScope by MainScope(), private var mBinding: FragmentSampleListBinding? = null + private var mTimer: Timer? = null + private val sOnNewClickListener = View.OnClickListener { - findNavController().navigate(SampleListFragmentDirections + if (findNavController().currentDestination?.id == R.id.sample_list_fragment) { + findNavController().navigate(SampleListFragmentDirections .actionToNewSample(mExpId)) - + } } /** @@ -93,17 +95,20 @@ class SampleListFragment : Fragment(), CoroutineScope by MainScope(), sViewModel.insertSampleAsync(sample).await() activity?.runOnUiThread { - findNavController().navigate(SampleListFragmentDirections - .actionToScanList(mExpId, sample.name)) + if (findNavController().currentDestination?.id == R.id.sample_list_fragment) { + findNavController().navigate(SampleListFragmentDirections + .actionToScanList(mExpId, sample.name)) + } } } updateUi() } - findNavController().navigate(SampleListFragmentDirections - .actionToBarcodeScan()) - + if (findNavController().currentDestination?.id == R.id.sample_list_fragment) { + findNavController().navigate(SampleListFragmentDirections + .actionToBarcodeScan()) + } } private lateinit var mSnackbar: SnackbarQueue @@ -227,6 +232,8 @@ class SampleListFragment : Fragment(), CoroutineScope by MainScope(), ui.setupToolbar() + startTimer() + startObservers() return ui.root @@ -297,8 +304,10 @@ class SampleListFragment : Fragment(), CoroutineScope by MainScope(), sDeviceViewModel.reset() } else { - findNavController().navigate(ExperimentListFragmentDirections + if (findNavController().currentDestination?.id == R.id.sample_list_fragment) { + findNavController().navigate(SampleListFragmentDirections .actionToConnectInstructions()) + } this?.startDeviceConnection() } @@ -340,15 +349,19 @@ class SampleListFragment : Fragment(), CoroutineScope by MainScope(), } else sOnNewClickListener fragSampleListSearchBtn.setOnClickListener { - findNavController().navigate(SampleListFragmentDirections - .actionToBarcodeSearch(mExpId)) + if (findNavController().currentDestination?.id == R.id.sample_list_fragment) { + findNavController().navigate(SampleListFragmentDirections + .actionToBarcodeSearch(mExpId)) + } } } override fun onListItemLongClicked(sample: IndexedSampleScanCount) { - findNavController().navigate(SampleListFragmentDirections - .actionToNewSample(sample.eid, sample.name, sample.note)) + if (findNavController().currentDestination?.id == R.id.sample_list_fragment) { + findNavController().navigate(SampleListFragmentDirections + .actionToNewSample(sample.eid, sample.name, sample.note)) + } } private fun FragmentSampleListBinding.setupRecyclerView() { @@ -396,7 +409,7 @@ class SampleListFragment : Fragment(), CoroutineScope by MainScope(), } - private fun startObservers() { + private fun startTimer() { //use the activity view model to access the current connection status val check = object : TimerTask() { @@ -418,11 +431,12 @@ class SampleListFragment : Fragment(), CoroutineScope by MainScope(), } } - Timer().cancel() + mTimer = Timer() - Timer().purge() + mTimer?.scheduleAtFixedRate(check, 0, 1500) + } - Timer().scheduleAtFixedRate(check, 0, 1500) + private fun startObservers() { //set the title header sViewModel.experiments.observe(viewLifecycleOwner, { experiments -> @@ -491,5 +505,15 @@ class SampleListFragment : Fragment(), CoroutineScope by MainScope(), } }) } + + override fun onDestroy() { + super.onDestroy() + + mTimer?.cancel() + + mTimer?.purge() + + mTimer = null + } } diff --git a/app/src/main/java/org/phenoapps/prospector/fragments/ScanListFragment.kt b/app/src/main/java/org/phenoapps/prospector/fragments/ScanListFragment.kt index 8fc939d..ef6a232 100644 --- a/app/src/main/java/org/phenoapps/prospector/fragments/ScanListFragment.kt +++ b/app/src/main/java/org/phenoapps/prospector/fragments/ScanListFragment.kt @@ -58,6 +58,8 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli private val TAG = this.tag ?: "ScanListFragment" + private val sDeviceScope = CoroutineScope(Dispatchers.IO) + private val sDeviceViewModel: DeviceViewModel by activityViewModels() private var mSelectedScanId: Long = -1 @@ -72,6 +74,10 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli private var mSampleName: String = String() + private var mTimer: Timer? = null + + private var mIsScanning: Boolean = false + private val mKeyUtil by lazy { KeyUtil(context) } @@ -85,40 +91,46 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli if (sDeviceViewModel.isConnected()) { - sDeviceViewModel.getDeviceInfo()?.let { connectedDeviceInfo -> + if (!mIsScanning) { + + mIsScanning = true - callScanDialog(connectedDeviceInfo) + sDeviceViewModel.getDeviceInfo()?.let { connectedDeviceInfo -> + + callScanDialog(connectedDeviceInfo) + } } + } else { - findNavController().navigate(ScanListFragmentDirections + if (findNavController().currentDestination?.id == R.id.scan_list_fragment) { + findNavController().navigate(ScanListFragmentDirections .actionToConnectInstructions()) + } } } private suspend fun insertScan(name: String, frames: List) { - val prefs = PreferenceManager.getDefaultSharedPreferences(requireContext()) - - frames.forEach { frame -> - - val sid = sViewModel.insertScanAsync(Scan(mExpId, name).apply { + val alias = mPrefs.getString(DEVICE_ALIAS, "") + val operator = mPrefs.getString(OPERATOR, "") ?: "" + val device = sDeviceViewModel.getDeviceInfo()?.DeviceID ?: "Unknown Device Id" + val deviceType = if (device.startsWith("NIR")) DEVICE_TYPE_NIR else DEVICE_TYPE_LS1 - val linkSquareApiDeviceId = sDeviceViewModel.getDeviceInfo()?.DeviceID ?: "Unknown Device Id" - - deviceId = linkSquareApiDeviceId - - this.deviceType = if (linkSquareApiDeviceId.startsWith("NIR")) DEVICE_TYPE_NIR else DEVICE_TYPE_LS1 - - this.alias = prefs.getString(DEVICE_ALIAS, "") + val scan = Scan(mExpId, name).apply { + this.deviceId = device + this.deviceType = deviceType + this.alias = alias + this.operator = operator + } - this.lightSource = frame.lightSource.toInt() + frames.forEach { frame -> - this.operator = prefs.getString(OPERATOR, "") ?: "" + scan.lightSource = frame.lightSource.toInt() - }).await() + val sid = sViewModel.insertScanAsync(scan).await() sViewModel.insertFrame(sid, SpectralFrame( sid, @@ -135,44 +147,50 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli */ private fun callScanDialog(device: LinkSquareAPI.LSDeviceInfo) { - sViewModel.experiments.observeOnce(viewLifecycleOwner, { experiments -> + activity?.let { act -> - val exp = experiments?.find { it.eid == mExpId } + context?.let { ctx -> - if (exp?.deviceType == resolveDeviceType(requireContext(), device)) { + sViewModel.experiments.observeOnce(viewLifecycleOwner, { experiments -> - val dialog = Dialogs.askForScan(requireActivity(), R.string.scanning, R.string.close) + val exp = experiments?.find { it.eid == mExpId } - dialog.create() + if (exp?.deviceType == resolveDeviceType(ctx, device)) { - val dialogInterface = dialog.show() + val dialog = Dialogs.askForScan(act, R.string.scanning, R.string.close) - sDeviceViewModel.scan(requireContext()).observe(viewLifecycleOwner) { + dialog.create() - it?.let { frames -> + val dialogInterface = dialog.show() - launch(Dispatchers.IO) { + sDeviceViewModel.scan(ctx).observeOnce(viewLifecycleOwner) { - insertScan(mSampleName, frames) + it?.let { frames -> - dialogInterface.dismiss() + sDeviceScope.launch { - activity?.runOnUiThread { - checkAudioTriggers() - loadGraph() + insertScan(mSampleName, frames) + + activity?.runOnUiThread { + dialogInterface.dismiss() + checkAudioTriggers() + loadGraph() + mIsScanning = false + } + } } } - } - } - } else { + } else { - mBinding?.let { ui -> + mBinding?.let { ui -> - mSnackbar.push(SnackbarQueue.SnackJob(ui.root, getString(R.string.frag_scan_device_type_mismatch))) - } + mSnackbar.push(SnackbarQueue.SnackJob(ui.root, getString(R.string.frag_scan_device_type_mismatch))) + } + } + }) } - }) + } } /** @@ -256,8 +274,6 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli val prefs = PreferenceManager.getDefaultSharedPreferences(requireContext()) - //ui.deleteOnClick = sOnClickDelete - ui.scanOnClick = sOnClickScan //check if experiment id is included in the arguments. @@ -300,6 +316,8 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli setupRecyclerView() + startTimer() + startObservers() } else { @@ -353,8 +371,10 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli sDeviceViewModel.reset() } else { - findNavController().navigate(ExperimentListFragmentDirections + if (findNavController().currentDestination?.id == R.id.scan_list_fragment) { + findNavController().navigate(ScanListFragmentDirections .actionToConnectInstructions()) + } this?.startDeviceConnection() } @@ -375,7 +395,7 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli if (booleanResult) { - launch { + sDeviceScope.launch { deleteScans(mExpId, mSampleName) @@ -477,7 +497,7 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli */ private fun reinsertScan(scan: Scan, frames: List) { - launch(Dispatchers.IO) { + sDeviceScope.launch { val sid = sViewModel.insertScanAsync(scan).await() @@ -512,7 +532,7 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli (ui.recyclerView.adapter as ScansAdapter) .currentList[viewHolder.absoluteAdapterPosition].also { scan -> - launch(Dispatchers.IO) { + sDeviceScope.launch { sViewModel.getSpectralValues(scan.eid, scan.sid ?: -1L).let { frames -> @@ -536,7 +556,7 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli } } - private fun startObservers() { + private fun startTimer() { val check = object : TimerTask() { @@ -560,11 +580,12 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli } } - Timer().cancel() + mTimer = Timer() - Timer().purge() + mTimer?.scheduleAtFixedRate(check, 0, 1500) + } - Timer().scheduleAtFixedRate(check, 0, 1500) + private fun startObservers() { //updates recycler view with available scans sViewModel.getScans(mExpId, mSampleName).observe(viewLifecycleOwner, { data -> @@ -605,7 +626,7 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli attachDeviceButtonPressListener() //set the title header - sViewModel.experiments.observe(viewLifecycleOwner, { experiments -> + sViewModel.experiments.observeOnce(viewLifecycleOwner, { experiments -> experiments.first { it.eid == mExpId }.also { @@ -627,20 +648,22 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli sDeviceViewModel.setEventListener { - activity?.runOnUiThread { + if (!mIsScanning) { - if (sDeviceViewModel.isConnected()) { + mIsScanning = true - sDeviceViewModel.getDeviceInfo()?.let { connectedDeviceInfo -> + activity?.runOnUiThread { - callScanDialog(connectedDeviceInfo) + if (sDeviceViewModel.isConnected()) { - } + sDeviceViewModel.getDeviceInfo()?.let { connectedDeviceInfo -> - } + callScanDialog(connectedDeviceInfo) + } + } + } } - }.observe(viewLifecycleOwner, {}) } catch (e: IllegalStateException) { @@ -670,7 +693,7 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli color?.let { nonNullColor -> - launch { + sDeviceScope.launch { sViewModel.updateScanColor(mExpId, id, nonNullColor) @@ -680,4 +703,14 @@ class ScanListFragment : Fragment(), CoroutineScope by MainScope(), GraphItemCli } } + + override fun onDestroy() { + super.onDestroy() + + mTimer?.cancel() + + mTimer?.purge() + + mTimer = null + } } \ No newline at end of file From aa214b04c023d69c9e552d442d5c2638a86a79d1 Mon Sep 17 00:00:00 2001 From: Trevor Rife Date: Wed, 29 Sep 2021 09:56:25 -0500 Subject: [PATCH 3/3] Updated version and changelog --- app/build.gradle | 4 ++-- app/src/main/res/raw/changelog.xml | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 805413d..49e9695 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -28,8 +28,8 @@ android { //link square api has minimum requirement of 21 minSdkVersion 21 targetSdkVersion 30 - versionCode 5 - versionName "1.1" + versionCode 6 + versionName "1.1.1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" multiDexEnabled true diff --git a/app/src/main/res/raw/changelog.xml b/app/src/main/res/raw/changelog.xml index 1f5ffa8..9633a38 100644 --- a/app/src/main/res/raw/changelog.xml +++ b/app/src/main/res/raw/changelog.xml @@ -1,4 +1,8 @@ + + Minor bug fixes + + Updated experiment and sample sort options Updated UI and optimized graphing