Skip to content

Commit

Permalink
Add ktlint and apply proper formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Max K committed Feb 12, 2024
1 parent 8407f2f commit bd28316
Show file tree
Hide file tree
Showing 42 changed files with 353 additions and 321 deletions.
26 changes: 26 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# https://editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
max_line_length = 140

end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{kt,kts}]
ktlint_code_style = intellij_idea
ktlint_standard_no-wildcard-imports = disabled
ktlint_function_naming_ignore_when_annotated_with=Composable
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true

[*.md]
trim_trailing_whitespace = false
max_line_length = unset

[*.yml]
indent_size = 2
7 changes: 6 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ permissions:
pull-requests: write

jobs:
ktlint:
uses: ./ktlint.yml
build:
name: Build
runs-on: ubuntu-latest
needs:
- ktlint
steps:
- name: Checkout
uses: actions/[email protected]
Expand Down Expand Up @@ -47,6 +51,7 @@ jobs:
runs-on: ubuntu-latest
permissions:
contents: write
if: github.ref == 'refs/heads/main'
steps:
- name: Checkout
uses: actions/[email protected]
Expand All @@ -56,4 +61,4 @@ jobs:
java-version: '17'
distribution: 'temurin'
- name: Generate and submit dependency graph
uses: gradle/actions/dependency-submission@v3
uses: gradle/actions/dependency-submission@v3
27 changes: 27 additions & 0 deletions .github/workflows/ktlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Ktlint

on:
workflow_call:

jobs:
Ktlint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/[email protected]
- name: Setup JDK
uses: actions/[email protected]
with:
java-version: '17'
distribution: 'temurin'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
with:
add-job-summary-as-pr-comment: never
build-scan-publish: false
- name: Run ktlint
run: ./gradlew ktlintCheck
- name: Upload report
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: ./app/build/reports/ktlint
11 changes: 11 additions & 0 deletions .idea/codeStyles/Project.xml

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

5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

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

11 changes: 10 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import org.jlleitschuh.gradle.ktlint.reporter.ReporterType

plugins {
alias(libs.plugins.com.android.application)
alias(libs.plugins.org.jetbrains.kotlin.android)
alias(libs.plugins.com.google.dagger.hilt.android)
alias(libs.plugins.com.google.devtools.ksp)
alias(libs.plugins.androidx.room)
alias(libs.plugins.ktlint)
}

android {
Expand Down Expand Up @@ -64,6 +67,12 @@ kotlin {
jvmToolchain(11)
}

ktlint {
reporters {
reporter(ReporterType.SARIF)
}
}

dependencies {
// Compose BOM specifics
val composeBom = platform(libs.androidx.compose.bom)
Expand All @@ -78,4 +87,4 @@ dependencies {
ksp(libs.bundles.appKsp)
testImplementation(libs.bundles.appUnitTest)
androidTestImplementation(libs.bundles.appAndroidTest)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ class ExampleInstrumentedTest {
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("de.langerhans.odintools", appContext.packageName)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ import android.app.Application
import dagger.hilt.android.HiltAndroidApp

@HiltAndroidApp
class OdinToolsApplication : Application()
class OdinToolsApplication : Application()
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ import de.langerhans.odintools.ui.composables.DialogButton
import de.langerhans.odintools.ui.composables.OdinTopAppBar

@Composable
fun AppOverrideListScreen(
viewModel: AppOverrideListViewModel = hiltViewModel(),
navigateToOverrides: (packageName: String) -> Unit
) {
fun AppOverrideListScreen(viewModel: AppOverrideListViewModel = hiltViewModel(), navigateToOverrides: (packageName: String) -> Unit) {
val uiState: AppOverrideListUiModel by viewModel.uiState.collectAsState()
Scaffold(topBar = { OdinTopAppBar(deviceVersion = uiState.deviceVersion) }) { contentPadding ->

Expand All @@ -37,32 +34,35 @@ fun AppOverrideListScreen(
{
viewModel.dismissAppSelectDialog()
navigateToOverrides(it)
}, {
},
{
viewModel.dismissAppSelectDialog()
}
},
)
}

LazyColumn(
modifier = Modifier
.fillMaxWidth()
.padding(contentPadding)
.padding(contentPadding),
) {
item {
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier
.fillMaxWidth()
.clickable { viewModel.addClicked() }
.padding(vertical = 12.dp, horizontal = 16.dp)
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.fillMaxWidth()
.clickable { viewModel.addClicked() }
.padding(vertical = 12.dp, horizontal = 16.dp),
) {
Image(
painter = painterResource(id = R.drawable.ic_add),
contentDescription = null,
colorFilter = ColorFilter.tint(LocalContentColor.current),
modifier = Modifier.size(48.dp)
modifier = Modifier.size(48.dp),
)
Text(
text = stringResource(id = R.string.addOverride),
modifier = Modifier.padding(start = 16.dp)
modifier = Modifier.padding(start = 16.dp),
)
}
}
Expand All @@ -72,7 +72,7 @@ fun AppOverrideListScreen(
it.appName,
it.appIcon,
48.dp,
it.subtitle
it.subtitle,
) { packageName ->
navigateToOverrides(packageName)
}
Expand All @@ -82,38 +82,29 @@ fun AppOverrideListScreen(
}

@Composable
fun AppItem(
packageName: String,
label: String,
icon: Drawable,
iconSize: Dp = 48.dp,
subLabel: String? = "",
onClick: (String) -> Unit
) {
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier
.fillMaxWidth()
.clickable { onClick(packageName) }
.padding(vertical = 12.dp, horizontal = 16.dp)
fun AppItem(packageName: String, label: String, icon: Drawable, iconSize: Dp = 48.dp, subLabel: String? = "", onClick: (String) -> Unit) {
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.fillMaxWidth()
.clickable { onClick(packageName) }
.padding(vertical = 12.dp, horizontal = 16.dp),
) {
Image(painter = rememberDrawablePainter(drawable = icon), contentDescription = null, Modifier.size(iconSize))
Column(modifier = Modifier.padding(start = 16.dp)) {
Text(text = label, modifier = Modifier.padding(bottom = 4.dp))
if (subLabel?.isNotEmpty() == true) {
Text(
text = subLabel,
style = MaterialTheme.typography.bodySmall
style = MaterialTheme.typography.bodySmall,
)
}
}
}
}

@Composable
fun AppPickerDialog(
apps: List<AppUiModel>,
onAppSelected: (String) -> Unit,
onDismiss: () -> Unit
) {
fun AppPickerDialog(apps: List<AppUiModel>, onAppSelected: (String) -> Unit, onDismiss: () -> Unit) {
AlertDialog(onDismissRequest = {}, confirmButton = { }, dismissButton = {
DialogButton(text = stringResource(id = R.string.cancel), onDismiss)
}, title = {}, text = {
Expand All @@ -122,7 +113,7 @@ fun AppPickerDialog(
return@AlertDialog
}
LazyColumn(
modifier = Modifier.fillMaxWidth()
modifier = Modifier.fillMaxWidth(),
) {
items(items = apps, itemContent = {
AppItem(
Expand All @@ -131,7 +122,7 @@ fun AppPickerDialog(
it.appIcon,
36.dp,
"",
onAppSelected
onAppSelected,
)
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package de.langerhans.odintools.appsettings

import android.graphics.drawable.Drawable
import de.langerhans.odintools.models.ControllerStyle
import de.langerhans.odintools.models.L2R2Style
import de.langerhans.odintools.models.FanMode
import de.langerhans.odintools.models.L2R2Style
import de.langerhans.odintools.models.PerfMode

data class AppOverrideListUiModel(
val deviceVersion: String = "",
val showAppSelectDialog: Boolean = false,
val overrideList: List<AppUiModel> = emptyList(),
val overrideCandidates: List<AppUiModel> = emptyList()
val overrideCandidates: List<AppUiModel> = emptyList(),
)

data class AppOverridesUiModel(
Expand All @@ -21,7 +21,7 @@ data class AppOverridesUiModel(
val showDeleteConfirmDialog: Boolean = false,
val navigateBack: Boolean = false,
val isNewApp: Boolean = false,
val disabledFanModeKeys: List<String> = emptyList()
val disabledFanModeKeys: List<String> = emptyList(),
)

data class AppUiModel(
Expand All @@ -32,5 +32,5 @@ data class AppUiModel(
val controllerStyle: ControllerStyle? = null,
val l2r2Style: L2R2Style? = null,
val fanMode: FanMode? = null,
val perfMode: PerfMode? = null
)
val perfMode: PerfMode? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import javax.inject.Inject
class AppOverrideListViewModel @Inject constructor(
private val appOverrideDao: AppOverrideDao,
private val appOverrideMapper: AppOverrideMapper,
private val deviceUtils: DeviceUtils
private val deviceUtils: DeviceUtils,
) : ViewModel() {

private val _uiState = MutableStateFlow(AppOverrideListUiModel())
Expand All @@ -32,7 +32,7 @@ class AppOverrideListViewModel @Inject constructor(
it.copy(
overrideList = appOverrideMapper.mapAppOverrides(overrides),
overrideCandidates = appOverrideMapper.mapOverrideCandidates(overrides),
deviceVersion = deviceUtils.getDeviceVersion()
deviceVersion = deviceUtils.getDeviceVersion(),
)
}
}
Expand All @@ -56,4 +56,4 @@ class AppOverrideListViewModel @Inject constructor(
* performance_mode: standard 0, performance 1, high performance 2
* fan_mode: disabled 0, quiet 1, (balance 2), (performance 3), smart 4, sport 5, custom 6
*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import de.langerhans.odintools.R
import de.langerhans.odintools.data.AppOverrideEntity
import de.langerhans.odintools.models.ControllerStyle
import de.langerhans.odintools.models.L2R2Style
import de.langerhans.odintools.models.FanMode
import de.langerhans.odintools.models.L2R2Style
import de.langerhans.odintools.models.PerfMode
import javax.inject.Inject

class AppOverrideMapper @Inject constructor(
@ApplicationContext private val context: Context
@ApplicationContext private val context: Context,
) {

fun mapOverrideCandidates(
existingOverrides: List<AppOverrideEntity>
): List<AppUiModel> {
fun mapOverrideCandidates(existingOverrides: List<AppOverrideEntity>): List<AppUiModel> {
return context.packageManager.getInstalledApplications(PackageManager.GET_META_DATA).filter {
it.flags and ApplicationInfo.FLAG_SYSTEM == 0 && it.enabled
}.filterNot { appInfo ->
Expand Down Expand Up @@ -55,13 +53,11 @@ class AppOverrideMapper @Inject constructor(
controllerStyle = controllerStyle,
l2r2Style = l2R2Style,
perfMode = perfMode,
fanMode = fanMode
fanMode = fanMode,
)
}

fun mapEmptyOverride(
packageName: String
): AppUiModel {
fun mapEmptyOverride(packageName: String): AppUiModel {
// If this crashes then something is fishy...
val appInfo = context.packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)

Expand Down Expand Up @@ -100,4 +96,4 @@ class AppOverrideMapper @Inject constructor(
}
}.trimEnd(' ', '|').ifEmpty { null }
}
}
}
Loading

0 comments on commit bd28316

Please sign in to comment.