Skip to content

Commit

Permalink
Merge pull request #7 from mutualmobile/development
Browse files Browse the repository at this point in the history
Add sensor implementation, onError, accuracy state and update groupId for dependency
  • Loading branch information
shubhamsinghmutualmobile committed Mar 26, 2023
2 parents 34841d4 + 2fe421d commit 7ad7419
Show file tree
Hide file tree
Showing 16 changed files with 613 additions and 398 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ dependencyResolutionManagement {

The library provides straightforward state methods for multiple sensors like Accelerometer, Gyroscope, etc (more mentioned below). Following is an example on how to get the current values from the `Accelerometer`:
```
val accelerometerState = rememberAccelerometerState()
val accelerometerState = rememberAccelerometerSensorState()
```
Use it in an example:
```
val accelerometerState = rememberAccelerometerState()
// Optional: You could also write: rememberAccelerometerState(sensorDelay = SensorDelay.Fastest) for fetching sensor data faster
val accelerometerState = rememberAccelerometerSensorState()
// Optional: You could also write: rememberAccelerometerSensorState(sensorDelay = SensorDelay.Fastest) for fetching sensor data faster
Text(
text = "Force X: ${accelerometerState.xForce}" +
Expand All @@ -71,13 +71,13 @@ Text(
ComposeSensors plans to support the following Android sensors:
Sensor | Status | Composable
------------- | ------------- | -------------
Accelerometer | ✅ | rememberAccelerometerState()
Magnetic Field | ✅ | rememberMagneticFieldState()
Gyroscope | ✅ | rememberGyroscopeState()
Light | ✅️ | rememberLightState()
Pressure | ✅️ | rememberPressureState()
Accelerometer | ✅ | rememberAccelerometerSensorState()
Magnetic Field | ✅ | rememberMagneticFieldSensorState()
Gyroscope | ✅ | rememberGyroscopeSensorState()
Light | ✅️ | rememberLightSensorState()
Pressure | ✅️ | rememberPressureSensorState()
Proximity | ⚠️ | WIP
Gravity | ️ | WIP
Gravity | ️ | rememberGravitySensorState()
Linear Acceleration | ⚠️ | WIP
Rotation Vector | ⚠️ | WIP
Relative Humidity | ⚠️ | WIP
Expand Down
4 changes: 2 additions & 2 deletions composesensors/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ SONATYPE_HOST=DEFAULT
RELEASE_SIGNING_ENABLED=true
SONATYPE_AUTOMATIC_RELEASE=true

GROUP=com.mutualmobile.composesensors
GROUP=com.mutualmobile
POM_ARTIFACT_ID=composesensors
VERSION_NAME=1.0.0-SNAPSHOT
VERSION_NAME=0.0.1-SNAPSHOT
VERSION_CODE=1

POM_NAME=Compose Sensors
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.mutualmobile.composesensors

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember

/**
* An acceleration sensor determines the acceleration that is applied to a device by measuring the
* forces that are applied to the sensor itself.
* @param xForce Acceleration force along the x axis (including gravity) in m/s^2. Defaults to 0f.
* @param yForce Acceleration force along the y axis (including gravity) in m/s^2. Defaults to 0f.
* @param zForce Acceleration force along the z axis (including gravity) in m/s^2. Defaults to 0f.
* @param isAvailable Whether the current device has an accelerometer sensor. Defaults to false.
* @param accuracy Accuracy factor of the accelerometer sensor. Defaults to 0.
*/
@Immutable
class AccelerometerSensorState internal constructor(
val xForce: Float = 0f,
val yForce: Float = 0f,
val zForce: Float = 0f,
val isAvailable: Boolean = false,
val accuracy: Int = 0,
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is AccelerometerSensorState) return false

if (xForce != other.xForce) return false
if (yForce != other.yForce) return false
if (zForce != other.zForce) return false
if (isAvailable != other.isAvailable) return false
if (accuracy != other.accuracy) return false

return true
}

override fun hashCode(): Int {
var result = xForce.hashCode()
result = 31 * result + yForce.hashCode()
result = 31 * result + zForce.hashCode()
result = 31 * result + isAvailable.hashCode()
result = 31 * result + accuracy.hashCode()
return result
}

override fun toString(): String {
return "AccelerometerSensorState(xForce=$xForce, yForce=$yForce, zForce=$zForce, " +
"isAvailable=$isAvailable, accuracy=$accuracy)"
}
}

/**
* Creates and [remember]s an instance of [AccelerometerSensorState].
* @param sensorDelay The rate at which the raw sensor data should be received.
* Defaults to [SensorDelay.Normal].
* @param onError Callback invoked on every error state.
*/
@Composable
fun rememberAccelerometerSensorState(
sensorDelay: SensorDelay = SensorDelay.Normal,
onError: (throwable: Throwable) -> Unit = {},
): AccelerometerSensorState {
val sensorState = rememberSensorState(
sensorType = SensorType.Accelerometer,
sensorDelay = sensorDelay,
onError = onError,
)
val accelerometerSensorState = remember { mutableStateOf(AccelerometerSensorState()) }

LaunchedEffect(
key1 = sensorState,
block = {
val sensorStateValues = sensorState.data
if (sensorStateValues.isNotEmpty()) {
accelerometerSensorState.value = AccelerometerSensorState(
xForce = sensorStateValues[0],
yForce = sensorStateValues[1],
zForce = sensorStateValues[2],
isAvailable = sensorState.isAvailable,
accuracy = sensorState.accuracy
)
}
}
)

return accelerometerSensorState.value
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.mutualmobile.composesensors

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember

/**
* Measures the force of gravity in m/s2 that is applied to a device on all three physical axes
* (x, y, z).
* @param xForce Force of gravity along the x axis. Defaults to 0f.
* @param yForce Force of gravity along the y axis. Defaults to 0f.
* @param zForce Force of gravity along the z axis. Defaults to 0f.
* @param isAvailable Whether the current device has a Gravity sensor. Defaults to false.
* @param accuracy Accuracy factor of the Gravity sensor. Defaults to 0.
*/
@Immutable
class GravitySensorState internal constructor(
val xForce: Float = 0f,
val yForce: Float = 0f,
val zForce: Float = 0f,
val isAvailable: Boolean = false,
val accuracy: Int = 0,
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is GravitySensorState) return false

if (xForce != other.xForce) return false
if (yForce != other.yForce) return false
if (zForce != other.zForce) return false
if (isAvailable != other.isAvailable) return false

return true
}

override fun hashCode(): Int {
var result = xForce.hashCode()
result = 31 * result + yForce.hashCode()
result = 31 * result + zForce.hashCode()
result = 31 * result + isAvailable.hashCode()
return result
}

override fun toString(): String {
return "GravitySensorState(xForce=$xForce, yForce=$yForce, " +
"zForce=$zForce, isAvailable=$isAvailable, accuracy=$accuracy)"
}
}

/**
* Creates and [remember]s an instance of [GravitySensorState].
* @param sensorDelay The rate at which the raw sensor data should be received.
* Defaults to [SensorDelay.Normal].
* @param onError Callback invoked on every error state.
*/
@Composable
fun rememberGravitySensorState(
sensorDelay: SensorDelay = SensorDelay.Normal,
onError: (throwable: Throwable) -> Unit = {},
): GravitySensorState {
val sensorState = rememberSensorState(
sensorType = SensorType.Gravity,
sensorDelay = sensorDelay,
onError = onError,
)
val gravitySensorState = remember { mutableStateOf(GravitySensorState()) }

LaunchedEffect(
key1 = sensorState,
block = {
val sensorStateValues = sensorState.data
if (sensorStateValues.isNotEmpty()) {
gravitySensorState.value = GravitySensorState(
xForce = sensorStateValues[0],
yForce = sensorStateValues[1],
zForce = sensorStateValues[2],
isAvailable = sensorState.isAvailable,
accuracy = sensorState.accuracy
)
}
}
)

return gravitySensorState.value
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.mutualmobile.composesensors

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember

/**
* Measures a device's rate of rotation in rad/s around each of the three physical axes
* (x, y, and z).
* @param xRotation Rate of rotation around the x axis. Defaults to 0f.
* @param yRotation Rate of rotation around the y axis. Defaults to 0f.
* @param zRotation Rate of rotation around the z axis. Defaults to 0f.
* @param isAvailable Whether the current device has a gyroscope sensor. Defaults to false.
* @param accuracy Accuracy factor of the gyroscope sensor. Defaults to 0.
*/
@Immutable
class GyroscopeSensorState internal constructor(
val xRotation: Float = 0f,
val yRotation: Float = 0f,
val zRotation: Float = 0f,
val isAvailable: Boolean = false,
val accuracy: Int = 0,
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is GyroscopeSensorState) return false

if (xRotation != other.xRotation) return false
if (yRotation != other.yRotation) return false
if (zRotation != other.zRotation) return false
if (isAvailable != other.isAvailable) return false

return true
}

override fun hashCode(): Int {
var result = xRotation.hashCode()
result = 31 * result + yRotation.hashCode()
result = 31 * result + zRotation.hashCode()
result = 31 * result + isAvailable.hashCode()
return result
}

override fun toString(): String {
return "GyroscopeSensorState(xRotation=$xRotation, yRotation=$yRotation, " +
"zRotation=$zRotation, isAvailable=$isAvailable, accuracy=$accuracy)"
}
}

/**
* Creates and [remember]s an instance of [GyroscopeSensorState].
* @param sensorDelay The rate at which the raw sensor data should be received.
* Defaults to [SensorDelay.Normal].
* @param onError Callback invoked on every error state.
*/
@Composable
fun rememberGyroscopeSensorState(
sensorDelay: SensorDelay = SensorDelay.Normal,
onError: (throwable: Throwable) -> Unit = {},
): GyroscopeSensorState {
val sensorState = rememberSensorState(
sensorType = SensorType.Gyroscope,
sensorDelay = sensorDelay,
onError = onError,
)
val gyroscopeSensorState = remember { mutableStateOf(GyroscopeSensorState()) }

LaunchedEffect(
key1 = sensorState,
block = {
val sensorStateValues = sensorState.data
if (sensorStateValues.isNotEmpty()) {
gyroscopeSensorState.value = GyroscopeSensorState(
xRotation = sensorStateValues[0],
yRotation = sensorStateValues[1],
zRotation = sensorStateValues[2],
isAvailable = sensorState.isAvailable,
accuracy = sensorState.accuracy
)
}
}
)

return gyroscopeSensorState.value
}

0 comments on commit 7ad7419

Please sign in to comment.