Skip to content

Commit

Permalink
Fix formatting and optimize imports
Browse files Browse the repository at this point in the history
This commit includes minor code style improvements such as fixing formatting inconsistencies and optimizing imports. These changes enhance the readability and maintainability of the
 codebase.
  • Loading branch information
BobbyESP committed Aug 13, 2024
1 parent 46a5e1a commit bf18bef
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 31 deletions.
17 changes: 11 additions & 6 deletions app/src/main/java/pl/lambada/songsync/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import java.io.File
*/
class MainActivity : ComponentActivity() {
val viewModel: MainViewModel by viewModels()

/**
* Called when the activity is starting.
*
Expand Down Expand Up @@ -100,7 +101,8 @@ class MainActivity : ComponentActivity() {
viewModel.sdCardPath = sdCardPath
}

val includeTranslation = dataStore.get(booleanPreferencesKey("include_translation"), false)
val includeTranslation =
dataStore.get(booleanPreferencesKey("include_translation"), false)
viewModel.includeTranslation = includeTranslation

val blacklist = dataStore.get(stringPreferencesKey("blacklist"), null)
Expand All @@ -111,7 +113,8 @@ class MainActivity : ComponentActivity() {
val hideLyrics = dataStore.get(booleanPreferencesKey("hide_lyrics"), false)
viewModel.hideLyrics = hideLyrics

val provider = dataStore.get(stringPreferencesKey("provider"), Providers.SPOTIFY.displayName)
val provider =
dataStore.get(stringPreferencesKey("provider"), Providers.SPOTIFY.displayName)
viewModel.provider = Providers.entries.find { it.displayName == provider }!!

val embedLyrics = dataStore.get(booleanPreferencesKey("embed_lyrics"), false)
Expand Down Expand Up @@ -160,7 +163,7 @@ class MainActivity : ComponentActivity() {
}
)

Surface( modifier = Modifier.fillMaxSize() ) {
Surface(modifier = Modifier.fillMaxSize()) {
if (!hasLoadedPermissions) {
LoadingScreen()
} else if (!hasPermissions) {
Expand Down Expand Up @@ -194,14 +197,15 @@ class MainActivity : ComponentActivity() {
NoInternetDialog(
onConfirm = { finishAndRemoveTask() },
onIgnore = {
internetConnection = true // assume connected (if spotify is down, can use other providers)
internetConnection =
true // assume connected (if spotify is down, can use other providers)
}
)
}
}
}
}
}
}

override fun onResume() {
val notificationManager =
Expand Down Expand Up @@ -232,7 +236,8 @@ class MainActivity : ComponentActivity() {
fun RequestPermissions(onGranted: () -> Unit, context: Context, onDone: () -> Unit) {
var storageManager: ActivityResultLauncher<Intent>? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
storageManager = rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
storageManager =
rememberLauncherForActivityResult(ActivityResultContracts.StartActivityForResult()) {
if (Environment.isExternalStorageManager()) {
onGranted()
}
Expand Down
17 changes: 13 additions & 4 deletions app/src/main/java/pl/lambada/songsync/data/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.kyant.taglib.PropertyMap
import com.kyant.taglib.TagLib
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
Expand Down Expand Up @@ -134,7 +133,11 @@ class MainViewModel : ViewModel() {
when (this.provider) {
Providers.SPOTIFY -> SpotifyLyricsAPI().getSyncedLyrics(songLink, version)
Providers.LRCLIB -> LRCLibAPI().getSyncedLyrics(this.lrcLibID)
Providers.NETEASE -> NeteaseAPI().getSyncedLyrics(this.neteaseID, includeTranslation)
Providers.NETEASE -> NeteaseAPI().getSyncedLyrics(
this.neteaseID,
includeTranslation
)

Providers.APPLE -> AppleAPI().getSyncedLyrics(this.appleID)
}
} catch (e: Exception) {
Expand Down Expand Up @@ -264,17 +267,20 @@ class MainViewModel : ViewModel() {
val data: List<Song> = when {
cachedFilteredSongs.value.isNotEmpty() -> cachedFilteredSongs.value
cachedSongs != null -> cachedSongs!!
else -> { return@launch }
else -> {
return@launch
}
}

val results = data.filter {
it.title?.contains(query, ignoreCase = true) == true ||
it.artist?.contains(query, ignoreCase = true) == true
it.artist?.contains(query, ignoreCase = true) == true
}

_searchResults.value = results
}
}

/**
* Loads all songs' folders
* @param context The application context.
Expand Down Expand Up @@ -314,10 +320,12 @@ class MainViewModel : ViewModel() {
)
}
}

hideLyrics -> {
_cachedFilteredSongs?.value = cachedSongs!!
.filter { it.filePath.toLrcFile()?.exists() != true }
}

hideFolders -> {
_cachedFilteredSongs?.value = cachedSongs!!.filter {
!blacklistedFolders.contains(
Expand All @@ -328,6 +336,7 @@ class MainViewModel : ViewModel() {
)
}
}

else -> {
_cachedFilteredSongs?.value = emptyList()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class AppleAPI {
songName = result.songName,
artistName = result.artistName,
songLink = result.url,
albumCoverLink = result.artwork.replace("{w}", "100").replace("{h}", "100").replace("{f}", "png"),
albumCoverLink = result.artwork.replace("{w}", "100").replace("{h}", "100")
.replace("{f}", "png"),
appleID = result.id
)
}
Expand Down Expand Up @@ -80,11 +81,13 @@ class AppleAPI {
syncedLyrics.append("<${line.endtime.toLrcTimestamp()}>\n")
}
}

"Line" -> {
for (line in lines) {
syncedLyrics.append("[${line.timestamp.toLrcTimestamp()}]${line.text[0].text}\n")
}
}

else -> return null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.parameter
import io.ktor.client.statement.bodyAsText
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.ExperimentalSerializationApi
import pl.lambada.songsync.data.EmptyQueryException
import pl.lambada.songsync.data.InternalErrorException
Expand All @@ -15,7 +13,6 @@ import pl.lambada.songsync.domain.model.lyrics_providers.others.NeteaseLyricsRes
import pl.lambada.songsync.domain.model.lyrics_providers.others.NeteaseResponse
import pl.lambada.songsync.util.networking.Ktor.client
import pl.lambada.songsync.util.networking.Ktor.json
import java.net.URLEncoder

class NeteaseAPI {
private val baseURL = "http://music.163.com/api/"
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/pl/lambada/songsync/ui/Navigator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import kotlinx.serialization.Serializable
import pl.lambada.songsync.data.MainViewModel
import pl.lambada.songsync.domain.model.Song
import pl.lambada.songsync.ui.screens.AboutScreen
import pl.lambada.songsync.ui.screens.SearchScreen
import pl.lambada.songsync.ui.screens.HomeScreen
import pl.lambada.songsync.ui.screens.SearchScreen

/**
* Composable function for handling navigation within the app.
Expand Down
22 changes: 17 additions & 5 deletions app/src/main/java/pl/lambada/songsync/ui/components/CommonTexts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay
import pl.lambada.songsync.data.MainViewModel

@Composable
fun AnimatedText(
Expand All @@ -52,10 +51,23 @@ fun AnimatedText(
modifier: Modifier = Modifier,
) {
if (animate) {
MarqueeText(text = text, fontSize = fontSize, fontWeight = fontWeight, modifier = modifier, color = color)
}
else {
Text(text = text, fontSize = fontSize, fontWeight = fontWeight, modifier = modifier, color = color, maxLines = 1, overflow = TextOverflow.Ellipsis)
MarqueeText(
text = text,
fontSize = fontSize,
fontWeight = fontWeight,
modifier = modifier,
color = color
)
} else {
Text(
text = text,
fontSize = fontSize,
fontWeight = fontWeight,
modifier = modifier,
color = color,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Switch
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -21,7 +21,8 @@ fun SwitchItem(
modifier: Modifier = Modifier,
innerPaddingValues: PaddingValues = PaddingValues(
horizontal = 22.dp,
vertical = 16.dp),
vertical = 16.dp
),
onClick: () -> Unit,
) {
Row(
Expand Down
20 changes: 14 additions & 6 deletions app/src/main/java/pl/lambada/songsync/ui/screens/AboutScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
Expand All @@ -52,7 +51,6 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.datastore.preferences.core.booleanPreferencesKey
import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.stringPreferencesKey
import androidx.navigation.NavController
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -126,7 +124,10 @@ fun AboutScreen(
) {
viewModel.pureBlack.value = !selected
selected = !selected
dataStore.set(key = booleanPreferencesKey("pure_black"), value = selected)
dataStore.set(
key = booleanPreferencesKey("pure_black"),
value = selected
)
}
}
}
Expand All @@ -142,7 +143,10 @@ fun AboutScreen(
) {
viewModel.disableMarquee.value = !selected
selected = !selected
dataStore.set(key = booleanPreferencesKey("marquee_disable"), value = selected)
dataStore.set(
key = booleanPreferencesKey("marquee_disable"),
value = selected
)
}
}
}
Expand All @@ -157,7 +161,10 @@ fun AboutScreen(
) {
viewModel.includeTranslation = !selected
selected = !selected
dataStore.set(key = booleanPreferencesKey("include_translation"), value = selected)
dataStore.set(
key = booleanPreferencesKey("include_translation"),
value = selected
)
}
}
}
Expand Down Expand Up @@ -306,7 +313,8 @@ fun AboutScreen(
item {
AboutItem(stringResource(R.string.contributors)) {
Contributor.entries.forEach {
val additionalInfo = stringResource(id = it.contributionLevel.stringResource)
val additionalInfo =
stringResource(id = it.contributionLevel.stringResource)
Column(
modifier = Modifier
.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,17 @@ fun SharedTransitionScope.SearchScreen(
val embeddedToFile = kotlin.runCatching {
viewModel.embedLyricsInFile(
context,
filePath ?: throw NullPointerException("filePath is null"),
filePath
?: throw NullPointerException("filePath is null"),
lrc
)
}

if(embeddedToFile.isFailure) {
if (embeddedToFile.isFailure) {
Toast.makeText(
context,
embeddedToFile.exceptionOrNull()?.message ?: context.getString(R.string.error),
embeddedToFile.exceptionOrNull()?.message
?: context.getString(R.string.error),
Toast.LENGTH_LONG
).show()
return@Button
Expand Down

0 comments on commit bf18bef

Please sign in to comment.