Skip to content

Commit

Permalink
feat: Add detekt in the project (#1651)
Browse files Browse the repository at this point in the history
Co-authored-by: Theo L.M. <[email protected]>
  • Loading branch information
Secozzi and theolm authored Jul 1, 2024
1 parent 1b80548 commit affe651
Show file tree
Hide file tree
Showing 14 changed files with 2,340 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build_pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ jobs:
- name: Build app and run unit tests
uses: gradle/gradle-command-action@v3
with:
arguments: ktlintCheck assembleStandardRelease testReleaseUnitTest
arguments: detekt assembleStandardRelease testReleaseUnitTest
2 changes: 1 addition & 1 deletion .github/workflows/build_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
- name: Build app and run unit tests
uses: gradle/gradle-command-action@v3
with:
arguments: ktlintCheck assembleStandardRelease testReleaseUnitTest
arguments: detekt assembleStandardRelease testReleaseUnitTest

# Sign APK and create release for tags

Expand Down
1 change: 0 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,6 @@ dependencies {
implementation(compose.ui.util)
implementation(compose.accompanist.webview)
implementation(compose.accompanist.systemuicontroller)
lintChecks(compose.lintchecks)

implementation(androidx.paging.runtime)
implementation(androidx.paging.compose)
Expand Down
31 changes: 15 additions & 16 deletions app/src/main/java/eu/kanade/tachiyomi/ui/player/PlayerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class PlayerActivity : BaseActivity() {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
}
}

private const val MAX_BRIGHTNESS = 255F
}

override fun onNewIntent(intent: Intent) {
Expand Down Expand Up @@ -756,23 +758,20 @@ class PlayerActivity : BaseActivity() {
verticalScrollLeft(0F)
}

@Suppress("ReturnCount")
private fun getMaxBrightness(): Float {
val powerManager = getSystemService(POWER_SERVICE) as? PowerManager
if (powerManager != null) {
val fields = powerManager.javaClass.declaredFields
for (field in fields) {
// https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/os/PowerManager.java
if (field.getName().equals("BRIGHTNESS_ON")) {
field.isAccessible = true
return try {
(field.get(powerManager) as Int).toFloat()
} catch (e: IllegalAccessException) {
255F
}
}
}
val powerManager = getSystemService(POWER_SERVICE) as? PowerManager ?: return MAX_BRIGHTNESS
val brightnessField = powerManager.javaClass.declaredFields.find {
it.name == "BRIGHTNESS_ON"
} ?: return MAX_BRIGHTNESS

brightnessField.isAccessible = true
return try {
(brightnessField.get(powerManager) as Int).toFloat()
} catch (e: IllegalAccessException) {
logcat(LogPriority.ERROR, e) { "Unable to access BRIGHTNESS_ON field" }
MAX_BRIGHTNESS
}
return 255F
}

private fun getCurrentBrightness(): Float {
Expand All @@ -783,11 +782,11 @@ class PlayerActivity : BaseActivity() {
return try {
Settings.System.getInt(resolver, Settings.System.SCREEN_BRIGHTNESS) / getMaxBrightness()
} catch (e: Settings.SettingNotFoundException) {
logcat(LogPriority.ERROR, e) { "Unable to get screen brightness" }
0.5F
}
}


@Suppress("DEPRECATION")
private fun setupMediaSession() {
mediaSession = MediaSessionCompat(this, "Aniyomi_Player_Session").apply {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ subprojects {
}

plugins.withType<BasePlugin> {
plugins.apply("tachiyomi.lint")
plugins.apply("detekt")
configure<BaseExtension> {
compileSdkVersion(AndroidConfig.compileSdk)
defaultConfig {
Expand Down
4 changes: 2 additions & 2 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ plugins {
}

dependencies {
implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
implementation(androidxLibs.gradle)

implementation(kotlinLibs.gradle)
implementation(libs.ktlint)
implementation(libs.detekt.gradlePlugin)
implementation(gradleApi())
}

Expand Down
47 changes: 47 additions & 0 deletions buildSrc/src/main/kotlin/detekt.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import io.gitlab.arturbosch.detekt.Detekt
import io.gitlab.arturbosch.detekt.DetektCreateBaselineTask
import org.gradle.accessors.dm.LibrariesForLibs

plugins {
id("io.gitlab.arturbosch.detekt")
}

val libs = the<LibrariesForLibs>()
dependencies {
detektPlugins(libs.detekt.rules.formatting)
detektPlugins(libs.detekt.rules.compose)
}

private val configFile = files("$rootDir/config/detekt/detekt.yml")
private val baselineFile = file("$rootDir/config/detekt/baseline.xml")
private val kotlinFiles = "**/*.kt"
private val resourceFiles = "**/resources/**"
private val buildFiles = "**/build/**"
private val generatedFiles = "**/generated/**"
private val scriptsFiles = "**/*.kts"

detekt {
buildUponDefaultConfig = true
parallel = true
autoCorrect = false
ignoreFailures = false
config.setFrom(configFile)
baseline = file(baselineFile)
}

tasks.withType<Detekt>().configureEach {
include(kotlinFiles)
exclude(resourceFiles, buildFiles, generatedFiles, scriptsFiles)
reports {
html.required.set(true)
xml.required.set(false)
txt.required.set(false)
}
}

tasks.withType<Detekt>().configureEach {
jvmTarget = JavaVersion.VERSION_17.toString()
}
tasks.withType<DetektCreateBaselineTask>().configureEach {
jvmTarget = JavaVersion.VERSION_17.toString()
}
23 changes: 0 additions & 23 deletions buildSrc/src/main/kotlin/tachiyomi.lint.gradle.kts

This file was deleted.

Loading

0 comments on commit affe651

Please sign in to comment.