Skip to content

Commit

Permalink
Wire "Route Start Play" up to RoutePlayer
Browse files Browse the repository at this point in the history
RoutePlayer is the initial code to playback routes, this change means
that clicking "Start Play" from within the RouteDetailsScreen will now
trigger starting playback.
  • Loading branch information
davecraig committed Feb 4, 2025
1 parent 086c669 commit c5c614e
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ class MainActivity : AppCompatActivity() {
intent.action = ""
}
}

// Pick the current route
setupCurrentRoute()
}
}
}
Expand Down Expand Up @@ -265,10 +262,6 @@ class MainActivity : AppCompatActivity() {
}
}

fun setupCurrentRoute() {
soundscapeServiceConnection.soundscapeService?.setupCurrentRoute()
}

/**
* Creates and starts the Soundscape foreground service.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import kotlinx.coroutines.flow.asStateFlow
import org.scottishtecharmy.soundscape.geojsonparser.geojson.LngLatAlt
import org.scottishtecharmy.soundscape.services.SoundscapeBinder
import org.scottishtecharmy.soundscape.services.SoundscapeService
import org.scottishtecharmy.soundscape.geoengine.utils.TileGrid
import javax.inject.Inject

@ActivityRetainedScoped
Expand Down Expand Up @@ -43,6 +42,10 @@ class SoundscapeServiceConnection @Inject constructor() {
soundscapeService?.setStreetPreviewMode(on, latitude, longitude)
}

fun startRoute(routeName: String) {
soundscapeService?.startRoute(routeName)
}

// needed to communicate with the service.
private val connection = object : ServiceConnection {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavController
import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.compose.rememberNavController
import org.scottishtecharmy.soundscape.R
import org.scottishtecharmy.soundscape.database.local.model.RouteData
Expand All @@ -54,6 +55,7 @@ fun RouteDetailsScreenVM(
modifier,
uiState,
getRouteByName = { viewModel.getRouteByName(routeName) },
startRoute = { viewModel.startRoute(routeName) },
clearErrorMessage = { viewModel.clearErrorMessage() }
)
}
Expand All @@ -65,6 +67,7 @@ fun RouteDetailsScreen(
modifier: Modifier,
uiState: RouteDetailsUiState,
getRouteByName: (routeName: String) -> Unit,
startRoute: (routeName: String) -> Unit,
clearErrorMessage: () -> Unit,
) {
// Observe the UI state from the ViewModel
Expand Down Expand Up @@ -151,7 +154,16 @@ fun RouteDetailsScreen(
iconText = stringResource(R.string.route_detail_action_start_route),
fontSize = 28.sp,
fontWeight = FontWeight.Bold,
onClick = { /*TODO*/ })
onClick = {
startRoute(route.name)
// Pop up to the home screen
navController.navigate(HomeRoutes.Home.route) {
popUpTo(navController.graph.findStartDestination().id) {
inclusive = true
}
launchSingleTop = true
}
})
IconWithTextButton(
icon = Icons.Default.Edit,
iconModifier = Modifier.size(40.dp),
Expand Down Expand Up @@ -202,6 +214,7 @@ fun RoutesDetailsPopulatedPreview() {
selectedRoute = RouteData("Route 2", "Description 2")
),
getRouteByName = {},
startRoute = {},
clearErrorMessage = {}
)
}
Expand All @@ -217,6 +230,7 @@ fun RoutesDetailsLoadingPreview() {
uiState = RouteDetailsUiState(isLoading = true),
modifier = Modifier,
getRouteByName = {},
startRoute = {},
clearErrorMessage = {}
)
}
Expand All @@ -232,6 +246,7 @@ fun RoutesDetailsEmptyPreview() {
modifier = Modifier,
uiState = RouteDetailsUiState(),
getRouteByName = {},
startRoute = {},
clearErrorMessage = {}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import org.scottishtecharmy.soundscape.SoundscapeServiceConnection
import org.scottishtecharmy.soundscape.database.repository.RoutesRepository
import javax.inject.Inject


@HiltViewModel
class RouteDetailsViewModel @Inject constructor(
private val routesRepository: RoutesRepository
private val routesRepository: RoutesRepository,
private val soundscapeServiceConnection: SoundscapeServiceConnection
) : ViewModel() {

private val _uiState = MutableStateFlow(RouteDetailsUiState())
val uiState: StateFlow<RouteDetailsUiState> = _uiState.asStateFlow()

Expand Down Expand Up @@ -61,6 +62,10 @@ class RouteDetailsViewModel @Inject constructor(
}
}

fun startRoute(routeName: String) {
soundscapeServiceConnection.startRoute(routeName)
}

fun clearErrorMessage() {
_uiState.value = _uiState.value.copy(errorMessage = null)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ class RoutePlayer(val service: SoundscapeService) {
private var currentMarker = -1
private val coroutineScope = CoroutineScope(Job())

fun setupCurrentRoute() {
fun startRoute(routeName: String) {
val realm = RealmConfiguration.getMarkersInstance()
val routesDao = RoutesDao(realm)
val routesRepository = RoutesRepository(routesDao)

Log.e(TAG, "setupCurrentRoute")
Log.e(TAG, "startRoute")
coroutineScope.launch {
val dbRoutes = routesRepository.getRoutes()
val dbRoutes = routesRepository.getRoute(routeName)
if(dbRoutes.isNotEmpty()) {
currentRouteData = dbRoutes[0].copyFromRealm()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class SoundscapeService : MediaSessionService() {
private var audioBeacon: Long = 0

// Geo engine
var geoEngine = GeoEngine()
private var geoEngine = GeoEngine()

// Flow to return beacon location
private val _beaconFlow = MutableStateFlow<LngLatAlt?>(null)
Expand Down Expand Up @@ -358,9 +358,9 @@ class SoundscapeService : MediaSessionService() {
}

private lateinit var routePlayer : RoutePlayer
fun setupCurrentRoute() {
// routePlayer = RoutePlayer(this)
// routePlayer.setupCurrentRoute()
fun startRoute(routeName: String) {
routePlayer = RoutePlayer(this)
routePlayer.startRoute(routeName)
}

/**
Expand Down

0 comments on commit c5c614e

Please sign in to comment.