Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ permissions:

jobs:
lint:
if: github.event_name == 'workflow_dispatch' || github.event.action != 'closed'
if: github.event_name == 'workflow_dispatch' || github.event.action != 'closed' || github.event.pull_request.merged == true
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
package com.shreyash.sensorapp.presentation.compass

import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloat
import androidx.compose.animation.core.infiniteRepeatable
import androidx.compose.animation.core.rememberInfiniteTransition
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.aspectRatio
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.CenterAlignedTopAppBar
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
Expand All @@ -40,11 +28,9 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Path
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.nativeCanvas
import androidx.compose.ui.platform.LocalLifecycleOwner
Expand All @@ -57,7 +43,6 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.shreyash.sensorapp.presentation.theme.SensorAppTheme
import com.shreyash.sensorapp.presentation.theme.SensorGreen
import kotlin.math.cos
import kotlin.math.roundToInt
import kotlin.math.sin
Expand Down Expand Up @@ -133,10 +118,6 @@ private fun CompassScreenContent(
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
LiveIndicator()

Spacer(Modifier.height(24.dp))

CompassView(
heading = heading,
modifier = Modifier
Expand Down Expand Up @@ -170,43 +151,15 @@ private fun CompassScreenContent(
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
}

@Composable
private fun LiveIndicator() {
val infiniteTransition = rememberInfiniteTransition()
val alpha by infiniteTransition.animateFloat(
initialValue = 1f,
targetValue = 0.3f,
animationSpec = infiniteRepeatable(
animation = tween(800),
repeatMode = RepeatMode.Reverse
)
)
Card(
shape = RoundedCornerShape(20.dp),
colors = CardDefaults.cardColors(
containerColor = SensorGreen.copy(alpha = 0.15f)
)
) {
Row(
modifier = Modifier.padding(horizontal = 16.dp, vertical = 6.dp),
verticalAlignment = Alignment.CenterVertically
) {
Box(
modifier = Modifier
.size(10.dp)
.clip(CircleShape)
.background(SensorGreen.copy(alpha = alpha))
)
Spacer(Modifier.width(8.dp))
Spacer(Modifier.weight(1f))

Text(
text = "LIVE",
style = MaterialTheme.typography.labelLarge,
fontWeight = FontWeight.Bold,
color = SensorGreen
text = "Accuracy may be affected by nearby magnetic fields, electronic devices, or metal objects.",
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant.copy(alpha = 0.6f),
textAlign = TextAlign.Center,
modifier = Modifier.padding(horizontal = 16.dp, vertical = 8.dp)
)
}
}
Expand All @@ -230,16 +183,9 @@ private fun CompassView(
val outlineVariant = MaterialTheme.colorScheme.outlineVariant
val onSurface = MaterialTheme.colorScheme.onSurface
val primary = MaterialTheme.colorScheme.primary
val surface = MaterialTheme.colorScheme.surface

Card(
modifier = modifier,
shape = RoundedCornerShape(20.dp),
colors = CardDefaults.cardColors(
containerColor = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.3f)
),
elevation = CardDefaults.cardElevation(defaultElevation = 1.dp)
) {
Canvas(modifier = Modifier.fillMaxSize().padding(28.dp)) {
Canvas(modifier = modifier.padding(8.dp)) {
val cx = size.width / 2f
val cy = size.height / 2f
val r = minOf(cx, cy)
Expand Down Expand Up @@ -319,7 +265,7 @@ private fun CompassView(
drawPath(bottomIndicator, color = Color(0xFF444444))

drawCircle(
color = MaterialTheme.colorScheme.surface,
color = surface,
radius = 6.dp.toPx(),
center = Offset(cx, cy)
)
Expand All @@ -329,7 +275,6 @@ private fun CompassView(
center = Offset(cx, cy)
)
}
}
}

private fun directionFromHeading(heading: Float): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,25 @@ package com.shreyash.sensorapp.presentation.dashboard
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.shreyash.sensorapp.domain.model.SensorAvailability
import com.shreyash.sensorapp.domain.model.SensorReading
import com.shreyash.sensorapp.domain.model.SensorState
import com.shreyash.sensorapp.domain.model.SensorType
import com.shreyash.sensorapp.domain.usecase.GetSensorAvailabilityUseCase
import com.shreyash.sensorapp.domain.usecase.ObserveSensorUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class DashboardViewModel @Inject constructor(
private val getSensorAvailabilityUseCase: GetSensorAvailabilityUseCase,
private val observeSensorUseCase: ObserveSensorUseCase
private val getSensorAvailabilityUseCase: GetSensorAvailabilityUseCase
) : ViewModel() {

private val _sensorStates = MutableStateFlow<List<SensorState>>(emptyList())
val sensorStates: StateFlow<List<SensorState>> = _sensorStates.asStateFlow()

private val activeJobs = mutableMapOf<SensorType, Job>()

init {
viewModelScope.launch {
val sensorTypes = SensorType.entries
Expand All @@ -40,28 +33,6 @@ class DashboardViewModel @Inject constructor(
)
}
_sensorStates.value = states
states.filter { it.availability is SensorAvailability.Available }
.forEach { observeSensor(it.type) }
}
}

private fun observeSensor(sensorType: SensorType) {
activeJobs[sensorType] = viewModelScope.launch {
observeSensorUseCase(sensorType).collect { reading: SensorReading ->
_sensorStates.update { states ->
states.map { state ->
if (state.type == sensorType) {
state.copy(latestReading = reading)
} else state
}
}
}
}
}

override fun onCleared() {
activeJobs.values.forEach { it.cancel() }
activeJobs.clear()
super.onCleared()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.shreyash.sensorapp.presentation.detail

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.shreyash.sensorapp.domain.model.SensorReading
import com.shreyash.sensorapp.domain.model.SensorType

@Composable
fun AccelerometerScreen(
onBack: () -> Unit,
sensorType: SensorType = SensorType.ACCELEROMETER
) {
SensorDetailScaffold(sensorType = sensorType, onBack = onBack) { currentReading, chartReadings ->
AccelerometerContent(currentReading = currentReading, chartReadings = chartReadings)
}
}

@Composable
private fun AccelerometerContent(
currentReading: SensorReading?,
chartReadings: List<SensorReading>
) {
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState())
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
LiveIndicator()

Spacer(Modifier.height(16.dp))

LiveValueDisplay(
reading = currentReading,
sensorType = SensorType.ACCELEROMETER
)

Spacer(Modifier.height(24.dp))

LiveLineChart(
readings = chartReadings,
sensorType = SensorType.ACCELEROMETER,
modifier = Modifier.fillMaxWidth().height(260.dp)
)

Spacer(Modifier.height(12.dp))

Text(
text = "X: ${formatDetailValue(currentReading?.values?.getOrNull(0))} ${SensorType.ACCELEROMETER.unitX}" +
" Y: ${formatDetailValue(currentReading?.values?.getOrNull(1))} ${SensorType.ACCELEROMETER.unitY}" +
" Z: ${formatDetailValue(currentReading?.values?.getOrNull(2))} ${SensorType.ACCELEROMETER.unitZ}",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
textAlign = TextAlign.Center
)

Spacer(Modifier.height(16.dp))

SensorUsageHint(sensorType = SensorType.ACCELEROMETER)

Spacer(Modifier.height(16.dp))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.shreyash.sensorapp.presentation.detail

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.shreyash.sensorapp.domain.model.SensorReading
import com.shreyash.sensorapp.domain.model.SensorType

@Composable
fun GravityScreen(
onBack: () -> Unit,
sensorType: SensorType = SensorType.GRAVITY
) {
SensorDetailScaffold(sensorType = sensorType, onBack = onBack) { currentReading, chartReadings ->
GravityContent(currentReading = currentReading, chartReadings = chartReadings)
}
}

@Composable
private fun GravityContent(
currentReading: SensorReading?,
chartReadings: List<SensorReading>
) {
Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState())
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
LiveIndicator()

Spacer(Modifier.height(16.dp))

LiveValueDisplay(
reading = currentReading,
sensorType = SensorType.GRAVITY
)

Spacer(Modifier.height(24.dp))

LiveLineChart(
readings = chartReadings,
sensorType = SensorType.GRAVITY,
modifier = Modifier.fillMaxWidth().height(260.dp)
)

Spacer(Modifier.height(12.dp))

Text(
text = "X: ${formatDetailValue(currentReading?.values?.getOrNull(0))} ${SensorType.GRAVITY.unitX}" +
" Y: ${formatDetailValue(currentReading?.values?.getOrNull(1))} ${SensorType.GRAVITY.unitY}" +
" Z: ${formatDetailValue(currentReading?.values?.getOrNull(2))} ${SensorType.GRAVITY.unitZ}",
style = MaterialTheme.typography.bodyMedium,
color = MaterialTheme.colorScheme.onSurfaceVariant,
textAlign = TextAlign.Center
)

Spacer(Modifier.height(16.dp))

SensorUsageHint(sensorType = SensorType.GRAVITY)

Spacer(Modifier.height(16.dp))
}
}
Loading
Loading