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

Add dialog before prompting for Improv permissions #4837

Merged
merged 1 commit into from
Nov 22, 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
@@ -0,0 +1,93 @@
package io.homeassistant.companion.android.improv.ui

import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.ui.platform.ComposeView
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.fragment.app.setFragmentResult
import androidx.lifecycle.lifecycleScope
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.common.data.prefs.PrefsRepository
import io.homeassistant.companion.android.improv.ImprovRepository
import io.homeassistant.companion.android.util.compose.HomeAssistantAppTheme
import io.homeassistant.companion.android.util.setLayoutAndExpandedByDefault
import javax.inject.Inject
import kotlinx.coroutines.launch

@AndroidEntryPoint
class ImprovPermissionDialog : BottomSheetDialogFragment() {

@Inject
lateinit var improvRepository: ImprovRepository

@Inject
lateinit var prefsRepository: PrefsRepository

private val requestPermissions =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
setFragmentResult(RESULT_KEY, bundleOf(RESULT_GRANTED to it.all { result -> result.value }))
dismiss()
}

companion object {
const val TAG = "ImprovPermissionDialog"

const val RESULT_KEY = "ImprovPermissionResult"
const val RESULT_GRANTED = "granted"
}

private var neededPermissions = arrayOf<String>()
private var increasedCount = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val permissions = improvRepository.getRequiredPermissions()
context?.let { ctx ->
permissions.forEach {
val granted = ContextCompat.checkSelfPermission(ctx, it) == PackageManager.PERMISSION_GRANTED
if (!granted) neededPermissions += it
}
}
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return ComposeView(requireContext()).apply {
setContent {
HomeAssistantAppTheme {
ImprovPermissionView(
needsBluetooth = neededPermissions.any { it.contains("BLUETOOTH", ignoreCase = true) },
needsLocation = neededPermissions.any { it == Manifest.permission.ACCESS_FINE_LOCATION },
onContinue = { requestPermissions.launch(neededPermissions) },
onSkip = { dismiss() }
)
}
}
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setLayoutAndExpandedByDefault()
}

