Skip to content

Rewrite BatteryOptimizationsFragment to Compose #580

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

Merged
merged 19 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -16,6 +16,40 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContract
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Card
import androidx.compose.material.Checkbox
import androidx.compose.material.MaterialTheme
import androidx.compose.material.OutlinedButton
import androidx.compose.material.Switch
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalUriHandler
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.content.getSystemService
import androidx.databinding.ObservableBoolean
import androidx.fragment.app.Fragment
Expand All @@ -25,11 +59,11 @@ import androidx.lifecycle.MutableLiveData
import at.bitfire.davdroid.App
import at.bitfire.davdroid.BuildConfig
import at.bitfire.davdroid.R
import at.bitfire.davdroid.databinding.IntroBatteryOptimizationsBinding
import at.bitfire.davdroid.settings.SettingsManager
import at.bitfire.davdroid.ui.UiUtils
import at.bitfire.davdroid.ui.intro.BatteryOptimizationsFragment.Model.Companion.HINT_AUTOSTART_PERMISSION
import at.bitfire.davdroid.ui.intro.BatteryOptimizationsFragment.Model.Companion.HINT_BATTERY_OPTIMIZATIONS
import at.bitfire.davdroid.ui.widget.SafeAndroidUriHandler
import com.google.accompanist.themeadapter.material.MdcTheme
import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
Expand All @@ -53,28 +87,42 @@ class BatteryOptimizationsFragment: Fragment() {


override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val binding = IntroBatteryOptimizationsBinding.inflate(inflater, container, false)
binding.lifecycleOwner = viewLifecycleOwner
binding.model = model

model.shouldBeWhitelisted.observe(viewLifecycleOwner) { shouldBeWhitelisted ->
@SuppressLint("BatteryLife")
if (shouldBeWhitelisted && !model.isWhitelisted.value!!)
ignoreBatteryOptimizationsResultLauncher.launch(BuildConfig.APPLICATION_ID)
}
binding.batteryText.text = getString(R.string.intro_battery_text, getString(R.string.app_name))

binding.autostartHeading.text = getString(R.string.intro_autostart_title, WordUtils.capitalize(Build.MANUFACTURER))
binding.autostartText.setText(R.string.intro_autostart_text)
binding.autostartMoreInfo.setOnClickListener {
UiUtils.launchUri(requireActivity(), App.homepageUrl(requireActivity()).buildUpon()
.appendPath("faq").appendPath("synchronization-is-not-run-as-expected")
.appendQueryParameter("manufacturer", Build.MANUFACTURER.lowercase(Locale.ROOT)).build())
return ComposeView(requireContext()).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setContent {
MdcTheme {
var dontShowBattery by remember { mutableStateOf(model.dontShowBattery.get()) }
var dontShowAutostart by remember { mutableStateOf(model.dontShowAutostart.get()) }
val shouldBeWhitelisted by model.shouldBeWhitelisted.observeAsState(false)
val isWhitelisted by model.isWhitelisted.observeAsState(false)

LaunchedEffect(shouldBeWhitelisted, isWhitelisted) {
if (shouldBeWhitelisted && !isWhitelisted)
ignoreBatteryOptimizationsResultLauncher.launch(BuildConfig.APPLICATION_ID)
}

val uriHandler = SafeAndroidUriHandler(LocalContext.current)
CompositionLocalProvider(LocalUriHandler provides uriHandler) {
Content(
dontShowBattery = dontShowBattery,
onChangeDontShowBattery = {
model.dontShowBattery.set(it)
dontShowBattery = it
},
isWhitelisted = isWhitelisted,
shouldBeWhitelisted = shouldBeWhitelisted,
onChangeShouldBeWhitelisted = model.shouldBeWhitelisted::postValue,
dontShowAutostart = dontShowAutostart,
onChangeDontShowAutostart = {
model.dontShowAutostart.set(it)
dontShowAutostart = it
},
manufacturerWarning = Model.manufacturerWarning
)
}
}
}
}

binding.infoLeaveUnchecked.text = getString(R.string.intro_leave_unchecked, getString(R.string.app_settings_reset_hints))

return binding.root
}

