Skip to content

Commit

Permalink
Refactored code for QuickLyricsSearch UI
Browse files Browse the repository at this point in the history
- Improved code style and formatting.

Signed-off-by: Gabriel Fontán <gabilessto@gmail.com>
  • Loading branch information
BobbyESP committed Nov 15, 2024
1 parent 5a6e4a8 commit 33bb6f6
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package pl.lambada.songsync.activities.quicksearch

import android.content.Intent
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.view.WindowManager
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
Expand All @@ -14,13 +10,9 @@ import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.ModalBottomSheetDefaults
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import coil.ImageLoader
import coil.ImageLoaderFactory
import coil.disk.DiskCache
import coil.imageLoader
import coil.memory.MemoryCache
import kotlinx.coroutines.Dispatchers
import pl.lambada.songsync.activities.quicksearch.viewmodel.QuickLyricsSearchViewModel
Expand Down Expand Up @@ -96,10 +88,17 @@ class QuickLyricsSearchActivity : AppCompatActivity() {
private fun handleShareIntent(intent: Intent) {
when (intent.action) {
Intent.ACTION_SEND -> {
val songName = intent.getStringExtra("songName") ?: "" //TODO: Change to a exception in the VM
val artistName = intent.getStringExtra("artistName") ?: "" //TODO: Change to a exception in the VM
val songName =
intent.getStringExtra("songName") ?: "" //TODO: Change to a exception in the VM
val artistName = intent.getStringExtra("artistName")
?: "" //TODO: Change to a exception in the VM

viewModel.onEvent(QuickLyricsSearchViewModel.Event.Fetch(songName to artistName, this))
viewModel.onEvent(
QuickLyricsSearchViewModel.Event.Fetch(
songName to artistName,
this
)
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package pl.lambada.songsync.activities.quicksearch

import android.util.Log
import androidx.compose.animation.Crossfade
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
Expand All @@ -14,18 +13,13 @@ import androidx.compose.material.icons.automirrored.rounded.Send
import androidx.compose.material.icons.filled.Cloud
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.rounded.Subtitles
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
Expand All @@ -42,7 +36,6 @@ import pl.lambada.songsync.activities.quicksearch.components.SyncedLyricsColumn
import pl.lambada.songsync.activities.quicksearch.viewmodel.QuickLyricsSearchViewModel
import pl.lambada.songsync.util.ResourceState
import pl.lambada.songsync.util.ScreenState
import pl.lambada.songsync.util.parseLyrics

@Composable
fun QuickLyricsSearchPage(
Expand Down Expand Up @@ -175,15 +168,17 @@ fun QuickLyricsSearchPage(
is ResourceState.Success<*> -> {
state.value.lyricsState.data?.let { _ -> //This crunches the animation lol
ExpandableOutlinedCard(
title = stringResource(R.string.song_lyrics),
title = stringResource(R.string.song_lyrics),
subtitle = stringResource(R.string.lyrics_subtitle),
icon = Icons.Rounded.Subtitles,
isExpanded = false,
modifier = Modifier.fillMaxWidth()
) {
SyncedLyricsColumn(
lyricsList = state.value.parsedLyrics,
modifier = Modifier.fillMaxWidth().padding(8.dp)
modifier = Modifier
.fillMaxWidth()
.padding(8.dp)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.ExpandLess
import androidx.compose.material.icons.outlined.PermDeviceInformation
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.FilledTonalIconButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
Expand Down Expand Up @@ -56,7 +54,9 @@ fun ExpandableOutlinedCard(
shape = MaterialTheme.shapes.small
) {
Row(
modifier = Modifier.fillMaxWidth().padding(8.dp),
modifier = Modifier
.fillMaxWidth()
.padding(8.dp),
verticalAlignment = Alignment.CenterVertically,
) {
Icon(
Expand All @@ -65,7 +65,10 @@ fun ExpandableOutlinedCard(
contentDescription = null,
)
Column(
modifier = Modifier.fillMaxWidth().padding(6.dp).weight(1f),
modifier = Modifier
.fillMaxWidth()
.padding(6.dp)
.weight(1f),
) {
Text(
text = title,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ fun QuickLyricsSongInfo(
)
) {
Row(
modifier = Modifier.fillMaxWidth().padding(8.dp),
modifier = Modifier
.fillMaxWidth()
.padding(8.dp),
horizontalArrangement = Arrangement.spacedBy(16.dp),
verticalAlignment = Alignment.CenterVertically
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ fun ButtonWithIconAndText(
enabled: Boolean = true,
backgroundColor: Color = MaterialTheme.colorScheme.secondaryContainer,
shape: CornerBasedShape = MaterialTheme.shapes.small,
onClick : () -> Unit = {}
onClick: () -> Unit = {}
) {
Surface(
modifier = modifier.semantics { role = Role.Button }.alpha(if (enabled) 1f else 0.4f),
modifier = modifier
.semantics { role = Role.Button }
.alpha(if (enabled) 1f else 0.4f),
onClick = onClick,
enabled = enabled,
shape = shape,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import pl.lambada.songsync.util.parseLyrics
class QuickLyricsSearchViewModel(
val userSettingsController: UserSettingsController,
private val lyricsProviderService: LyricsProviderService
): ViewModel() {
) : ViewModel() {
private val mutableState = MutableStateFlow(QuickSearchViewState())
val state = mutableState.asStateFlow()

Expand Down Expand Up @@ -55,7 +55,7 @@ class QuickLyricsSearchViewModel(
version = context.getVersion()
)

if(syncedLyrics != null) {
if (syncedLyrics != null) {
updateLyricsState(ResourceState.Success(syncedLyrics))
parseLyrics(syncedLyrics).let { parsedLyrics ->
Log.d("QuickLyricsSearchVM", "Parsed lyrics: $parsedLyrics")
Expand All @@ -80,23 +80,23 @@ class QuickLyricsSearchViewModel(
)

private fun updateScreenState(screenState: ScreenState<SongInfo>) {
if(screenState != mutableState.value.screenState) {
if (screenState != mutableState.value.screenState) {
mutableState.update {
it.copy(screenState = screenState)
}
}
}

private fun updateLyricsState(lyricsState: ResourceState<String>) {
if(lyricsState != mutableState.value.lyricsState) {
if (lyricsState != mutableState.value.lyricsState) {
mutableState.update {
it.copy(lyricsState = lyricsState)
}
}
}

fun onEvent(event: Event) {
when(event) {
when (event) {
is Event.Fetch -> {
mutableState.update {
it.copy(song = event.song)
Expand All @@ -107,6 +107,6 @@ class QuickLyricsSearchViewModel(
}

interface Event {
data class Fetch(val song: Pair<String, String>, val context: Context): Event
data class Fetch(val song: Pair<String, String>, val context: Context) : Event
}
}

0 comments on commit 33bb6f6

Please sign in to comment.