mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2025-12-24 01:44:23 +01:00
Remove CloseableViewModel
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
package no.nordicsemi.android.csc.data
|
||||
|
||||
import kotlinx.coroutines.channels.BufferOverflow
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import no.nordicsemi.android.csc.view.SpeedUnit
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
@@ -12,6 +15,9 @@ internal class CSCRepository @Inject constructor() {
|
||||
private val _data = MutableStateFlow(CSCData())
|
||||
val data: StateFlow<CSCData> = _data
|
||||
|
||||
private val _command = MutableSharedFlow<CSCServiceCommand>(extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_LATEST)
|
||||
val command = _command.asSharedFlow()
|
||||
|
||||
fun setWheelSize(wheelSize: Int, wheelSizeDisplay: String) {
|
||||
_data.tryEmit(_data.value.copy(
|
||||
wheelSize = wheelSize,
|
||||
@@ -44,6 +50,10 @@ internal class CSCRepository @Inject constructor() {
|
||||
_data.tryEmit(_data.value.copy(batteryLevel = batteryLevel))
|
||||
}
|
||||
|
||||
fun sendNewServiceCommand(workingMode: CSCServiceCommand) {
|
||||
_command.tryEmit(workingMode)
|
||||
}
|
||||
|
||||
fun clear() {
|
||||
_data.tryEmit(CSCData())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package no.nordicsemi.android.csc.data
|
||||
|
||||
internal sealed class CSCServiceCommand
|
||||
|
||||
internal data class SetWheelSizeCommand(val size: Int) : CSCServiceCommand()
|
||||
|
||||
internal object DisconnectCommand : CSCServiceCommand()
|
||||
@@ -70,12 +70,6 @@ internal class CSCManager(context: Context, private val dataHolder: CSCRepositor
|
||||
// CSC characteristic is required
|
||||
setNotificationCallback(cscMeasurementCharacteristic)
|
||||
.with(object : CyclingSpeedAndCadenceMeasurementDataCallback() {
|
||||
override fun onDataReceived(device: BluetoothDevice, data: Data) {
|
||||
log(LogContract.Log.Level.APPLICATION, "\"" + parse(data) + "\" received")
|
||||
|
||||
// Pass through received data
|
||||
super.onDataReceived(device, data)
|
||||
}
|
||||
|
||||
override fun getWheelCircumference(): Float {
|
||||
return wheelSize.toFloat()
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015, Nordic Semiconductor
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
||||
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
package no.nordicsemi.android.csc.repository
|
||||
|
||||
import no.nordicsemi.android.ble.data.Data
|
||||
|
||||
object CSCMeasurementParser {
|
||||
|
||||
private const val WHEEL_REV_DATA_PRESENT: Byte = 0x01 // 1 bit
|
||||
private const val CRANK_REV_DATA_PRESENT: Byte = 0x02 // 1 bit
|
||||
|
||||
@JvmStatic
|
||||
fun parse(data: Data): String {
|
||||
var offset = 0
|
||||
val flags = data.getByte(offset)!!.toInt() // 1 byte
|
||||
offset += 1
|
||||
val wheelRevPresent = flags and WHEEL_REV_DATA_PRESENT.toInt() > 0
|
||||
val crankRevPreset = flags and CRANK_REV_DATA_PRESENT.toInt() > 0
|
||||
var wheelRevolutions = 0
|
||||
var lastWheelEventTime = 0
|
||||
if (wheelRevPresent) {
|
||||
wheelRevolutions = data.getIntValue(Data.FORMAT_UINT32, offset)!!
|
||||
offset += 4
|
||||
lastWheelEventTime = data.getIntValue(Data.FORMAT_UINT16, offset)!! // 1/1024 s
|
||||
offset += 2
|
||||
}
|
||||
var crankRevolutions = 0
|
||||
var lastCrankEventTime = 0
|
||||
if (crankRevPreset) {
|
||||
crankRevolutions = data.getIntValue(Data.FORMAT_UINT16, offset)!!
|
||||
offset += 2
|
||||
lastCrankEventTime = data.getIntValue(Data.FORMAT_UINT16, offset)!!
|
||||
//offset += 2;
|
||||
}
|
||||
val builder = StringBuilder()
|
||||
if (wheelRevPresent) {
|
||||
builder.append("Wheel rev: ").append(wheelRevolutions).append(",\n")
|
||||
builder.append("Last wheel event time: ").append(lastWheelEventTime).append(",\n")
|
||||
}
|
||||
if (crankRevPreset) {
|
||||
builder.append("Crank rev: ").append(crankRevolutions).append(",\n")
|
||||
builder.append("Last crank event time: ").append(lastCrankEventTime).append(",\n")
|
||||
}
|
||||
if (!wheelRevPresent && !crankRevPreset) {
|
||||
builder.append("No wheel or crank data")
|
||||
}
|
||||
builder.setLength(builder.length - 2)
|
||||
return builder.toString()
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,31 @@
|
||||
package no.nordicsemi.android.csc.repository
|
||||
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import no.nordicsemi.android.csc.data.CSCRepository
|
||||
import no.nordicsemi.android.csc.data.DisconnectCommand
|
||||
import no.nordicsemi.android.csc.data.SetWheelSizeCommand
|
||||
import no.nordicsemi.android.service.ForegroundBleService
|
||||
import no.nordicsemi.android.utils.exhaustive
|
||||
import javax.inject.Inject
|
||||
|
||||
@AndroidEntryPoint
|
||||
internal class CSCService : ForegroundBleService() {
|
||||
|
||||
@Inject
|
||||
lateinit var dataHolder: CSCRepository
|
||||
lateinit var repository: CSCRepository
|
||||
|
||||
override val manager: CSCManager by lazy { CSCManager(this, dataHolder) }
|
||||
override val manager: CSCManager by lazy { CSCManager(this, repository) }
|
||||
|
||||
override fun onCreate() {
|
||||
super.onCreate()
|
||||
|
||||
repository.command.onEach {
|
||||
when (it) {
|
||||
DisconnectCommand -> stopSelf()
|
||||
is SetWheelSizeCommand -> manager.setWheelSize(it.size)
|
||||
}.exhaustive
|
||||
}.launchIn(scope)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package no.nordicsemi.android.csc.viewmodel
|
||||
|
||||
import androidx.lifecycle.ViewModel
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import no.nordicsemi.android.csc.data.CSCRepository
|
||||
import no.nordicsemi.android.csc.view.CSCViewEvent
|
||||
@@ -8,14 +9,13 @@ import no.nordicsemi.android.csc.view.OnDisconnectButtonClick
|
||||
import no.nordicsemi.android.csc.view.OnSelectedSpeedUnitSelected
|
||||
import no.nordicsemi.android.csc.view.OnShowEditWheelSizeDialogButtonClick
|
||||
import no.nordicsemi.android.csc.view.OnWheelSizeSelected
|
||||
import no.nordicsemi.android.theme.viewmodel.CloseableViewModel
|
||||
import no.nordicsemi.android.utils.exhaustive
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
internal class CSCViewModel @Inject constructor(
|
||||
private val dataHolder: CSCRepository
|
||||
) : CloseableViewModel() {
|
||||
) : ViewModel() {
|
||||
|
||||
val state = dataHolder.data
|
||||
|
||||
|
||||
Reference in New Issue
Block a user