Skip to content

Commit

Permalink
feat: Added notification request
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbyESP committed Apr 5, 2023
1 parent 7fbcfd6 commit 723f8b5
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.compose.dialog
import androidx.navigation.navArgument
import androidx.navigation.navDeepLink
import androidx.navigation.navOptions
import androidx.navigation.navigation
import com.bobbyesp.library.SpotDL
import com.bobbyesp.spowlo.App
Expand Down Expand Up @@ -452,7 +453,9 @@ fun InitialEntry(
onBackPressed,
downloaderViewModel,
navController
) { id -> navController.navigate(Route.PLAYLIST_PAGE + "/" + "playlist" + "/" + id) }
) { id -> navController.navigate(Route.PLAYLIST_PAGE + "/" + "playlist" + "/" + id, navOptions = navOptions {
launchSingleTop = true
}) }
// }

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.bobbyesp.spowlo.ui.pages.downloader

import android.Manifest
import android.os.Build
import android.util.Log
import androidx.activity.compose.BackHandler
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.FastOutSlowInEasing
Expand Down Expand Up @@ -91,6 +92,7 @@ import com.bobbyesp.spowlo.ui.pages.settings.about.LocalAsset
import com.bobbyesp.spowlo.ui.theme.harmonizeWith
import com.bobbyesp.spowlo.utils.CONFIGURE
import com.bobbyesp.spowlo.utils.DEBUG
import com.bobbyesp.spowlo.utils.NOTIFICATION
import com.bobbyesp.spowlo.utils.PreferencesUtil
import com.bobbyesp.spowlo.utils.PreferencesUtil.getBoolean
import com.bobbyesp.spowlo.utils.ToastUtil
Expand Down Expand Up @@ -124,6 +126,30 @@ fun DownloaderPage(
}
}

val notificationsPermission = rememberPermissionState(
permission = Manifest.permission.ACCESS_NOTIFICATION_POLICY
) { b: Boolean ->
Log.d("DownloaderPage", "notificationsPermission: $b")
if (b) {
PreferencesUtil.updateValue(NOTIFICATION, true)
} else {
PreferencesUtil.updateValue(NOTIFICATION, false)
ToastUtil.makeToast(R.string.permission_denied)
}
}

val modernNotificationPermission = rememberPermissionState(
permission = Manifest.permission.POST_NOTIFICATIONS
) { b: Boolean ->
Log.d("DownloaderPage", "modernNotificationPermission: $b")
if (b) {
PreferencesUtil.updateValue(NOTIFICATION, true)
} else {
PreferencesUtil.updateValue(NOTIFICATION, false)
ToastUtil.makeToast(R.string.permission_denied)
}
}

//STATE FLOWS
val viewState by downloaderViewModel.viewStateFlow.collectAsStateWithLifecycle()
val downloaderState by Downloader.downloaderState.collectAsStateWithLifecycle()
Expand All @@ -146,6 +172,20 @@ fun DownloaderPage(
if (CONFIGURE.getBoolean()) navigateToDownloaderSheet()
else checkPermissionOrDownload()
keyboardController?.hide()
if(NOTIFICATION.getBoolean()){
when(Build.VERSION.SDK_INT){
in 23..31 -> {
if(notificationsPermission.status != PermissionStatus.Granted){
notificationsPermission.launchPermissionRequest()
}
}
in 32..Int.MAX_VALUE -> {
if(modernNotificationPermission.status != PermissionStatus.Granted){
modernNotificationPermission.launchPermissionRequest()
}
}
}
}
}

val songCardClicked = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class DownloaderViewModel @Inject constructor() : ViewModel() {
}

fun startDownloadSong(skipInfoFetch: Boolean = false) {

val url = viewStateFlow.value.url
Downloader.clearErrorState()
if (!Downloader.isDownloaderAvailable())
Expand All @@ -76,6 +77,7 @@ class DownloaderViewModel @Inject constructor() : ViewModel() {
showErrorMessage(R.string.url_empty)
return
}
//request notification permission
Downloader.getInfoAndDownload(url, skipInfoFetch = skipInfoFetch)
}

Expand Down

0 comments on commit 723f8b5

Please sign in to comment.