Skip to content

Commit

Permalink
Address PR comments:
Browse files Browse the repository at this point in the history
* Making the default dispatcher in DataManager to be computation
* Removing other dispatcher switches in scope.launch-es
  • Loading branch information
florina-muntenescu committed Mar 21, 2019
1 parent 61a1763 commit 19f168f
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions core/src/main/java/io/plaidapp/core/data/DataManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.util.concurrent.atomic.AtomicInteger
import javax.inject.Inject

Expand All @@ -52,8 +53,8 @@ class DataManager @Inject constructor(
private val dispatcherProvider: CoroutinesDispatcherProvider
) : DataLoadingSubject {

private var parentJob = SupervisorJob()
private val scope = CoroutineScope(dispatcherProvider.main + parentJob)
private val parentJob = SupervisorJob()
private val scope = CoroutineScope(dispatcherProvider.computation + parentJob)

private val parentJobs = mutableMapOf<InFlightRequestData, Job>()

Expand All @@ -80,6 +81,7 @@ class DataManager @Inject constructor(

init {
sourcesRepository.registerFilterChangedCallback(filterListener)
// build a map of source keys to pages initialized to 0
pageIndexes = sourcesRepository.getSourcesSync().map { it.key to 0 }.toMap().toMutableMap()
}

Expand All @@ -93,7 +95,7 @@ class DataManager @Inject constructor(
onDataLoadedCallback?.onDataLoaded(data)
}

suspend fun loadMore() {
suspend fun loadMore() = withContext(dispatcherProvider.computation) {
sourcesRepository.getSources().forEach { loadSource(it) }
}

Expand Down Expand Up @@ -157,23 +159,22 @@ class DataManager @Inject constructor(
parentJobs.remove(request)
}

private fun launchLoadDesignerNewsStories(data: InFlightRequestData) =
scope.launch(dispatcherProvider.computation) {
val result = loadStories(data.page)
when (result) {
is Result.Success -> sourceLoaded(
result.data,
SOURCE_DESIGNER_NEWS_POPULAR,
data
)
is Result.Error -> loadFailed(data)
}.exhaustive
}
private fun launchLoadDesignerNewsStories(data: InFlightRequestData) = scope.launch {
val result = loadStories(data.page)
when (result) {
is Result.Success -> sourceLoaded(
result.data,
SOURCE_DESIGNER_NEWS_POPULAR,
data
)
is Result.Error -> loadFailed(data)
}.exhaustive
}

private fun loadDesignerNewsSearch(
source: DesignerNewsSearchSource,
data: InFlightRequestData
) = scope.launch(dispatcherProvider.computation) {
) = scope.launch {
val result = searchStories(source.key, data.page)
when (result) {
is Result.Success -> sourceLoaded(result.data, source.key, data)
Expand All @@ -182,7 +183,7 @@ class DataManager @Inject constructor(
}

private fun loadDribbbleSearch(source: DribbbleSourceItem, data: InFlightRequestData) =
scope.launch(dispatcherProvider.computation) {
scope.launch {
val result = shotsRepository.search(source.query, data.page)
when (result) {
is Result.Success -> sourceLoaded(result.data, source.key, data)
Expand Down

0 comments on commit 19f168f

Please sign in to comment.