Skip to content

Commit

Permalink
#17 in progress - map
Browse files Browse the repository at this point in the history
  • Loading branch information
mjureczko committed Mar 19, 2024
1 parent 8174adc commit cdedad2
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 13 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test_app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ jobs:
- name: Setup JDK
uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: '11'
distribution: 'adopt'
java-version: '17'

- name: Setup Android SDK
uses: android-actions/setup-android@v2
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Mały Poszukiwacz Skarbów

GPLv3 (https://www.gnu.org/licenses/gpl-3.0.html)


TODO:
- add screens titles
-- on searching screen title show which treasure is being searched
-
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import dagger.hilt.android.AndroidEntryPoint
import pl.marianjureczko.poszukiwacz.R
import pl.marianjureczko.poszukiwacz.activity.map.n.MapScreen
import pl.marianjureczko.poszukiwacz.activity.map.n.PARAMETER_ROUTE_NAME_2
import pl.marianjureczko.poszukiwacz.activity.photo.n.PARAMETER_TIP_PHOTO
import pl.marianjureczko.poszukiwacz.activity.photo.n.TipPhotoScreen
import pl.marianjureczko.poszukiwacz.activity.result.n.PARAMETER_RESULT_TYPE
Expand Down Expand Up @@ -79,7 +81,8 @@ private fun ComposeRoot(settings: Settings, resources: Resources, onClickGuide:
resources = resources,
onClickOnGuide = onClickGuide,
goToTipPhoto = { navController.navigate("tipPhoto/$it") },
goToResult = { navController.navigate("result/$it") }
goToResult = { navController.navigate("result/$it") },
goToMap = { navController.navigate("map/$it") }
)
}
composable(
Expand All @@ -92,5 +95,11 @@ private fun ComposeRoot(settings: Settings, resources: Resources, onClickGuide:
) {
TipPhotoScreen(navController = navController, onClickOnGuide = onClickGuide)
}
composable(
route = "map/{$PARAMETER_ROUTE_NAME_2}",
arguments = listOf(navArgument(PARAMETER_ROUTE_NAME) { type = NavType.StringType }),
) {
MapScreen(navController = navController, onClickOnGuide = onClickGuide, resources = resources)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package pl.marianjureczko.poszukiwacz.activity.map.n

import android.annotation.SuppressLint
import android.content.res.Resources
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.viewinterop.AndroidView
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.mapbox.bindgen.Value
import com.mapbox.maps.MapView
import com.mapbox.maps.Style
import pl.marianjureczko.poszukiwacz.App
import pl.marianjureczko.poszukiwacz.model.Route
import pl.marianjureczko.poszukiwacz.shared.MapHelper
import pl.marianjureczko.poszukiwacz.ui.components.TopBar

@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun MapScreen(
navController: NavController?,
resources: Resources,
onClickOnGuide: () -> Unit
) {
Scaffold(
topBar = { TopBar(navController, onClickOnGuide) },
content = {
MapScreenBody(resources)
}
)
}

@Composable
fun MapScreenBody(resources: Resources) {
val viewModel: MapViewModel = hiltViewModel()
val state = viewModel.state.value
MapboxMap(state.route, resources)
}

@Composable
fun MapboxMap(route: Route, resources: Resources) {
val mapView = MapView(App.getAppContext())

MapHelper.renderTreasures(route, mapView, resources)
mapView.getMapboxMap().loadStyleUri(Style.MAPBOX_STREETS) { style -> hideRoads(style) }

AndroidView({ mapView }, Modifier.fillMaxSize())
MapHelper.positionMapOnTreasures(route, mapView, 400.0)
}

private fun hideRoads(style: Style) {
style.styleLayers.asSequence()
.filter { l -> l.id.startsWith("road") }
.forEach { l ->
style.setStyleLayerProperty(l.id, "visibility", Value("none"))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package pl.marianjureczko.poszukiwacz.activity.map.n

import pl.marianjureczko.poszukiwacz.model.Route

data class MapState(
val route: Route
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package pl.marianjureczko.poszukiwacz.activity.map.n

import androidx.compose.runtime.MutableState
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import pl.marianjureczko.poszukiwacz.model.Route
import pl.marianjureczko.poszukiwacz.shared.StorageHelper
import javax.inject.Inject

const val PARAMETER_ROUTE_NAME_2 = "route_name"

@HiltViewModel
class MapViewModel @Inject constructor(
private val storageHelper: StorageHelper,
private val stateHandle: SavedStateHandle
) : ViewModel() {

private var _state: MutableState<MapState> = mutableStateOf(createState())

val state: State<MapState>
get() = _state

private fun createState(): MapState {
val routeName = stateHandle.get<String>(PARAMETER_ROUTE_NAME_2)!!
return MapState(loadRoute(routeName))
}

private fun loadRoute(routeName: String): Route {
// return Route(
// "Kalinowice", mutableListOf(
// TreasureDescription(
// 1, 25.1, 26.1, "g01abc", null, null
// )
// )
// )
return storageHelper.loadRoute(routeName)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import kotlinx.coroutines.launch
import pl.marianjureczko.poszukiwacz.App
import pl.marianjureczko.poszukiwacz.R
import pl.marianjureczko.poszukiwacz.activity.result.n.ResultType
import pl.marianjureczko.poszukiwacz.model.Route
import pl.marianjureczko.poszukiwacz.model.TreasureDescription
import pl.marianjureczko.poszukiwacz.shared.errorTone
import pl.marianjureczko.poszukiwacz.ui.Screen.dh
Expand All @@ -62,14 +63,15 @@ fun SearchingScreen(
resources: Resources,
onClickOnGuide: () -> Unit,
goToTipPhoto: (String) -> Unit,
goToResult: (ResultType) -> Unit
goToResult: (ResultType) -> Unit,
goToMap: (String) -> Unit
) {
val scaffoldState: ScaffoldState = rememberScaffoldState()
Scaffold(
scaffoldState = scaffoldState,
topBar = { TopBar(navController, onClickOnGuide) },
content = {
SearchingScreenBody(isClassicMode, resources, scaffoldState, goToTipPhoto, goToResult)
SearchingScreenBody(isClassicMode, resources, scaffoldState, goToTipPhoto, goToResult, goToMap)
}
)
}
Expand All @@ -81,7 +83,8 @@ private fun SearchingScreenBody(
resources: Resources,
scaffoldState: ScaffoldState,
goToTipPhoto: (String) -> Unit,
goToResult: (ResultType) -> Unit
goToResult: (ResultType) -> Unit,
goToMap: (String) -> Unit
) {
val viewModel: SearchingViewModel = hiltViewModel()
val state = viewModel.state.value
Expand All @@ -100,7 +103,16 @@ private fun SearchingScreenBody(
Scores(isClassicMode, state.treasuresProgress.knowledge)
Compass(state.needleRotation)
Steps(state.stepsToTreasure)
Buttons(scanQrCallback, state.currentTreasure, scaffoldState, resources, state.mediaPlayer, goToTipPhoto)
Buttons(
scanQrCallback,
state.currentTreasure,
state.route,
scaffoldState,
resources,
state.mediaPlayer,
goToTipPhoto,
goToMap
)
Spacer(
modifier = Modifier
.weight(0.01f)
Expand Down Expand Up @@ -228,10 +240,12 @@ fun Steps(stepsToTreasure: Int?) {
fun Buttons(
scanQrCallback: () -> Unit,
currentTreasure: TreasureDescription,
route: Route,
scaffoldState: ScaffoldState,
resources: Resources,
mediaPlayer: MediaPlayer,
goToTipPhoto: (String) -> Unit
goToTipPhoto: (String) -> Unit,
goToMap: (String) -> Unit
) {
val snackbarCoroutineScope: CoroutineScope = rememberCoroutineScope()
Row(
Expand All @@ -243,7 +257,7 @@ fun Buttons(
verticalAlignment = Alignment.CenterVertically,
) {
Column(modifier = Modifier.width(0.2.dw)) {
ShowMapButton()
ShowMapButton(route, goToMap)
ChangeTreasureButton()
}
Column(modifier = Modifier.width(0.6.dw)) {
Expand All @@ -257,10 +271,12 @@ fun Buttons(
}

@Composable
private fun ShowMapButton() {
private fun ShowMapButton(route: Route, goToMap: (String) -> Unit) {
Image(
painterResource(R.drawable.map),
modifier = Modifier.padding(10.dp),
modifier = Modifier
.padding(10.dp)
.clickable { goToMap(route.name) },
contentDescription = null,
contentScale = ContentScale.Inside,
)
Expand Down Expand Up @@ -329,7 +345,7 @@ private fun SoundTipButton(
modifier = Modifier
.padding(10.dp)
.clickable {
if(currentTreasure.tipFileName != null) {
if (currentTreasure.tipFileName != null) {
SoundTipPlayer.playSound(mediaPlayer, currentTreasure.tipFileName!!)
} else {
errorTone()
Expand All @@ -347,6 +363,6 @@ private fun SoundTipButton(
@Composable
fun SearchingDefaultPreview() {
AppTheme {
SearchingScreen(null, false, App.getResources(), {}, {}, {})
SearchingScreen(null, false, App.getResources(), {}, {}, {}, {})
}
}

0 comments on commit cdedad2

Please sign in to comment.