-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from mutualmobile/development
Add sensor implementation, onError, accuracy state and update groupId for dependency
- Loading branch information
Showing
16 changed files
with
613 additions
and
398 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
composesensors/src/main/java/com/mutualmobile/composesensors/AccelerometerSensorState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
75 changes: 0 additions & 75 deletions
75
composesensors/src/main/java/com/mutualmobile/composesensors/AccelerometerState.kt
This file was deleted.
Oops, something went wrong.
87 changes: 87 additions & 0 deletions
87
composesensors/src/main/java/com/mutualmobile/composesensors/GravitySensorState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
87 changes: 87 additions & 0 deletions
87
composesensors/src/main/java/com/mutualmobile/composesensors/GyroscopeSensorState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.