Skip to content

Commit

Permalink
Cherry pick (#3243) | update kujaku dependency (#3282)
Browse files Browse the repository at this point in the history
* Update kujaku dependency (#3243)
* Mapview initializing fix
* Add Mapbox SDK token for ci/cd
* Add unit tests ✅

--------- 

Co-authored-by: Martin Ndegwa <[email protected]>
  • Loading branch information
aurangzaibumer and ndegwamartin authored Jun 19, 2024
1 parent eebb9a7 commit 8463098
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 23 deletions.
7 changes: 3 additions & 4 deletions android/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ plugins {
alias(libs.plugins.org.owasp.dependencycheck)
alias(libs.plugins.com.diffplug.spotless) apply false
alias(libs.plugins.android.junit5) apply false

}

tasks.dokkaHtmlMultiModule {
Expand All @@ -35,21 +34,21 @@ tasks.dokkaHtmlMultiModule {
}
}

apply(from = "mapbox.gradle.kts")

allprojects {
repositories {
gradlePluginPortal()
mavenLocal()
google()
mavenCentral()
maven(url = "https://oss.sonatype.org/content/repositories/snapshots")
maven(url = "https://s01.oss.sonatype.org/content/repositories/snapshots")
maven(url = "https://jcenter.bintray.com/")
apply(plugin = "org.owasp.dependencycheck")
tasks.dependencyCheckAggregate{
dependencyCheck.formats.add("XML")
}
configurations.all{
resolutionStrategy.force ("com.google.android.gms:play-services-location:19.0.1")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import com.mapbox.geojson.MultiPoint
import com.mapbox.geojson.Point
import com.mapbox.mapboxsdk.Mapbox
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory
import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException
import com.mapbox.mapboxsdk.geometry.LatLngBounds
import com.mapbox.mapboxsdk.maps.Style
import com.mapbox.mapboxsdk.style.expressions.Expression
Expand Down Expand Up @@ -91,7 +92,13 @@ class GeoWidgetFragment : Fragment() {
savedInstanceState: Bundle?,
): View {
Mapbox.getInstance(requireContext(), BuildConfig.MAPBOX_SDK_TOKEN)
return setupViews()
val view = setupViews()
mapView.onCreate(savedInstanceState)
return view
}

fun setKujakuMapview(kujakuMapView: KujakuMapView) {
mapView = kujakuMapView
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -124,24 +131,29 @@ class GeoWidgetFragment : Fragment() {
}

private fun setUpMapView(): KujakuMapView {
return KujakuMapView(requireActivity()).apply {
id = R.id.kujaku_widget
val builder = Style.Builder().fromUri(context.getString(R.string.style_map_fhir_core))
getMapAsync { mapboxMap ->
mapboxMap.setStyle(builder) { style ->
geoJsonSource = style.getSourceAs(context.getString(R.string.data_set_quest))
addIconsLayer(style)
addMapStyle(style)
if (geoJsonSource != null && featureCollection != null) {
geoJsonSource!!.setGeoJson(featureCollection)
return try {
KujakuMapView(requireActivity()).apply {
id = R.id.kujaku_widget
val builder = Style.Builder().fromUri(context.getString(R.string.style_map_fhir_core))
getMapAsync { mapboxMap ->
mapboxMap.setStyle(builder) { style ->
geoJsonSource = style.getSourceAs(context.getString(R.string.data_set_quest))
addIconsLayer(style)
addMapStyle(style)
if (geoJsonSource != null && featureCollection != null) {
geoJsonSource!!.setGeoJson(featureCollection)
}
}
}
}

if (showAddLocationButton) {
setOnAddLocationListener(this)
if (showAddLocationButton) {
setOnAddLocationListener(this)
}
setOnClickLocationListener(this)
}
setOnClickLocationListener(this)
} catch (e: MapboxConfigurationException) {
Timber.e(e)
mapView
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.smartregister.fhircore.geowidget.screens

import android.os.Build
import android.os.Bundle
import com.mapbox.geojson.FeatureCollection
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource
import dagger.hilt.android.testing.HiltAndroidRule
Expand Down Expand Up @@ -75,7 +76,7 @@ class GeoWidgetFragmentTest {
}

@Test
fun test_add_and_cancel_location_points() {
fun testAddAndCancelLocationPoints() {
// Mock dependencies
val mockFeatureCollection = mockk<FeatureCollection>(relaxed = true)
val mockGeoJsonSource = mockk<GeoJsonSource>(relaxed = true)
Expand All @@ -89,4 +90,28 @@ class GeoWidgetFragmentTest {
// Verify mocks
verify { kujakuMapView.addPoint(any(), any()) }
}

@Test
fun testOnCreateViewAddsSavedStateToMapView() {
val activity = Robolectric.buildActivity(GeoWidgetTestActivity::class.java).create().get()

val geowidgetFragment = GeoWidgetFragment()

var kujakuMapView = mockk<KujakuMapView>(relaxed = true)

geowidgetFragment.setKujakuMapview(kujakuMapView)

activity.supportFragmentManager
.beginTransaction()
.add(android.R.id.content, geowidgetFragment, "")
.commitNow()

every { kujakuMapView.parent } returns null

val savedInstanceBundle: Bundle = mockk()

geowidgetFragment.onCreateView(activity.layoutInflater, null, savedInstanceBundle)

verify { kujakuMapView.onCreate(savedInstanceBundle) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package org.smartregister.fhircore.geowidget.screens

import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint
import org.smartregister.fhircore.engine.util.annotation.ExcludeFromJacocoGeneratedReport

/** Created by Ephraim Kigamba - [email protected] on 22-08-2022. */
@ExcludeFromJacocoGeneratedReport
@AndroidEntryPoint
class GeoWidgetTestActivity : AppCompatActivity()
4 changes: 2 additions & 2 deletions android/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ knowledge = "0.1.0-alpha03-preview5-SNAPSHOT"
kotlin = "1.9.22"
kotlinx-coroutines = "1.7.3"
kotlinx-serialization-json = "1.6.0"
kujaku-library = "0.9.0"
kujaku-library = "0.10.2-SNAPSHOT"
ktlint = "0.50.0"
leakcanary-android = "2.10"
lifecycle= "2.7.0"
Expand Down Expand Up @@ -148,7 +148,7 @@ kotlinx-serialization-json = { group = "org.jetbrains.kotlinx", name = "kotlinx-
ktlint-main = { group = "com.pinterest", name = "ktlint", version.ref = "ktlint"}
ktlint-cli-ruleset = { group = "com.pinterest.ktlint", name = "ktlint-cli-ruleset-core", version.ref = "ktlint" }
ktlint-rule-engine-core = { group = "com.pinterest.ktlint", name = "ktlint-rule-engine-core", version.ref = "ktlint" }
kujaku-library = { group = "io.ona.kujaku", name = "library", version.ref = "kujaku-library" }
kujaku-library = { group = "io.ona.kujaku", name ="library", version.ref = "kujaku-library" }
leakcanary-android = { group = "com.squareup.leakcanary", name = "leakcanary-android", version.ref = "leakcanary-android" }
lifecycle-livedata-ktx = { group = "androidx.lifecycle", name = "lifecycle-livedata-ktx", version.ref = "lifecycle" }
lifecycle-viewmodel-compose = { group = "androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "lifecycle" }
Expand Down
32 changes: 32 additions & 0 deletions android/mapbox.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.InputStreamReader
import java.util.Properties

fun readProperties(file: String): Properties {
val properties = Properties()
val localProperties = File(file)
if (localProperties.isFile) {
InputStreamReader(FileInputStream(localProperties), Charsets.UTF_8).use { reader
->
properties.load(reader)
}
}
else throw FileNotFoundException("\u001B[34mFile $file not found\u001B[0m")

return properties
}

val mapboxSdkToken = System.getenv("MAPBOX_SDK_TOKEN") ?: readProperties((project.properties["localPropertiesFile"] ?: "${rootProject.projectDir}/local.properties").toString()).getProperty("MAPBOX_SDK_TOKEN")

allprojects {
repositories {
maven {
url = uri("https://api.mapbox.com/downloads/v2/releases/maven")
credentials.username = "mapbox"
credentials.password = mapboxSdkToken
authentication.create<BasicAuthentication>("basic")
}
}
}

0 comments on commit 8463098

Please sign in to comment.