Skip to content

Commit 6519503

Browse files
committed
runOnUiThread() replaced by Coroutines
1 parent 3a13ed8 commit 6519503

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

app/src/main/java/github/umer0586/sensorserver/activities/MainActivity.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import androidx.appcompat.app.ActionBarDrawerToggle
1111
import androidx.appcompat.app.AppCompatActivity
1212
import androidx.fragment.app.Fragment
1313
import androidx.fragment.app.FragmentActivity
14+
import androidx.lifecycle.lifecycleScope
1415
import androidx.viewpager2.adapter.FragmentStateAdapter
1516
import com.google.android.material.navigation.NavigationBarView
1617
import github.umer0586.sensorserver.R
@@ -21,6 +22,8 @@ import github.umer0586.sensorserver.fragments.ServerFragment
2122
import github.umer0586.sensorserver.service.SensorService
2223
import github.umer0586.sensorserver.service.SensorService.LocalBinder
2324
import github.umer0586.sensorserver.service.ServiceBindHelper
25+
import kotlinx.coroutines.Dispatchers
26+
import kotlinx.coroutines.launch
2427

2528
class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListener
2629
{
@@ -120,7 +123,9 @@ class MainActivity : AppCompatActivity(), NavigationBarView.OnItemSelectedListen
120123

121124
sensorService?.onConnectionsCountChange { count ->
122125

123-
runOnUiThread { setConnectionCountBadge(count) }
126+
lifecycleScope.launch(Dispatchers.Main) {
127+
setConnectionCountBadge(count)
128+
}
124129
}
125130

126131
}

app/src/main/java/github/umer0586/sensorserver/fragments/ConnectionsFragment.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
package github.umer0586.sensorserver.fragments
22

3-
import android.content.ComponentName
4-
import android.content.ServiceConnection
53
import android.os.Bundle
64
import android.os.IBinder
75
import android.util.Log
86
import android.view.LayoutInflater
97
import android.view.View
108
import android.view.ViewGroup
119
import androidx.fragment.app.Fragment
10+
import androidx.lifecycle.lifecycleScope
1211
import androidx.recyclerview.widget.LinearLayoutManager
1312
import github.umer0586.sensorserver.databinding.FragmentConnectionsBinding
1413
import github.umer0586.sensorserver.fragments.customadapters.ConnectionsRecyclerViewAdapter
1514
import github.umer0586.sensorserver.service.SensorService
1615
import github.umer0586.sensorserver.service.SensorService.LocalBinder
1716
import github.umer0586.sensorserver.service.ServiceBindHelper
18-
import github.umer0586.sensorserver.util.UIUtil
17+
import kotlinx.coroutines.Dispatchers
18+
import kotlinx.coroutines.launch
1919
import org.java_websocket.WebSocket
2020

2121
/**
@@ -86,7 +86,9 @@ class ConnectionsFragment : Fragment()
8686
this.webSockets.addAll(webSockets)
8787

8888
handleNoConnectionsText()
89-
UIUtil.runOnUiThread { connectionsRecyclerViewAdapter.notifyDataSetChanged() }
89+
lifecycleScope.launch(Dispatchers.Main) {
90+
connectionsRecyclerViewAdapter.notifyDataSetChanged()
91+
}
9092
}
9193

9294

@@ -114,12 +116,15 @@ class ConnectionsFragment : Fragment()
114116

115117
private fun handleNoConnectionsText()
116118
{
117-
if (webSockets.size > 0) UIUtil.runOnUiThread {
118-
binding.noConnectionsText.visibility = View.INVISIBLE
119+
lifecycleScope.launch(Dispatchers.Main) {
120+
if (webSockets.size > 0)
121+
binding.noConnectionsText.visibility = View.INVISIBLE
122+
else
123+
binding.noConnectionsText.visibility = View.VISIBLE
119124
}
120-
else UIUtil.runOnUiThread { binding.noConnectionsText.visibility = View.VISIBLE }
121125
}
122126

127+
123128
companion object
124129
{
125130
private val TAG: String = ConnectionsFragment::class.java.getSimpleName()

app/src/main/java/github/umer0586/sensorserver/fragments/ServerFragment.kt

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import android.view.ViewGroup
1111
import android.widget.Toast
1212
import androidx.core.content.ContextCompat
1313
import androidx.fragment.app.Fragment
14+
import androidx.lifecycle.lifecycleScope
1415
import com.google.android.material.snackbar.Snackbar
1516
import github.umer0586.sensorserver.R
1617
import github.umer0586.sensorserver.customextensions.isHotSpotEnabled
@@ -20,8 +21,9 @@ import github.umer0586.sensorserver.service.SensorService.LocalBinder
2021
import github.umer0586.sensorserver.service.ServerStateListener
2122
import github.umer0586.sensorserver.service.ServiceBindHelper
2223
import github.umer0586.sensorserver.setting.AppSettings
23-
import github.umer0586.sensorserver.util.UIUtil
2424
import github.umer0586.sensorserver.websocketserver.ServerInfo
25+
import kotlinx.coroutines.Dispatchers
26+
import kotlinx.coroutines.launch
2527
import java.net.BindException
2628
import java.net.UnknownHostException
2729

@@ -154,36 +156,38 @@ class ServerFragment : Fragment(), ServerStateListener
154156
override fun onServerStarted(serverInfo: ServerInfo)
155157
{
156158
Log.d(TAG, "onServerStarted() called")
157-
UIUtil.runOnUiThread {
159+
lifecycleScope.launch(Dispatchers.Main) {
160+
158161
showServerAddress("ws://" + serverInfo.ipAddress + ":" + serverInfo.port)
159162
showPulseAnimation()
160163

161164
binding.startButton.tag = "started"
162165
binding.startButton.text = "STOP"
163166

164167
showMessage("Server started")
168+
165169
}
166170
}
167171

168172
override fun onServerStopped()
169173
{
170174
Log.d(TAG, "onServerStopped() called ")
171175

172-
UIUtil.runOnUiThread {
176+
lifecycleScope.launch(Dispatchers.Main) {
173177

174-
hideServerAddress()
175-
hidePulseAnimation()
178+
hideServerAddress()
179+
hidePulseAnimation()
176180

177-
binding.startButton.tag = "stopped"
178-
binding.startButton.text = "START"
181+
binding.startButton.tag = "stopped"
182+
binding.startButton.text = "START"
179183

180-
showMessage("Server Stopped")
181-
}
184+
showMessage("Server Stopped")
185+
}
182186
}
183187

184188
override fun onServerError(ex: Exception?)
185189
{
186-
UIUtil.runOnUiThread {
190+
lifecycleScope.launch(Dispatchers.Main) {
187191
if (ex is BindException)
188192
showMessage("Port already in use")
189193
else if (ex is UnknownHostException)
@@ -203,7 +207,7 @@ class ServerFragment : Fragment(), ServerStateListener
203207
override fun onServerAlreadyRunning(serverInfo: ServerInfo)
204208
{
205209
Log.d(TAG, "onServerAlreadyRunning() called")
206-
UIUtil.runOnUiThread {
210+
lifecycleScope.launch(Dispatchers.Main) {
207211
showServerAddress("ws://" + serverInfo.ipAddress + ":" + serverInfo.port)
208212
Toast.makeText(context, "service running", Toast.LENGTH_SHORT).show()
209213
binding.startButton.tag = "started"

0 commit comments

Comments
 (0)