From 61b2b5e43606666931a429d7dc11d59f6f57477d Mon Sep 17 00:00:00 2001 From: lihenggui Date: Fri, 8 Mar 2024 15:04:20 -0800 Subject: [PATCH] Make notification module as the multiplatform library --- core/notifications/build.gradle.kts | 21 +++++++------ .../{main => androidMain}/AndroidManifest.xml | 0 .../core/notifications/SystemTrayNotifier.kt | 11 +++---- .../res/values/strings.xml | 0 .../core/notifications/NoOpNotifier.kt | 3 +- .../core/notifications/Notifier.kt | 0 .../notifications/di}/NotificationsModule.kt | 20 +++++------- .../core/notifications/NotificationsModule.kt | 31 ------------------- 8 files changed, 25 insertions(+), 61 deletions(-) rename core/notifications/src/{main => androidMain}/AndroidManifest.xml (100%) rename core/notifications/src/{main => androidMain}/kotlin/com/google/samples/apps/nowinandroid/core/notifications/SystemTrayNotifier.kt (95%) rename core/notifications/src/{main => androidMain}/res/values/strings.xml (100%) rename core/notifications/src/{main => commonMain}/kotlin/com/google/samples/apps/nowinandroid/core/notifications/NoOpNotifier.kt (91%) rename core/notifications/src/{main => commonMain}/kotlin/com/google/samples/apps/nowinandroid/core/notifications/Notifier.kt (100%) rename core/notifications/src/{demo/kotlin/com/google/samples/apps/nowinandroid/core/notifications => commonMain/kotlin/com/google/samples/apps/nowinandroid/core/notifications/di}/NotificationsModule.kt (61%) delete mode 100644 core/notifications/src/prod/kotlin/com/google/samples/apps/nowinandroid/core/notifications/NotificationsModule.kt diff --git a/core/notifications/build.gradle.kts b/core/notifications/build.gradle.kts index 92871b72b..40a616470 100644 --- a/core/notifications/build.gradle.kts +++ b/core/notifications/build.gradle.kts @@ -14,19 +14,22 @@ * limitations under the License. */ plugins { - alias(libs.plugins.nowinandroid.android.library) - alias(libs.plugins.nowinandroid.android.hilt) + alias(libs.plugins.nowinandroid.kmp.library) + alias(libs.plugins.nowinandroid.kotlin.inject) } android { namespace = "com.google.samples.apps.nowinandroid.core.notifications" } -dependencies { - api(projects.core.model) - - implementation(projects.core.common) - - compileOnly(platform(libs.androidx.compose.bom)) - compileOnly(libs.androidx.compose.runtime) +kotlin { + sourceSets { + commonMain.dependencies { + api(projects.core.model) + implementation(projects.core.common) + } + androidMain.dependencies { + implementation(libs.androidx.core.ktx) + } + } } diff --git a/core/notifications/src/main/AndroidManifest.xml b/core/notifications/src/androidMain/AndroidManifest.xml similarity index 100% rename from core/notifications/src/main/AndroidManifest.xml rename to core/notifications/src/androidMain/AndroidManifest.xml diff --git a/core/notifications/src/main/kotlin/com/google/samples/apps/nowinandroid/core/notifications/SystemTrayNotifier.kt b/core/notifications/src/androidMain/kotlin/com/google/samples/apps/nowinandroid/core/notifications/SystemTrayNotifier.kt similarity index 95% rename from core/notifications/src/main/kotlin/com/google/samples/apps/nowinandroid/core/notifications/SystemTrayNotifier.kt rename to core/notifications/src/androidMain/kotlin/com/google/samples/apps/nowinandroid/core/notifications/SystemTrayNotifier.kt index 1c9e7ab63..735fcc122 100644 --- a/core/notifications/src/main/kotlin/com/google/samples/apps/nowinandroid/core/notifications/SystemTrayNotifier.kt +++ b/core/notifications/src/androidMain/kotlin/com/google/samples/apps/nowinandroid/core/notifications/SystemTrayNotifier.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023 The Android Open Source Project + * 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. @@ -33,9 +33,6 @@ import androidx.core.app.NotificationCompat.InboxStyle import androidx.core.app.NotificationManagerCompat import androidx.core.net.toUri import com.google.samples.apps.nowinandroid.core.model.data.NewsResource -import dagger.hilt.android.qualifiers.ApplicationContext -import javax.inject.Inject -import javax.inject.Singleton private const val MAX_NUM_NOTIFICATIONS = 5 private const val TARGET_ACTIVITY_NAME = "com.google.samples.apps.nowinandroid.MainActivity" @@ -49,9 +46,9 @@ private const val FOR_YOU_PATH = "foryou" /** * Implementation of [Notifier] that displays notifications in the system tray. */ -@Singleton -internal class SystemTrayNotifier @Inject constructor( - @ApplicationContext private val context: Context, + +internal class SystemTrayNotifier constructor( + private val context: Context, ) : Notifier { override fun postNewsNotifications( diff --git a/core/notifications/src/main/res/values/strings.xml b/core/notifications/src/androidMain/res/values/strings.xml similarity index 100% rename from core/notifications/src/main/res/values/strings.xml rename to core/notifications/src/androidMain/res/values/strings.xml diff --git a/core/notifications/src/main/kotlin/com/google/samples/apps/nowinandroid/core/notifications/NoOpNotifier.kt b/core/notifications/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/notifications/NoOpNotifier.kt similarity index 91% rename from core/notifications/src/main/kotlin/com/google/samples/apps/nowinandroid/core/notifications/NoOpNotifier.kt rename to core/notifications/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/notifications/NoOpNotifier.kt index 863c1a662..93a0029df 100644 --- a/core/notifications/src/main/kotlin/com/google/samples/apps/nowinandroid/core/notifications/NoOpNotifier.kt +++ b/core/notifications/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/notifications/NoOpNotifier.kt @@ -17,11 +17,10 @@ package com.google.samples.apps.nowinandroid.core.notifications import com.google.samples.apps.nowinandroid.core.model.data.NewsResource -import javax.inject.Inject /** * Implementation of [Notifier] which does nothing. Useful for tests and previews. */ -internal class NoOpNotifier @Inject constructor() : Notifier { +internal class NoOpNotifier : Notifier { override fun postNewsNotifications(newsResources: List) = Unit } diff --git a/core/notifications/src/main/kotlin/com/google/samples/apps/nowinandroid/core/notifications/Notifier.kt b/core/notifications/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/notifications/Notifier.kt similarity index 100% rename from core/notifications/src/main/kotlin/com/google/samples/apps/nowinandroid/core/notifications/Notifier.kt rename to core/notifications/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/notifications/Notifier.kt diff --git a/core/notifications/src/demo/kotlin/com/google/samples/apps/nowinandroid/core/notifications/NotificationsModule.kt b/core/notifications/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/notifications/di/NotificationsModule.kt similarity index 61% rename from core/notifications/src/demo/kotlin/com/google/samples/apps/nowinandroid/core/notifications/NotificationsModule.kt rename to core/notifications/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/notifications/di/NotificationsModule.kt index 99ba10fa7..1cf85ab09 100644 --- a/core/notifications/src/demo/kotlin/com/google/samples/apps/nowinandroid/core/notifications/NotificationsModule.kt +++ b/core/notifications/src/commonMain/kotlin/com/google/samples/apps/nowinandroid/core/notifications/di/NotificationsModule.kt @@ -1,5 +1,5 @@ /* - * Copyright 2023 The Android Open Source Project + * 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. @@ -14,18 +14,14 @@ * limitations under the License. */ -package com.google.samples.apps.nowinandroid.core.notifications +package com.google.samples.apps.nowinandroid.core.notifications.di -import dagger.Binds -import dagger.Module -import dagger.hilt.InstallIn -import dagger.hilt.components.SingletonComponent +import com.google.samples.apps.nowinandroid.core.notifications.NoOpNotifier +import com.google.samples.apps.nowinandroid.core.notifications.Notifier +import me.tatarka.inject.annotations.Provides -@Module -@InstallIn(SingletonComponent::class) internal abstract class NotificationsModule { - @Binds - abstract fun bindNotifier( - notifier: NoOpNotifier, - ): Notifier + + @Provides + internal fun provideNotifier(): Notifier = NoOpNotifier() } diff --git a/core/notifications/src/prod/kotlin/com/google/samples/apps/nowinandroid/core/notifications/NotificationsModule.kt b/core/notifications/src/prod/kotlin/com/google/samples/apps/nowinandroid/core/notifications/NotificationsModule.kt deleted file mode 100644 index c2e1f76ca..000000000 --- a/core/notifications/src/prod/kotlin/com/google/samples/apps/nowinandroid/core/notifications/NotificationsModule.kt +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 2023 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.notifications - -import dagger.Binds -import dagger.Module -import dagger.hilt.InstallIn -import dagger.hilt.components.SingletonComponent - -@Module -@InstallIn(SingletonComponent::class) -internal abstract class NotificationsModule { - @Binds - abstract fun bindNotifier( - notifier: SystemTrayNotifier, - ): Notifier -}