Skip to content

Commit

Permalink
Merge pull request #410 from Assocify-Team/rb/gps
Browse files Browse the repository at this point in the history
Added GPS location on the map
  • Loading branch information
Polymeth authored Jun 2, 2024
2 parents 2ca7539 + d5e53b9 commit 96bf6ca
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.se.assocify.screens

import androidx.compose.ui.test.ExperimentalTestApi
import androidx.compose.ui.test.assertHasClickAction
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertIsNotDisplayed
Expand Down Expand Up @@ -102,26 +101,6 @@ class EventScreenTest : TestCase(kaspressoBuilder = Kaspresso.Builder.withCompos
}
}

@OptIn(ExperimentalTestApi::class)
@Test
fun testTabSwitching() {
composeTestRule.setContent {
EventScreen(mockNavActions, EventScreenViewModel(mockNavActions, mockTaskAPI, mockEventAPI))
}
with(composeTestRule) {
onNodeWithTag("scheduleTab").assertIsDisplayed()
onNodeWithTag("scheduleTab").performClick()
onNodeWithTag("scheduleTab").assertIsSelected()

onNodeWithTag("tasksTab").assertIsDisplayed()
onNodeWithTag("tasksTab").performClick()
onNodeWithTag("tasksTab").assertIsSelected()

onNodeWithTag("mapTab").assertIsDisplayed()
onNodeWithTag("mapTab").performClick()
}
}

@Test
fun testFilterChipShowAssoc() {
every { mockEventAPI.getEvents(any(), any()) } answers
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<application
android:allowBackup="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ fun EventScreen(navActions: NavigationActions, eventScreenViewModel: EventScreen
},
floatingActionButton = {
when (state.currentTab) {
EventPageIndex.Map -> {
/*TODO: implement for map screen*/
}
EventPageIndex.Map -> {}
else -> {
FloatingActionButton(
modifier = Modifier.testTag("floatingButtonEvent"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.github.se.assocify.ui.screens.event.maptab

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
Expand All @@ -13,6 +18,8 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.viewinterop.AndroidView
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleObserver
Expand All @@ -26,6 +33,13 @@ import org.osmdroid.views.CustomZoomButtonsController
import org.osmdroid.views.MapView
import org.osmdroid.views.overlay.Marker
import org.osmdroid.views.overlay.TilesOverlay
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay

// The location overlay to display the user's location
private var myLocationOverlay: MyLocationNewOverlay? = null
// Permission code for accessing GPS location
const val LOCATION_PERMISSION_REQUEST_CODE = 1000

/** A screen that displays a map of the event: location with the associated tasks. */
@Composable
Expand Down Expand Up @@ -84,6 +98,18 @@ fun rememberMapViewWithLifecycle(): MapView {
onDispose { lifecycle.removeObserver(lifecycleObserver) }
}

// Request location permissions and initialize myLocationOverlay
LaunchedEffect(Unit) {
val permissionGranted = requestLocationPermission(context)
if (permissionGranted) {
val myLocationProvider = GpsMyLocationProvider(context)
myLocationProvider.startLocationProvider(null)
myLocationOverlay = MyLocationNewOverlay(myLocationProvider, mapView)
myLocationOverlay?.enableMyLocation()
mapView.overlays.add(myLocationOverlay)
}
}

return mapView
}

Expand Down Expand Up @@ -148,3 +174,20 @@ fun EPFLMapView(
AndroidView(
factory = { mapViewState }, modifier = modifier, update = { view -> onLoad?.invoke(view) })
}

/**
* Displays a permission dialog to request location permission
*
* @param context the context to display the dialog
*/
fun requestLocationPermission(context: Context): Boolean {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(
context as Activity,
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
LOCATION_PERMISSION_REQUEST_CODE)
return false
}
return true
}

0 comments on commit 96bf6ca

Please sign in to comment.