From 377920dac2c8d95128fa27d244729640fa03006c Mon Sep 17 00:00:00 2001 From: Serchinastico <54cymru@gmail.com> Date: Wed, 6 Feb 2019 19:27:29 +0100 Subject: [PATCH] Create thanos work --- .../superheroes/SuperHeroesApplication.kt | 14 +++++++++++ .../repository/LocalSuperHeroDataSource.kt | 16 ++++++++++++ .../data/repository/room/SuperHeroDao.kt | 6 +++++ .../superheroes/worker/ThanosWorker.kt | 25 +++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 app/src/main/java/com/karumi/jetpack/superheroes/worker/ThanosWorker.kt diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/SuperHeroesApplication.kt b/app/src/main/java/com/karumi/jetpack/superheroes/SuperHeroesApplication.kt index 8b26bf2..a47d37a 100644 --- a/app/src/main/java/com/karumi/jetpack/superheroes/SuperHeroesApplication.kt +++ b/app/src/main/java/com/karumi/jetpack/superheroes/SuperHeroesApplication.kt @@ -1,6 +1,8 @@ package com.karumi.jetpack.superheroes import android.app.Application +import androidx.work.OneTimeWorkRequest +import androidx.work.WorkManager import com.karumi.jetpack.superheroes.common.SuperHeroesDatabase import com.karumi.jetpack.superheroes.common.module import com.karumi.jetpack.superheroes.data.repository.LocalSuperHeroDataSource @@ -8,6 +10,7 @@ import com.karumi.jetpack.superheroes.data.repository.RemoteSuperHeroDataSource import com.karumi.jetpack.superheroes.data.repository.SuperHeroRepository import com.karumi.jetpack.superheroes.data.repository.SuperHeroesBoundaryCallback import com.karumi.jetpack.superheroes.data.repository.room.SuperHeroDao +import com.karumi.jetpack.superheroes.worker.ThanosWorker import org.kodein.di.DKodein import org.kodein.di.Kodein import org.kodein.di.KodeinAware @@ -25,6 +28,17 @@ class SuperHeroesApplication : Application(), KodeinAware { import(androidModule(this@SuperHeroesApplication)) } + override fun onCreate() { + super.onCreate() + startThanosWork() + } + + private fun startThanosWork() { + val work = OneTimeWorkRequest.Builder(ThanosWorker::class.java) + .build() + WorkManager.getInstance().enqueue(work) + } + fun override(overrideModule: Kodein.Module) { kodein = Kodein.lazy { import(appDependencies()) diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/data/repository/LocalSuperHeroDataSource.kt b/app/src/main/java/com/karumi/jetpack/superheroes/data/repository/LocalSuperHeroDataSource.kt index f905541..52bf4d3 100644 --- a/app/src/main/java/com/karumi/jetpack/superheroes/data/repository/LocalSuperHeroDataSource.kt +++ b/app/src/main/java/com/karumi/jetpack/superheroes/data/repository/LocalSuperHeroDataSource.kt @@ -1,5 +1,6 @@ package com.karumi.jetpack.superheroes.data.repository +import android.util.Log import androidx.lifecycle.LiveData import androidx.lifecycle.Transformations import androidx.paging.LivePagedListBuilder @@ -35,6 +36,21 @@ class LocalSuperHeroDataSource( return superHero } + fun deleteRandomHalf() = executor.execute { + val superHeroes = dao.getAllSuperHeroes() + + if (superHeroes.size < 2) { + return@execute + } + + val randomSuperHeroIds = superHeroes + .shuffled() + .take(superHeroes.size / 2) + .map { it.superHero.id } + Log.d("GERSIO", "Deleting: $randomSuperHeroIds") + dao.deleteAll(randomSuperHeroIds) + } + private fun SuperHeroEntity.toSuperHero(): SuperHero = superHero private fun SuperHero.toEntity(): SuperHeroEntity = SuperHeroEntity(this) } \ No newline at end of file diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/data/repository/room/SuperHeroDao.kt b/app/src/main/java/com/karumi/jetpack/superheroes/data/repository/room/SuperHeroDao.kt index 2415ee9..b1653e8 100644 --- a/app/src/main/java/com/karumi/jetpack/superheroes/data/repository/room/SuperHeroDao.kt +++ b/app/src/main/java/com/karumi/jetpack/superheroes/data/repository/room/SuperHeroDao.kt @@ -10,6 +10,9 @@ import androidx.room.Update @Dao interface SuperHeroDao { + @Query("SELECT * FROM superheroes") + fun getAllSuperHeroes(): List + @Query("SELECT * FROM superheroes ORDER BY superhero_id ASC") fun getAll(): DataSource.Factory @@ -24,4 +27,7 @@ interface SuperHeroDao { @Query("DELETE FROM superheroes") fun deleteAll() + + @Query("DELETE FROM superheroes WHERE superhero_id IN (:ids)") + fun deleteAll(ids: List) } \ No newline at end of file diff --git a/app/src/main/java/com/karumi/jetpack/superheroes/worker/ThanosWorker.kt b/app/src/main/java/com/karumi/jetpack/superheroes/worker/ThanosWorker.kt new file mode 100644 index 0000000..2d6a7a2 --- /dev/null +++ b/app/src/main/java/com/karumi/jetpack/superheroes/worker/ThanosWorker.kt @@ -0,0 +1,25 @@ +package com.karumi.jetpack.superheroes.worker + +import android.content.Context +import android.util.Log +import androidx.work.Worker +import androidx.work.WorkerParameters +import com.karumi.jetpack.superheroes.data.repository.LocalSuperHeroDataSource +import org.kodein.di.KodeinAware +import org.kodein.di.android.closestKodein +import org.kodein.di.erased.instance + +class ThanosWorker( + context: Context, + params: WorkerParameters +) : Worker(context, params), KodeinAware { + + override val kodein by closestKodein(context) + private val localSuperHeroesDataSource: LocalSuperHeroDataSource by instance() + + override fun doWork(): Result { + Log.d("GERSIO", "doWork!") + localSuperHeroesDataSource.deleteRandomHalf() + return Result.success() + } +} \ No newline at end of file