Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix runBlocking in coroutines #4340

Merged
merged 6 commits into from
Apr 20, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,7 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {
}
}

private fun sendLocationUpdate(location: Location, serverId: Int, trigger: LocationUpdateTrigger?) {
private suspend fun sendLocationUpdate(location: Location, serverId: Int, trigger: LocationUpdateTrigger?) {
Log.d(
TAG,
"Last Location: " +
Expand All @@ -844,35 +844,33 @@ class LocationSensorManager : BroadcastReceiver(), SensorManager {
val updateLocation: UpdateLocation
val updateLocationAs: String
val updateLocationString: String
runBlocking {
updateLocationAs = getSendLocationAsSetting(serverId)
if (updateLocationAs == SEND_LOCATION_AS_ZONE_ONLY) {
val zones = getZones(serverId)
val locationZone = zones
.filter { !it.attributes.passive && it.containsWithAccuracy(location) }
.minByOrNull { it.attributes.radius }
updateLocation = UpdateLocation(
gps = null,
gpsAccuracy = null,
locationName = locationZone?.entityId?.split(".")?.get(1) ?: ZONE_NAME_NOT_HOME,
speed = null,
altitude = null,
course = null,
verticalAccuracy = null
)
updateLocationString = updateLocation.locationName!!
} else {
updateLocation = UpdateLocation(
gps = arrayOf(location.latitude, location.longitude),
gpsAccuracy = accuracy,
locationName = null,
speed = location.speed.toInt(),
altitude = location.altitude.toInt(),
course = location.bearing.toInt(),
verticalAccuracy = if (Build.VERSION.SDK_INT >= 26) location.verticalAccuracyMeters.toInt() else 0
)
updateLocationString = updateLocation.gps.contentToString()
}
updateLocationAs = getSendLocationAsSetting(serverId)
if (updateLocationAs == SEND_LOCATION_AS_ZONE_ONLY) {
val zones = getZones(serverId)
val locationZone = zones
.filter { !it.attributes.passive && it.containsWithAccuracy(location) }
.minByOrNull { it.attributes.radius }
updateLocation = UpdateLocation(
gps = null,
gpsAccuracy = null,
locationName = locationZone?.entityId?.split(".")?.get(1) ?: ZONE_NAME_NOT_HOME,
speed = null,
altitude = null,
course = null,
verticalAccuracy = null
)
updateLocationString = updateLocation.locationName!!
} else {
updateLocation = UpdateLocation(
gps = arrayOf(location.latitude, location.longitude),
gpsAccuracy = accuracy,
locationName = null,
speed = location.speed.toInt(),
altitude = location.altitude.toInt(),
course = location.bearing.toInt(),
verticalAccuracy = if (Build.VERSION.SDK_INT >= 26) location.verticalAccuracyMeters.toInt() else 0
)
updateLocationString = updateLocation.gps.contentToString()
}

val now = System.currentTimeMillis()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.annotation.RequiresApi
import io.homeassistant.companion.android.common.R as commonR
import io.homeassistant.companion.android.common.data.integration.Entity
import io.homeassistant.companion.android.common.data.integration.IntegrationRepository
import kotlinx.coroutines.runBlocking

@RequiresApi(Build.VERSION_CODES.R)
object DefaultSliderControl : HaControl {
Expand Down Expand Up @@ -45,16 +44,14 @@ object DefaultSliderControl : HaControl {
integrationRepository: IntegrationRepository,
action: ControlAction
): Boolean {
return runBlocking {
integrationRepository.callService(
action.templateId.split(".")[0],
"set_value",
hashMapOf(
"entity_id" to action.templateId,
"value" to (action as? FloatAction)?.newValue.toString()
)
integrationRepository.callService(
action.templateId.split(".")[0],
"set_value",
hashMapOf(
"entity_id" to action.templateId,
"value" to (action as? FloatAction)?.newValue.toString()
)
return@runBlocking true
}
)
return true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class LanguagesManager @Inject constructor(
private const val SYSTEM_MANAGES_LOCALE = "system_managed"
}

fun getCurrentLang(): String {
return runBlocking {
suspend fun getCurrentLang(): String {
return run {
val lang = prefs.getCurrentLang()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
migrateLangSetting()
Expand All @@ -38,8 +38,8 @@ class LanguagesManager @Inject constructor(
}
}

fun saveLang(lang: String?) {
return runBlocking {
suspend fun saveLang(lang: String?) {
return run {
if (!lang.isNullOrEmpty()) {
val currentLang = getCurrentLang()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class ThemesManager @Inject constructor(
private val themesUseCase: PrefsRepository
) {

fun getCurrentTheme(): String {
return runBlocking {
suspend fun getCurrentTheme(): String {
return run {
val theme = themesUseCase.getCurrentTheme()
if (theme.isNullOrEmpty()) {
val toSetTheme = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
Expand All @@ -32,8 +32,8 @@ class ThemesManager @Inject constructor(
}
}

fun saveTheme(theme: String?) {
return runBlocking {
suspend fun saveTheme(theme: String?) {
return run {
if (!theme.isNullOrEmpty()) {
val currentTheme = getCurrentTheme()
if (currentTheme != theme) {
Expand All @@ -46,7 +46,7 @@ class ThemesManager @Inject constructor(

@Suppress("DEPRECATION")
fun setThemeForWebView(context: Context, webSettings: WebSettings) {
val theme = getCurrentTheme()
val theme = runBlocking { getCurrentTheme() }
setNightModeBasedOnTheme(theme)

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import info.hannes.changelog.ChangeLog
import io.homeassistant.companion.android.R
import io.homeassistant.companion.android.themes.ThemesManager
import javax.inject.Inject
import kotlinx.coroutines.runBlocking

class ChangeLog @Inject constructor() {

@Inject
lateinit var themesManager: ThemesManager

fun showChangeLog(context: Context, forceShow: Boolean) {
val isDarkTheme = when (themesManager.getCurrentTheme()) {
val isDarkTheme = when (runBlocking { themesManager.getCurrentTheme() }) {
"android", "system" -> {
val nightModeFlags =
context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK
Expand Down