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

Task/initial app permissions #150

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -7,14 +7,14 @@ import dagger.hilt.android.testing.HiltAndroidRule
import dagger.hilt.android.testing.HiltAndroidTest
import edu.stanford.bdh.engagehf.R
import edu.stanford.bdh.engagehf.bluetooth.data.models.UiState
import edu.stanford.bdh.engagehf.simulator.BluetoothScreenSimulator
import edu.stanford.bdh.engagehf.simulator.HomeScreenSimulator
import edu.stanford.spezi.core.design.component.ComposeContentActivity
import org.junit.Before
import org.junit.Rule
import org.junit.Test

@HiltAndroidTest
class BluetoothScreenTest {
class HomeScreenTest {

@get:Rule
val hiltRule = HiltAndroidRule(this)
Expand All @@ -33,42 +33,42 @@ class BluetoothScreenTest {
@Before
fun init() {
composeTestRule.activity.setScreen {
BluetoothScreen()
HomeScreen()
}
}

@Test
fun `test bluetooth screen root is displayed`() {
bluetoothScreen {
fun `test home screen root is displayed`() {
homeScreen {
assertIsDisplayed()
}
}

@Test
fun `test bluetooth screen message title is displayed`() {
bluetoothScreen {
fun `test home screen message title is displayed`() {
homeScreen {
assertMessageTitle(composeTestRule.activity.getString(R.string.messages))
}
}

@Test
fun `test bluetooth screen vital title is displayed`() {
bluetoothScreen {
fun `test home screen vital title is displayed`() {
homeScreen {
assertVitalTitle(composeTestRule.activity.getString(R.string.vitals))
}
}

@Test
fun `test bluetooth screen vital is displayed`() {
bluetoothScreen {
fun `test home screen vital is displayed`() {
homeScreen {
val uiState = UiState()
assertVital(uiState.weight.title)
assertVital(uiState.heartRate.title)
assertVital(uiState.bloodPressure.title)
}
}

private fun bluetoothScreen(block: BluetoothScreenSimulator.() -> Unit) {
BluetoothScreenSimulator(composeTestRule).apply(block)
private fun homeScreen(block: HomeScreenSimulator.() -> Unit) {
HomeScreenSimulator(composeTestRule).apply(block)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package edu.stanford.bdh.engagehf.di

import dagger.Module
import dagger.Provides
import dagger.hilt.components.SingletonComponent
import dagger.hilt.testing.TestInstallIn
import edu.stanford.spezi.core.notification.NotificationPermissions
import edu.stanford.spezi.core.notification.di.NotificationModule

@Module
@TestInstallIn(
components = [SingletonComponent::class],
replaces = [NotificationModule.Bindings::class]
)
class AppTestModule {

/**
* Since our instrumented tests run for android versions 31 and 34, we can't use grant rule
* to grant notification permission by default as it would fail for version 31. Hence, we simply
* replace the dependency to return empty permissions
*/
@Provides
fun provideNotificationPermissions(): NotificationPermissions {
return object : NotificationPermissions {
override fun getRequiredPermissions(): Set<String> = emptySet()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ package edu.stanford.bdh.engagehf.simulator
import androidx.compose.ui.test.assertIsDisplayed
import androidx.compose.ui.test.assertTextEquals
import androidx.compose.ui.test.junit4.ComposeTestRule
import edu.stanford.bdh.engagehf.bluetooth.screen.BluetoothScreenTestIdentifier
import edu.stanford.bdh.engagehf.bluetooth.screen.HomeScreenTestIdentifier
import edu.stanford.spezi.core.testing.onNodeWithIdentifier

class BluetoothScreenSimulator(
class HomeScreenSimulator(
private val composeTestRule: ComposeTestRule,
) {
private val root = composeTestRule.onNodeWithIdentifier(BluetoothScreenTestIdentifier.ROOT)
private val root = composeTestRule.onNodeWithIdentifier(HomeScreenTestIdentifier.ROOT)

private val messageTitle =
composeTestRule.onNodeWithIdentifier(BluetoothScreenTestIdentifier.MESSAGE_TITLE)
composeTestRule.onNodeWithIdentifier(HomeScreenTestIdentifier.MESSAGE_TITLE)

private val vitalTitle =
composeTestRule.onNodeWithIdentifier(BluetoothScreenTestIdentifier.VITAL_TITLE)
composeTestRule.onNodeWithIdentifier(HomeScreenTestIdentifier.VITAL_TITLE)

fun assertVital(vitalTitle: String) {
composeTestRule.onNodeWithIdentifier(BluetoothScreenTestIdentifier.VITALS, vitalTitle)
composeTestRule.onNodeWithIdentifier(HomeScreenTestIdentifier.VITALS, vitalTitle)
.assertIsDisplayed()
}

Expand Down
6 changes: 3 additions & 3 deletions app/src/main/kotlin/edu/stanford/bdh/engagehf/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.toRoute
import dagger.hilt.android.AndroidEntryPoint
import edu.stanford.bdh.engagehf.bluetooth.BluetoothViewModel
import edu.stanford.bdh.engagehf.bluetooth.HomeViewModel
import edu.stanford.bdh.engagehf.bluetooth.data.models.Action
import edu.stanford.bdh.engagehf.contact.ui.ContactScreen
import edu.stanford.bdh.engagehf.navigation.AppNavigationEvent
Expand Down Expand Up @@ -59,15 +59,15 @@ class MainActivity : FragmentActivity() {

private val viewModel by viewModels<MainActivityViewModel>()

private val bluetoothViewModel by viewModels<BluetoothViewModel>()
private val homeViewModel by viewModels<HomeViewModel>()

@Inject
@Dispatching.Main
lateinit var mainDispatcher: CoroutineDispatcher

override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
bluetoothViewModel.onAction(Action.NewIntent(intent))
homeViewModel.onAction(Action.NewIntent(intent))
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import edu.stanford.bdh.engagehf.bluetooth.data.models.UiState
import edu.stanford.bdh.engagehf.bluetooth.measurements.MeasurementsRepository
import edu.stanford.bdh.engagehf.bluetooth.service.EngageBLEService
import edu.stanford.bdh.engagehf.bluetooth.service.EngageBLEServiceEvent
import edu.stanford.bdh.engagehf.bluetooth.service.EngageBLEServiceState
import edu.stanford.bdh.engagehf.education.EngageEducationRepository
import edu.stanford.bdh.engagehf.messages.HealthSummaryService
import edu.stanford.bdh.engagehf.messages.Message
Expand All @@ -26,6 +27,7 @@ import edu.stanford.bdh.engagehf.navigation.AppNavigationEvent
import edu.stanford.bdh.engagehf.navigation.screens.BottomBarItem
import edu.stanford.spezi.core.logging.speziLogger
import edu.stanford.spezi.core.navigation.Navigator
import edu.stanford.spezi.core.notification.NotificationPermissions
import edu.stanford.spezi.core.notification.notifier.FirebaseMessage
import edu.stanford.spezi.core.notification.notifier.FirebaseMessage.Companion.FIREBASE_MESSAGE_KEY
import edu.stanford.spezi.core.utils.MessageNotifier
Expand All @@ -39,7 +41,7 @@ import javax.inject.Inject

@HiltViewModel
@Suppress("LongParameterList")
class BluetoothViewModel @Inject internal constructor(
class HomeViewModel @Inject internal constructor(
private val bleService: EngageBLEService,
private val uiStateMapper: BluetoothUiStateMapper,
private val measurementsRepository: MeasurementsRepository,
Expand All @@ -50,10 +52,15 @@ class BluetoothViewModel @Inject internal constructor(
private val healthSummaryService: HealthSummaryService,
private val messageNotifier: MessageNotifier,
@ApplicationContext private val context: Context,
notificationPermissions: NotificationPermissions,
) : ViewModel() {
private val logger by speziLogger()

private val _uiState = MutableStateFlow(UiState())
private val _uiState = MutableStateFlow(
UiState(
missingPermissions = notificationPermissions.getRequiredPermissions()
)
)

val uiState = _uiState.asStateFlow()

Expand All @@ -68,7 +75,16 @@ class BluetoothViewModel @Inject internal constructor(
viewModelScope.launch {
bleService.state.collect { state ->
logger.i { "Received EngageBLEService state $state" }
_uiState.update { it.copy(bluetooth = uiStateMapper.mapBleServiceState(state)) }
_uiState.update { currentState ->
val missingPermissions = currentState.missingPermissions.toMutableSet()
if (state is EngageBLEServiceState.MissingPermissions) {
missingPermissions.addAll(state.permissions)
}
currentState.copy(
bluetooth = uiStateMapper.mapBleServiceState(state),
missingPermissions = missingPermissions,
)
}
}
}

Expand Down Expand Up @@ -144,28 +160,22 @@ class BluetoothViewModel @Inject internal constructor(
}

is Action.MessageItemClicked -> {
viewModelScope.launch {
uiStateMapper.mapMessagesAction(action.message.action)
.onFailure { error ->
logger.e(error) { "Error while mapping action: ${action.message.action}" }
}
.onSuccess { messagesAction ->
messagesAction?.let {
onMessage(
messagesAction = it,
messageId = action.message.id,
isDismissible = action.message.isDismissible,
)
}
}
}
onMessageClicked(message = action.message)
}

is Action.ToggleExpand -> {
handleToggleExpandAction(action)
}

is Action.PermissionGranted, is Action.Resumed -> {
is Action.Resumed -> {
bleService.start()
}

is Action.PermissionResult -> {
_uiState.update { state ->
val missingPermission = state.missingPermissions.filterNot { it == action.permission }
state.copy(missingPermissions = missingPermission.toSet())
}
bleService.start()
}

Expand All @@ -189,6 +199,24 @@ class BluetoothViewModel @Inject internal constructor(
}
}

private fun onMessageClicked(message: Message) {
viewModelScope.launch {
uiStateMapper.mapMessagesAction(message.action)
.onFailure { error ->
logger.e(error) { "Error while mapping action: ${message.action}" }
}
.onSuccess { messagesAction ->
messagesAction?.let {
onMessage(
messagesAction = it,
messageId = message.id,
isDismissible = message.isDismissible,
)
}
}
}
}

private fun handleNewIntent(intent: Intent) {
val firebaseMessage =
intent.getParcelableExtra<FirebaseMessage>(FIREBASE_MESSAGE_KEY)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class BluetoothUiStateMapper @Inject constructor(
is EngageBLEServiceState.MissingPermissions -> {
BluetoothUiState.Idle(
description = R.string.bluetooth_permissions_not_granted_description,
missingPermissions = state.permissions,
settingsAction = Action.Settings.AppSettings,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ sealed interface Action {
data object DismissDialog : Action
data class MessageItemClicked(val message: Message) : Action
data class ToggleExpand(val message: Message) : Action
data class PermissionGranted(val permission: String) : Action
data class PermissionResult(val permission: String) : Action
data object Resumed : Action
data object BLEDevicePairing : Action
data class NewIntent(val intent: Intent) : Action
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import edu.stanford.bdh.engagehf.R
sealed interface BluetoothUiState {
data class Idle(
val description: Int = R.string.bluetooth_not_enabled_description,
val missingPermissions: List<String>? = null,
val settingsAction: Action.Settings? = null,
) : BluetoothUiState

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ data class UiState(
val weight: VitalDisplayData = VitalDisplayData(
title = "Weight",
),
val missingPermissions: Set<String> = emptySet(),
val messages: List<Message> = emptyList(),
val bluetooth: BluetoothUiState = BluetoothUiState.Idle(),
val measurementDialog: MeasurementDialogUiState = MeasurementDialogUiState(),
Expand Down
Loading
Loading