Skip to content

Commit

Permalink
Create thanos work
Browse files Browse the repository at this point in the history
  • Loading branch information
Serchinastico committed Mar 7, 2019
1 parent 8d6be19 commit 377920d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
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
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
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import androidx.room.Update

@Dao
interface SuperHeroDao {
@Query("SELECT * FROM superheroes")
fun getAllSuperHeroes(): List<SuperHeroEntity>

@Query("SELECT * FROM superheroes ORDER BY superhero_id ASC")
fun getAll(): DataSource.Factory<Int, SuperHeroEntity>

Expand All @@ -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<String>)
}
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit 377920d

Please sign in to comment.