Мое приложение настроено следующим образом:
Код: Выделить всё
@HiltAndroidApp
class NotakuApplication : Application(), Configuration.Provider {
@Inject
lateinit var workerFactory: HiltWorkerFactory
override fun onCreate() {
super.onCreate()
val workRequest = OneTimeWorkRequestBuilder().build()
WorkManager.getInstance(this).enqueue(workRequest)
}
override fun getWorkManagerConfiguration(): Configuration {
return Configuration.Builder()
.setWorkerFactory(workerFactory)
.build()
}
}
Код: Выделить всё
@HiltWorker
class MyWorker @AssistedInject constructor(
@Assisted context: Context,
@Assisted workerParams: WorkerParameters,
private val getTrackedAnimesUseCase: GetTrackedAnimesUseCase
) : CoroutineWorker(context, workerParams) {
override suspend fun doWork(): Result {
return try {
val trackedAnimes = getTrackedAnimesUseCase.execute()
trackedAnimes.forEach { anime ->
val message = "Episode ${anime.episodeNumber} of ${anime.title} is available"
sendNotification(applicationContext, anime.title, message)
}
Result.success()
} catch (e: Exception) {
e.printStackTrace()
Result.retry()
}
}
private fun sendNotification(context: Context, title: String, message: String) {
val notificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channelId = "notaku_channel"
if (notificationManager.getNotificationChannel(channelId) == null) {
val channel = NotificationChannel(
channelId,
"Notaku Notifications",
NotificationManager.IMPORTANCE_DEFAULT
)
notificationManager.createNotificationChannel(channel)
}
val builder = NotificationCompat.Builder(context, channelId)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
notificationManager.notify(System.currentTimeMillis().toInt(), builder.build())
}
}
Код: Выделить всё
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
// ---------- 🔥 Firebase ----------
@Provides
@Singleton
fun provideFirebaseAuth(): FirebaseAuth = FirebaseAuth.getInstance()
@Provides
@Singleton
fun provideFirebaseFirestore(): FirebaseFirestore = FirebaseFirestore.getInstance()
Код: Выделить всё
@Module
@InstallIn(SingletonComponent::class)
object RepositoryModule {
@Provides
fun provideFirebaseRepository(firebaseDataSource: FirebaseDataSource): FirebaseRepository {
return FirebaseRepositoryImpl(firebaseDataSource)
}
}
Код: Выделить всё
class FirebaseDataSource @Inject constructor(
private val firebaseAuth: FirebaseAuth,
private val firestore: FirebaseFirestore
) {
private val currentUserId: String
get() = firebaseAuth.currentUser?.uid
?: throw IllegalStateException("User is not logged in")
private val favoritesRef
get() = firestore.collection("users")
.document(currentUserId)
.collection("tracked_animes")
suspend fun getTrackedAnimes(): List {
return favoritesRef.get().await().documents.mapNotNull { doc ->
doc.toObject(FavoriteAnimeModel::class.java)
}
}
}
Код: Выделить всё
class FirebaseRepositoryImpl @Inject constructor(
private val firebaseDataSource: FirebaseDataSource
) : FirebaseRepository {
override suspend fun getTrackedAnimes(): List {
return firebaseDataSource.getTrackedAnimes()
}
}
Код: Выделить всё
interface FirebaseRepository {
suspend fun getTrackedAnimes(): List
}
Код: Выделить всё
class GetTrackedAnimesUseCase @Inject constructor(private val repository: FirebaseRepository) {
suspend fun execute(): List {
return repository.getTrackedAnimes()
}
}
Код: Выделить всё
[versions]
hiltAndroid = "2.57.2"
hiltNavigationCompose = "1.3.0"
hiltCommon = "1.3.0"
hiltWork = "1.3.0"
[libraries]
hilt-android = { module = "com.google.dagger:hilt-android", version.ref = "hiltAndroid" }
hilt-compiler = { module = "com.google.dagger:hilt-compiler", version.ref = "hiltAndroid" }
androidx-hilt-navigation-compose = { module = "androidx.hilt:hilt-navigation-compose", version.ref = "hiltNavigationCompose" }
androidx-hilt-common = { group = "androidx.hilt", name = "hilt-common", version.ref = "hiltCommon" }
androidx-hilt-work = { group = "androidx.hilt", name = "hilt-work", version.ref = "hiltWork" }
[plugins]
hilt = { id = "com.google.dagger.hilt.android", version.ref = "hiltAndroid" }
Код: Выделить всё
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
id("com.google.dagger.hilt.android")
kotlin("kapt")
}
android {
namespace = "com.sergiogv.notaku"
compileSdk = 36
defaultConfig {
applicationId = "com.sergiogv.notaku"
minSdk = 26
targetSdk = 36
versionCode = 1
versionName = "1.0"
}
}
dependencies {
// Hilt + WorkManager
implementation(libs.hilt.android)
kapt(libs.hilt.compiler)
implementation(libs.androidx.hilt.navigation.compose)
implementation(libs.androidx.hilt.common)
implementation(libs.androidx.hilt.work)
implementation("androidx.work:work-runtime-ktx:2.8.0")
}
Код: Выделить всё
plugins {
alias(libs.plugins.android.application) apply false
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.kotlin.compose) apply false
id("com.google.dagger.hilt.android") version "2.57.1" apply false
id("com.google.gms.google-services") version "4.4.3" apply false
}
E/WM-WorkerFactory: не удалось создать экземпляр com.sergiogv.notaku.MyWorker
java.lang.NoSuchMethodException: com.sergiogv.notaku.MyWorker. [class android.content.Context, class androidx.work.WorkerParameters]
Буду признателен, если вы поможете мне устранить эту ошибку.
Подробнее здесь: https://stackoverflow.com/questions/797 ... dexception
Мобильная версия