ProjectInteractor.kt 2.42 KB
Newer Older
1 2
package ru.terrakok.gitlabclient.model.interactor.project

3
import io.reactivex.Single
4
import ru.terrakok.gitlabclient.entity.OrderBy
5
import ru.terrakok.gitlabclient.entity.Project
6
import ru.terrakok.gitlabclient.model.repository.project.ProjectRepository
7
import ru.terrakok.gitlabclient.model.repository.tools.Base64Tools
8
import ru.terrakok.gitlabclient.model.system.SchedulersProvider
9
import javax.inject.Inject
10 11 12 13

/**
 * @author Konstantin Tskhovrebov (aka terrakok) on 26.04.17.
 */
14
class ProjectInteractor @Inject constructor(
15 16 17
    private val projectRepository: ProjectRepository,
    private val schedulers: SchedulersProvider,
    private val base64Tools: Base64Tools
18
) {
19

20
    fun getMainProjects(page: Int) = projectRepository
21 22 23 24 25 26
        .getProjectsList(
            page = page,
            membership = true,
            orderBy = OrderBy.LAST_ACTIVITY_AT,
            archived = false
        )
27 28

    fun getMyProjects(page: Int) = projectRepository
29 30 31 32 33 34
        .getProjectsList(
            page = page,
            owned = true,
            orderBy = OrderBy.LAST_ACTIVITY_AT,
            archived = false
        )
35 36

    fun getStarredProjects(page: Int) = projectRepository
37 38 39 40 41 42
        .getProjectsList(
            page = page,
            starred = true,
            orderBy = OrderBy.LAST_ACTIVITY_AT,
            archived = false
        )
43

44 45
    fun getProject(id: Long) = projectRepository.getProject(id)

46 47 48
    fun getProjectReadme(project: Project) =
        Single
            .defer {
49
                if (project.defaultBranch != null && project.readmeUrl != null) {
50 51 52
                    val readmePath = project.readmeUrl.substringAfter(
                        "${project.webUrl}/blob/${project.defaultBranch}/"
                    )
53
                    projectRepository.getBlobFile(project.id, readmePath, project.defaultBranch)
54 55 56
                } else {
                    Single.error(ReadmeNotFound())
                }
57
            }
58 59 60 61
            .observeOn(schedulers.computation())
            .map { file -> base64Tools.decode(file.content) }
            .observeOn(schedulers.ui())

62
    fun getProjectFiles(
63 64 65 66
        projectId: Long,
        path: String,
        branchName: String,
        page: Int
67
    ) = projectRepository.getProjectFiles(projectId = projectId, path = path, branchName = branchName, page = page)
68

69 70 71 72
    fun getProjectBranches(
        projectId: Long
    ) = projectRepository.getProjectBranches(projectId)

73
    class ReadmeNotFound : Exception()
74
}