@@ -40,12 +40,9 @@ import com.google.android.gms.maps.model.MarkerOptions
4040import com.google.android.material.dialog.MaterialAlertDialogBuilder
4141import com.google.android.material.progressindicator.LinearProgressIndicator
4242import dagger.hilt.android.AndroidEntryPoint
43- import kotlinx.coroutines.Dispatchers
44- import kotlinx.coroutines.cancel
43+ import kotlinx.coroutines.*
4544import kotlinx.coroutines.channels.awaitClose
4645import kotlinx.coroutines.flow.callbackFlow
47- import kotlinx.coroutines.launch
48- import kotlinx.coroutines.withContext
4946import java.io.IOException
5047import java.util.regex.Pattern
5148import kotlin.properties.Delegates
@@ -74,7 +71,7 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMapCli
7471 private var xposedDialog: AlertDialog ? = null
7572 private lateinit var alertDialog: MaterialAlertDialogBuilder
7673 private lateinit var dialog: AlertDialog
77- var listAddress : List <Address >? = null
74+ private var addressList : List <Address >? = null
7875
7976
8077 override fun onCreate (savedInstanceState : Bundle ? ) {
@@ -205,7 +202,7 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMapCli
205202 return super .onOptionsItemSelected(item)
206203 }
207204
208- private fun moveMapToNewLocation (moveNewLocation : Boolean ) {
205+ private inline fun moveMapToNewLocation (moveNewLocation : Boolean ) {
209206 if (moveNewLocation) {
210207 mLatLng = LatLng (lat, lon)
211208 mLatLng.let { latLng ->
@@ -255,28 +252,24 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMapCli
255252 alertDialog.setView(view)
256253 alertDialog.setPositiveButton(" Search" ) { _, _ ->
257254 if (isNetworkConnected()){
258- lifecycleScope.launch {
255+ lifecycleScope.launch( Dispatchers . Main ) {
259256 val getInput = editText.text.toString()
260257 if (getInput.isNotEmpty()){
261258 getSearchAddress(getInput).let {
262259 it.collect { value ->
263- when (value){
260+ when (value) {
264261 is SearchProgress .Progress -> {
265262 progressBar.show()
266263 }
267264 is SearchProgress .Complete -> {
268- progressBar.cancel()
269265 val address = value.address
270- lat = address.latitude
271- lon = address.longitude
272- moveMapToNewLocation(true )
273- }
274- is SearchProgress .RegexMatch -> {
275- lat = value.lat
276- lon = value.lon
266+ val split = address.split(" ," )
267+ lat = split[0 ].toDouble()
268+ lon = split[1 ].toDouble()
277269 progressBar.cancel()
278270 moveMapToNewLocation(true )
279271 }
272+ //
280273 is SearchProgress .Fail -> {
281274 showToast(value.error!! )
282275 progressBar.cancel()
@@ -447,29 +440,33 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMapCli
447440 private suspend fun getSearchAddress (address : String ) = callbackFlow {
448441 withContext(Dispatchers .IO ){
449442 trySend(SearchProgress .Progress )
450-
451443 if (isRegexMatch(address)){
452- val split = address.split( " , " )
453- trySend(SearchProgress .RegexMatch (split[ 0 ].toDouble(),split[ 1 ].toDouble() ))
444+ delay( 3000 )
445+ trySend(SearchProgress .Complete (address ))
454446 }
455-
456447 try {
457448 val geocoder = Geocoder (this @MapsActivity)
458- listAddress = geocoder.getFromLocationName(address,5 )
459- val list: List <Address >? = listAddress
449+ addressList = geocoder.getFromLocationName(address,5 )
450+ val list: List <Address >? = addressList
460451 if (list == null ) {
461452 trySend(SearchProgress .Fail (null ))
462453 }
463454 if (list?.size == 1 ){
464- trySend(SearchProgress .Complete (list[0 ]))
455+ val sb = StringBuilder ()
456+ sb.append(list[0 ].latitude)
457+ sb.append(" ," )
458+ sb.append(list[0 ].longitude)
459+ delay(3000 )
460+ trySend(SearchProgress .Complete (sb.toString()))
465461 } else {
466- if (listAddress ?.size != 0 ) {
462+ if (addressList ?.size != 0 ) {
467463 trySend(SearchProgress .Fail (null ))
468464 }
469465 trySend(SearchProgress .Fail (" Address not found" ))
470466 }
471467 } catch (io : IOException ){
472- trySend(SearchProgress .Fail (" No internet" ))
468+ io.printStackTrace()
469+ trySend(SearchProgress .Fail (" No internet" ))
473470 }
474471 }
475472
@@ -490,9 +487,8 @@ class MapsActivity : AppCompatActivity(), OnMapReadyCallback, GoogleMap.OnMapCli
490487
491488sealed class SearchProgress {
492489 object Progress : SearchProgress()
493- data class Complete (val address : Address ) : SearchProgress()
490+ data class Complete (val address : String ) : SearchProgress()
494491 data class Fail (val error : String? ) : SearchProgress()
495- data class RegexMatch (val lat : Double , val lon : Double ) : SearchProgress()
496492
497493}
498494
0 commit comments