Skip to content

Commit

Permalink
refactor: fix ktlint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
teccheck committed Jan 6, 2025
1 parent 1f52c79 commit 4e8f3b7
Show file tree
Hide file tree
Showing 57 changed files with 252 additions and 285 deletions.
Empty file added .github/workflows/build.yml
Empty file.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package io.github.teccheck.fastlyrics

import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4

import androidx.test.platform.app.InstrumentationRegistry
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith

import org.junit.Assert.*

/**
* Instrumented test, which will execute on an Android device.
*
Expand All @@ -19,6 +17,6 @@ class ExampleInstrumentedTest {
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("io.github.teccheck.fastlyrics", appContext.packageName)
Assert.assertEquals("io.github.teccheck.fastlyrics", appContext.packageName)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ abstract class BaseActivity : AppCompatActivity() {
private fun setNightMode(mode: Int) {
AppCompatDelegate.setDefaultNightMode(mode)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,4 @@ class FastLyricsApp : Application() {
MediaSession.init(this)
ProviderOrder.init(this)
}

}
}
31 changes: 14 additions & 17 deletions app/src/main/java/io/github/teccheck/fastlyrics/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ import io.github.teccheck.fastlyrics.ui.permission.PermissionActivity
import io.github.teccheck.fastlyrics.ui.saved.SavedActivity
import io.github.teccheck.fastlyrics.ui.settings.SettingsActivity

class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedListener {
class MainActivity :
BaseActivity(),
NavigationView.OnNavigationItemSelectedListener {

private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var navController: NavController
Expand Down Expand Up @@ -50,23 +52,20 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
return super.onCreateOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
return when (item.itemId) {
R.id.app_bar_search -> {
if (navController.currentDestination?.id != R.id.nav_search) {
navController.navigate(R.id.nav_search)
}
true
override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
R.id.app_bar_search -> {
if (navController.currentDestination?.id != R.id.nav_search) {
navController.navigate(R.id.nav_search)
}

else -> super.onOptionsItemSelected(item)
true
}
}

override fun onSupportNavigateUp(): Boolean {
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
else -> super.onOptionsItemSelected(item)
}

override fun onSupportNavigateUp(): Boolean =
navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()

override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.nav_saved -> startActivity(Intent(this, SavedActivity::class.java))
Expand All @@ -80,11 +79,9 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList
return false
}

fun getSearchMenuItem(): MenuItem? {
return searchMenuItem
}
fun getSearchMenuItem(): MenuItem? = searchMenuItem

companion object {
private const val TAG = "MainActivity"
}
}
}
27 changes: 9 additions & 18 deletions app/src/main/java/io/github/teccheck/fastlyrics/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,18 @@ class Settings(context: Context) {
}

@StyleRes
fun getMaterialStyle(): Int {
return when (sharedPreferences.getString(KEY_MATERIAL_STYLE, DEFAULT_MATERIAL_STYLE)) {
MATERIAL_STYLE_ONE -> R.style.Theme_FastLyrics_Material1
MATERIAL_STYLE_TWO -> R.style.Theme_FastLyrics_Material2
MATERIAL_STYLE_THREE -> R.style.Theme_FastLyrics_Material3
else -> R.style.Theme_FastLyrics_Material2
}
fun getMaterialStyle(): Int = when (sharedPreferences.getString(KEY_MATERIAL_STYLE, DEFAULT_MATERIAL_STYLE)) {
MATERIAL_STYLE_ONE -> R.style.Theme_FastLyrics_Material1
MATERIAL_STYLE_TWO -> R.style.Theme_FastLyrics_Material2
MATERIAL_STYLE_THREE -> R.style.Theme_FastLyrics_Material3
else -> R.style.Theme_FastLyrics_Material2
}

fun getIsAutoRefreshEnabled(): Boolean {
return sharedPreferences.getBoolean(KEY_AUTO_REFRESH, false)
}
fun getIsAutoRefreshEnabled(): Boolean = sharedPreferences.getBoolean(KEY_AUTO_REFRESH, false)

fun getSyncedLyricsByDefault(): Boolean {
return sharedPreferences.getBoolean(KEY_SYNCED_LYRICS_BY_DEFAULT, false)
}
fun getSyncedLyricsByDefault(): Boolean = sharedPreferences.getBoolean(KEY_SYNCED_LYRICS_BY_DEFAULT, false)

fun getTextSize(): Int {
return sharedPreferences.getInt(KEY_TEXT_SIZE, 18)
}
fun getTextSize(): Int = sharedPreferences.getInt(KEY_TEXT_SIZE, 18)

companion object {
private const val KEY_APP_THEME = "app_theme"
Expand All @@ -51,5 +43,4 @@ class Settings(context: Context) {
private const val DEFAULT_APP_THEME = "-1"
private const val DEFAULT_MATERIAL_STYLE = MATERIAL_STYLE_TWO
}

}
}
2 changes: 1 addition & 1 deletion app/src/main/java/io/github/teccheck/fastlyrics/Tokens.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package io.github.teccheck.fastlyrics
object Tokens {
const val GENIUS_API = "ZTejoT_ojOEasIkT9WrMBhBQOz6eYKK5QULCMECmOhvwqjRZ6WbpamFe3geHnvp3"
const val PETIT_LYRICS_API = "p1110417"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import android.content.Context
import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.room.Room
import dev.forkhandles.result4k.Result
import io.github.teccheck.fastlyrics.api.storage.LyricsDatabase
import io.github.teccheck.fastlyrics.exceptions.LyricsApiException
import io.github.teccheck.fastlyrics.exceptions.LyricsNotFoundException
import io.github.teccheck.fastlyrics.model.LyricsType
import io.github.teccheck.fastlyrics.model.SongWithLyrics
import io.github.teccheck.fastlyrics.utils.Utils
import java.util.concurrent.Executors
import dev.forkhandles.result4k.Result
import io.github.teccheck.fastlyrics.model.LyricsType

object LyricStorage {
private const val TAG = "LyricsStorage"
Expand All @@ -34,15 +34,14 @@ object LyricStorage {
executor.submit {
liveDataTarget.postValue(
Utils.result(
database.songsDao().getAll(), LyricsNotFoundException()
database.songsDao().getAll(),
LyricsNotFoundException()
)
)
}
}

fun getSongAsync(
id: Long, liveDataTarget: MutableLiveData<Result<SongWithLyrics, LyricsApiException>>
) {
fun getSongAsync(id: Long, liveDataTarget: MutableLiveData<Result<SongWithLyrics, LyricsApiException>>) {
executor.submit {
liveDataTarget.postValue(
Utils.result(getSong(id), LyricsNotFoundException())
Expand All @@ -51,14 +50,14 @@ object LyricStorage {
}

fun store(song: SongWithLyrics) {
if (findSong(song.title, song.artist, song.type) == null)
if (findSong(song.title, song.artist, song.type) == null) {
database.songsDao().insert(song)
}
}

private fun getSong(id: Long): SongWithLyrics? = database.songsDao().getSong(id)

fun findSong(title: String, artist: String): SongWithLyrics? =
database.songsDao().findSong(title, artist)
fun findSong(title: String, artist: String): SongWithLyrics? = database.songsDao().findSong(title, artist)

fun findSong(title: String, artist: String, type: LyricsType): SongWithLyrics? =
database.songsDao().findSong(title, artist, type)
Expand Down
29 changes: 18 additions & 11 deletions app/src/main/java/io/github/teccheck/fastlyrics/api/LyricsApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ object LyricsApi {
executor.submit { liveDataTarget.postValue(provider.search(query)) }
}

private fun fetchLyrics(
songMeta: SongMeta, synced: Boolean = false
): Result<SongWithLyrics, LyricsApiException> {
private fun fetchLyrics(songMeta: SongMeta, synced: Boolean = false): Result<SongWithLyrics, LyricsApiException> {
Log.d(TAG, "fetchLyrics($songMeta, $synced)")
var bestResult: SearchResult? = null
var bestResultScore = 0.0
Expand Down Expand Up @@ -106,17 +104,26 @@ object LyricsApi {
private fun getResultScore(songMeta: SongMeta, searchResult: SearchResult): Double {
var score = 0.0

if (songMeta.title == searchResult.title) score += 0.5
else if (songMeta.title.startsWith(searchResult.title)) score += 0.4
else if (searchResult.title.startsWith(songMeta.title)) score += 0.3
if (songMeta.title == searchResult.title) {
score += 0.5
} else if (songMeta.title.startsWith(searchResult.title)) {
score += 0.4
} else if (searchResult.title.startsWith(songMeta.title)) {
score += 0.3
}

if (songMeta.artist == null) return score
else if (songMeta.artist == searchResult.artist) score += 0.5
else if (songMeta.artist.startsWith(searchResult.artist)) score += 0.4
else if (searchResult.artist.startsWith(songMeta.artist)) score += 0.3
if (songMeta.artist == null) {
return score
} else if (songMeta.artist == searchResult.artist) {
score += 0.5
} else if (songMeta.artist.startsWith(searchResult.artist)) {
score += 0.4
} else if (searchResult.artist.startsWith(songMeta.artist)) {
score += 0.3
}

if (songMeta.album == searchResult.album) score += 0.5

return score
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ object MediaSession {
private var initialized = false

fun init(context: Context) {
if (!DummyNotificationListenerService.canAccessNotifications(context))
if (!DummyNotificationListenerService.canAccessNotifications(context)) {
return
}

if (initialized)
if (initialized) {
return
}

initialized = true

Expand Down Expand Up @@ -136,19 +138,28 @@ object MediaSession {
}

private fun isActive(playbackState: PlaybackState?): Boolean {
if (playbackState == null)
if (playbackState == null) {
return false
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
return playbackState.isActive
}

return when (playbackState.state) {
PlaybackState.STATE_FAST_FORWARDING, PlaybackState.STATE_REWINDING, PlaybackState.STATE_SKIPPING_TO_PREVIOUS, PlaybackState.STATE_SKIPPING_TO_NEXT, PlaybackState.STATE_SKIPPING_TO_QUEUE_ITEM, PlaybackState.STATE_BUFFERING, PlaybackState.STATE_CONNECTING, PlaybackState.STATE_PLAYING -> true
PlaybackState.STATE_FAST_FORWARDING,
PlaybackState.STATE_REWINDING,
PlaybackState.STATE_SKIPPING_TO_PREVIOUS,
PlaybackState.STATE_SKIPPING_TO_NEXT,
PlaybackState.STATE_SKIPPING_TO_QUEUE_ITEM,
PlaybackState.STATE_BUFFERING,
PlaybackState.STATE_CONNECTING,
PlaybackState.STATE_PLAYING -> true
else -> false
}
}

fun interface SongMetaCallback {
fun onSongMetaChanged(songMeta: SongMeta)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,15 @@ object Deezer : LyricsProvider {
)
}

private fun parseSyncedLyrics(lines: JsonArray): String {
return lines.joinToString(separator = "\n") {
val line = it.asJsonObject
"${line.get("lrcTimestamp").asString}${line.get("line").asString}"
}
private fun parseSyncedLyrics(lines: JsonArray): String = lines.joinToString(separator = "\n") {
val line = it.asJsonObject
"${line.get("lrcTimestamp").asString}${line.get("line").asString}"
}

private fun getAuthToken(): String? {
if (authToken == null) authToken =
apiService.auth()?.execute()?.body()?.get(KEY_JWT)?.asString
if (authToken == null) {
authToken = apiService.auth()?.execute()?.body()?.get(KEY_JWT)?.asString
}
return authToken
}

Expand Down Expand Up @@ -245,7 +244,7 @@ object Deezer : LyricsProvider {
}
}
}
""".trimIndent()
""".trimIndent()
queryJson.addProperty("query", query)
return queryJson
}
Expand Down Expand Up @@ -314,8 +313,6 @@ object Deezer : LyricsProvider {
fun auth(): Call<JsonObject>?

@POST("https://pipe.deezer.com/api")
fun query(
@Body body: JsonObject, @Header("Authorization") authString: String
): Call<JsonObject>?
fun query(@Body body: JsonObject, @Header("Authorization") authString: String): Call<JsonObject>?
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.github.teccheck.fastlyrics.exceptions.ParseException
import io.github.teccheck.fastlyrics.model.LyricsType
import io.github.teccheck.fastlyrics.model.SearchResult
import io.github.teccheck.fastlyrics.model.SongWithLyrics
import java.io.IOException
import okhttp3.OkHttpClient
import okhttp3.Request
import org.json.JSONException
Expand All @@ -23,7 +24,6 @@ import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.GET
import retrofit2.http.Path
import retrofit2.http.Query
import java.io.IOException

object Genius : LyricsProvider {

Expand Down Expand Up @@ -184,4 +184,4 @@ object Genius : LyricsProvider {
@GET("songs/{songId}")
fun fetchSongInfo(@Path("songId") songId: Long): Call<JsonElement>?
}
}
}
Loading

0 comments on commit 4e8f3b7

Please sign in to comment.