Skip to content

Commit

Permalink
Add Home Page filter
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed Jul 31, 2024
1 parent 82b9724 commit 4a2a1f6
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 7 deletions.
14 changes: 14 additions & 0 deletions app/src/main/java/com/maxrave/simpmusic/common/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ object Config {

val REMOVED_SONG_DATE_TIME = LocalDateTime.of(2003, Month.AUGUST, 26, 3, 0)

val listOfHomeChip = listOf(
R.string.all,
R.string.relax,
R.string.sleep,
R.string.energize,
R.string.sad,
R.string.romance,
R.string.feel_good,
R.string.workout,
R.string.party,
R.string.commute,
R.string.focus
)

}

object DownloadState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,11 +577,11 @@ class MainRepository
}
}.flowOn(Dispatchers.IO)

suspend fun getHomeData(): Flow<Resource<ArrayList<HomeItem>>> =
suspend fun getHomeData(params: String? = null): Flow<Resource<ArrayList<HomeItem>>> =
flow {
runCatching {
val limit = dataStoreManager.homeLimit.first()
YouTube.customQuery(browseId = "FEmusic_home").onSuccess { result ->
YouTube.customQuery(browseId = "FEmusic_home", params = params).onSuccess { result ->
val list: ArrayList<HomeItem> = arrayListOf()
if (result.contents?.singleColumnBrowseResultsRenderer?.tabs?.get(
0,
Expand Down
51 changes: 51 additions & 0 deletions app/src/main/java/com/maxrave/simpmusic/ui/component/ChipGroup.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.maxrave.simpmusic.ui.component

import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Done
import androidx.compose.material3.ElevatedFilterChip
import androidx.compose.material3.FilterChipDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.maxrave.simpmusic.ui.theme.colorPrimaryDark

@Composable
fun Chip(isSelected: Boolean = false, text: String, onClick: () -> Unit) {
ElevatedFilterChip(
shape = RoundedCornerShape(12.dp),
colors = FilterChipDefaults.elevatedFilterChipColors(
containerColor = colorPrimaryDark,
iconColor = Color.White,
selectedContainerColor = Color.DarkGray.copy(alpha = 0.8f),
labelColor = Color.LightGray,
selectedLabelColor = Color.LightGray
),
onClick = { onClick.invoke() },
label = {
Text(text)
},
border = FilterChipDefaults.filterChipBorder(
enabled = true,
selected = isSelected,
selectedBorderColor = Color.Transparent,
borderColor = Color.Gray.copy(alpha = 0.8f)
),
selected = isSelected,
leadingIcon = if (isSelected) {
{
Icon(
imageVector = Icons.Filled.Done,
contentDescription = "Done icon",
modifier = Modifier.size(FilterChipDefaults.IconSize)
)
}
} else {
null
},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.compose.animation.fadeOut
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.gestures.snapping.SnapLayoutInfoProvider
import androidx.compose.foundation.gestures.snapping.rememberSnapFlingBehavior
import androidx.compose.foundation.horizontalScroll
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -21,6 +22,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.grid.GridCells
Expand All @@ -29,6 +31,7 @@ import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
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.ExperimentalMaterial3Api
import androidx.compose.material3.Text
Expand Down Expand Up @@ -58,6 +61,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.media3.common.util.UnstableApi
import androidx.navigation.NavController
import com.maxrave.kotlinytmusicscraper.config.Constants
import com.maxrave.simpmusic.R
import com.maxrave.simpmusic.common.CHART_SUPPORTED_COUNTRY
import com.maxrave.simpmusic.common.Config
Expand All @@ -71,6 +75,7 @@ import com.maxrave.simpmusic.extension.toTrack
import com.maxrave.simpmusic.service.PlaylistType
import com.maxrave.simpmusic.service.QueueData
import com.maxrave.simpmusic.ui.component.CenterLoadingBox
import com.maxrave.simpmusic.ui.component.Chip
import com.maxrave.simpmusic.ui.component.DropdownButton
import com.maxrave.simpmusic.ui.component.EndOfPage
import com.maxrave.simpmusic.ui.component.HomeItem
Expand Down Expand Up @@ -119,6 +124,8 @@ fun HomeScreen(
rememberPullToRefreshState(
20.dp,
)
val chipRowState = rememberScrollState()
val params by viewModel.params.collectAsState()
val scaleFraction =
if (pullToRefreshState.isRefreshing) {
1f
Expand Down Expand Up @@ -157,6 +164,44 @@ fun HomeScreen(
}
Column {
HomeTopAppBar(navController)
Row (
modifier = Modifier
.horizontalScroll(chipRowState)
.padding(vertical = 8.dp, horizontal = 15.dp),
) {
Config.listOfHomeChip.forEach { id ->
Spacer(modifier = Modifier.width(4.dp))
Chip(isSelected =
when(params) {
Constants.HOME_PARAMS_RELAX -> id == R.string.relax
Constants.HOME_PARAMS_SLEEP -> id == R.string.sleep
Constants.HOME_PARAMS_ENERGIZE -> id == R.string.energize
Constants.HOME_PARAMS_SAD -> id == R.string.sad
Constants.HOME_PARAMS_ROMANCE -> id == R.string.romance
Constants.HOME_PARAMS_FEEL_GOOD -> id == R.string.feel_good
Constants.HOME_PARAMS_WORKOUT -> id == R.string.workout
Constants.HOME_PARAMS_PARTY -> id == R.string.party
Constants.HOME_PARAMS_COMMUTE -> id == R.string.commute
Constants.HOME_PARAMS_FOCUS -> id == R.string.focus
else -> id == R.string.all
}, text = stringResource(id = id)) {
when(id) {
R.string.all -> viewModel.setParams(null)
R.string.relax -> viewModel.setParams(Constants.HOME_PARAMS_RELAX)
R.string.sleep -> viewModel.setParams(Constants.HOME_PARAMS_SLEEP)
R.string.energize -> viewModel.setParams(Constants.HOME_PARAMS_ENERGIZE)
R.string.sad -> viewModel.setParams(Constants.HOME_PARAMS_SAD)
R.string.romance -> viewModel.setParams(Constants.HOME_PARAMS_ROMANCE)
R.string.feel_good -> viewModel.setParams(Constants.HOME_PARAMS_FEEL_GOOD)
R.string.workout -> viewModel.setParams(Constants.HOME_PARAMS_WORKOUT)
R.string.party -> viewModel.setParams(Constants.HOME_PARAMS_PARTY)
R.string.commute -> viewModel.setParams(Constants.HOME_PARAMS_COMMUTE)
R.string.focus -> viewModel.setParams(Constants.HOME_PARAMS_FOCUS)
}
}
Spacer(modifier = Modifier.width(4.dp))
}
}
Box(
modifier =
Modifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@ fun PlaylistScreen(
"",
),
color = Color.White,
style = typo.bodyMedium,
modifier = Modifier.padding(vertical = 8.dp),
)
AnimatedVisibility(visible = shouldShowSuggestions) {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/maxrave/simpmusic/ui/theme/Color.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ val md_theme_dark_scrim = Color(0xFF000000)
val colorPrimaryDark = Color(0x19000000)
val back_button_color = Color(0x197E7E7E)

val checkedFilterColor = Color(0xff4d4848)

val shimmerBackground = Color(0x7E383737)
val shimmerLine = Color(0xFF4D4848)

Expand Down
24 changes: 19 additions & 5 deletions app/src/main/java/com/maxrave/simpmusic/viewModel/HomeViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.first
Expand Down Expand Up @@ -77,6 +78,9 @@ class HomeViewModel
private val _songEntity: MutableStateFlow<SongEntity?> = MutableStateFlow(null)
val songEntity: StateFlow<SongEntity?> = _songEntity

private var _params: MutableStateFlow<String?> = MutableStateFlow(null)
val params: StateFlow<String?> = _params

private val exceptionHandler =
CoroutineExceptionHandler { _, throwable ->
onError("Exception handled: ${throwable.localizedMessage}")
Expand All @@ -92,21 +96,21 @@ class HomeViewModel
launch {
dataStoreManager.location.distinctUntilChanged().collect {
regionCode = it
getHomeItemList()
getHomeItemList(params.value)
}
}
// refresh when language change
val job2 =
launch {
dataStoreManager.language.distinctUntilChanged().collect {
language = it
getHomeItemList()
getHomeItemList(params.value)
}
}
val job3 =
launch {
dataStoreManager.cookie.distinctUntilChanged().collect {
getHomeItemList()
getHomeItemList(params.value)
_accountInfo.emit(
Pair(
dataStoreManager.getString("AccountName").first(),
Expand All @@ -115,13 +119,19 @@ class HomeViewModel
)
}
}
val job4 = launch {
params.collectLatest {
getHomeItemList(it)
}
}
job1.join()
job2.join()
job3.join()
job4.join()
}
}

fun getHomeItemList() {
fun getHomeItemList(params: String? = null) {
language =
runBlocking {
dataStoreManager.getString(SELECTED_LANGUAGE).first()
Expand All @@ -135,7 +145,7 @@ class HomeViewModel
// regionCode,
// SUPPORTED_LANGUAGE.serverCodes[SUPPORTED_LANGUAGE.codes.indexOf(language)]
// ),
mainRepository.getHomeData(),
mainRepository.getHomeData(params),
mainRepository.getMoodAndMomentsData(),
mainRepository.getChartData(dataStoreManager.chartKey.first()),
mainRepository.getNewRelease(),
Expand Down Expand Up @@ -420,4 +430,8 @@ class HomeViewModel
mainRepository.insertPairSongLocalPlaylist(pairSongLocalPlaylist)
}
}

fun setParams(params: String?) {
_params.value = params
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,14 @@
<string name="translucent_bottom_navigation_bar">Translucent bottom navigation bar</string>
<string name="you_can_see_the_content_below_the_bottom_bar">You can see the content below the bottom bar</string>
<string name="donation">If you love my work, give me a coffee</string>
<string name="relax">Relax</string>
<string name="sleep">Sleep</string>
<string name="energize">Energize</string>
<string name="sad">Sad</string>
<string name="romance">Romance</string>
<string name="feel_good">Feel Good</string>
<string name="workout">Workout</string>
<string name="party">Party</string>
<string name="commute">Commute</string>
<string name="focus">Focus</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.maxrave.kotlinytmusicscraper.config

object Constants {

//Home params
const val HOME_PARAMS_RELAX = "ggM8SgQIBxADSgQIBRABSgQICRABSgQIChABSgQIDRABSgQICBABSgQIBBABSgQIDhABSgQIAxABSgQIBhAB"
const val HOME_PARAMS_SLEEP = "ggM8SgQIBxABSgQIBRADSgQICRABSgQIChABSgQIDRABSgQICBABSgQIBBABSgQIDhABSgQIAxABSgQIBhAB"
const val HOME_PARAMS_ENERGIZE = "ggM8SgQIBxABSgQIBRABSgQICRADSgQIChABSgQIDRABSgQICBABSgQIBBABSgQIDhABSgQIAxABSgQIBhAB"
const val HOME_PARAMS_SAD = "ggM8SgQIBxABSgQIBRABSgQICRABSgQIChADSgQIDRABSgQICBABSgQIBBABSgQIDhABSgQIAxABSgQIBhAB"
const val HOME_PARAMS_ROMANCE = "ggM8SgQIBxABSgQIBRABSgQICRABSgQIChABSgQIDRADSgQICBABSgQIBBABSgQIDhABSgQIAxABSgQIBhAB"
const val HOME_PARAMS_FEEL_GOOD = "ggM8SgQIBxABSgQIBRABSgQICRABSgQIChABSgQIDRABSgQICBADSgQIBBABSgQIDhABSgQIAxABSgQIBhAB"
const val HOME_PARAMS_WORKOUT = "ggM8SgQIBxABSgQIBRABSgQICRABSgQIChABSgQIDRABSgQICBABSgQIBBADSgQIDhABSgQIAxABSgQIBhAB"
const val HOME_PARAMS_PARTY = "ggM8SgQIBxABSgQIBRABSgQICRABSgQIChABSgQIDRABSgQICBABSgQIBBABSgQIDhADSgQIAxABSgQIBhAB"
const val HOME_PARAMS_COMMUTE = "ggM8SgQIBxABSgQIBRABSgQICRABSgQIChABSgQIDRABSgQICBABSgQIBBABSgQIDhABSgQIAxADSgQIBhAB"
const val HOME_PARAMS_FOCUS = "ggM8SgQIBxABSgQIBRABSgQICRABSgQIChABSgQIDRABSgQICBABSgQIBBABSgQIDhABSgQIAxABSgQIBhAD"

}

0 comments on commit 4a2a1f6

Please sign in to comment.