Skip to content

Commit

Permalink
Add log in warning
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed Nov 6, 2024
1 parent 56f891c commit b33b416
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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(),
Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/maxrave/simpmusic/viewModel/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -80,7 +82,14 @@ class HomeViewModel(
private var _params: MutableStateFlow<String?> = MutableStateFlow(null)
val params: StateFlow<String?> = _params

// For showing alert that should log in to YouTube
private val _showLogInAlert: MutableStateFlow<Boolean> = MutableStateFlow(false)
val showLogInAlert: StateFlow<Boolean> = _showLogInAlert

init {
if (YouTube.cookie.isNullOrEmpty()) {
_showLogInAlert.update { true }
}
homeJob = Job()
viewModelScope.launch {
regionCodeChart.value = dataStoreManager.chartKey.first()
Expand Down Expand Up @@ -126,6 +135,10 @@ class HomeViewModel(
}
}

fun doneShowLogInAlert() {
_showLogInAlert.update { false }
}

fun getHomeItemList(params: String? = null) {
language =
runBlocking {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,6 @@
<string name="can_t_delete_from_youtube_playlist">Can\'t delete song from YouTube Playlist</string>
<string name="loading">Loading</string>
<string name="updating">Updating</string>
<string name="go_to_log_in_page">Go to log in page</string>
<string name="log_in_warning">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.</string>
</resources>

0 comments on commit b33b416

Please sign in to comment.