Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue 3370 #3381

Merged
merged 24 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fd8b3b9
Making fields nullable with null as the default value
rodrigoliveirac Jul 30, 2024
084b0f5
Fetch contributors with paging
rodrigoliveirac Jul 30, 2024
d8de9cb
Include anonymous contributors
rodrigoliveirac Jul 30, 2024
f16ea5f
Merge branch 'main' into fix-issue-3370
rodrigoliveirac Jul 30, 2024
5271e81
Changing the return value in unit tests
rodrigoliveirac Jul 30, 2024
fe4045c
Merge remote-tracking branch 'origin/fix-issue-3370' into fix-issue-3370
rodrigoliveirac Jul 30, 2024
01ee1b2
Refactor with Either approach
rodrigoliveirac Jul 31, 2024
482343e
migrating to the compilerOptions DSL
rodrigoliveirac Aug 1, 2024
4268cbd
Solving Detekt issues
rodrigoliveirac Aug 1, 2024
692ae86
Update unit tests
rodrigoliveirac Aug 1, 2024
1c7c562
Adding Ktor client mock
rodrigoliveirac Aug 1, 2024
ab903df
Refactor
rodrigoliveirac Aug 1, 2024
d5ad9f4
Create FakeDataSource
rodrigoliveirac Aug 1, 2024
dbf01a2
Create ContributorsDataSourceTest
rodrigoliveirac Aug 1, 2024
146c25b
Drop unnecessary query variables
rodrigoliveirac Aug 1, 2024
4cafd93
Add Fetch Contributors test
rodrigoliveirac Aug 1, 2024
56958ea
Merge branch 'main' into fix-issue-3370
rodrigoliveirac Aug 1, 2024
6bd6a00
Refactor just using mockk
rodrigoliveirac Aug 2, 2024
229adbf
Merge remote-tracking branch 'origin/fix-issue-3370' into fix-issue-3370
rodrigoliveirac Aug 2, 2024
60cfd61
Merge branch 'main' into fix-issue-3370
rodrigoliveirac Aug 2, 2024
ca26875
Drop Tests
rodrigoliveirac Aug 2, 2024
c5e4759
Merge remote-tracking branch 'origin/fix-issue-3370' into fix-issue-3370
rodrigoliveirac Aug 2, 2024
1f6b2b8
Refactor
rodrigoliveirac Aug 5, 2024
c4d39c4
Merge branch 'main' into fix-issue-3370
rodrigoliveirac Aug 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,18 @@ class ContributorsViewModel @Inject constructor(
}

private suspend fun fetchContributors() {
val contributors = ivyWalletRepositoryDataSource.fetchContributors()?.map {
val contributors = ivyWalletRepositoryDataSource.fetchContributors().map {
Contributor(
name = it.login,
photoUrl = it.avatarUrl,
name = it.login ?: "Anonymous",
photoUrl = it.avatarUrl ?: "",
contributionsCount = it.contributions.toString(),
githubProfileUrl = it.link
githubProfileUrl = it.link ?: ""
)
}

if (contributors != null) {
contributorsResponse.value = ContributorsResponse.Success(
contributors.toImmutableList()
)
} else {
contributorsResponse.value = ContributorsResponse.Error("Error")
ILIYANGERMANOV marked this conversation as resolved.
Show resolved Hide resolved
}
contributorsResponse.value = ContributorsResponse.Success(
contributors.toImmutableList()
)
}

private suspend fun fetchProjectInfo() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import timber.log.Timber
import javax.inject.Inject

class IvyWalletRepositoryDataSource @Inject constructor(
Expand All @@ -17,12 +18,12 @@ class IvyWalletRepositoryDataSource @Inject constructor(
@Keep
@Serializable
data class ContributorDto(
val login: String,
val login: String? = null,
@SerialName("avatar_url")
val avatarUrl: String,
val avatarUrl: String? = null,
val contributions: Int,
@SerialName("html_url")
val link: String
val link: String? = null,
)

@Keep
Expand All @@ -38,19 +39,48 @@ class IvyWalletRepositoryDataSource @Inject constructor(

companion object {
private const val CONTRIBUTORS_PER_PAGE = 100
private const val DISPLAY_ANONYMOUS_CONTRIBUTORS = true
private const val INITIAL_PAGE = 1
}

suspend fun fetchContributors(): List<ContributorDto>? {
return try {
withContext(Dispatchers.IO) {
suspend fun fetchContributors(): List<ContributorDto> {
return withContext(Dispatchers.IO) {
rodrigoliveirac marked this conversation as resolved.
Show resolved Hide resolved
val contributors = mutableListOf<ContributorDto>()
paging(addContributors = { results ->
results?.forEach { contributor -> contributors.add(contributor) }
})
contributors.toList()
}
}

private suspend fun paging(addContributors: (List<ContributorDto>?) -> Unit) {
var currentPage: Int? = INITIAL_PAGE
while (currentPage != null) {
val contributors = try {
httpClient
.get("https://api.github.com/repos/Ivy-Apps/ivy-wallet/contributors") {
parameter("anon", DISPLAY_ANONYMOUS_CONTRIBUTORS)
ILIYANGERMANOV marked this conversation as resolved.
Show resolved Hide resolved
parameter("per_page", CONTRIBUTORS_PER_PAGE)
parameter("page", currentPage)
}
.body<List<ContributorDto>>()
} catch (e: Exception) {
Timber.tag("FETCH_CONTRIBUTORS").d(e)
null
}
} catch (e: Exception) {
currentPage = getCurrentPage(contributors, currentPage)
currentPage?.let { addContributors(contributors) }
}
}

private fun getCurrentPage(
rodrigoliveirac marked this conversation as resolved.
Show resolved Hide resolved
contributors: List<ContributorDto>?,
currentPage: Int
): Int? {
return if (contributors?.isEmpty() == true) {
rodrigoliveirac marked this conversation as resolved.
Show resolved Hide resolved
null
} else {
currentPage + 1
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class ContributorsViewModelTest : ComposeViewModelTest() {
@Test
fun `unhappy path, both error`() {
// given
coEvery { repoDataSource.fetchContributors() } returns null
coEvery { repoDataSource.fetchContributors() } returns emptyList()
coEvery { repoDataSource.fetchRepositoryInfo() } returns null

viewModel.runTest {
Expand Down
Loading