Skip to content
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

Merge Feature/42 feature driver rates controls #44

Merged
merged 19 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions core/entities/src/commonMain/kotlin/business/Currency.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ interface Currency: Comparable<Currency> {
fun format(): String {
return form.format(amount)
}
operator fun plus(other: Currency): Currency {
when (other.form.centsPerUnit) {
form.centsPerUnit -> return Currency(amount + other.amount, form)
else -> {
val thisValue = amount
val otherValue = other.amount * other.form.centsPerUnit / form.centsPerUnit
return Currency(thisValue + otherValue, form)
}
}
}

sealed class Form(
val prefix: String,
Expand Down
4 changes: 4 additions & 0 deletions core/entities/src/commonMain/kotlin/geo/Length.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ interface Length: Comparable<Length> {

fun to(otherUnit: LengthUnit): Length
fun format(): String
operator fun plus(other: Length): Length {
val otherToThis = other.to(unit)
return LengthValue(value + otherToThis.value, unit)
}
}

enum class LengthUnit(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024 Pointyware. Use of this software is governed by the GPL-3.0 license.
*/

package org.pointyware.xyz.drive.data

import org.pointyware.xyz.core.entities.geo.LatLong
import org.pointyware.xyz.drive.RideFilter
import org.pointyware.xyz.drive.entities.DriverRates

/**
* Maintains Driver settings.
*/
interface DriverSettingsRepository {
/**
*
*/
fun getFilter(): RideFilter
/**
*
*/
fun setFilter(filter: RideFilter)
/**
*
*/
fun getDriverRates(): DriverRates
/**
*
*/
fun setDriverRates(rates: DriverRates)

fun getDriverLocation(): LatLong
fun setDriverLocation(location: LatLong)
}
16 changes: 16 additions & 0 deletions feature/drive/src/commonMain/kotlin/entities/DriverRates.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) 2024 Pointyware. Use of this software is governed by the GPL-3.0 license.
*/

package org.pointyware.xyz.drive.entities

import org.pointyware.xyz.core.entities.business.Rate

/**
* Rates for a driver's services.
*/
data class DriverRates(
val maintenanceCost: Rate,
val pickupCost: Rate,
val dropoffCost: Rate,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2024 Pointyware. Use of this software is governed by the GPL-3.0 license.
*/

package org.pointyware.xyz.drive.interactors

import org.pointyware.xyz.core.entities.geo.Length
import org.pointyware.xyz.drive.entities.DriverRates
import org.pointyware.xyz.drive.entities.Request

/**
* A request with estimated pickup distance and driver rates.
*/
data class EstimatedRequest(
val request: Request,
val pickupDistance: Length,
val driverRates: DriverRates
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 Pointyware. Use of this software is governed by the GPL-3.0 license.
*/

package org.pointyware.xyz.drive.interactors

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import org.pointyware.xyz.drive.data.DriverSettingsRepository
import org.pointyware.xyz.drive.data.RideRepository

/**
*
*/
class WatchRatedRequests(
private val repository: RideRepository,
private val driverSettingsRepository: DriverSettingsRepository
) {

suspend fun invoke(): Result<Flow<List<EstimatedRequest>>> {
val filter = driverSettingsRepository.getFilter()
val rates = driverSettingsRepository.getDriverRates()
val location = driverSettingsRepository.getDriverLocation()

repository.watchRequests(filter)
.onSuccess { flow ->
val estimatedFlow = flow.map { request ->
request.map {
EstimatedRequest(
it,
location.distanceTo(it.route.start.coordinates),
rates
)
}
}
return Result.success(estimatedFlow)
}
.onFailure {
return Result.failure(it)
}
throw IllegalStateException("Unreachable code")
}
}
81 changes: 74 additions & 7 deletions feature/drive/src/commonMain/kotlin/ui/RideRequestView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@

package org.pointyware.xyz.drive.ui

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import org.pointyware.xyz.drive.viewmodels.RideRequestUiState

/**
Expand All @@ -24,14 +30,75 @@ fun RideRequestView(
) {
Column(
modifier = modifier
.fillMaxWidth()
) {
Text("New Ride Request")
Text(state.riderName)
Text(state.route.start.name)
Text(state.route.end.name)
Text(state.distanceFromDriver.toString())
Text(state.route.distance.toString())
Text(state.totalFair.toString())
Row {
// AsyncImage(
// model = ImageRequest(),
// contentDescription = "Start location image",
// modifier = Modifier.fillMaxWidth()
// )
Text(
text = state.riderName,
modifier = Modifier.semantics {
contentDescription = "Rider Name: ${state.riderName}"
}
)
}
Row(
modifier = Modifier.semantics(mergeDescendants = true) {}
) {
Image(
imageVector = Icons.Default.PlayArrow,
contentDescription = "Pickup Location"
)
Text(
text = state.route.start.name,
)
}
Row(
modifier = Modifier.semantics(mergeDescendants = true) {
contentDescription = "Pickup Metrics"
}
) {
Text(
text = state.pickupDistance.format() + " @ " + state.pickupRate.format(),
)
Image(
imageVector = Icons.Default.PlayArrow,
contentDescription = null
)
Text(
text = state.pickupPrice.format() + " - " + state.pickupCost.format(),
)
}
Row(
modifier = Modifier.semantics(mergeDescendants = true) {}
) {
Image(
imageVector = Icons.Default.PlayArrow,
contentDescription = "Dropoff Location"
)
Text(
text = state.route.end.name,
)
}
Row(
modifier = Modifier.semantics(mergeDescendants = true) {
contentDescription = "Route Metrics"
}
) {
Text(
text = state.dropoffDistance.format() + " @ " + state.dropoffRate.format(),
)
Image(
imageVector = Icons.Default.PlayArrow,
contentDescription = null
)
Text(
text = state.dropoffPrice.format() + " - " + state.dropoffCost.format(),
)
}

Row {
Button(onClick = onReject) {
Expand Down
19 changes: 11 additions & 8 deletions feature/drive/src/commonMain/kotlin/viewmodels/DriveViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import kotlinx.coroutines.launch
import org.pointyware.xyz.core.entities.Uuid
import org.pointyware.xyz.core.entities.geo.LatLong
import org.pointyware.xyz.core.viewmodels.MapViewModelImpl
import org.pointyware.xyz.drive.SimpleRideFilter
import org.pointyware.xyz.drive.data.RideRepository
import org.pointyware.xyz.drive.interactors.WatchRatedRequests
import org.pointyware.xyz.drive.ui.DriveScreenState

/**
*
*/
class DriveViewModel(
private val repository: RideRepository
private val repository: RideRepository,
private val watchRatedRequests: WatchRatedRequests
): MapViewModelImpl() {

private val mutableState = MutableStateFlow<DriveScreenState>(DriveScreenState.AvailableRequests(emptyList()))
Expand All @@ -35,17 +36,19 @@ class DriveViewModel(
private fun watchRequests() {
requestsJob?.cancel()
requestsJob = viewModelScope.launch {
repository.watchRequests(SimpleRideFilter.Permissive)
watchRatedRequests.invoke()
.onSuccess { flow ->
flow.collect { requestList ->
mutableState.value = DriveScreenState.AvailableRequests(
requests = requestList.map {
val request = it.request
RideRequestUiStateImpl(
requestId = it.rideId,
riderName = it.rider.name.given,
route = it.route,
distanceFromDriver = it.route.start.coordinates.distanceTo(driverLocation),
riderServiceRate = it.rate
requestId = request.rideId,
riderName = request.rider.name.given,
route = request.route,
riderServiceRate = request.rate,
it.pickupDistance,
it.driverRates
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import org.pointyware.xyz.core.entities.Uuid
import org.pointyware.xyz.core.entities.business.Currency
import org.pointyware.xyz.core.entities.business.Rate
import org.pointyware.xyz.core.entities.geo.Length
import org.pointyware.xyz.core.entities.geo.Location
import org.pointyware.xyz.core.entities.geo.Route
import org.pointyware.xyz.drive.entities.DriverRates

/**
* Displays information about a new ride request: distance from driver, distance of route, and rider service rate.
Expand All @@ -18,18 +18,59 @@ interface RideRequestUiState {
val requestId: Uuid
val riderName: String
val route: Route
val distanceFromDriver: Length
val riderServiceRate: Rate
val totalFair: Currency

val pickupDistance: Length
val dropoffDistance: Length
val totalDistance: Length

val pickupRate: Rate
val dropoffRate: Rate
val maintenanceRate: Rate

val pickupPrice: Currency
val dropoffPrice: Currency

val pickupCost: Currency
val dropoffCost: Currency

val grossProfit: Currency
}

data class RideRequestUiStateImpl(
override val requestId: Uuid,
override val riderName: String,
override val route: Route,
override val distanceFromDriver: Length,
override val riderServiceRate: Rate,

override val pickupDistance: Length,
val driverRates: DriverRates
): RideRequestUiState {
override val totalFair: Currency
get() = riderServiceRate * route.distance

override val dropoffDistance: Length
get() = route.distance
override val totalDistance: Length
get() = pickupDistance + dropoffDistance

override val pickupRate: Rate
get() = driverRates.pickupCost
override val dropoffRate: Rate
get() = driverRates.dropoffCost
override val maintenanceRate: Rate
get() = driverRates.maintenanceCost

override val pickupPrice: Currency
get() = pickupRate * pickupDistance
override val dropoffPrice: Currency
get() = dropoffRate * dropoffDistance
override val grossProfit: Currency
get() = pickupPrice + dropoffPrice

override val pickupCost: Currency
get() = maintenanceRate * pickupDistance
override val dropoffCost: Currency
get() = maintenanceRate * dropoffDistance
}
Loading