load issue template!

This commit is contained in:
k0shk0sh 2019-07-30 20:32:47 +02:00
parent 23d2efd1db
commit f62d6d8cde
6 changed files with 117 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import com.fastaccess.github.ui.modules.auth.login.LoginViewModel
import com.fastaccess.github.ui.modules.editor.dialog.UploadPictureViewModel
import com.fastaccess.github.ui.modules.feed.fragment.viewmodel.FeedsViewModel
import com.fastaccess.github.ui.modules.issue.fragment.viewmodel.IssueTimelineViewModel
import com.fastaccess.github.ui.modules.issuesprs.edit.EditIssuePrViewModel
import com.fastaccess.github.ui.modules.issuesprs.edit.assignees.viewmodel.AssigneesViewModel
import com.fastaccess.github.ui.modules.issuesprs.edit.labels.viewmodel.LabelsViewModel
import com.fastaccess.github.ui.modules.issuesprs.edit.milestone.viewmodel.MilestoneViewModel
@ -100,4 +101,7 @@ abstract class ViewModelModule {
@Binds @IntoMap @ViewModelKey(UploadPictureViewModel::class)
abstract fun bindUploadPictureViewModel(viewModel: UploadPictureViewModel): ViewModel
@Binds @IntoMap @ViewModelKey(EditIssuePrViewModel::class)
abstract fun bindEditIssuePrViewModel(viewModel: EditIssuePrViewModel): ViewModel
}

View File

@ -7,10 +7,13 @@ import android.view.MotionEvent
import android.view.View
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelProviders
import com.fastaccess.data.model.parcelable.EditIssuePrBundleModel
import com.fastaccess.github.R
import com.fastaccess.github.base.BaseFragment
import com.fastaccess.github.base.BaseViewModel
import com.fastaccess.github.extensions.observeNotNull
import com.fastaccess.github.extensions.routeForResult
import com.fastaccess.github.utils.EDITOR_DEEPLINK
import com.fastaccess.github.utils.EXTRA
@ -24,8 +27,11 @@ import javax.inject.Inject
* Created by Kosh on 2019-07-27.
*/
class EditIssuePrFragment : BaseFragment() {
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory
@Inject lateinit var markwon: Markwon
private val viewModel by lazy { ViewModelProviders.of(this, viewModelFactory).get(EditIssuePrViewModel::class.java) }
private val model by lazy {
arguments?.getParcelable(EXTRA) as? EditIssuePrBundleModel ?: throw NullPointerException("EditIssuePrBundleModel is null")
}
@ -53,6 +59,12 @@ class EditIssuePrFragment : BaseFragment() {
val description = model.description
if (!description.isNullOrEmpty()) {
descriptionEditText.post { markwon.setMarkdown(descriptionEditText, description) }
} else {
viewModel.templateLiveData.observeNotNull(this) {
model.description = it
descriptionEditText.post { markwon.setMarkdown(descriptionEditText, it) }
}
viewModel.loadTemplate(model.login, model.repo)
}
}
descriptionEditText.setOnTouchListener { _, event ->

View File

@ -0,0 +1,34 @@
package com.fastaccess.github.ui.modules.issuesprs.edit
import androidx.lifecycle.MutableLiveData
import com.fastaccess.github.base.BaseViewModel
import com.fastaccess.github.usecase.files.GetFileContentUseCase
import timber.log.Timber
import javax.inject.Inject
/**
* Created by Kosh on 2019-07-30.
*/
class EditIssuePrViewModel @Inject constructor(
private val getFileContentUseCase: GetFileContentUseCase
) : BaseViewModel() {
val templateLiveData = MutableLiveData<String>()
fun loadTemplate(
login: String,
repo: String
) {
getFileContentUseCase.login = login
getFileContentUseCase.repo = repo
getFileContentUseCase.path = "master:.github/ISSUE_TEMPLATE.md"
justSubscribe(getFileContentUseCase.buildObservable()
.map {
it.text?.replace(Regex("(?s)<!--.*?-->"), "")?.replace("<>", "") ?: "" // replace all comments! keep the text small!
}
.doOnNext {
Timber.e(it)
templateLiveData.postValue(it)
})
}
}

View File

@ -0,0 +1,40 @@
package com.fastaccess.github.usecase.files
import com.apollographql.apollo.ApolloClient
import com.apollographql.apollo.rx2.Rx2Apollo
import com.fastaccess.data.model.FileConentModel
import com.fastaccess.data.repository.SchedulerProvider
import com.fastaccess.domain.usecase.base.BaseObservableUseCase
import github.GetFileContentQuery
import io.reactivex.Observable
import javax.inject.Inject
/**
* Created by Kosh on 2019-07-30.
*/
class GetFileContentUseCase @Inject constructor(
private val apolloClient: ApolloClient,
private val schedulerProvider: SchedulerProvider
) : BaseObservableUseCase() {
var repo: String = ""
var login: String = ""
var path: String = ""
override fun buildObservable(): Observable<FileConentModel> = Rx2Apollo.from(
apolloClient.query(
GetFileContentQuery.builder()
.login(login)
.repo(repo)
.path(path)
.build()
)
)
.subscribeOn(schedulerProvider.ioThread())
.observeOn(schedulerProvider.uiThread())
.map {
val response = it.data()?.repositoryOwner?.repository?.`object` as? GetFileContentQuery.AsBlob
return@map FileConentModel(response?.text, response?.isBinary, response?.isTruncated, response?.byteSize)
}
}

View File

@ -0,0 +1,14 @@
query getFileContent($login: String!, $repo: String!, $path: String!) {
repositoryOwner(login: $login) {
repository(name: $repo) {
object(expression: $path) {
... on Blob {
isBinary
byteSize
isTruncated
text
}
}
}
}
}

View File

@ -0,0 +1,13 @@
package com.fastaccess.data.model
import com.google.gson.annotations.SerializedName
/**
* Created by Kosh on 2019-07-30.
*/
data class FileConentModel(
@SerializedName("text") var text: String? = null,
@SerializedName("isBinary") var isBinary: Boolean? = null,
@SerializedName("isTruncated") var isTruncated: Boolean? = null,
@SerializedName("byteSize") var byteSize: Int? = null
)