Skip to content

Commit 5a4745e

Browse files
committed
PR feedback
1 parent 322b4f3 commit 5a4745e

File tree

1 file changed

+34
-55
lines changed
  • display-device-location-with-nmea-data-sources/src/main/java/com/esri/arcgismaps/sample/displaydevicelocationwithnmeadatasources

1 file changed

+34
-55
lines changed

display-device-location-with-nmea-data-sources/src/main/java/com/esri/arcgismaps/sample/displaydevicelocationwithnmeadatasources/MainActivity.kt

Lines changed: 34 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ import com.esri.arcgismaps.sample.displaydevicelocationwithnmeadatasources.datab
3939
import com.google.android.material.floatingactionbutton.FloatingActionButton
4040
import com.google.android.material.snackbar.Snackbar
4141
import kotlinx.coroutines.launch
42-
import java.io.BufferedReader
4342
import java.io.File
44-
import java.io.FileReader
4543
import java.nio.charset.StandardCharsets
4644
import java.util.*
4745
import kotlin.concurrent.timerTask
@@ -55,14 +53,14 @@ class MainActivity : AppCompatActivity() {
5553
}
5654

5755
// create a new NMEA location data source
58-
private var nmeaLocationDataSource: NmeaLocationDataSource =
56+
private val nmeaLocationDataSource: NmeaLocationDataSource =
5957
NmeaLocationDataSource(SpatialReference.wgs84())
6058

6159
// create a timer to simulate a stream of NMEA data
6260
private var timer = Timer()
6361

6462
// list of nmea location sentences
65-
private var nmeaSentences: MutableList<String>? = null
63+
private var nmeaSentences: List<String>? = emptyList()
6664

6765
// index of nmea location sentence
6866
private var locationIndex = 0
@@ -115,14 +113,19 @@ class MainActivity : AppCompatActivity() {
115113
)
116114
)
117115

118-
// set the nmea location data source onto the map view's location display
119-
val locationDisplay = mapView.locationDisplay
120-
locationDisplay.dataSource = nmeaLocationDataSource
121-
locationDisplay.setAutoPanMode(LocationDisplayAutoPanMode.Recenter)
116+
mapView.locationDisplay.apply {
117+
// set the map view's location display to use the nmea location data source
118+
dataSource = nmeaLocationDataSource
119+
// set the map view to recenter on location changed events
120+
setAutoPanMode(LocationDisplayAutoPanMode.Recenter)
121+
}
122122

123123
// disable map view interaction, the location display will automatically center on the mock device location
124-
mapView.interactionOptions.isPanEnabled = false
125-
mapView.interactionOptions.isZoomEnabled = false
124+
mapView.interactionOptions.apply {
125+
isPanEnabled = false
126+
isZoomEnabled = false
127+
isRotateEnabled = false
128+
}
126129

