Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
florina-muntenescu committed Mar 21, 2019
1 parent 27e719b commit 61a1763
Showing 1 changed file with 8 additions and 25 deletions.
33 changes: 8 additions & 25 deletions core/src/main/java/io/plaidapp/core/data/DataManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.launch
import java.util.ArrayList
import java.util.HashMap
import java.util.concurrent.atomic.AtomicInteger
import javax.inject.Inject

Expand Down Expand Up @@ -60,7 +58,7 @@ class DataManager @Inject constructor(
private val parentJobs = mutableMapOf<InFlightRequestData, Job>()

private val loadingCount = AtomicInteger(0)
private var loadingCallbacks: MutableList<DataLoadingSubject.DataLoadingCallbacks>? = null
private var loadingCallbacks = mutableListOf<DataLoadingSubject.DataLoadingCallbacks>()
private var onDataLoadedCallback: OnDataLoadedCallback<List<PlaidItem>>? = null
private lateinit var pageIndexes: MutableMap<String, Int>

Expand All @@ -81,10 +79,8 @@ class DataManager @Inject constructor(
}

init {
setOnDataLoadedCallback(onDataLoadedCallback)

sourcesRepository.registerFilterChangedCallback(filterListener)
setupPageIndexes()
pageIndexes = sourcesRepository.getSourcesSync().map { it.key to 0 }.toMap().toMutableMap()
}

fun setOnDataLoadedCallback(
Expand Down Expand Up @@ -129,20 +125,10 @@ class DataManager @Inject constructor(
}
}

private fun setupPageIndexes() {
val dateSources = sourcesRepository.getSourcesSync()
pageIndexes = HashMap(dateSources.size)
dateSources.forEach {
if (pageIndexes[it.key] != null) {
pageIndexes[it.key] = 0
}
}
}

private fun getNextPageIndex(dataSource: String): Int {
var nextPage = 1 // default to one – i.e. for newly added sources
if (pageIndexes.containsKey(dataSource)) {
nextPage = pageIndexes[dataSource]?.or(0)?.plus(1) ?: 1
nextPage = pageIndexes.getValue(dataSource) + 1
}
pageIndexes[dataSource] = nextPage
return nextPage
Expand Down Expand Up @@ -214,10 +200,7 @@ class DataManager @Inject constructor(
}

override fun registerCallback(callback: DataLoadingSubject.DataLoadingCallbacks) {
if (loadingCallbacks == null) {
loadingCallbacks = ArrayList(1)
}
loadingCallbacks?.add(callback)
loadingCallbacks.add(callback)
}

private fun loadStarted() {
Expand All @@ -233,8 +216,8 @@ class DataManager @Inject constructor(
}

private fun setPage(items: List<PlaidItem>, page: Int) {
for (item in items) {
item.page = page
items.forEach {
it.page = page
}
}

Expand All @@ -245,13 +228,13 @@ class DataManager @Inject constructor(
}

private fun dispatchLoadingStartedCallbacks() {
loadingCallbacks?.forEach {
loadingCallbacks.forEach {
it.dataStartedLoading()
}
}

private fun dispatchLoadingFinishedCallbacks() {
loadingCallbacks?.forEach {
loadingCallbacks.forEach {
it.dataFinishedLoading()
}
}
Expand Down

0 comments on commit 61a1763

Please sign in to comment.