Skip to content

Commit

Permalink
Tweak Ktlint rules (#2512)
Browse files Browse the repository at this point in the history
* Revert "Spotless 7.0.0"

This reverts commit ddbfbff.

* Update rules

* Spotless 7.0.1

* Fix styles

* chore(deps): update plugin com.diffplug.spotless to v7.0.1

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
  • Loading branch information
Goooler and renovate[bot] authored Jan 8, 2025
1 parent bf22168 commit e1bc78f
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 95 deletions.
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ trim_trailing_whitespace = true
ij_kotlin_imports_layout = *
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
ij_kotlin_name_count_to_use_star_import = 999
ij_kotlin_name_count_to_use_star_import_for_members = 999
ij_kotlin_packages_to_use_import_on_demand = unset
ktlint_code_style = intellij_idea
ktlint_function_naming_ignore_when_annotated_with = Composable
ktlint_standard_discouraged-comment-location = disabled
ktlint_standard_function-expression-body = disabled
ktlint_compose_lambda-param-event-trailing = disabled

[*.md]
trim_trailing_whitespace = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ class GithubApiModule {

@Provides
@Singleton
fun providesGitHubContributorsApi(): GitHubContributorsAPI = Retrofit.Builder()
.baseUrl("https://api.github.com/")
.addConverterFactory(kotlinxJson.asConverterFactory("application/json".toMediaType()))
.build()
.create()
fun providesGitHubContributorsApi(): GitHubContributorsAPI {
return Retrofit.Builder()
.baseUrl("https://api.github.com/")
.addConverterFactory(kotlinxJson.asConverterFactory("application/json".toMediaType()))
.build()
.create()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ class WebsiteApiModule {

@Provides
@Singleton
fun providesWebsiteIconRequestApi(): IconRequestSettingsAPI = Retrofit.Builder()
.baseUrl("https://lawnchair.app/")
.addConverterFactory(kotlinxJson.asConverterFactory("application/json".toMediaType()))
.build()
.create()
fun providesWebsiteIconRequestApi(): IconRequestSettingsAPI {
return Retrofit.Builder()
.baseUrl("https://lawnchair.app/")
.addConverterFactory(kotlinxJson.asConverterFactory("application/json".toMediaType()))
.build()
.create()
}
}
20 changes: 11 additions & 9 deletions app/src/main/kotlin/app/lawnchair/lawnicons/model/IconInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ data class IconInfo(
* @return A new list of [IconInfo] objects with merged component names for icons
* sharing the same drawable name.
*/
fun List<IconInfo>.mergeByDrawableName(): List<IconInfo> = groupBy { it.drawableName }
.map { (drawableName, icons) ->
val mergedComponentNames = icons.flatMap { it.componentNames }
IconInfo(
componentNames = mergedComponentNames,
drawableName = drawableName,
id = icons.first().id,
)
}
fun List<IconInfo>.mergeByDrawableName(): List<IconInfo> {
return groupBy { it.drawableName }
.map { (drawableName, icons) ->
val mergedComponentNames = icons.flatMap { it.componentNames }
IconInfo(
componentNames = mergedComponentNames,
drawableName = drawableName,
id = icons.first().id,
)
}
}

/**
* Splits [IconInfo] objects with multiple component names into a list where each
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ abstract class BasePreferenceManager(
fun toggle() = set(!get())

@Composable
fun asState(): State<Boolean> = produceState(initialValue = get(), this) {
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, changedKey ->
if (changedKey == key) {
value = get() // Update the state value when the preference changes
fun asState(): State<Boolean> {
return produceState(initialValue = get(), this) {
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, changedKey ->
if (changedKey == key) {
value = get() // Update the state value when the preference changes
}
}
prefs.registerOnSharedPreferenceChangeListener(listener)
awaitDispose {
prefs.unregisterOnSharedPreferenceChangeListener(listener)
}
}
prefs.registerOnSharedPreferenceChangeListener(listener)
awaitDispose {
prefs.unregisterOnSharedPreferenceChangeListener(listener)
}
}
}
Expand All @@ -61,15 +63,17 @@ abstract class BasePreferenceManager(
fun set(value: Int) = editor.putInt(key, value).apply()

@Composable
fun asState(): State<Int> = produceState(initialValue = get(), this) {
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, changedKey ->
if (changedKey == key) {
value = get() // Update the state value when the preference changes
fun asState(): State<Int> {
return produceState(initialValue = get(), this) {
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, changedKey ->
if (changedKey == key) {
value = get() // Update the state value when the preference changes
}
}
prefs.registerOnSharedPreferenceChangeListener(listener)
awaitDispose {
prefs.unregisterOnSharedPreferenceChangeListener(listener)
}
}
prefs.registerOnSharedPreferenceChangeListener(listener)
awaitDispose {
prefs.unregisterOnSharedPreferenceChangeListener(listener)
}
}
}
Expand Down Expand Up @@ -97,16 +101,20 @@ class PreferenceManager private constructor(
/**
* Returns a singleton instance of [PreferenceManager]
*/
fun getInstance(context: Context): PreferenceManager = instance ?: synchronized(this) {
instance ?: PreferenceManager(
context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE),
).also { instance = it }
fun getInstance(context: Context): PreferenceManager {
return instance ?: synchronized(this) {
instance ?: PreferenceManager(
context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE),
).also { instance = it }
}
}

/**
* Get dummy instance of [PreferenceManager] for testing and Compose previews
*/
fun getDummyInstance(): PreferenceManager = PreferenceManager(DummySharedPreferences())
fun getDummyInstance(): PreferenceManager {
return PreferenceManager(DummySharedPreferences())
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,32 @@ import android.content.pm.ResolveInfo
import app.lawnchair.lawnicons.model.IconInfo
import app.lawnchair.lawnicons.model.LabelAndComponent

fun Context.getPackagesList(): List<ResolveInfo> = try {
packageManager.queryIntentActivities(
Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER),
PackageManager.GET_RESOLVED_FILTER,
)
} catch (e: Exception) {
listOf()
fun Context.getPackagesList(): List<ResolveInfo> {
return try {
packageManager.queryIntentActivities(
Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER),
PackageManager.GET_RESOLVED_FILTER,
)
} catch (e: Exception) {
listOf()
}
}

fun Context.getSystemIconInfoAppfilter(): List<IconInfo> = getPackagesList().map { ri ->
with(ri) {
val riPkg = activityInfo.packageName
val component = "$riPkg/${activityInfo.name}"
fun Context.getSystemIconInfoAppfilter(): List<IconInfo> {
return getPackagesList().map { ri ->
with(ri) {
val riPkg = activityInfo.packageName
val component = "$riPkg/${activityInfo.name}"

val name = loadLabel(packageManager) ?: riPkg
val name = loadLabel(packageManager) ?: riPkg

IconInfo(
drawableName = "",
componentNames = listOf(
LabelAndComponent(name.toString(), component),
),
id = 0,
)
IconInfo(
drawableName = "",
componentNames = listOf(
LabelAndComponent(name.toString(), component),
),
id = 0,
)
}
}
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
id("com.google.devtools.ksp") version "2.1.0-1.0.29" apply false
id("com.google.dagger.hilt.android") version "2.54" apply false
id("app.cash.licensee") version "1.12.0" apply false
id("com.diffplug.spotless") version "7.0.0" apply false
id("com.diffplug.spotless") version "7.0.1" apply false
id("org.gradle.android.cache-fix") version "3.0.1" apply false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,61 @@ object AppfilterDiffCreator {

private fun getPreviousReleaseLines(
appFilterFile: String,
): List<String> = try {
runGitCommand(listOf("fetch", "--tags"))

val tagCommand =
listOf("/usr/bin/bash", "-c", "git tag --sort=-creatordate | head -n 1")
val tagProcess = ProcessBuilder(tagCommand)
.redirectErrorStream(true)
.start()

val latestTag = tagProcess.inputStream.bufferedReader().readLine()
if (tagProcess.waitFor() != 0) {
throw RuntimeException("Failed to get latest tag")
}
): List<String> {
return try {
runGitCommand(listOf("fetch", "--tags"))

val tagCommand =
listOf("/usr/bin/bash", "-c", "git tag --sort=-creatordate | head -n 1")
val tagProcess = ProcessBuilder(tagCommand)
.redirectErrorStream(true)
.start()

val latestTag = tagProcess.inputStream.bufferedReader().readLine()
if (tagProcess.waitFor() != 0) {
throw RuntimeException("Failed to get latest tag")
}

runGitCommand(listOf("show", "$latestTag:$appFilterFile"))
} catch (e: Exception) {
println(e)
listOf()
runGitCommand(listOf("show", "$latestTag:$appFilterFile"))
} catch (e: Exception) {
println(e)
listOf()
}
}

private fun runGitCommand(
args: List<String>,
): List<String> = try {
val command = listOf("git") + args
): List<String> {
return try {
val command = listOf("git") + args

val process = ProcessBuilder(command)
.redirectErrorStream(true)
.start()
val process = ProcessBuilder(command)
.redirectErrorStream(true)
.start()

val result = process.inputStream.bufferedReader().readLines()
if (process.waitFor() != 0) {
throw RuntimeException("Failed to execute $command: $result")
}
println("task git $args completed")
val result = process.inputStream.bufferedReader().readLines()
if (process.waitFor() != 0) {
throw RuntimeException("Failed to execute $command: $result")
}
println("task git $args completed")

result
} catch (e: Exception) {
println(e)
listOf()
result
} catch (e: Exception) {
println(e)
listOf()
}
}

private fun readFileContents(filePath: String): List<String> = File(filePath).readLines()
private fun readFileContents(filePath: String): List<String> {
return File(filePath).readLines()
}

private fun getLineDiff(
mainLines: List<String>,
developLines: List<String>,
): List<String> = developLines.filterNot { it in mainLines }
): List<String> {
return developLines.filterNot { it in mainLines }
}

private fun writeDiffToFile(
diff: List<String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ import org.dom4j.io.XMLWriter
object XmlUtil {
private val UTF_8 = Charsets.UTF_8.name()

fun getElements(document: Document, path: String): List<Element> = document.rootElement.elements(path)
fun getElements(document: Document, path: String): List<Element> {
return document.rootElement.elements(path)
}

fun getDocument(xmlPath: String): Document = SAXReader().apply { encoding = UTF_8 }.read(xmlPath)
fun getDocument(xmlPath: String): Document {
return SAXReader().apply { encoding = UTF_8 }.read(xmlPath)
}

fun getFileWithExtension(target: Path, extension: String = "xml"): String {
val svgFilePath = target.toFile().absolutePath
Expand Down

0 comments on commit e1bc78f

Please sign in to comment.