override fun onResume() {
super.onResume()
if (increasedCount) return
lifecycleScope.launch {
prefsRepository.addImprovPermissionDisplayedCount()
increasedCount = true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package io.homeassistant.companion.android.improv.ui

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.material.TextButton
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.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial
import io.homeassistant.companion.android.common.R as commonR
import io.homeassistant.companion.android.onboarding.OnboardingHeaderView
import io.homeassistant.companion.android.onboarding.OnboardingPermissionBullet
import io.homeassistant.companion.android.util.compose.ModalBottomSheet

@Composable
fun ImprovPermissionView(
needsBluetooth: Boolean,
needsLocation: Boolean,
onContinue: () -> Unit,
onSkip: () -> Unit
) {
ModalBottomSheet(title = null) {
Column(
modifier = Modifier
.padding(horizontal = 16.dp)
.padding(bottom = 16.dp)
) {
OnboardingHeaderView(
icon = CommunityMaterial.Icon3.cmd_radar,
title = stringResource(commonR.string.improv_permission_title)
)
Text(
text = stringResource(commonR.string.improv_permission_text),
textAlign = TextAlign.Center,
modifier = Modifier
.padding(bottom = 16.dp)
.align(Alignment.CenterHorizontally)
)
if (needsBluetooth) {
OnboardingPermissionBullet(
icon = CommunityMaterial.Icon.cmd_bluetooth,
text = stringResource(commonR.string.improv_permission_bluetooth)
)
}
if (needsLocation) {
OnboardingPermissionBullet(
icon = CommunityMaterial.Icon3.cmd_map_marker,
text = stringResource(commonR.string.improv_permission_location)
)
}
Spacer(Modifier.height(96.dp))
Row {
TextButton(onClick = onSkip) {
Text(stringResource(id = commonR.string.skip))
}
Spacer(modifier = Modifier.weight(1f))
Button(onClick = onContinue) {
Text(stringResource(id = commonR.string.continue_connect))
}
}
}
}
}

@Preview
@Composable
fun ImprovPermissionViewPreview() {
ImprovPermissionView(
needsBluetooth = true,
needsLocation = true,
onContinue = {},
onSkip = {}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,19 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.FrameLayout
import androidx.compose.runtime.collectAsState
import androidx.compose.ui.platform.ComposeView
import androidx.core.os.bundleOf
import androidx.fragment.app.setFragmentResult
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.google.android.material.R
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.common.data.wifi.WifiHelper
import io.homeassistant.companion.android.improv.ImprovRepository
import io.homeassistant.companion.android.util.compose.HomeAssistantAppTheme
import io.homeassistant.companion.android.util.setLayoutAndExpandedByDefault
import javax.inject.Inject
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -111,14 +107,7 @@ class ImprovSetupDialog : BottomSheetDialogFragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
dialog?.setOnShowListener {
val dialog = it as BottomSheetDialog
dialog.window?.setDimAmount(0.03f)
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
val bottomSheet = dialog.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
}
setLayoutAndExpandedByDefault()
}

override fun onResume() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.homeassistant.companion.android.onboarding
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
Expand Down Expand Up @@ -73,3 +74,26 @@ fun OnboardingHeaderView(
)
}
}

@Composable
fun OnboardingPermissionBullet(
icon: IIcon,
text: String
) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.padding(vertical = 12.dp)
) {
Image(
asset = icon,
colorFilter = ColorFilter.tint(MaterialTheme.colors.onSurface),
contentDescription = null
)
Text(
text = text,
modifier = Modifier
.padding(start = 16.dp)
.fillMaxWidth()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,19 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.Button
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.mikepenz.iconics.compose.Image
import com.mikepenz.iconics.typeface.IIcon
import com.mikepenz.iconics.typeface.library.community.material.CommunityMaterial
import io.homeassistant.companion.android.common.R as commonR
import io.homeassistant.companion.android.onboarding.OnboardingHeaderView
import io.homeassistant.companion.android.onboarding.OnboardingPermissionBullet
import io.homeassistant.companion.android.onboarding.OnboardingScreen
import io.homeassistant.companion.android.util.compose.HomeAssistantAppTheme

Expand All @@ -50,11 +47,11 @@ fun NotificationPermissionView(
.padding(bottom = 48.dp)
.align(Alignment.CenterHorizontally)
)
NotificationPermissionBullet(
OnboardingPermissionBullet(
icon = CommunityMaterial.Icon.cmd_alert_decagram,
text = stringResource(id = commonR.string.onboarding_notifications_bullet_alert)
)
NotificationPermissionBullet(
OnboardingPermissionBullet(
icon = CommunityMaterial.Icon3.cmd_text,
text = stringResource(id = commonR.string.onboarding_notifications_bullet_commands)
)
Expand All @@ -71,29 +68,6 @@ fun NotificationPermissionView(
}
}

@Composable
fun NotificationPermissionBullet(
icon: IIcon,
text: String
) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier.padding(vertical = 12.dp)
) {
Image(
asset = icon,
colorFilter = ColorFilter.tint(MaterialTheme.colors.onSurface),
contentDescription = null
)
Text(
text = text,
modifier = Modifier
.padding(start = 16.dp)
.fillMaxWidth()
)
}
}

@Preview(showSystemUi = true)
@Composable
fun NotificationPermissionViewPreview() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.compose.ui.platform.ComposeView
import androidx.core.os.bundleOf
import androidx.fragment.app.setFragmentResult
import com.google.android.material.R
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import dagger.hilt.android.AndroidEntryPoint
import io.homeassistant.companion.android.common.data.servers.ServerManager
import io.homeassistant.companion.android.util.compose.HomeAssistantAppTheme
import io.homeassistant.companion.android.util.setLayoutAndExpandedByDefault
import javax.inject.Inject

@AndroidEntryPoint
Expand Down Expand Up @@ -52,12 +49,6 @@ class ServerChooserFragment : BottomSheetDialogFragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
dialog?.setOnShowListener {
val dialog = it as BottomSheetDialog
dialog.window?.setDimAmount(0.03f)
val bottomSheet = dialog.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
}
setLayoutAndExpandedByDefault()
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
package io.homeassistant.companion.android.util

import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import com.google.android.material.R
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

/**
* Check if the state of the fragment is at least started,
* meaning onStart has been called but the state has not reached onSavedInstanceState (yet).
*/
val Fragment.isStarted: Boolean
get() = lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)

/**
* Set the layout for a BottomSheetDialogFragment to a shared default for the app,
* and expand it to full size by default instead of peek height only.
*/
fun BottomSheetDialogFragment.setLayoutAndExpandedByDefault() {
dialog?.setOnShowListener {
val dialog = it as BottomSheetDialog
dialog.window?.setDimAmount(0.03f)
dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
val bottomSheet = dialog.findViewById<View>(R.id.design_bottom_sheet) as FrameLayout
val bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
bottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
}
}
Loading