-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new settings screen to change Sensor Update Frequency (#2317)
* Add option for faster sensor updates, and also while charging * Move settings to its own view, change logic to store in DB * Set default value * Try another way * Try new default method * Use correct default * Clean up * Review comments * Remove dependency on is_charging sensor * Switch to a better condition check per review * Fix db
- Loading branch information
Showing
18 changed files
with
326 additions
and
72 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
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
61 changes: 61 additions & 0 deletions
61
.../java/io/homeassistant/companion/android/settings/sensor/SensorUpdateFrequencyFragment.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,61 @@ | ||
package io.homeassistant.companion.android.settings.sensor | ||
|
||
import android.content.Intent | ||
import android.net.Uri | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.Menu | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.ui.platform.ComposeView | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.viewModels | ||
import com.google.android.material.composethemeadapter.MdcTheme | ||
import io.homeassistant.companion.android.R | ||
import io.homeassistant.companion.android.settings.SettingViewModel | ||
import io.homeassistant.companion.android.settings.sensor.views.SensorUpdateFrequencyView | ||
import io.homeassistant.companion.android.common.R as commonR | ||
|
||
class SensorUpdateFrequencyFragment : Fragment() { | ||
|
||
val viewModel: SettingViewModel by viewModels() | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setHasOptionsMenu(true) | ||
} | ||
|
||
override fun onPrepareOptionsMenu(menu: Menu) { | ||
super.onPrepareOptionsMenu(menu) | ||
|
||
menu.findItem(R.id.get_help)?.let { | ||
it.isVisible = true | ||
it.intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://companion.home-assistant.io/docs/core/sensors#android-sensors")) | ||
} | ||
} | ||
|
||
override fun onCreateView( | ||
inflater: LayoutInflater, | ||
container: ViewGroup?, | ||
savedInstanceState: Bundle? | ||
): View { | ||
return ComposeView(requireContext()).apply { | ||
setContent { | ||
MdcTheme { | ||
val settings = viewModel.getSettingFlow(0) | ||
.collectAsState(initial = viewModel.getSetting(0)) | ||
SensorUpdateFrequencyView( | ||
sensorUpdateFrequency = settings.value.sensorUpdateFrequency, | ||
onSettingChanged = { viewModel.updateSensorSetting(0, it) } | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun onResume() { | ||
super.onResume() | ||
activity?.title = getString(commonR.string.sensor_update_frequency) | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...ava/io/homeassistant/companion/android/settings/sensor/views/SensorUpdateFrequencyView.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,64 @@ | ||
package io.homeassistant.companion.android.settings.sensor.views | ||
|
||
import android.os.Build | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.rememberScrollState | ||
import androidx.compose.foundation.verticalScroll | ||
import androidx.compose.material.Divider | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.unit.dp | ||
import io.homeassistant.companion.android.common.R | ||
import io.homeassistant.companion.android.common.sensors.SensorWorkerBase | ||
import io.homeassistant.companion.android.database.settings.SensorUpdateFrequencySetting | ||
import io.homeassistant.companion.android.util.compose.InfoNotification | ||
import io.homeassistant.companion.android.util.compose.RadioButtonRow | ||
|
||
@Composable | ||
fun SensorUpdateFrequencyView( | ||
sensorUpdateFrequency: SensorUpdateFrequencySetting, | ||
onSettingChanged: (SensorUpdateFrequencySetting) -> Unit | ||
) { | ||
val scrollState = rememberScrollState() | ||
Column( | ||
modifier = Modifier | ||
.padding(20.dp) | ||
.verticalScroll(scrollState) | ||
) { | ||
Row( | ||
verticalAlignment = Alignment.CenterVertically, | ||
modifier = Modifier.padding(bottom = 20.dp) | ||
) { | ||
Text(stringResource(R.string.sensor_update_frequency_description)) | ||
} | ||
Divider() | ||
RadioButtonRow( | ||
text = stringResource(R.string.sensor_update_frequency_normal), | ||
selected = sensorUpdateFrequency == SensorUpdateFrequencySetting.NORMAL, | ||
onClick = { onSettingChanged(SensorUpdateFrequencySetting.NORMAL) } | ||
) | ||
RadioButtonRow( | ||
text = stringResource(R.string.sensor_update_frequency_fast_charging), | ||
selected = sensorUpdateFrequency == SensorUpdateFrequencySetting.FAST_WHILE_CHARGING, | ||
onClick = { onSettingChanged(SensorUpdateFrequencySetting.FAST_WHILE_CHARGING) } | ||
) | ||
RadioButtonRow( | ||
text = stringResource(R.string.sensor_update_frequency_fast_always), | ||
selected = sensorUpdateFrequency == SensorUpdateFrequencySetting.FAST_ALWAYS, | ||
onClick = { onSettingChanged(SensorUpdateFrequencySetting.FAST_ALWAYS) } | ||
) | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
InfoNotification( | ||
infoString = R.string.sensor_update_notification, | ||
channelId = SensorWorkerBase.channelId, | ||
buttonString = R.string.sensor_worker_notification_channel | ||
) | ||
} | ||
} | ||
} |
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
Oops, something went wrong.