From b33b41624b321fa29c7a9086158fa3630007e87d Mon Sep 17 00:00:00 2001 From: Nguyen Duc Tuan Minh Date: Wed, 6 Nov 2024 23:46:45 +0700 Subject: [PATCH] Add log in warning --- .../simpmusic/ui/screen/home/HomeScreen.kt | 34 +++++++++++++++++++ .../simpmusic/viewModel/HomeViewModel.kt | 13 +++++++ app/src/main/res/values/strings.xml | 2 ++ 3 files changed, 49 insertions(+) diff --git a/app/src/main/java/com/maxrave/simpmusic/ui/screen/home/HomeScreen.kt b/app/src/main/java/com/maxrave/simpmusic/ui/screen/home/HomeScreen.kt index 75e4f533..f0435341 100644 --- a/app/src/main/java/com/maxrave/simpmusic/ui/screen/home/HomeScreen.kt +++ b/app/src/main/java/com/maxrave/simpmusic/ui/screen/home/HomeScreen.kt @@ -37,8 +37,10 @@ import androidx.compose.foundation.lazy.items import androidx.compose.foundation.lazy.rememberLazyListState import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.shape.CircleShape +import androidx.compose.material3.AlertDialog import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Text +import androidx.compose.material3.TextButton import androidx.compose.material3.TopAppBar import androidx.compose.material3.pulltorefresh.PullToRefreshBox import androidx.compose.material3.pulltorefresh.PullToRefreshDefaults @@ -131,6 +133,8 @@ fun HomeScreen( val chipRowState = rememberScrollState() val params by viewModel.params.collectAsState() + val shouldShowLogInAlert by viewModel.showLogInAlert.collectAsState() + val onRefresh: () -> Unit = { isRefreshing = true viewModel.getHomeItemList() @@ -160,6 +164,36 @@ fun HomeScreen( LaunchedEffect(key1 = homeData) { accountShow = homeData.find { it.subtitle == accountInfo?.first } == null } + + if (shouldShowLogInAlert) { + AlertDialog( + title = { + Text(stringResource(R.string.warning)) + }, + text = { + Text(text = stringResource(R.string.log_in_warning)) + }, + confirmButton = { + TextButton(onClick = { + viewModel.doneShowLogInAlert() + navController.navigateSafe(R.id.action_global_logInFragment) + }) { + Text(stringResource(R.string.go_to_log_in_page)) + } + }, + dismissButton = { + TextButton(onClick = { + viewModel.doneShowLogInAlert() + }) { + Text(stringResource(R.string.cancel)) + } + }, + onDismissRequest = { + viewModel.doneShowLogInAlert() + } + ) + } + Column { AnimatedVisibility( visible = scrollState.isScrollingUp(), diff --git a/app/src/main/java/com/maxrave/simpmusic/viewModel/HomeViewModel.kt b/app/src/main/java/com/maxrave/simpmusic/viewModel/HomeViewModel.kt index 75e369bf..8b6f3f05 100644 --- a/app/src/main/java/com/maxrave/simpmusic/viewModel/HomeViewModel.kt +++ b/app/src/main/java/com/maxrave/simpmusic/viewModel/HomeViewModel.kt @@ -7,6 +7,7 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import androidx.media3.common.util.UnstableApi import androidx.media3.exoplayer.offline.Download +import com.maxrave.kotlinytmusicscraper.YouTube import com.maxrave.simpmusic.R import com.maxrave.simpmusic.common.DownloadState import com.maxrave.simpmusic.common.SELECTED_LANGUAGE @@ -33,6 +34,7 @@ import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import kotlinx.coroutines.runBlocking import org.koin.android.annotation.KoinViewModel @@ -80,7 +82,14 @@ class HomeViewModel( private var _params: MutableStateFlow = MutableStateFlow(null) val params: StateFlow = _params + // For showing alert that should log in to YouTube + private val _showLogInAlert: MutableStateFlow = MutableStateFlow(false) + val showLogInAlert: StateFlow = _showLogInAlert + init { + if (YouTube.cookie.isNullOrEmpty()) { + _showLogInAlert.update { true } + } homeJob = Job() viewModelScope.launch { regionCodeChart.value = dataStoreManager.chartKey.first() @@ -126,6 +135,10 @@ class HomeViewModel( } } + fun doneShowLogInAlert() { + _showLogInAlert.update { false } + } + fun getHomeItemList(params: String? = null) { language = runBlocking { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 19947b85..3ef21755 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -359,4 +359,6 @@ Can\'t delete song from YouTube Playlist Loading Updating + Go to log in page + YouTube Music now requires you to log in to stream music, Piped Instance sometime can\'t work too. You should log in to YouTube to get better experience with SimpMusic. \ No newline at end of file