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

Tidy up PowerController #1469

Merged
merged 1 commit into from
Aug 21, 2023
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
2 changes: 1 addition & 1 deletion core/powercontroller/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ kotlin {
val commonMain by getting {
dependencies {
implementation(projects.core.base)
api(projects.core.preferences)
}
}

val androidMain by getting {
dependencies {
api(projects.core.preferences)
implementation(libs.androidx.core)

implementation(libs.kotlininject.runtime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,21 @@
package app.tivi.util

import android.app.Application
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.net.ConnectivityManager
import android.os.Build
import android.os.PowerManager
import androidx.annotation.RequiresApi
import androidx.core.content.getSystemService
import app.tivi.settings.TiviPreferences
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.onCompletion
import kotlinx.coroutines.flow.onStart
import me.tatarka.inject.annotations.Inject

@Inject
class AndroidPowerController(
private val context: Application,
private val preferences: TiviPreferences,
) : PowerController {
private val powerManager: PowerManager = context.getSystemService()!!
private val connectivityManager: ConnectivityManager = context.getSystemService()!!

override fun observeShouldSaveData(ignorePreference: Boolean): Flow<SaveData> {
return merge(
context.flowBroadcasts(IntentFilter(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)),
context.flowBroadcasts(IntentFilter(ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED)),
).map {
shouldSaveData()
}.onStart {
emit(shouldSaveData())
}
}
private val powerManager: PowerManager by lazy { context.getSystemService()!! }
private val connectivityManager: ConnectivityManager by lazy { context.getSystemService()!! }

override fun shouldSaveData(): SaveData = when {
preferences.useLessData -> {
Expand All @@ -63,16 +42,3 @@ class AndroidPowerController(
ConnectivityManager.RESTRICT_BACKGROUND_STATUS_ENABLED
}
}

private fun Context.flowBroadcasts(intentFilter: IntentFilter): Flow<Intent> {
val resultChannel = MutableStateFlow(Intent())

val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
resultChannel.value = intent
}
}

return resultChannel.onStart { registerReceiver(receiver, intentFilter) }
.onCompletion { unregisterReceiver(receiver) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

package app.tivi.util

import app.tivi.inject.ApplicationScope
import me.tatarka.inject.annotations.Provides

actual interface PowerControllerComponent {
@Provides
@ApplicationScope
fun providePowerController(bind: AndroidPowerController): PowerController = bind
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,28 @@

package app.tivi.util

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
import app.tivi.settings.TiviPreferences
import me.tatarka.inject.annotations.Inject

interface PowerController {
fun observeShouldSaveData(ignorePreference: Boolean): Flow<SaveData>
fun shouldSaveData(): SaveData
}

sealed class SaveData {
object Disabled : SaveData()
data object Disabled : SaveData()
data class Enabled(val reason: SaveDataReason) : SaveData()
}

object EmptyPowerController : PowerController {
override fun observeShouldSaveData(ignorePreference: Boolean): Flow<SaveData> = emptyFlow()

override fun shouldSaveData(): SaveData = SaveData.Disabled
}

enum class SaveDataReason {
PREFERENCE, SYSTEM_DATA_SAVER, SYSTEM_POWER_SAVER
}

@Inject
class DefaultPowerController(
private val preferences: TiviPreferences,
) : PowerController {
override fun shouldSaveData(): SaveData = when {
preferences.useLessData -> SaveData.Enabled(SaveDataReason.PREFERENCE)
else -> SaveData.Disabled
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

package app.tivi.util

import app.tivi.inject.ApplicationScope
import me.tatarka.inject.annotations.Provides

actual interface PowerControllerComponent {
@Provides
fun providePowerController(): PowerController = EmptyPowerController
@ApplicationScope
fun providePowerController(bind: DefaultPowerController): PowerController = bind
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

package app.tivi.util

import app.tivi.inject.ApplicationScope
import me.tatarka.inject.annotations.Provides

actual interface PowerControllerComponent {
@Provides
fun providePowerController(): PowerController = EmptyPowerController
@ApplicationScope
fun providePowerController(bind: DefaultPowerController): PowerController = bind
}
Loading