Skip to content

Commit 6202c6a

Browse files
Separated reusable functions which had args/params difference for post/data queries.
1 parent da3f06e commit 6202c6a

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

app/src/main/java/com/developerbreach/developerbreach/repository/AppRepository.kt

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ class AppRepository(
1616
private val database: ArticleDatabase
1717
) {
1818

19-
suspend fun getArticlesData(): List<Article> {
19+
suspend fun getArticlesData(
20+
totalPostsToDoRunQueryOn: Int
21+
): List<Article> {
2022
val listData: List<Article>
2123
withContext(Dispatchers.IO) {
22-
listData = getArticles()
24+
listData = getArticles(totalPostsToDoRunQueryOn)
2325
}
2426
return listData
2527
}
@@ -34,10 +36,12 @@ class AppRepository(
3436
return data
3537
}
3638

37-
suspend fun getSearchableArticlesData(): List<Search> {
39+
suspend fun getSearchableArticlesData(
40+
totalPostsToDoRunQueryOn: Int
41+
): List<Search> {
3842
val listData: List<Search>
3943
withContext(Dispatchers.IO) {
40-
listData = getSearchableArticles()
44+
listData = getSearchableArticles(totalPostsToDoRunQueryOn)
4145
}
4246
return listData
4347
}
@@ -50,12 +54,13 @@ class AppRepository(
5054
return listData
5155
}
5256

53-
suspend fun getArticlesByCategoryId(
54-
categoryId: Int
57+
suspend fun getArticlesByCategory(
58+
categoryId: Int,
59+
postsPage: Int
5560
): List<Article> {
5661
val listData: List<Article>
5762
withContext(Dispatchers.IO) {
58-
listData = getArticlesByCategory(categoryId)
63+
listData = getArticlesByCategoryId(categoryId, postsPage)
5964
}
6065
return listData
6166
}

app/src/main/java/com/developerbreach/developerbreach/view/home/HomeViewModel.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class HomeViewModel(
4141
val articleDataState: LiveData<DataState>
4242
get() = _state
4343

44+
private val totalPostsToDoRunQueryOn = 5
45+
4446
init {
4547
launchToLoadArticleData()
4648
}
@@ -53,7 +55,7 @@ class HomeViewModel(
5355
viewModelScope.launch {
5456
_state.value = DataState.LOADING
5557
try {
56-
val articlesData: List<Article> = repository.getArticlesData()
58+
val articlesData = repository.getArticlesData(totalPostsToDoRunQueryOn)
5759
_articles.postValue(articlesData)
5860
_state.value = DataState.DONE
5961
} catch (e: Exception) {

app/src/main/java/com/developerbreach/developerbreach/view/search/SearchViewModel.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class SearchViewModel(
2525
val filteredArticles: LiveData<List<Search>>
2626
get() = _filteredArticles
2727

28+
private val totalPostsToDoRunQueryOn = 100
29+
2830
private var searchableArticles = listOf<Search>()
2931

3032
init {
@@ -34,7 +36,7 @@ class SearchViewModel(
3436
private fun loadSearchableArticlesToFilter() {
3537
viewModelScope.launch {
3638
try {
37-
searchableArticles = repository.getSearchableArticlesData()
39+
searchableArticles = repository.getSearchableArticlesData(totalPostsToDoRunQueryOn)
3840
} catch (e: Exception) {
3941
Timber.e("Exception caught in SearchViewModel ${e.message}")
4042
}

0 commit comments

Comments
 (0)