Skip to content

Commit

Permalink
fix netease
Browse files Browse the repository at this point in the history
  • Loading branch information
nift4 committed Apr 30, 2024
1 parent 13be15a commit b3abc76
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 13 deletions.
10 changes: 10 additions & 0 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 16 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 @@ -4,6 +4,7 @@ import android.content.ContentUris
import android.content.Context
import android.net.Uri
import android.provider.MediaStore
import android.util.Log
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
Expand Down Expand Up @@ -46,10 +47,10 @@ class MainViewModel : ViewModel() {
var provider = Providers.SPOTIFY

// LRCLib Track ID
var lrcLibID = 0
private var lrcLibID = 0

// Netease Track ID
var neteaseID = 0
private var neteaseID = 0L
// TODO: Use values from SongInfo object returned by search instead of storing them here

/**
Expand All @@ -65,7 +66,10 @@ class MainViewModel : ViewModel() {
* @param offset (optional) The offset used for trying to find a better match or searching again.
* @return The SongInfo object containing the song information.
*/
@Throws(UnknownHostException::class, FileNotFoundException::class, NoTrackFoundException::class)
@Throws(
UnknownHostException::class, FileNotFoundException::class, NoTrackFoundException::class,
EmptyQueryException::class, InternalErrorException::class
)
suspend fun getSongInfo(query: SongInfo, offset: Int? = 0): SongInfo {
return try {
when (this.provider) {
Expand All @@ -78,8 +82,14 @@ class MainViewModel : ViewModel() {
this.neteaseID = it?.neteaseID ?: 0
} ?: throw NoTrackFoundException()
}
} catch (e: InternalErrorException) {
throw e
} catch (e: NoTrackFoundException) {
throw e
} catch (e: EmptyQueryException) {
throw e
} catch (e: Exception) {
throw NoTrackFoundException()
throw InternalErrorException(Log.getStackTraceString(e))
}
}

Expand Down Expand Up @@ -245,4 +255,6 @@ class MainViewModel : ViewModel() {

class NoTrackFoundException : Exception()

class InternalErrorException(msg: String) : Exception(msg)

class EmptyQueryException : Exception()
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pl.lambada.songsync.data.remote.lyrics_providers.others

import android.util.Log
import io.ktor.client.request.get
import io.ktor.client.request.header
import io.ktor.client.request.parameter
Expand All @@ -8,6 +9,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.ExperimentalSerializationApi
import pl.lambada.songsync.data.EmptyQueryException
import pl.lambada.songsync.data.InternalErrorException
import pl.lambada.songsync.data.NoTrackFoundException
import pl.lambada.songsync.domain.model.SongInfo
import pl.lambada.songsync.domain.model.lyrics_providers.others.NeteaseLyricsResponse
Expand Down Expand Up @@ -73,7 +75,7 @@ class NeteaseAPI {
try {
neteaseResponse = json.decodeFromString<NeteaseResponse>(responseBody)
} catch (e: kotlinx.serialization.MissingFieldException) {
throw NoTrackFoundException()
throw InternalErrorException(Log.getStackTraceString(e))
}

val artists = neteaseResponse.result.songs[0].artists.joinToString(", ") { it.name }
Expand All @@ -90,7 +92,7 @@ class NeteaseAPI {
* @param id The ID of the song from search results.
* @return The synced lyrics as a string.
*/
suspend fun getSyncedLyrics(id: Int): String? {
suspend fun getSyncedLyrics(id: Long): String? {
val response = client.get(
baseURL + "song/lyric"
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ data class SongInfo(
var songLink: String? = null,
var albumCoverLink: String? = null,
var lrcLibID: Int? = null, // LRCLib-only
var neteaseID: Int? = null, // Netease-only
var neteaseID: Long? = null, // Netease-only
) : Parcelable
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ data class NeteaseResult(
@Serializable
data class NeteaseSong(
val name: String,
val id: Int,
val id: Long,
val artists: List<NeteaseArtist>,
)

Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/pl/lambada/songsync/ui/screens/BrowseScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pl.lambada.songsync.ui.screens
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -50,6 +51,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import pl.lambada.songsync.R
import pl.lambada.songsync.data.EmptyQueryException
import pl.lambada.songsync.data.InternalErrorException
import pl.lambada.songsync.data.MainViewModel
import pl.lambada.songsync.data.NoTrackFoundException
import pl.lambada.songsync.domain.model.Song
Expand All @@ -68,6 +70,7 @@ import java.net.UnknownHostException
*/
@Composable
fun BrowseScreen(viewModel: MainViewModel) {
val tag = "BrowseScreen"

var nextSong: Song? by rememberSaveable { mutableStateOf(null) }
if (viewModel.nextSong != null) {
Expand Down Expand Up @@ -177,6 +180,7 @@ fun BrowseScreen(viewModel: MainViewModel) {

else -> {
queryStatus = QueryStatus.Failed
Log.e(tag, Log.getStackTraceString(e))
failReason = e
}
}
Expand Down Expand Up @@ -386,13 +390,19 @@ fun BrowseScreen(viewModel: MainViewModel) {
},
title = { Text(text = stringResource(id = R.string.error)) },
text = {
when (failReason) {
when (val e = failReason) {
is NoTrackFoundException -> {
Text(
text = stringResource(R.string.no_results)
)
}

is InternalErrorException -> {
Text(
text = stringResource(R.string.internal_error, e.message.toString())
)
}

is EmptyQueryException -> {
Text(
text = stringResource(R.string.invalid_query)
Expand All @@ -408,7 +418,7 @@ fun BrowseScreen(viewModel: MainViewModel) {
}

else -> {
Text(text = failReason.toString())
Text(text = e.toString())
}
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ import kotlinx.parcelize.Parcelize
import pl.lambada.songsync.MainActivity
import pl.lambada.songsync.R
import pl.lambada.songsync.data.EmptyQueryException
import pl.lambada.songsync.data.InternalErrorException
import pl.lambada.songsync.data.MainViewModel
import pl.lambada.songsync.data.NoTrackFoundException
import pl.lambada.songsync.domain.model.Song
Expand Down Expand Up @@ -745,7 +746,7 @@ fun BatchDownloadLyrics(songs: List<Song>, viewModel: MainViewModel, onDone: ()
continue
}

is NoTrackFoundException, is EmptyQueryException -> {
is NoTrackFoundException, is EmptyQueryException, is InternalErrorException -> {
// not increasing notFoundInARow because that is for rate limit
failedCount++
}
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,5 @@
<string name="provider">Lyrics provider</string>
<string name="provider_summary">Please select lyrics provider below</string>
<string name="ignore">Ignore</string>
<string name="internal_error">Internal error: %s</string>
</resources>
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[versions]
accompanist-systemuicontroller = "0.31.3-beta"
accompanist-permissions = "0.31.5-beta"
agp = "8.2.0-rc01"
agp = "8.3.2"
coil-compose = "2.1.0"
kotlin = "1.8.10"
core-ktx = "1.12.0"
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Sat May 27 18:37:16 CEST 2023
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

0 comments on commit b3abc76

Please sign in to comment.