Skip to content
This repository has been archived by the owner on Aug 23, 2023. It is now read-only.

Adds data layer for location background sample. #227

Merged
merged 2 commits into from
Mar 19, 2020
Merged

Conversation

codingjeremy
Copy link
Contributor

No description provided.

@codingjeremy codingjeremy changed the base branch from master to LocationUpdateUI March 13, 2020 03:06
@codingjeremy codingjeremy changed the base branch from LocationUpdateUI to master March 13, 2020 03:06
@codingjeremy
Copy link
Contributor Author

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.

Copy link

@nic0lette nic0lette left a 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

// 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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line not needed

Copy link

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.

Copy link
Contributor Author

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. :)

* 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,

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

Copy link
Contributor Author

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()

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, good call, moved.

MyLocationDatabase::class.java,
DATABASE_NAME
)
.build()

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

Copy link
Contributor Author

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(),

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

Copy link
Contributor Author

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 {

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link

@objcode objcode left a 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) {

Copy link

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
Copy link

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)

Copy link
Contributor Author

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.

*/
class MyLocationManager private constructor(private val context: Context) {

val trackingLocation: MutableLiveData<Boolean> by lazy {
Copy link

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
Copy link

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
Copy link

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 =

Copy link
Contributor Author

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.

@codingjeremy
Copy link
Contributor Author

PTAL, I think I addressed everything. Thank you again for reviewing!

Copy link
Contributor Author

@codingjeremy codingjeremy left a 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.

Copy link
Contributor Author

@codingjeremy codingjeremy left a 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.

Copy link

@objcode objcode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM nice one!

@codingjeremy
Copy link
Contributor Author

@objcode - Thanks!

Talked to Florina yesterday and she approved.

@nic0lette - I will do another followup PR if you have any additional changes.

@codingjeremy codingjeremy merged commit 2d141ce into master Mar 19, 2020
@codingjeremy codingjeremy deleted the add-data-layer branch March 19, 2020 17:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants