Skip to content

Commit 36cacbd

Browse files
authored
Merge pull request #1039 from SimonMarquis/kotlinify
Kotlinify codebase
2 parents 4238aa4 + 707117a commit 36cacbd

File tree

46 files changed

+161
-285
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+161
-285
lines changed

app/src/androidTest/kotlin/com/google/samples/apps/nowinandroid/ui/NavigationTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class NavigationTest {
9090
lateinit var topicsRepository: TopicsRepository
9191

9292
private fun AndroidComposeTestRule<*, *>.stringResource(@StringRes resId: Int) =
93-
ReadOnlyProperty<Any?, String> { _, _ -> activity.getString(resId) }
93+
ReadOnlyProperty<Any, String> { _, _ -> activity.getString(resId) }
9494

9595
// The strings used for matching in these tests
9696
private val navigateUp by composeTestRule.stringResource(FeatureForyouR.string.feature_foryou_navigate_up)

app/src/main/kotlin/com/google/samples/apps/nowinandroid/MainActivity.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,7 @@ class MainActivity : ComponentActivity() {
9090
lifecycleScope.launch {
9191
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
9292
viewModel.uiState
93-
.onEach {
94-
uiState = it
95-
}
93+
.onEach { uiState = it }
9694
.collect()
9795
}
9896
}

app/src/main/kotlin/com/google/samples/apps/nowinandroid/di/JankStatsModule.kt

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.app.Activity
2020
import android.util.Log
2121
import android.view.Window
2222
import androidx.metrics.performance.JankStats
23+
import androidx.metrics.performance.JankStats.OnFrameListener
2324
import dagger.Module
2425
import dagger.Provides
2526
import dagger.hilt.InstallIn
@@ -29,26 +30,20 @@ import dagger.hilt.android.components.ActivityComponent
2930
@InstallIn(ActivityComponent::class)
3031
object JankStatsModule {
3132
@Provides
32-
fun providesOnFrameListener(): JankStats.OnFrameListener {
33-
return JankStats.OnFrameListener { frameData ->
34-
// Make sure to only log janky frames.
35-
if (frameData.isJank) {
36-
// We're currently logging this but would better report it to a backend.
37-
Log.v("NiA Jank", frameData.toString())
38-
}
33+
fun providesOnFrameListener(): OnFrameListener = OnFrameListener { frameData ->
34+
// Make sure to only log janky frames.
35+
if (frameData.isJank) {
36+
// We're currently logging this but would better report it to a backend.
37+
Log.v("NiA Jank", frameData.toString())
3938
}
4039
}
4140

4241
@Provides
43-
fun providesWindow(activity: Activity): Window {
44-
return activity.window
45-
}
42+
fun providesWindow(activity: Activity): Window = activity.window
4643

4744
@Provides
4845
fun providesJankStats(
4946
window: Window,
50-
frameListener: JankStats.OnFrameListener,
51-
): JankStats {
52-
return JankStats.createAndTrack(window, frameListener)
53-
}
47+
frameListener: OnFrameListener,
48+
): JankStats = JankStats.createAndTrack(window, frameListener)
5449
}

app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaApp.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ fun NiaApp(
9797
) {
9898
val shouldShowGradientBackground =
9999
appState.currentTopLevelDestination == TopLevelDestination.FOR_YOU
100-
var showSettingsDialog by rememberSaveable {
101-
mutableStateOf(false)
102-
}
100+
var showSettingsDialog by rememberSaveable { mutableStateOf(false) }
103101

104102
NiaBackground {
105103
NiaGradientBackground(

app/src/main/kotlin/com/google/samples/apps/nowinandroid/ui/NiaAppState.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,7 @@ class NiaAppState(
164164
}
165165
}
166166

167-
fun navigateToSearch() {
168-
navController.navigateToSearch()
169-
}
167+
fun navigateToSearch() = navController.navigateToSearch()
170168
}
171169

172170
/**

benchmarks/src/main/kotlin/androidx/test/uiautomator/UiAutomatorHelpers.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,11 @@ import androidx.test.uiautomator.HasChildrenOp.EXACTLY
2929
fun untilHasChildren(
3030
childCount: Int = 1,
3131
op: HasChildrenOp = AT_LEAST,
32-
): UiObject2Condition<Boolean> {
33-
return object : UiObject2Condition<Boolean>() {
34-
override fun apply(element: UiObject2): Boolean {
35-
return when (op) {
36-
AT_LEAST -> element.childCount >= childCount
37-
EXACTLY -> element.childCount == childCount
38-
AT_MOST -> element.childCount <= childCount
39-
}
40-
}
32+
): UiObject2Condition<Boolean> = object : UiObject2Condition<Boolean>() {
33+
override fun apply(element: UiObject2): Boolean = when (op) {
34+
AT_LEAST -> element.childCount >= childCount
35+
EXACTLY -> element.childCount == childCount
36+
AT_MOST -> element.childCount <= childCount
4137
}
4238
}
4339

benchmarks/src/main/kotlin/com/google/samples/apps/nowinandroid/baselineprofile/StartupBaselineProfile.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.samples.apps.nowinandroid.baselineprofile
1818

19+
import androidx.benchmark.macro.MacrobenchmarkScope
1920
import androidx.benchmark.macro.junit4.BaselineProfileRule
2021
import com.google.samples.apps.nowinandroid.PACKAGE_NAME
2122
import com.google.samples.apps.nowinandroid.startActivityAndAllowNotifications
@@ -30,11 +31,9 @@ class StartupBaselineProfile {
3031
@get:Rule val baselineProfileRule = BaselineProfileRule()
3132

3233
@Test
33-
fun generate() =
34-
baselineProfileRule.collect(
35-
PACKAGE_NAME,
36-
includeInStartupProfile = true,
37-
) {
38-
startActivityAndAllowNotifications()
39-
}
34+
fun generate() = baselineProfileRule.collect(
35+
PACKAGE_NAME,
36+
includeInStartupProfile = true,
37+
profileBlock = MacrobenchmarkScope::startActivityAndAllowNotifications,
38+
)
4039
}

build-logic/convention/src/main/kotlin/com/google/samples/apps/nowinandroid/PrintTestApks.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,12 @@ internal abstract class PrintApkLocationTask : DefaultTask() {
8686
fun taskAction() {
8787
val hasFiles = sources.orNull?.any { directory ->
8888
directory.asFileTree.files.any {
89-
it.isFile && it.parentFile.path.contains("build${File.separator}generated").not()
89+
it.isFile && "build${File.separator}generated" !in it.parentFile.path
9090
}
9191
} ?: throw RuntimeException("Cannot check androidTest sources")
9292

9393
// Don't print APK location if there are no androidTest source files
94-
if (!hasFiles) {
95-
return
96-
}
94+
if (!hasFiles) return
9795

9896
val builtArtifacts = builtArtifactsLoader.get().load(apkFolder.get())
9997
?: throw RuntimeException("Cannot load APKs")

core/common/src/main/kotlin/com/google/samples/apps/nowinandroid/core/result/Result.kt

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,10 @@ import kotlinx.coroutines.flow.onStart
2323

2424
sealed interface Result<out T> {
2525
data class Success<T>(val data: T) : Result<T>
26-
data class Error(val exception: Throwable? = null) : Result<Nothing>
26+
data class Error(val exception: Throwable) : Result<Nothing>
2727
data object Loading : Result<Nothing>
2828
}
2929

30-
fun <T> Flow<T>.asResult(): Flow<Result<T>> {
31-
return this
32-
.map<T, Result<T>> {
33-
Result.Success(it)
34-
}
35-
.onStart { emit(Result.Loading) }
36-
.catch { emit(Result.Error(it)) }
37-
}
30+
fun <T> Flow<T>.asResult(): Flow<Result<T>> = map<T, Result<T>> { Result.Success(it) }
31+
.onStart { emit(Result.Loading) }
32+
.catch { emit(Result.Error(it)) }

core/data/src/main/kotlin/com/google/samples/apps/nowinandroid/core/data/repository/DefaultRecentSearchRepository.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ internal class DefaultRecentSearchRepository @Inject constructor(
3939

4040
override fun getRecentSearchQueries(limit: Int): Flow<List<RecentSearchQuery>> =
4141
recentSearchQueryDao.getRecentSearchQueryEntities(limit).map { searchQueries ->
42-
searchQueries.map {
43-
it.asExternalModel()
44-
}
42+
searchQueries.map { it.asExternalModel() }
4543
}
4644

4745
override suspend fun clearRecentSearches() = recentSearchQueryDao.clearRecentSearchQueries()

0 commit comments

Comments
 (0)