-
Notifications
You must be signed in to change notification settings - Fork 551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Administrative area in Location Detection #2637
base: development
Are you sure you want to change the base?
Changes from all commits
dc69998
76db127
6bd9dab
875ca79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,4 +61,47 @@ class LocationServiceImpl( | |
} | ||
} | ||
} | ||
|
||
@SuppressLint("MissingPermission") | ||
override fun getCityName(): Single<String> { | ||
val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as? LocationManager | ||
?: return Single.error(IllegalStateException()) | ||
val geoCoder = Geocoder(context, Locale.getDefault()) | ||
|
||
return Single.create { emitter -> | ||
try { | ||
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0f, | ||
object : LocationListener { | ||
override fun onLocationChanged(location: Location?) { | ||
if (location == null) { | ||
emitter.onError(java.lang.IllegalStateException()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What IllegalState? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given for the condition if incase it fails to update location to the variable then it gives the error for it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't see that message in IllegalStateException constructor. How would I know? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should I remove the it? or I had a question is there need of AdminArea if we are implementing it using cityName if not then should I change the method for calling the admin area with the city name as it will also save from creating another function for it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, you should throw error with a proper message There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay, I will fix it soon |
||
return | ||
} | ||
val addresses = geoCoder.getFromLocation(location.latitude, location.longitude, 1) | ||
try { | ||
val adminArea = if (addresses.isNotEmpty()) addresses[0].locality | ||
else resource.getString(R.string.no_location).nullToEmpty() | ||
emitter.onSuccess(adminArea) | ||
} catch (e: IllegalArgumentException) { | ||
emitter.onError(e) | ||
} | ||
} | ||
|
||
override fun onStatusChanged(provider: String?, status: Int, extras: Bundle?) { | ||
// To change body of created functions use File | Settings | File Templates. | ||
} | ||
|
||
override fun onProviderEnabled(provider: String?) { | ||
// To change body of created functions use File | Settings | File Templates. | ||
} | ||
|
||
override fun onProviderDisabled(provider: String?) { | ||
// To change body of created functions use File | Settings | File Templates. | ||
} | ||
}) | ||
} catch (e: SecurityException) { | ||
emitter.onError(e) | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add single cancel handler
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will create a memory leak when Single is cancelled. Add logic to handle the cancellation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay