-
Notifications
You must be signed in to change notification settings - Fork 141
Add usecase to get Loi reports #3605
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a373cff
move LoiReport to sepparate package
andreia-ferreira da0fc50
move LoiProperties to domain
andreia-ferreira 0bfdb80
add usecase to get the loi report
andreia-ferreira c587e3b
add unit tests
andreia-ferreira 70aaf74
fix codestyle
andreia-ferreira 2bd3cd0
remove unused code
andreia-ferreira 632f89e
apply suggestions for usecase
andreia-ferreira 1bed725
apply suggestions for di module
andreia-ferreira e346529
add documentation
andreia-ferreira 6d587b0
Merge branch 'master' into andreia/3575/add-usecase
andreia-ferreira 222107b
update documentation
andreia-ferreira File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
app/src/main/java/org/groundplatform/android/di/DomainModule.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.groundplatform.android.di | ||
|
|
||
| import dagger.Binds | ||
| import dagger.Module | ||
| import dagger.Provides | ||
| import dagger.hilt.InstallIn | ||
| import dagger.hilt.components.SingletonComponent | ||
| import org.groundplatform.android.repository.LoiDataProvider | ||
| import org.groundplatform.domain.usecases.GetLoiReportUseCase | ||
| import org.groundplatform.domain.usecases.LoiDataProviderInterface | ||
|
|
||
| @InstallIn(SingletonComponent::class) | ||
| @Module | ||
| internal abstract class DomainModule { | ||
| @Binds abstract fun bindLoiDataProvider(impl: LoiDataProvider): LoiDataProviderInterface | ||
|
|
||
| companion object { | ||
| @Provides | ||
| fun provideGetLoiReportUseCase(loiDataProvider: LoiDataProviderInterface) = | ||
| GetLoiReportUseCase(loiDataProvider) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
app/src/main/java/org/groundplatform/android/repository/LoiDataProvider.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.groundplatform.android.repository | ||
|
|
||
| import javax.inject.Inject | ||
| import org.groundplatform.domain.usecases.LoiDataProviderInterface | ||
|
|
||
| class LoiDataProvider | ||
| @Inject | ||
| constructor(private val locationOfInterestRepository: LocationOfInterestRepository) : | ||
| LoiDataProviderInterface { | ||
| override suspend fun get(surveyId: String, loiId: String): LoiDataProviderInterface.LoiData? { | ||
| val loi = locationOfInterestRepository.getOfflineLoi(surveyId, loiId) | ||
| return loi?.let { | ||
| LoiDataProviderInterface.LoiData(geometry = it.geometry, properties = it.properties) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...ommonMain/kotlin/org/groundplatform/domain/model/locationofinterest/LocationOfInterest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.groundplatform.domain.model.locationofinterest | ||
|
|
||
| /** Alias for a map of properties with string names. */ | ||
| typealias LoiProperties = Map<String, Any> | ||
|
|
||
| const val LOI_NAME_PROPERTY = "name" | ||
|
|
||
| fun generateProperties(loiName: String? = null): LoiProperties = | ||
| loiName?.let { mapOf(LOI_NAME_PROPERTY to it) } ?: mapOf() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
core/domain/src/commonMain/kotlin/org/groundplatform/domain/usecases/GetLoiReportUseCase.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * Copyright 2026 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.groundplatform.domain.usecases | ||
|
|
||
| import kotlinx.serialization.json.JsonArray | ||
| import kotlinx.serialization.json.JsonElement | ||
| import kotlinx.serialization.json.JsonObject | ||
| import kotlinx.serialization.json.JsonPrimitive | ||
| import org.groundplatform.domain.model.geometry.Coordinates | ||
| import org.groundplatform.domain.model.geometry.Geometry | ||
| import org.groundplatform.domain.model.geometry.LineString | ||
| import org.groundplatform.domain.model.geometry.LinearRing | ||
| import org.groundplatform.domain.model.geometry.MultiPolygon | ||
| import org.groundplatform.domain.model.geometry.Point | ||
| import org.groundplatform.domain.model.geometry.Polygon | ||
| import org.groundplatform.domain.model.locationofinterest.LoiProperties | ||
| import org.groundplatform.domain.model.locationofinterest.LoiReport | ||
|
|
||
| /** | ||
| * Use case that generates a [LoiReport] containing the LOI geometry and properties as a GeoJSON. | ||
| * | ||
| * Supported geometry types: [Point], [LineString], [Polygon], [MultiPolygon]. | ||
| */ | ||
| class GetLoiReportUseCase(private val loiGeometryProvider: LoiDataProviderInterface) { | ||
shobhitagarwal1612 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * Returns a [LoiReport] for the given LOI, or `null` if it does not exist. | ||
| * | ||
| * @param loiId the identifier of the location of interest. | ||
| * @param surveyId the identifier of the survey the LOI belongs to. | ||
| * @throws IllegalStateException if the LOI geometry is a bare [LinearRing]. | ||
| */ | ||
| suspend operator fun invoke(loiId: String, surveyId: String): LoiReport? { | ||
| val loiData = loiGeometryProvider.get(surveyId, loiId) | ||
| return loiData?.let { LoiReport(it.geometry.toGeoJson(it.properties)) } | ||
| } | ||
|
|
||
| /** | ||
| * Converts a [Geometry] to its GeoJSON representation as defined by | ||
| * [RFC 7946](https://datatracker.ietf.org/doc/html/rfc7946). | ||
| */ | ||
| private fun Geometry.toGeoJson(loiProperties: LoiProperties): JsonObject { | ||
| val geometryJson = | ||
| when (this) { | ||
| is Point -> geoJsonObject(TYPE_POINT, coordinatesToPosition(coordinates)) | ||
| is LineString -> geoJsonObject(TYPE_LINE_STRING, coordinatesToPositions(coordinates)) | ||
| is LinearRing -> | ||
| error( | ||
| "LinearRing cannot be exported as GeoJSON. They are only used inside Polygon coordinates." | ||
| ) | ||
| is Polygon -> geoJsonObject(TYPE_POLYGON, polygonToCoordinates(this)) | ||
| is MultiPolygon -> | ||
| geoJsonObject(TYPE_MULTI_POLYGON, JsonArray(polygons.map { polygonToCoordinates(it) })) | ||
| } | ||
| return JsonObject( | ||
| mapOf( | ||
| KEY_TYPE to JsonPrimitive(TYPE_FEATURE), | ||
| KEY_PROPERTIES to JsonObject(loiProperties.mapValues { it.value.toJsonPrimitive() }), | ||
| KEY_GEOMETRY to geometryJson, | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| private fun Any.toJsonPrimitive(): JsonPrimitive = | ||
| when (this) { | ||
| is String -> JsonPrimitive(this) | ||
| is Number -> JsonPrimitive(this) | ||
| is Boolean -> JsonPrimitive(this) | ||
| else -> JsonPrimitive(this.toString()) | ||
| } | ||
|
|
||
| private fun geoJsonObject(type: String, coordinates: JsonElement): JsonObject = | ||
| JsonObject(mapOf(KEY_TYPE to JsonPrimitive(type), KEY_COORDINATES to coordinates)) | ||
|
|
||
| /** Converts a single [Coordinates] to a GeoJSON position: [lng, lat]. */ | ||
| private fun coordinatesToPosition(coordinates: Coordinates): JsonArray = | ||
| JsonArray(listOf(JsonPrimitive(coordinates.lng), JsonPrimitive(coordinates.lat))) | ||
|
|
||
| /** Converts a list of [Coordinates] to a GeoJSON array of positions. */ | ||
| private fun coordinatesToPositions(coordinates: List<Coordinates>): JsonArray = | ||
| JsonArray(coordinates.map { coordinatesToPosition(it) }) | ||
|
|
||
| /** Converts a [Polygon] to its GeoJSON coordinates (shell + holes as ring arrays). */ | ||
| private fun polygonToCoordinates(polygon: Polygon): JsonArray { | ||
| val rings = mutableListOf(coordinatesToPositions(polygon.shell.coordinates)) | ||
| polygon.holes.forEach { rings.add(coordinatesToPositions(it.coordinates)) } | ||
| return JsonArray(rings) | ||
| } | ||
|
|
||
| private companion object { | ||
| const val KEY_TYPE = "type" | ||
| const val TYPE_FEATURE = "Feature" | ||
| const val KEY_PROPERTIES = "properties" | ||
| const val KEY_GEOMETRY = "geometry" | ||
| const val KEY_COORDINATES = "coordinates" | ||
| const val TYPE_POINT = "Point" | ||
| const val TYPE_LINE_STRING = "LineString" | ||
| const val TYPE_POLYGON = "Polygon" | ||
| const val TYPE_MULTI_POLYGON = "MultiPolygon" | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.