127130
// read nmea location sentences from file
128131
nmeaSentences = getNMEASentenceList()
@@ -136,30 +139,22 @@ class MainActivity : AppCompatActivity() {
136139
* Reads NMEA location sentences from the .nmea file and
137140
* returns it as a [MutableList]
138141
*/
139-
private fun getNMEASentenceList(): MutableList<String>? {
140-
// create list of nmea location sentences
141-
val nmeaSentences: MutableList<String> = mutableListOf()
142+
private fun getNMEASentenceList(): List<String>? {
142143
val simulatedNmeaDataFile = File("$provisionPath/Redlands.nmea")
143144
if (!simulatedNmeaDataFile.exists()) {
144145
showError("NMEA file does not exist")
145146
return null
146147
}
148+
// create list of nmea location sentences
149+
var nmeaSentences: List<String> = emptyList()
150+
// create a buffered reader using the .nmea file
151+
val bufferedReader = File(simulatedNmeaDataFile.path).bufferedReader()
147152
// read the nmea file contents using a buffered reader and store the mock data sentences in a list
148-
return try {
149-
// create a buffered reader using the .nmea file
150-
val bufferedReader = BufferedReader(FileReader(simulatedNmeaDataFile.path))
151-
var line = bufferedReader.readLine()
152-
while (line != null) {
153-
// add carriage return for nmea location data source parser
154-
nmeaSentences.add(line + "\n")
155-
line = bufferedReader.readLine()
156-
}
157-
bufferedReader.close()
158-
nmeaSentences
159-
} catch (e: Exception) {
160-
showError("Error creating NMEA sentences " + e.message)
161-
null
153+
bufferedReader.useLines { bufferReaderLines ->
154+
// add carriage return for nmea location data source parser
155+
nmeaSentences = bufferReaderLines.map { it + "\n" }.toList()
162156
}
157+
return nmeaSentences
163158
}
164159

165160
/**
@@ -171,6 +166,7 @@ class MainActivity : AppCompatActivity() {
171166
// as updates are received, they will be displayed on the map
172167
nmeaLocationDataSource.start().onFailure {
173168
showError("NmeaLocationDataSource failed to start: ${it.message}")
169+
return@launch
174170
}
175171
// starts the NMEA mock data sentences
176172
nmeaSentences?.let { startNMEAMockData(it) }
@@ -181,7 +177,7 @@ class MainActivity : AppCompatActivity() {
181177
// cancel up the timer task
182178
timer.cancel()
183179
setButtonStatus(false)
184-
clearInformation()
180+
clearUI()
185181
}
186182
}
187183

@@ -246,38 +242,21 @@ class MainActivity : AppCompatActivity() {
246242
*/
247243
private fun collectSatelliteChanges() = lifecycleScope.launch {
248244
nmeaLocationDataSource.satellitesChanged.collect { nmeaSatelliteInfoList ->
249-
val uniqueSatelliteIDs = mutableListOf<Int>()
250245
var satelliteSystems = ""
251246
// set the text of the satellite count label
252247
satelliteCountTV.text = getString(R.string.satellite_count) + nmeaSatelliteInfoList.size
253248
// get the system of the first satellite
254-
when (nmeaSatelliteInfoList.first().system) {
255-
NmeaGnssSystem.Bds -> {
256-
satelliteSystems = "BDS"
257-
}
258-
NmeaGnssSystem.Galileo -> {
259-
satelliteSystems = "Galileo"
260-
}
261-
NmeaGnssSystem.Glonass -> {
262-
satelliteSystems = "Glonass"
263-
}
264-
NmeaGnssSystem.Gps -> {
265-
satelliteSystems = "GPS"
266-
}
267-
NmeaGnssSystem.NavIc -> {
268-
satelliteSystems = "NavIc"
269-
}
270-
NmeaGnssSystem.Qzss -> {
271-
satelliteSystems = "Qzss"
272-
}
273-
NmeaGnssSystem.Unknown -> {
274-
satelliteSystems = "Unknown"
275-
}
249+
satelliteSystems = when (nmeaSatelliteInfoList.first().system) {
250+
NmeaGnssSystem.Bds -> "BDS"
251+
NmeaGnssSystem.Galileo -> "Galileo"
252+
NmeaGnssSystem.Glonass -> "Glonass"
253+
NmeaGnssSystem.Gps -> "GPS"
254+
NmeaGnssSystem.NavIc -> "NavIc"
255+
NmeaGnssSystem.Qzss -> "Qzss"
256+
NmeaGnssSystem.Unknown -> "Unknown"
276257
}
277258
// get the satellite IDs from the info list
278-
nmeaSatelliteInfoList.forEach { satelliteInfo ->
279-
uniqueSatelliteIDs.add(satelliteInfo.id)
280-
}
259+
val uniqueSatelliteIDs = nmeaSatelliteInfoList.map { it.id }
281260
// display the satellite system and id information
282261
systemTypeTV.text = getString(R.string.system) + satelliteSystems
283262
satelliteIDsTV.text = getString(R.string.satellite_ids) + uniqueSatelliteIDs
@@ -287,7 +266,7 @@ class MainActivity : AppCompatActivity() {
287266
/**
288267
* Clears out the info messages when LocationDataSource is paused.
289268
*/
290-
private fun clearInformation() {
269+
private fun clearUI() {
291270
accuracyTV.text = getString(R.string.accuracy)
292271
satelliteCountTV.text = getString(R.string.satellite_count)
293272
satelliteIDsTV.text = getString(R.string.satellite_ids)

0 commit comments

Comments
 (0)