override fun onResume() {
Expand All @@ -83,6 +131,164 @@ class BatteryOptimizationsFragment: Fragment() {
}


@Preview(showBackground = true, showSystemUi = true)
@Composable
fun Content_Preview() {
MdcTheme {
Content(
dontShowBattery = true,
onChangeDontShowBattery = {},
isWhitelisted = false,
shouldBeWhitelisted = true,
onChangeShouldBeWhitelisted = {},
dontShowAutostart = false,
onChangeDontShowAutostart = {},
manufacturerWarning = true
)
}
}

@Composable
private fun Content(
dontShowBattery: Boolean,
onChangeDontShowBattery: (Boolean) -> Unit,
isWhitelisted: Boolean,
shouldBeWhitelisted: Boolean,
onChangeShouldBeWhitelisted: (Boolean) -> Unit,
dontShowAutostart: Boolean,
onChangeDontShowAutostart: (Boolean) -> Unit,
manufacturerWarning: Boolean
) {
val uriHandler = LocalUriHandler.current

Column(
modifier = Modifier
.fillMaxSize()
.verticalScroll(rememberScrollState())
.padding(8.dp)
) {
Card {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
) {
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = stringResource(R.string.intro_battery_title),
style = MaterialTheme.typography.h6,
modifier = Modifier.weight(1f)
)
Switch(
checked = shouldBeWhitelisted,
onCheckedChange = {
// Only accept click events if not whitelisted
if (!isWhitelisted) {
onChangeShouldBeWhitelisted(it)
}
},
enabled = !dontShowBattery
)
}
Text(
text = stringResource(
R.string.intro_battery_text,
getString(R.string.app_name)
),
style = MaterialTheme.typography.body1,
modifier = Modifier.padding(top = 12.dp)
)
AnimatedVisibility(visible = !isWhitelisted) {
Row(
verticalAlignment = Alignment.CenterVertically
) {
Checkbox(
checked = dontShowBattery,
onCheckedChange = onChangeDontShowBattery,
enabled = !isWhitelisted
)
Text(
text = stringResource(R.string.intro_battery_dont_show),
style = MaterialTheme.typography.caption,
modifier = Modifier
.clickable { onChangeDontShowBattery(!dontShowBattery) }
)
}
}
}
}
AnimatedVisibility(visible = manufacturerWarning) {
Card(
modifier = Modifier.padding(top = 8.dp)
) {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
) {
Text(
text = stringResource(
R.string.intro_autostart_title,
WordUtils.capitalize(Build.MANUFACTURER)
),
style = MaterialTheme.typography.h6,
modifier = Modifier.fillMaxWidth()
)
Text(
text = stringResource(R.string.intro_autostart_text),
style = MaterialTheme.typography.body1,
modifier = Modifier.padding(top = 12.dp)
)
OutlinedButton(
onClick = {
uriHandler.openUri(
App.homepageUrl(requireActivity())
.buildUpon()
.appendPath("faq")
.appendPath("synchronization-is-not-run-as-expected")
.appendQueryParameter(
"manufacturer",
Build.MANUFACTURER.lowercase(Locale.ROOT)
)
.build().toString()
)
}
) {
Text(stringResource(R.string.intro_more_info))
}
Row(
verticalAlignment = Alignment.CenterVertically
) {
Checkbox(
checked = dontShowAutostart,
onCheckedChange = onChangeDontShowAutostart
)
Text(
text = stringResource(R.string.intro_autostart_dont_show),
style = MaterialTheme.typography.caption,
modifier = Modifier
.clickable { onChangeDontShowAutostart(!dontShowAutostart) }
)
}
}
}
}
Text(
text = stringResource(
R.string.intro_leave_unchecked,
stringResource(R.string.app_settings_reset_hints)
),
style = MaterialTheme.typography.body2,
modifier = Modifier.padding(top = 8.dp)
)
Spacer(modifier = Modifier.height(90.dp))
}
}


@HiltViewModel
class Model @Inject constructor(
application: Application,
Expand Down Expand Up @@ -164,7 +370,6 @@ class BatteryOptimizationsFragment: Fragment() {
if (whitelisted)
settings.remove(HINT_BATTERY_OPTIMIZATIONS)
}

}


Expand Down
Loading