-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/workmanager-7' into feature/5
# Conflicts: # app/src/main/java/kr/ksw/visitkorea/app/VisitKoreaApp.kt
- Loading branch information
Showing
7 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
70 changes: 70 additions & 0 deletions
70
app/src/main/java/kr/ksw/visitkorea/data/workmanager/AreaCodeWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters