Skip to content

Commit

Permalink
Switch fragment coroutine scopes to use lifecycleScope (#11405)
Browse files Browse the repository at this point in the history
  • Loading branch information
austinh0 authored Nov 4, 2021
1 parent 29747a2 commit 5e0f9d4
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,30 @@ import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.ArrayAdapter
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.ChipClusters
import chip.devicecontroller.ChipClusters.BasicCluster
import chip.devicecontroller.ChipDeviceController
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
import kotlinx.android.synthetic.main.basic_client_fragment.attributeNameSpinner
import kotlinx.android.synthetic.main.basic_client_fragment.basicClusterCommandStatus
import kotlinx.android.synthetic.main.basic_client_fragment.userLabelEd
import kotlinx.android.synthetic.main.basic_client_fragment.locationEd
import kotlinx.android.synthetic.main.basic_client_fragment.attributeNameSpinner
import kotlinx.android.synthetic.main.basic_client_fragment.view.writeUserLabelBtn
import kotlinx.android.synthetic.main.basic_client_fragment.view.writeLocationBtn
import kotlinx.android.synthetic.main.basic_client_fragment.view.writeLocalConfigDisabledSwitch
import kotlinx.android.synthetic.main.basic_client_fragment.userLabelEd
import kotlinx.android.synthetic.main.basic_client_fragment.view.attributeNameSpinner
import kotlinx.android.synthetic.main.basic_client_fragment.view.readAttributeBtn
import kotlinx.android.synthetic.main.basic_client_fragment.view.writeLocalConfigDisabledSwitch
import kotlinx.android.synthetic.main.basic_client_fragment.view.writeLocationBtn
import kotlinx.android.synthetic.main.basic_client_fragment.view.writeUserLabelBtn
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

class BasicClientFragment : Fragment() {
private val deviceController: ChipDeviceController
get() = ChipClient.getDeviceController(requireContext())

private val scope = CoroutineScope(Dispatchers.Main + Job())
private lateinit var scope: CoroutineScope

private lateinit var addressUpdateFragment: AddressUpdateFragment

Expand All @@ -42,6 +40,8 @@ class BasicClientFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
scope = viewLifecycleOwner.lifecycleScope

return inflater.inflate(R.layout.basic_client_fragment, container, false).apply {
deviceController.setCompletionListener(ChipControllerCallback())

Expand Down Expand Up @@ -85,10 +85,6 @@ class BasicClientFragment : Fragment() {
}
}

override fun onStop() {
super.onStop()
scope.cancel()
}

private fun makeAttributeNamesAdapter(): ArrayAdapter<String> {
return ArrayAdapter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.ChipClusters
import chip.devicecontroller.ChipDeviceController
import com.google.chip.chiptool.ChipClient
Expand All @@ -24,7 +25,7 @@ class MultiAdminClientFragment : Fragment() {
private val deviceController: ChipDeviceController
get() = ChipClient.getDeviceController(requireContext())

private val scope = CoroutineScope(Dispatchers.Main + Job())
private lateinit var scope: CoroutineScope

private lateinit var addressUpdateFragment: AddressUpdateFragment

Expand All @@ -33,6 +34,8 @@ class MultiAdminClientFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
scope = viewLifecycleOwner.lifecycleScope

return inflater.inflate(R.layout.multi_admin_client_fragment, container, false).apply {
deviceController.setCompletionListener(ChipControllerCallback())

Expand Down Expand Up @@ -74,11 +77,6 @@ class MultiAdminClientFragment : Fragment() {
}
}

override fun onStop() {
super.onStop()
scope.cancel()
}

private suspend fun sendBasicCommissioningCommandClick() {
val testDuration = 100
deviceController.openPairingWindow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import android.widget.EditText
import android.widget.SeekBar
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.ChipClusters
import chip.devicecontroller.ChipClusters.OnOffCluster
import chip.devicecontroller.ChipDeviceController
Expand All @@ -30,16 +31,14 @@ import kotlinx.android.synthetic.main.on_off_client_fragment.view.readBtn
import kotlinx.android.synthetic.main.on_off_client_fragment.view.showSubscribeDialogBtn
import kotlinx.android.synthetic.main.on_off_client_fragment.view.toggleBtn
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch

class OnOffClientFragment : Fragment() {
private val deviceController: ChipDeviceController
get() = ChipClient.getDeviceController(requireContext())

private val scope = CoroutineScope(Dispatchers.Main + Job())
private lateinit var scope: CoroutineScope

private lateinit var addressUpdateFragment: AddressUpdateFragment

Expand All @@ -48,6 +47,8 @@ class OnOffClientFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
scope = viewLifecycleOwner.lifecycleScope

return inflater.inflate(R.layout.on_off_client_fragment, container, false).apply {
deviceController.setCompletionListener(ChipControllerCallback())

Expand Down Expand Up @@ -166,11 +167,6 @@ class OnOffClientFragment : Fragment() {
}
}

override fun onStop() {
super.onStop()
scope.cancel()
}

private suspend fun sendLevelCommandClick() {
val cluster = ChipClusters.LevelControlCluster(
ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.ChipClusters
import chip.devicecontroller.ChipDeviceController
import com.google.chip.chiptool.ChipClient
Expand All @@ -15,16 +16,13 @@ import kotlinx.android.synthetic.main.op_cred_client_fragment.opCredClusterComma
import kotlinx.android.synthetic.main.op_cred_client_fragment.view.readCommissionedFabricBtn
import kotlinx.android.synthetic.main.op_cred_client_fragment.view.readSupportedFabricBtn
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

class OpCredClientFragment : Fragment() {
private val deviceController: ChipDeviceController
get() = ChipClient.getDeviceController(requireContext())

private val scope = CoroutineScope(Dispatchers.Main + Job())
private lateinit var scope: CoroutineScope

private lateinit var addressUpdateFragment: AddressUpdateFragment

Expand All @@ -33,6 +31,8 @@ class OpCredClientFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
scope = viewLifecycleOwner.lifecycleScope

return inflater.inflate(R.layout.op_cred_client_fragment, container, false).apply {
deviceController.setCompletionListener(ChipControllerCallback())

Expand Down Expand Up @@ -64,11 +64,6 @@ class OpCredClientFragment : Fragment() {
}
}

override fun onStop() {
super.onStop()
scope.cancel()
}

private suspend fun sendReadOpCredSupportedFabricAttrClick() {
getOpCredClusterForDevice().readSupportedFabricsAttribute(object : ChipClusters.IntegerAttributeCallback {
override fun onSuccess(value: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.ChipClusters
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.R
Expand All @@ -18,16 +19,26 @@ import com.jjoe64.graphview.LabelFormatter
import com.jjoe64.graphview.Viewport
import com.jjoe64.graphview.series.DataPoint
import com.jjoe64.graphview.series.LineGraphSeries
import kotlinx.android.synthetic.main.sensor_client_fragment.*
import kotlinx.android.synthetic.main.sensor_client_fragment.view.*
import kotlinx.coroutines.*
import java.text.SimpleDateFormat
import java.util.*
import java.util.Calendar
import java.util.Date
import kotlinx.android.synthetic.main.sensor_client_fragment.clusterNameSpinner
import kotlinx.android.synthetic.main.sensor_client_fragment.deviceIdEd
import kotlinx.android.synthetic.main.sensor_client_fragment.endpointIdEd
import kotlinx.android.synthetic.main.sensor_client_fragment.lastValueTv
import kotlinx.android.synthetic.main.sensor_client_fragment.sensorGraph
import kotlinx.android.synthetic.main.sensor_client_fragment.view.clusterNameSpinner
import kotlinx.android.synthetic.main.sensor_client_fragment.view.readSensorBtn
import kotlinx.android.synthetic.main.sensor_client_fragment.view.sensorGraph
import kotlinx.android.synthetic.main.sensor_client_fragment.view.watchSensorBtn
import kotlinx.android.synthetic.main.sensor_client_fragment.watchSensorBtn
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

private typealias ReadCallback = ChipClusters.IntegerAttributeCallback

class SensorClientFragment : Fragment() {
private val scope = CoroutineScope(Dispatchers.Main + Job())
private lateinit var scope: CoroutineScope

// History of sensor values
private val sensorData = LineGraphSeries<DataPoint>()
Expand All @@ -40,6 +51,8 @@ class SensorClientFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
scope = viewLifecycleOwner.lifecycleScope

return inflater.inflate(R.layout.sensor_client_fragment, container, false).apply {
ChipClient.getDeviceController(requireContext()).setCompletionListener(null)
deviceIdEd.setOnEditorActionListener { textView, actionId, _ ->
Expand Down Expand Up @@ -97,7 +110,6 @@ class SensorClientFragment : Fragment() {
}

override fun onStop() {
scope.cancel()
resetSensorGraph() // reset the graph on fragment exit
super.onStop()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,12 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.ChipDeviceController
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel

/**
* ClusterDetailFragment allows user to pick cluster, command, specify parameters and see
Expand All @@ -23,13 +21,15 @@ class ClusterDetailFragment : Fragment(){
private val deviceController: ChipDeviceController
get() = ChipClient.getDeviceController(requireContext())

private val scope = CoroutineScope(Dispatchers.Main + Job())
private lateinit var scope: CoroutineScope

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
scope = viewLifecycleOwner.lifecycleScope

return inflater.inflate(R.layout.cluster_detail_fragment, container, false).apply {
deviceController.setCompletionListener(GenericChipDeviceListener())
}
Expand All @@ -41,11 +41,6 @@ class ClusterDetailFragment : Fragment(){
}
}

override fun onStop() {
super.onStop()
scope.cancel()
}

companion object {
private const val TAG = "ClusterDetailFragment"
fun newInstance(): ClusterDetailFragment = ClusterDetailFragment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,25 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import chip.clusterinfo.ClusterInfo
import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.ClusterInfoMapping
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import chip.devicecontroller.ClusterInfoMapping
import com.google.chip.chiptool.clusterclient.AddressUpdateFragment
import kotlinx.android.synthetic.main.cluster_interaction_fragment.view.endpointList
import kotlinx.android.synthetic.main.cluster_interaction_fragment.view.getEndpointListBtn
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

class ClusterInteractionFragment : Fragment() {
private val deviceController: ChipDeviceController
get() = ChipClient.getDeviceController(requireContext())

private val scope = CoroutineScope(Dispatchers.Main + Job())
private lateinit var scope: CoroutineScope
private lateinit var addressUpdateFragment: AddressUpdateFragment
private lateinit var clusterMap: Map<String, ClusterInfo>

Expand All @@ -36,6 +34,8 @@ class ClusterInteractionFragment : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
scope = viewLifecycleOwner.lifecycleScope

return inflater.inflate(R.layout.cluster_interaction_fragment, container, false).apply {
deviceController.setCompletionListener(ChipControllerCallback())
getEndpointListBtn.setOnClickListener {
Expand Down Expand Up @@ -84,11 +84,6 @@ class ClusterInteractionFragment : Fragment() {
}
}

override fun onStop() {
super.onStop()
scope.cancel()
}

companion object {
private const val TAG = "ClusterInteractionFragment"
fun newInstance(): ClusterInteractionFragment = ClusterInteractionFragment()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
Expand All @@ -34,10 +35,7 @@ import com.google.chip.chiptool.setuppayloadscanner.CHIPDeviceInfo
import com.google.chip.chiptool.util.DeviceIdUtil
import com.google.chip.chiptool.util.FragmentUtil
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

@ExperimentalCoroutinesApi
Expand All @@ -52,13 +50,14 @@ class DeviceProvisioningFragment : Fragment() {
ProvisionNetworkType.fromName(arguments?.getString(ARG_PROVISION_NETWORK_TYPE))
)

private val scope = CoroutineScope(Dispatchers.Main + Job())
private lateinit var scope: CoroutineScope

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
scope = viewLifecycleOwner.lifecycleScope
deviceInfo = checkNotNull(requireArguments().getParcelable(ARG_DEVICE_INFO))
return inflater.inflate(R.layout.single_fragment_container, container, false).apply {
if (savedInstanceState == null) {
Expand All @@ -74,7 +73,6 @@ class DeviceProvisioningFragment : Fragment() {
override fun onStop() {
super.onStop()
gatt = null
scope.cancel()
}

private fun pairDeviceWithAddress() {
Expand Down
Loading

0 comments on commit 5e0f9d4

Please sign in to comment.