Skip to content

Commit

Permalink
Asking notification permission at the beginning of the home screen la…
Browse files Browse the repository at this point in the history
…unch
  • Loading branch information
nkuppan committed Mar 24, 2024
1 parent 97cb436 commit 3148143
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
30 changes: 30 additions & 0 deletions app/src/main/kotlin/com/naveenapps/expensemanager/ui/HomeScreen.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.naveenapps.expensemanager.ui

import android.Manifest
import android.app.Activity
import android.os.Build
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.BottomAppBar
Expand All @@ -9,8 +13,12 @@ import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
Expand Down Expand Up @@ -153,6 +161,28 @@ fun HomeScreen(viewModel: HomeViewModel = hiltViewModel()) {

val homeScreenBottomBarItems by viewModel.homeScreenBottomBarItems.collectAsState()

var hasNotificationPermission by remember { mutableStateOf(false) }

val notificationPermissionLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestPermission(),
onResult = {
hasNotificationPermission = it
if (it) {
viewModel.turnOnNotification()
}
}
)

LaunchedEffect(key1 = "permission") {
if (hasNotificationPermission.not()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
notificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
} else {
viewModel.turnOnNotification()
}
}
}

BackHandler {
if (homeScreenBottomBarItems != HomeScreenBottomBarItems.Home) {
viewModel.setUISystem(HomeScreenBottomBarItems.Home)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
package com.naveenapps.expensemanager.ui

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.naveenapps.expensemanager.core.domain.usecase.settings.reminder.UpdateReminderStatusUseCase
import com.naveenapps.expensemanager.core.notification.NotificationScheduler
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class HomeViewModel @Inject constructor() : ViewModel() {
class HomeViewModel @Inject constructor(
private val updateReminderStatusUseCase: UpdateReminderStatusUseCase,
private val notificationScheduler: NotificationScheduler
) : ViewModel() {

private val _homeScreenBottomBarItems = MutableStateFlow(HomeScreenBottomBarItems.Home)
val homeScreenBottomBarItems = _homeScreenBottomBarItems.asStateFlow()

fun setUISystem(homeScreenBottomBarItems: HomeScreenBottomBarItems) {
_homeScreenBottomBarItems.value = homeScreenBottomBarItems
}

fun turnOnNotification() {
viewModelScope.launch {
updateReminderStatusUseCase.invoke(true)
notificationScheduler.checkAndRestartReminder()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.naveenapps.expensemanager.core.domain.usecase.settings.reminder

import com.naveenapps.expensemanager.core.repository.ReminderTimeRepository
import javax.inject.Inject

class UpdateReminderStatusUseCase @Inject constructor(
private val repository: com.naveenapps.expensemanager.core.repository.ReminderTimeRepository,
private val repository: ReminderTimeRepository,
) {

suspend operator fun invoke(reminderStatus: Boolean) {
Expand Down

0 comments on commit 3148143

Please sign in to comment.