Fix compilation errors in the ui module

pull/2064/head
lihenggui 2 years ago
parent 05ae38a3cb
commit b275cdcb79

@ -44,6 +44,8 @@ import com.google.samples.apps.nowinandroid.core.data.repository.UserNewsResourc
import com.google.samples.apps.nowinandroid.core.data.util.NetworkMonitor
import com.google.samples.apps.nowinandroid.core.data.util.TimeZoneMonitor
import com.google.samples.apps.nowinandroid.core.designsystem.theme.NiaTheme
import com.google.samples.apps.nowinandroid.core.di.ApplicationComponent
import com.google.samples.apps.nowinandroid.core.di.ApplicationComponentProvider
import com.google.samples.apps.nowinandroid.core.model.data.DarkThemeConfig
import com.google.samples.apps.nowinandroid.core.model.data.ThemeBrand
import com.google.samples.apps.nowinandroid.core.ui.LocalTimeZone
@ -58,7 +60,7 @@ import javax.inject.Inject
private const val TAG = "MainActivity"
@AndroidEntryPoint
class MainActivity : ComponentActivity() {
class MainActivity : ComponentActivity(), ApplicationComponentProvider {
/**
* Lazily inject [JankStats], which is used to track jank throughout the app.
@ -80,6 +82,10 @@ class MainActivity : ComponentActivity() {
val viewModel: MainActivityViewModel by viewModels()
override val component by lazy(LazyThreadSafetyMode.NONE) {
ApplicationComponent::class.create(applicationContext)
}
override fun onCreate(savedInstanceState: Bundle?) {
val splashScreen = installSplashScreen()
super.onCreate(savedInstanceState)

@ -27,6 +27,11 @@ kotlin {
sourceSets {
commonMain.dependencies {
api(libs.logging)
api(libs.coil)
api(libs.coil.core)
api(libs.coil.svg)
api(libs.coil.network.ktor)
implementation(libs.kotlin.inject.runtime)
implementation(libs.kotlinx.coroutines.core)
}
}

@ -14,19 +14,14 @@
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.network
package com.google.samples.apps.nowinandroid.core.di
import android.content.Context
import android.app.Application
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
@Component
abstract class ApplicationComponent(
@get:Provides val context: Context,
) : PlatformComponent
interface ApplicationComponentProvider {
val component: ApplicationComponent
}
val Context.applicationComponent get() = (applicationContext as ApplicationComponentProvider).component
@ApplicationScope
abstract class AndroidApplicationComponent(
@get:Provides val application: Application,
)

@ -0,0 +1,26 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.di
import android.content.Context
import coil3.PlatformContext
import me.tatarka.inject.annotations.Inject
@Inject
class AndroidPlatformContextProvider(private val context: Context) : PlatformContextProvider {
override val platformContext: PlatformContext get() = context
}

@ -14,13 +14,13 @@
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.network
package com.google.samples.apps.nowinandroid.core.di
import android.content.Context
import android.app.Application
import coil3.PlatformContext
import me.tatarka.inject.annotations.Provides
interface PlatformComponent {
@Provides
fun providePlatformContext(context: Context): PlatformContext = context
fun providePlatformContext(application: Application): PlatformContext = application
}

@ -20,13 +20,18 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Provides
import me.tatarka.inject.annotations.Scope
typealias ApplicationScope = CoroutineScope
/**
* The application-level scope. There will only be one instance of anything annotated with this.
*/
@Scope
annotation class ApplicationScope
@Component
abstract class CoroutineScopeComponent {
@Provides
fun providesCoroutineScope(
dispatcher: DefaultDispatcher,
): ApplicationScope = CoroutineScope(SupervisorJob() + dispatcher)
): CoroutineScope = CoroutineScope(SupervisorJob() + dispatcher)
}

@ -14,7 +14,7 @@
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.network.di
package com.google.samples.apps.nowinandroid.core.di
import coil3.ImageLoader
import coil3.PlatformContext
@ -36,7 +36,6 @@ abstract class ImageLoaderComponent {
@Provides
fun provideImageLoader(
context: PlatformContext,
debug: Boolean,
): ImageLoader {
return ImageLoader.Builder(context)
.memoryCache {
@ -49,9 +48,7 @@ abstract class ImageLoaderComponent {
.crossfade(true)
// Enable logging if this is a debug build.
.apply {
if (debug) {
logger(DebugLogger())
}
logger(DebugLogger())
}
.build()
}

@ -0,0 +1,23 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.di
import coil3.PlatformContext
interface PlatformContextProvider {
val platformContext: PlatformContext
}

@ -0,0 +1,24 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.di
import coil3.PlatformContext
class JvmPlatformContextProvider : PlatformContextProvider {
override val platformContext: PlatformContext
get() = PlatformContext.INSTANCE
}

@ -0,0 +1,24 @@
/*
* Copyright 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.samples.apps.nowinandroid.core.di
import coil3.PlatformContext
class NativePlatformContextProvider : PlatformContextProvider {
override val platformContext: PlatformContext
get() = PlatformContext.INSTANCE
}

@ -1,3 +0,0 @@
# :core:datastore-test module
## Dependency graph
![Dependency graph](../../docs/images/graphs/dep_graph_core_datastore_test.svg)

@ -53,10 +53,6 @@ kotlin {
api(libs.kotlinx.datetime)
api(projects.core.common)
api(projects.core.model)
implementation(libs.coil)
implementation(libs.coil.core)
implementation(libs.coil.svg)
implementation(libs.coil.network.ktor)
implementation(libs.kotlinx.serialization.json)
implementation(libs.ktor.client.core)
implementation(libs.ktor.client.json)

@ -54,7 +54,9 @@ import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.onClick
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import coil3.ImageLoader
import coil3.compose.AsyncImagePainter
import coil3.compose.LocalPlatformContext
import coil3.compose.rememberAsyncImagePainter
import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaIconToggleButton
import com.google.samples.apps.nowinandroid.core.designsystem.component.NiaTopicTag
@ -104,7 +106,9 @@ fun NewsResourceCardExpanded(
Column {
if (!userNewsResource.headerImageUrl.isNullOrEmpty()) {
Row {
NewsResourceHeaderImage(userNewsResource.headerImageUrl)
NewsResourceHeaderImage(
userNewsResource.headerImageUrl,
)
}
}
Box(
@ -156,7 +160,7 @@ fun NewsResourceHeaderImage(
isLoading = state is AsyncImagePainter.State.Loading
isError = state is AsyncImagePainter.State.Error
},
imageLoader = imageLoader,
imageLoader = ImageLoader(LocalPlatformContext.current),
)
val isLocalInspection = LocalInspectionMode.current
Box(
@ -182,7 +186,7 @@ fun NewsResourceHeaderImage(
contentScale = ContentScale.Crop,
painter = imagePainter,
// if (isError.not() && !isLocalInspection) {
// imagePainter,
// imagePainter,
// } else {
// painterResource(drawable.core_designsystem_ic_placeholder_default)
// },

@ -21,6 +21,7 @@ import androidx.compose.foundation.lazy.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.toArgb
import coil3.compose.LocalPlatformContext
import com.google.samples.apps.nowinandroid.core.analytics.LocalAnalyticsHelper
import com.google.samples.apps.nowinandroid.core.model.data.UserNewsResource
@ -41,9 +42,9 @@ fun LazyListScope.userNewsResourceCardItems(
items = items,
key = { it.id },
itemContent = { userNewsResource ->
val resourceUrl = Uri.parse(userNewsResource.url)
val resourceUrl = userNewsResource.url
val backgroundColor = MaterialTheme.colorScheme.background.toArgb()
val context = LocalContext.current
val context = LocalPlatformContext.current
val analyticsHelper = LocalAnalyticsHelper.current
NewsResourceCardExpanded(
@ -55,7 +56,7 @@ fun LazyListScope.userNewsResourceCardItems(
analyticsHelper.logNewsResourceOpened(
newsResourceId = userNewsResource.id,
)
launchCustomChromeTab(context, resourceUrl, backgroundColor)
// launchCustomChromeTab(context, resourceUrl, backgroundColor)
onNewsResourceViewed(userNewsResource.id)
},
onTopicClick = onTopicClick,

Loading…
Cancel
Save