Skip to content

Commit

Permalink
Migrate to Kotlin Coroutines (#270)
Browse files Browse the repository at this point in the history
* Add coroutine as a dependency in build.gradle
* Migrate AsyncTasks to kotlin coroutines
* Migrate executors with the coroutines in repositories
  • Loading branch information
CuriousNikhil authored Mar 24, 2020
1 parent d390ee1 commit c7c5343
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 103 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ buildscript {
ext {
kotlinVersion = '1.3.70'
androidGradleVersion = '3.6.0'
coroutineVersion = '1.3.4'

// Google libraries
appCompatVersion = '1.1.0'
constraintLayoutVersion = '1.1.3'
materialComponentsVersion = '1.1.0'
roomVersion = '2.2.3'
roomVersion = '2.2.5'
lifecycleVersion = '2.2.0'
androidXCoreVersion = '2.1.0'

Expand Down
4 changes: 4 additions & 0 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ dependencies {

implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycleVersion"
implementation "androidx.room:room-ktx:$roomVersion"
implementation "androidx.room:room-runtime:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutineVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutineVersion"

implementation "com.google.code.gson:gson:$gsonVersion"
implementation "com.squareup.okhttp3:okhttp:$okhttp3Version"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import com.chuckerteam.chucker.internal.data.entity.HttpTransaction
import com.chuckerteam.chucker.internal.data.entity.RecordedThrowable
import com.chuckerteam.chucker.internal.data.repository.RepositoryProvider
import com.chuckerteam.chucker.internal.support.NotificationHelper
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
* The collector responsible of collecting data from a [ChuckerInterceptor] and
Expand Down Expand Up @@ -36,7 +39,9 @@ class ChuckerCollector @JvmOverloads constructor(
*/
fun onError(tag: String, throwable: Throwable) {
val recordedThrowable = RecordedThrowable(tag, throwable)
RepositoryProvider.throwable().saveThrowable(recordedThrowable)
CoroutineScope(Dispatchers.IO).launch {
RepositoryProvider.throwable().saveThrowable(recordedThrowable)
}
if (showNotification) {
notificationHelper.show(recordedThrowable)
}
Expand All @@ -48,7 +53,9 @@ class ChuckerCollector @JvmOverloads constructor(
* @param transaction The HTTP transaction sent
*/
internal fun onRequestSent(transaction: HttpTransaction) {
RepositoryProvider.transaction().insertTransaction(transaction)
CoroutineScope(Dispatchers.IO).launch {
RepositoryProvider.transaction().insertTransaction(transaction)
}
if (showNotification) {
notificationHelper.show(transaction)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import android.util.Log
import com.chuckerteam.chucker.api.Chucker.LOG_TAG
import com.chuckerteam.chucker.internal.data.repository.RepositoryProvider
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

/**
* Class responsible of holding the logic for the retention of your HTTP transactions
Expand Down Expand Up @@ -62,8 +65,10 @@ class RetentionManager @JvmOverloads constructor(
}

private fun deleteSince(threshold: Long) {
RepositoryProvider.transaction().deleteOldTransactions(threshold)
RepositoryProvider.throwable().deleteOldThrowables(threshold)
CoroutineScope(Dispatchers.IO).launch {
RepositoryProvider.transaction().deleteOldTransactions(threshold)
RepositoryProvider.throwable().deleteOldThrowables(threshold)
}
}

private fun isCleanupDue(now: Long) = now - getLastCleanup(now) > cleanupFrequency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import com.chuckerteam.chucker.internal.data.entity.HttpTransaction
import com.chuckerteam.chucker.internal.data.entity.HttpTransactionTuple
import com.chuckerteam.chucker.internal.data.room.ChuckerDatabase
import com.chuckerteam.chucker.internal.support.distinctUntilChanged
import java.util.concurrent.Executor
import java.util.concurrent.Executors

internal class HttpTransactionDatabaseRepository(private val database: ChuckerDatabase) : HttpTransactionRepository {

private val executor: Executor = Executors.newSingleThreadExecutor()

private val transactionDao get() = database.transactionDao()

override fun getFilteredTransactionTuples(code: String, path: String): LiveData<List<HttpTransactionTuple>> {
Expand All @@ -28,22 +24,20 @@ internal class HttpTransactionDatabaseRepository(private val database: ChuckerDa
return transactionDao.getSortedTuples()
}

override fun deleteAllTransactions() {
executor.execute { transactionDao.deleteAll() }
override suspend fun deleteAllTransactions() {
transactionDao.deleteAll()
}

override fun insertTransaction(transaction: HttpTransaction) {
executor.execute {
val id = transactionDao.insert(transaction)
transaction.id = id ?: 0
}
override suspend fun insertTransaction(transaction: HttpTransaction) {
val id = transactionDao.insert(transaction)
transaction.id = id ?: 0
}

override fun updateTransaction(transaction: HttpTransaction): Int {
return transactionDao.update(transaction)
}

override fun deleteOldTransactions(threshold: Long) {
executor.execute { transactionDao.deleteBefore(threshold) }
override suspend fun deleteOldTransactions(threshold: Long) {
transactionDao.deleteBefore(threshold)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import com.chuckerteam.chucker.internal.data.entity.HttpTransactionTuple
*/
internal interface HttpTransactionRepository {

fun insertTransaction(transaction: HttpTransaction)
suspend fun insertTransaction(transaction: HttpTransaction)

fun updateTransaction(transaction: HttpTransaction): Int

fun deleteOldTransactions(threshold: Long)
suspend fun deleteOldTransactions(threshold: Long)

fun deleteAllTransactions()
suspend fun deleteAllTransactions()

fun getSortedTransactionTuples(): LiveData<List<HttpTransactionTuple>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,28 @@ import com.chuckerteam.chucker.internal.data.entity.RecordedThrowable
import com.chuckerteam.chucker.internal.data.entity.RecordedThrowableTuple
import com.chuckerteam.chucker.internal.data.room.ChuckerDatabase
import com.chuckerteam.chucker.internal.support.distinctUntilChanged
import java.util.concurrent.Executor
import java.util.concurrent.Executors

internal class RecordedThrowableDatabaseRepository(
private val database: ChuckerDatabase
) : RecordedThrowableRepository {

private val executor: Executor = Executors.newSingleThreadExecutor()

override fun getRecordedThrowable(id: Long): LiveData<RecordedThrowable> {
return database.throwableDao().getById(id).distinctUntilChanged()
}

override fun deleteAllThrowables() {
executor.execute { database.throwableDao().deleteAll() }
override suspend fun deleteAllThrowables() {
database.throwableDao().deleteAll()
}

override fun getSortedThrowablesTuples(): LiveData<List<RecordedThrowableTuple>> {
return database.throwableDao().getTuples()
}

override fun saveThrowable(throwable: RecordedThrowable) {
executor.execute { database.throwableDao().insert(throwable) }
override suspend fun saveThrowable(throwable: RecordedThrowable) {
database.throwableDao().insert(throwable)
}

override fun deleteOldThrowables(threshold: Long) {
executor.execute { database.throwableDao().deleteBefore(threshold) }
override suspend fun deleteOldThrowables(threshold: Long) {
database.throwableDao().deleteBefore(threshold)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import com.chuckerteam.chucker.internal.data.entity.RecordedThrowableTuple
*/
internal interface RecordedThrowableRepository {

fun saveThrowable(throwable: RecordedThrowable)
suspend fun saveThrowable(throwable: RecordedThrowable)

fun deleteOldThrowables(threshold: Long)
suspend fun deleteOldThrowables(threshold: Long)

fun deleteAllThrowables()
suspend fun deleteAllThrowables()

fun getSortedThrowablesTuples(): LiveData<List<RecordedThrowableTuple>>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@ internal interface HttpTransactionDao {
fun getFilteredTuples(codeQuery: String, pathQuery: String): LiveData<List<HttpTransactionTuple>>

@Insert
fun insert(transaction: HttpTransaction): Long?
suspend fun insert(transaction: HttpTransaction): Long?

@Update(onConflict = OnConflictStrategy.REPLACE)
fun update(transaction: HttpTransaction): Int

@Query("DELETE FROM transactions")
fun deleteAll()
suspend fun deleteAll()

@Query("SELECT * FROM transactions WHERE id = :id")
fun getById(id: Long): LiveData<HttpTransaction?>

@Query("DELETE FROM transactions WHERE requestDate <= :threshold")
fun deleteBefore(threshold: Long)
suspend fun deleteBefore(threshold: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ internal interface RecordedThrowableDao {
@Query("SELECT id,tag,date,clazz,message FROM throwables ORDER BY date DESC")
fun getTuples(): LiveData<List<RecordedThrowableTuple>>

@Insert()
fun insert(throwable: RecordedThrowable): Long?
@Insert
suspend fun insert(throwable: RecordedThrowable): Long?

@Query("DELETE FROM throwables")
fun deleteAll()
suspend fun deleteAll()

@Query("SELECT * FROM throwables WHERE id = :id")
fun getById(id: Long): LiveData<RecordedThrowable>

@Query("DELETE FROM throwables WHERE date <= :threshold")
fun deleteBefore(threshold: Long)
suspend fun deleteBefore(threshold: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@ import android.app.IntentService
import android.content.Intent
import com.chuckerteam.chucker.internal.data.repository.RepositoryProvider
import java.io.Serializable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

internal class ClearDatabaseService : IntentService(CLEAN_DATABASE_SERVICE_NAME) {

override fun onHandleIntent(intent: Intent?) {
when (intent?.getSerializableExtra(EXTRA_ITEM_TO_CLEAR)) {
is ClearAction.Transaction -> {
RepositoryProvider.initialize(applicationContext)
RepositoryProvider.transaction().deleteAllTransactions()
CoroutineScope(Dispatchers.IO).launch {
RepositoryProvider.transaction().deleteAllTransactions()
}
NotificationHelper.clearBuffer()
NotificationHelper(this).dismissTransactionsNotification()
}
is ClearAction.Error -> {
RepositoryProvider.initialize(applicationContext)
RepositoryProvider.throwable().deleteAllThrowables()
CoroutineScope(Dispatchers.IO).launch {
RepositoryProvider.throwable().deleteAllThrowables()
}
NotificationHelper(this).dismissErrorsNotification()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.switchMap
import androidx.lifecycle.viewModelScope
import com.chuckerteam.chucker.internal.data.entity.HttpTransactionTuple
import com.chuckerteam.chucker.internal.data.entity.RecordedThrowableTuple
import com.chuckerteam.chucker.internal.data.repository.RepositoryProvider
import com.chuckerteam.chucker.internal.support.NotificationHelper
import kotlinx.coroutines.launch

internal class MainViewModel : ViewModel() {

Expand Down Expand Up @@ -38,11 +40,15 @@ internal class MainViewModel : ViewModel() {
}

fun clearTransactions() {
RepositoryProvider.transaction().deleteAllTransactions()
viewModelScope.launch {
RepositoryProvider.transaction().deleteAllTransactions()
}
NotificationHelper.clearBuffer()
}

fun clearThrowables() {
RepositoryProvider.throwable().deleteAllThrowables()
viewModelScope.launch {
RepositoryProvider.throwable().deleteAllThrowables()
}
}
}
Loading

0 comments on commit c7c5343

Please sign in to comment.