Skip to content

Commit

Permalink
Added WorkManager with hilt
Browse files Browse the repository at this point in the history
  • Loading branch information
freeskyES committed Oct 7, 2024
1 parent fafebf4 commit e3f3e85
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 25 deletions.
6 changes: 5 additions & 1 deletion demo/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ android {
}

dependencies {
implementation("com.github.freeskyES:notification-core:1.0.2")
implementation(project(":notificationcore"))
// implementation("com.github.freeskyES:notification-core:1.0.2")

implementation(libs.androidx.hilt.work)
kapt(libs.androidx.hilt.compiler)

implementation(libs.timber)

Expand Down
3 changes: 2 additions & 1 deletion demo/src/main/java/com/es/notificationdemo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class MainActivity : AppCompatActivity() {
Observer { notifications ->
binding.textView.text =
notifications.joinToString("\n\n") {
"Title: ${it.title}, Content: ${it.content}, Timestamp: ${it.timestamp}"
"Title: ${it.base.title}, Content: ${it.base.content}," +
"\npackageName: ${it.appId} +timestamp: ${it.base.notiAt}"
}
},
)
Expand Down
14 changes: 13 additions & 1 deletion demo/src/main/java/com/es/notificationdemo/MyApplication.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package com.es.notificationdemo

import android.app.Application
import androidx.hilt.work.HiltWorkerFactory
import androidx.work.Configuration
import dagger.hilt.android.HiltAndroidApp
import timber.log.Timber
import javax.inject.Inject

@HiltAndroidApp
class MyApplication : Application() {
class MyApplication : Application(), Configuration.Provider {

@Inject
lateinit var workerFactory: HiltWorkerFactory

override fun onCreate() {
super.onCreate()
Timber.plant(Timber.DebugTree())
}

override val workManagerConfiguration: Configuration
get() = Configuration.Builder()
.setWorkerFactory(workerFactory)
.build()
}
44 changes: 22 additions & 22 deletions demo/src/main/java/com/es/notificationdemo/NotificationViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,39 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.es.notificationcore.domain.model.NotificationData
import com.es.notificationcore.data.noti.Noti
import com.es.notificationcore.presentation.initializer.NotificationInitializer
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class NotificationViewModel
@Inject
constructor() : ViewModel() {
private lateinit var notificationInitializer: NotificationInitializer
@Inject
constructor() : ViewModel() {
private lateinit var notificationInitializer: NotificationInitializer

private val _notifications = MutableLiveData<List<NotificationData>>()
val notifications: LiveData<List<NotificationData>> = _notifications
private val _notifications = MutableLiveData<List<Noti>>()
val notifications: LiveData<List<Noti>> = _notifications

fun fetchNotifications() {
viewModelScope.launch {
try {
val notifications = notificationInitializer.getNotifications()
_notifications.postValue(notifications)
} catch (e: Exception) {
e.printStackTrace()
}
fun fetchNotifications() {
viewModelScope.launch {
try {
val notifications = notificationInitializer.getNotifications()
_notifications.postValue(notifications)
} catch (e: Exception) {
e.printStackTrace()
}
}
}

fun setNotificationInitializer(notificationInitializer: NotificationInitializer) {
this.notificationInitializer = notificationInitializer
initializeService()
}
fun setNotificationInitializer(notificationInitializer: NotificationInitializer) {
this.notificationInitializer = notificationInitializer
initializeService()
}

private fun initializeService() {
// 서비스 초기화 및 시작
notificationInitializer.initializeAndStartService()
}
private fun initializeService() {
// 서비스 초기화 및 시작
notificationInitializer.initializeAndStartService()
}
}
7 changes: 7 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
[versions]
agp = "8.4.0"
gson = "2.10.1"
hiltWork = "1.2.0"
kotlin = "1.9.0"
coreKtx = "1.13.1"
junit = "4.13.2"
Expand All @@ -13,11 +15,16 @@ constraintlayout = "2.1.4"
timber = "5.0.1"
hiltAndroid = "2.50"
roomRuntime = "2.6.1"
workRuntimeKtx = "2.9.1"

[libraries]
androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "coreKtx" }
androidx-hilt-compiler = { module = "androidx.hilt:hilt-compiler", version.ref = "hiltWork" }
androidx-hilt-work = { module = "androidx.hilt:hilt-work", version.ref = "hiltWork" }
androidx-lifecycle-livedata-ktx = { module = "androidx.lifecycle:lifecycle-livedata-ktx", version.ref = "lifecycleViewmodelKtx" }
androidx-lifecycle-viewmodel-ktx = { module = "androidx.lifecycle:lifecycle-viewmodel-ktx", version.ref = "lifecycleViewmodelKtx" }
androidx-work-runtime-ktx = { module = "androidx.work:work-runtime-ktx", version.ref = "workRuntimeKtx" }
gson = { module = "com.google.code.gson:gson", version.ref = "gson" }
junit = { group = "junit", name = "junit", version.ref = "junit" }
androidx-junit = { group = "androidx.test.ext", name = "junit", version.ref = "junitVersion" }
androidx-espresso-core = { group = "androidx.test.espresso", name = "espresso-core", version.ref = "espressoCore" }
Expand Down

0 comments on commit e3f3e85

Please sign in to comment.