Commit f509571f authored by Eugene Shapovalov's avatar Eugene Shapovalov 💬

Refactored code according to comments.

parent cd0e6d2c
......@@ -8,7 +8,7 @@ import com.google.gson.annotations.SerializedName
data class RepositoryTreeNode(
@SerializedName("id") val id: String,
@SerializedName("name") val name: String,
@SerializedName("type") val type: String,
@SerializedName("type") val type: RepositoryTreeNodeType,
@SerializedName("path") val path: String,
@SerializedName("mode") val mode: String
)
\ No newline at end of file
package ru.terrakok.gitlabclient.entity
import com.google.gson.annotations.SerializedName
/**
* Created by Eugene Shapovalov (@CraggyHaggy) on 08.02.18.
*/
enum class RepositoryTreeNodeType(private val jsonName: String) {
@SerializedName("tree") TREE("tree"),
@SerializedName("blob") BLOB("blob");
override fun toString() = jsonName
}
\ No newline at end of file
......@@ -15,6 +15,10 @@ class ProjectInteractor @Inject constructor(
private val base64Tools: Base64Tools
) {
companion object {
private val README_FILE_NAME = "readme.md"
}
fun getMainProjects(page: Int) = projectRepository
.getProjectsList(
page = page,
......@@ -43,11 +47,10 @@ class ProjectInteractor @Inject constructor(
fun getProjectReadme(id: Long, branchName: String) =
projectRepository.getRepositoryTree(projectId = id, branchName = branchName)
.flattenAsObservable { treeNodes -> treeNodes }
.filter({ repositoryTreeNode ->
repositoryTreeNode.name.toLowerCase().contains("readme.md")
})
.firstOrError()
.map { treeNodes ->
treeNodes.find { it.name.contains(README_FILE_NAME, true) }
?: throw NoSuchElementException("$README_FILE_NAME not found!")
}
.flatMap { treeNode ->
projectRepository.getFile(id, treeNode.name, branchName)
.observeOn(schedulers.computation())
......
package ru.terrakok.gitlabclient.presentation.project
import com.arellomobile.mvp.InjectViewState
import io.reactivex.Single
import ru.terrakok.cicerone.Router
import ru.terrakok.gitlabclient.model.interactor.project.ProjectInteractor
import ru.terrakok.gitlabclient.presentation.global.BasePresenter
......@@ -32,7 +33,12 @@ class ProjectInfoPresenter @Inject constructor(
.flatMap { project ->
projectInteractor
.getProjectReadme(project.id, project.defaultBranch)
.onErrorReturn { "" }
.onErrorResumeNext { throwable ->
when (throwable) {
is NoSuchElementException -> Single.just("")
else -> Single.error(throwable)
}
}
.flatMap { mdConverter.markdownToSpannable(it) }
.map { mdReadme -> Pair(project, mdReadme) }
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment