Skip to content

Commit

Permalink
Merge branch 'feature/workmanager-7' into feature/5
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/src/main/java/kr/ksw/visitkorea/app/VisitKoreaApp.kt
  • Loading branch information
ksw4015 committed Oct 2, 2024
2 parents 3232784 + 98dea22 commit 74bec00
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ dependencies {
ksp(libs.androidx.hilt.compiler)
androidTestImplementation(libs.androidx.core.testing)

// workmanager
implementation(libs.androidx.hilt.work)
implementation(libs.androidx.work)

// test
testImplementation(libs.junit)
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.VisitKorea">

<provider
android:name="androidx.startup.InitializationProvider"
android:authorities="{applicationId}.androidx-startup"
android:exported="false"
tools:replace="android:authorities">
<meta-data
android:name="androidx.work.WorkManagerInitializer"
android:value="androidx.startup"
tools:node="remove"/>
</provider>

<activity
android:name=".presentation.splash.SplashActivity"
android:exported="true"
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/kr/ksw/visitkorea/app/VisitKoreaApp.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package kr.ksw.visitkorea.app

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

@HiltAndroidApp
class VisitKoreaApp : Application()
class VisitKoreaApp : Application(), Configuration.Provider {
@Inject
lateinit var hiltWorkerFactory: HiltWorkerFactory

override val workManagerConfiguration: Configuration
get() = Configuration.Builder()
.setWorkerFactory(hiltWorkerFactory)
.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package kr.ksw.visitkorea.data.workmanager

import android.app.Notification
import android.content.Context
import androidx.core.app.NotificationCompat
import androidx.hilt.work.HiltWorker
import androidx.work.CoroutineWorker
import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kr.ksw.visitkorea.data.local.databases.AreaCodeDatabase
import kr.ksw.visitkorea.data.local.entity.AreaCodeEntity
import kr.ksw.visitkorea.data.local.entity.SigunguCodeEntity
import kr.ksw.visitkorea.data.repository.AreaCodeRepository

@HiltWorker
class AreaCodeWorker @AssistedInject constructor(
@Assisted private val appContext: Context,
@Assisted private val params: WorkerParameters,
private val areaCodeRepository: AreaCodeRepository,
private val areaCodeDatabase: AreaCodeDatabase
): CoroutineWorker(appContext, params) {

override suspend fun doWork(): Result {
val areaCodeItems = areaCodeRepository.getAreaCode().getOrNull() ?: return Result.failure()
areaCodeItems.forEach { areaCodeItem ->
withContext(Dispatchers.IO) {
areaCodeDatabase.areaCodeDao.upsertAreaCodeEntity(
AreaCodeEntity(
code = areaCodeItem.code,
name = areaCodeItem.name
)
)
val sigunguItems = areaCodeRepository.getSigunguCode(areaCodeItem.code).getOrNull()
sigunguItems?.forEach { sigunguItem ->
areaCodeDatabase.areaCodeDao.upsertSigunguCodeEntity(SigunguCodeEntity(
areaCode = areaCodeItem.code,
code = sigunguItem.code,
name = sigunguItem.name
))
}
}
}
return Result.success()
}

override suspend fun getForegroundInfo(): ForegroundInfo {
return ForegroundInfo(
NOTIFICATION_ID,
createNotification()
)
}

private fun createNotification(): Notification {
return NotificationCompat.Builder(
appContext,
NOTIFICATION_CHANNEL
).build()
}

companion object {
private const val NOTIFICATION_ID = 1000
private const val NOTIFICATION_CHANNEL = "Save AreaCode"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class SplashActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.initAreaCode(applicationContext)
setContent {
VisitKoreaTheme {
Surface {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package kr.ksw.visitkorea.presentation.splash

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.work.OneTimeWorkRequestBuilder
import androidx.work.WorkManager
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import kr.ksw.visitkorea.data.local.databases.AreaCodeDatabase
import kr.ksw.visitkorea.data.workmanager.AreaCodeWorker
import javax.inject.Inject

@HiltViewModel
class SplashViewModel @Inject constructor(): ViewModel() {
class SplashViewModel @Inject constructor(
private val areaCodeDatabase: AreaCodeDatabase
): ViewModel() {
private val _sideEffect = MutableSharedFlow<SplashSideEffect>(replay = 0)
val sideEffect: SharedFlow<SplashSideEffect> = _sideEffect.asSharedFlow()

Expand Down Expand Up @@ -45,6 +53,23 @@ class SplashViewModel @Inject constructor(): ViewModel() {
))
}
}

fun initAreaCode(context: Context) {
viewModelScope.launch {
val areaCodeItems = areaCodeDatabase.areaCodeDao.getAllAreaCodeEntities()
if(areaCodeItems.isEmpty()) {
val workRequest = OneTimeWorkRequestBuilder<AreaCodeWorker>().build()
WorkManager
.getInstance(context)
.enqueue(workRequest)
} else {
Log.d("SplashViewModel", areaCodeItems.toString())
areaCodeItems.forEach {
Log.d("SplashViewModel", areaCodeDatabase.areaCodeDao.getSigunguCodeByAreaCode(it.code).toString())
}
}
}
}
}

sealed interface SplashSideEffect {
Expand Down
4 changes: 4 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ composeBom = "2024.09.02"
coroutine-core = "1.8.1"
coroutine-test = "1.5.1"
room = "2.6.1"
work = "2.9.1"

retrofit = "2.9.0"
okhttp = "4.12.0"
Expand Down Expand Up @@ -44,10 +45,13 @@ androidx-activity-ktx = { group = "androidx.activity", name = "activity-ktx", ve
androidx-room-compiler = { group = "androidx.room", name = "room-compiler", version.ref = "room" }
androidx-room-ktx = { group = "androidx.room", name = "room-ktx", version.ref = "room" }

androidx-work = { group = "androidx.work", name = "work-runtime-ktx", version.ref = "work"}

hilt-android = { group = "com.google.dagger", name = "hilt-android", version.ref = "hilt" }
hilt-compiler = { group = "com.google.dagger", name = "hilt-compiler", version.ref = "hilt" }
hilt-android-testing = { group = "com.google.dagger", name = "hilt-android-testing", version.ref = "hilt" }
androidx-hilt-compiler = { group = "androidx.hilt", name = "hilt-compiler", version.ref = "hilt-compiler" }
androidx-hilt-work = { group = "androidx.hilt", name = "hilt-work", version.ref = "hilt-compiler"}
androidx-hilt-navigation-compose = { group = "androidx.hilt", name = "hilt-navigation-compose", version.ref = "hilt-compiler" }

okhttp = { group = "com.squareup.okhttp3", name = "okhttp", version.ref = "okhttp" }
Expand Down

0 comments on commit 74bec00

Please sign in to comment.