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 ...@@ -8,7 +8,7 @@ import com.google.gson.annotations.SerializedName
data class RepositoryTreeNode( data class RepositoryTreeNode(
@SerializedName("id") val id: String, @SerializedName("id") val id: String,
@SerializedName("name") val name: String, @SerializedName("name") val name: String,
@SerializedName("type") val type: String, @SerializedName("type") val type: RepositoryTreeNodeType,
@SerializedName("path") val path: String, @SerializedName("path") val path: String,
@SerializedName("mode") val mode: 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( ...@@ -15,6 +15,10 @@ class ProjectInteractor @Inject constructor(
private val base64Tools: Base64Tools private val base64Tools: Base64Tools
) { ) {
companion object {
private val README_FILE_NAME = "readme.md"
}
fun getMainProjects(page: Int) = projectRepository fun getMainProjects(page: Int) = projectRepository
.getProjectsList( .getProjectsList(
page = page, page = page,
...@@ -43,11 +47,10 @@ class ProjectInteractor @Inject constructor( ...@@ -43,11 +47,10 @@ class ProjectInteractor @Inject constructor(
fun getProjectReadme(id: Long, branchName: String) = fun getProjectReadme(id: Long, branchName: String) =
projectRepository.getRepositoryTree(projectId = id, branchName = branchName) projectRepository.getRepositoryTree(projectId = id, branchName = branchName)
.flattenAsObservable { treeNodes -> treeNodes } .map { treeNodes ->
.filter({ repositoryTreeNode -> treeNodes.find { it.name.contains(README_FILE_NAME, true) }
repositoryTreeNode.name.toLowerCase().contains("readme.md") ?: throw NoSuchElementException("$README_FILE_NAME not found!")
}) }
.firstOrError()
.flatMap { treeNode -> .flatMap { treeNode ->
projectRepository.getFile(id, treeNode.name, branchName) projectRepository.getFile(id, treeNode.name, branchName)
.observeOn(schedulers.computation()) .observeOn(schedulers.computation())
......
package ru.terrakok.gitlabclient.presentation.project package ru.terrakok.gitlabclient.presentation.project
import com.arellomobile.mvp.InjectViewState import com.arellomobile.mvp.InjectViewState
import io.reactivex.Single
import ru.terrakok.cicerone.Router import ru.terrakok.cicerone.Router
import ru.terrakok.gitlabclient.model.interactor.project.ProjectInteractor import ru.terrakok.gitlabclient.model.interactor.project.ProjectInteractor
import ru.terrakok.gitlabclient.presentation.global.BasePresenter import ru.terrakok.gitlabclient.presentation.global.BasePresenter
...@@ -32,7 +33,12 @@ class ProjectInfoPresenter @Inject constructor( ...@@ -32,7 +33,12 @@ class ProjectInfoPresenter @Inject constructor(
.flatMap { project -> .flatMap { project ->
projectInteractor projectInteractor
.getProjectReadme(project.id, project.defaultBranch) .getProjectReadme(project.id, project.defaultBranch)
.onErrorReturn { "" } .onErrorResumeNext { throwable ->
when (throwable) {
is NoSuchElementException -> Single.just("")
else -> Single.error(throwable)
}
}
.flatMap { mdConverter.markdownToSpannable(it) } .flatMap { mdConverter.markdownToSpannable(it) }
.map { mdReadme -> Pair(project, mdReadme) } .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