-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Adds data layer for location background sample. #227
Conversation
Hope to get your final input on the permission + location + DB design for this simple sample. I will probably move to coroutines + flow in next version. |
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.
Only a few minor things, but I'll let Florina + Sean comment
...google/android/gms/location/sample/locationupdatesbackgroundkotlin/data/MyLocationManager.kt
Outdated
Show resolved
Hide resolved
...oogle/android/gms/location/sample/locationupdatesbackgroundkotlin/data/LocationRepository.kt
Outdated
Show resolved
Hide resolved
// Background permissions didn't exit prior to Q, so it's approved by default. | ||
if (permission == Manifest.permission.ACCESS_BACKGROUND_LOCATION && | ||
android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.Q) { | ||
|
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.
new line not needed
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.
suggestion: ktlint on source to find formatting issues.
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.
ran ktlint and fixed a bunch of other stuff, but it says this is ok... ?
No idea, but I removed anyway. :)
...oogle/android/gms/location/sample/locationupdatesbackgroundkotlin/data/LocationRepository.kt
Outdated
Show resolved
Hide resolved
* Access point for database (MyLocation data) and location APIs (start/stop location tracking and | ||
* checking tracking status). | ||
*/ | ||
class LocationRepository private constructor(private val myLocationDatabase: MyLocationDatabase, |
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.
consider renaming MyLocationDatabase
to just LocationDatabase
but here, it looks like you don't need the entire database, but just the dao, so you can just pass it as a parameter
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.
Done.
For MyLocationDatabase, I wanted to imply it wasn't a database of the Android Location class, but maybe I am reading too much into it. I could rename them SimpleLocation or something. (They are basically simplified versions of Android's Location.)
IDK, what do you think?
// Database related fields/methods: | ||
private val locationDao = myLocationDatabase.locationDao() | ||
|
||
private val executor = Executors.newSingleThreadExecutor() |
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.
because you're creating the executor here, it means that you're not able to unit test this class easily.
I suggest passing the executor as a constructor parameter as well. In tests, this can be replaced with an instant executor.
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.
ah, good call, moved.
...google/android/gms/location/sample/locationupdatesbackgroundkotlin/data/MyLocationManager.kt
Show resolved
Hide resolved
.../google/android/gms/location/sample/locationupdatesbackgroundkotlin/data/db/MyLocationDao.kt
Show resolved
Hide resolved
MyLocationDatabase::class.java, | ||
DATABASE_NAME | ||
) | ||
.build() |
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 should go on the previous line
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.
Done.
* {@link android.location.Location} class). | ||
*/ | ||
@Entity(tableName = "my_location_table") | ||
data class MyLocationEntity (@PrimaryKey val id:UUID = UUID.randomUUID(), |
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.
Could be called LocationEntity
Put the id
on the next line and re-format the file
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.
Done.
) { | ||
|
||
override fun toString(): String { | ||
|
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.
nit: remove extra new line
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.
Done.
...roid/gms/location/sample/locationupdatesbackgroundkotlin/data/db/MyLocationTypeConverters.kt
Show resolved
Hide resolved
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.
A few comments, otherwise LGTM!
// Background permissions didn't exit prior to Q, so it's approved by default. | ||
if (permission == Manifest.permission.ACCESS_BACKGROUND_LOCATION && | ||
android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.Q) { | ||
|
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.
suggestion: ktlint on source to find formatting issues.
) { | ||
val provideRationale = shouldShowRequestPermissionRationale(permission) | ||
|
||
// If the user denied a previous request, but didn't check "Don't ask again", we provide |
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.
Move comment to KDoc (and add kdoc for other public method)
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.
ktlint said that is ok... so IDK? :-D
Moved comment up to KDoc.
...oogle/android/gms/location/sample/locationupdatesbackgroundkotlin/data/LocationRepository.kt
Show resolved
Hide resolved
...oogle/android/gms/location/sample/locationupdatesbackgroundkotlin/data/LocationRepository.kt
Show resolved
Hide resolved
*/ | ||
class MyLocationManager private constructor(private val context: Context) { | ||
|
||
val trackingLocation: MutableLiveData<Boolean> by lazy { |
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.
+1 remove this lazy since the LiveData should do no work when there are no observers.
Same note about exposing LiveData.
Also, add kdoc for public API.
fusedLocationClient.requestLocationUpdates(locationRequest, locationUpdatePendingIntent) | ||
|
||
} catch (e: SecurityException) { | ||
trackingLocation.value = false |
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.
Same note about main-thread only .value =
|
||
fun stopLocationUpdates() { | ||
Log.d(TAG, "stopLocationUpdates()") | ||
trackingLocation.value = false |
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.
Same note about main-thread only .value =
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.
Added to method call since it's only call from Fragment. LMK if that isn't what you meant.
...google/android/gms/location/sample/locationupdatesbackgroundkotlin/data/MyLocationManager.kt
Show resolved
Hide resolved
...oogle/android/gms/location/sample/locationupdatesbackgroundkotlin/data/LocationRepository.kt
Show resolved
Hide resolved
...oogle/android/gms/location/sample/locationupdatesbackgroundkotlin/data/LocationRepository.kt
Show resolved
Hide resolved
PTAL, I think I addressed everything. Thank you again for reviewing! |
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.
Resolved all issues, PTAL.
.../google/android/gms/location/sample/locationupdatesbackgroundkotlin/data/db/MyLocationDao.kt
Show resolved
Hide resolved
...google/android/gms/location/sample/locationupdatesbackgroundkotlin/data/MyLocationManager.kt
Show resolved
Hide resolved
...google/android/gms/location/sample/locationupdatesbackgroundkotlin/data/MyLocationManager.kt
Show resolved
Hide resolved
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.
Resolved all issues, PTAL.
...google/android/gms/location/sample/locationupdatesbackgroundkotlin/data/MyLocationManager.kt
Show resolved
Hide resolved
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.
LGTM nice one!
@objcode - Thanks! Talked to Florina yesterday and she approved. @nic0lette - I will do another followup PR if you have any additional changes. |
No description provided.