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

Implement progress for code highlight.

parent 9374390d
......@@ -43,7 +43,7 @@ class ProjectInteractor @Inject constructor(
fun getProject(id: Long) = projectRepository.getProject(id)
fun getProjectFileRawCode(projectId: Long, path: String, fileReference: String): Single<String> =
fun getProjectRawFile(projectId: Long, path: String, fileReference: String): Single<String> =
projectRepository.getProjectFile(projectId, path, fileReference)
.observeOn(schedulers.computation())
.map { file -> base64Tools.decode(file.content) }
......
......@@ -29,16 +29,26 @@ class ProjectFilePresenter @Inject constructor(
override fun onFirstViewAttach() {
super.onFirstViewAttach()
viewState.setTitle(filePath.extractFileNameFromPath())
projectInteractor.getProjectFileRawCode(projectId, filePath, fileReference)
.doOnSubscribe { viewState.showEmptyProgress(true) }
.doAfterTerminate { viewState.showEmptyProgress(false) }
// Progress will be hidden, after code will be highlighted.
viewState.showEmptyProgress(true)
projectInteractor.getProjectRawFile(projectId, filePath, fileReference)
.subscribe(
{ viewState.setRawCode(it) },
{ viewState.setRawFile(it) },
{ errorHandler.proceed(it, { viewState.showMessage(it) }) }
)
.connect()
}
fun onCodeHighlightStarted() {
viewState.showEmptyProgress(true)
}
fun onCodeHighlightSFinished() {
viewState.showEmptyProgress(false)
}
fun onBackPressed() = router.exit()
}
\ No newline at end of file
......@@ -12,7 +12,7 @@ import com.arellomobile.mvp.viewstate.strategy.StateStrategyType
interface ProjectFileView : MvpView {
fun setTitle(title: String)
fun setRawCode(code: String)
fun setRawFile(rawFile: String)
fun showEmptyProgress(show: Boolean)
@StateStrategyType(OneExecutionStateStrategy::class)
......
......@@ -16,6 +16,7 @@ import ru.terrakok.gitlabclient.toothpick.qualifier.FilePath
import ru.terrakok.gitlabclient.toothpick.qualifier.FileReference
import ru.terrakok.gitlabclient.toothpick.qualifier.ProjectId
import ru.terrakok.gitlabclient.ui.global.BaseFragment
import ru.terrakok.gitlabclient.ui.global.view.custom.code_highlight.CodeHighlightView
import toothpick.Scope
import toothpick.config.Module
......@@ -59,6 +60,32 @@ class ProjectFileFragment : BaseFragment(), ProjectFileView {
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
toolbar.setNavigationOnClickListener { onBackPressed() }
projectFileCodeHighlightView.setOnCodeHighlightProgressLister(
object : CodeHighlightView.OnCodeHighlightListener {
override fun onCodeHighlightStarted() {
presenter.onCodeHighlightStarted()
}
override fun onCodeHighlightFinished() {
presenter.onCodeHighlightSFinished()
}
}
)
}
override fun onResume() {
super.onResume()
projectFileCodeHighlightView.onResume()
}
override fun onPause() {
super.onPause()
projectFileCodeHighlightView.onPause()
}
override fun onDestroyView() {
projectFileCodeHighlightView.destroy()
super.onDestroyView()
}
override fun onBackPressed() {
......@@ -70,8 +97,8 @@ class ProjectFileFragment : BaseFragment(), ProjectFileView {
toolbar.title = title
}
override fun setRawCode(code: String) {
projectFileCodeHighlightView.highlightRawCode(code)
override fun setRawFile(rawFile: String) {
projectFileCodeHighlightView.highlightCode(rawFile)
}
override fun showEmptyProgress(show: Boolean) {
......
......@@ -6,20 +6,14 @@ object CodeHighlightJsPage {
"<!DOCTYPE html>\n" +
"<html>\n" +
"<head>\n" +
" <meta charset=\"utf-8\">\n" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0\">\n" +
" <style type=\"text/css\">\n" +
" html, body {\n" +
" width:100%;\n" +
" height: 100%;\n" +
" margin: 0px;\n" +
" padding: 0px;\n" +
" }\n" +
" <meta charset=\"utf-8\">\n" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
" <style type=\"text/css\">\n" +
" pre {\n" +
" margin: 0;\n" +
" }\n" +
" </style>\n" +
" <link rel=\"stylesheet\" href=\"./styles/$style\">\n" +
" <link rel=\"stylesheet\" href=\"./styles/$style\"/>\n" +
" <style type=\"text/css\">\n" +
" .hljs-line-numbers {\n" +
" \ttext-align: right;\n" +
......@@ -32,11 +26,11 @@ object CodeHighlightJsPage {
" \t-ms-user-select: none;\n" +
" \tuser-select: none; " +
" }\n" +
" </style>\n" +
" <script src=\"./highlight.pack.js\"></script>\n" +
" <script src=\"./highlightjs-line-numbers.min.js\"></script>\n" +
" <script>hljs.initHighlightingOnLoad();</script>\n" +
" <script>hljs.initLineNumbersOnLoad();</script>\n" +
" </style>\n" +
" <script src=\"./highlight.pack.js\"></script>\n" +
" <script src=\"./highlightjs-line-numbers.min.js\"></script>\n" +
" <script>hljs.initHighlightingOnLoad();</script>\n" +
" <script>hljs.initLineNumbersOnLoad();</script>\n" +
"</head>\n" +
"<body style=\"margin: 0; padding: 0\" class=\"hljs\">\n" +
"<pre><code>${rawCode.replace("<".toRegex(), "&lt;").replace(">".toRegex(), "&gt;")}</code></pre>\n" +
......
......@@ -4,6 +4,7 @@ import android.content.Context
import android.graphics.Bitmap
import android.util.AttributeSet
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient
......@@ -13,45 +14,59 @@ class CodeHighlightView @JvmOverloads constructor(
defStyleAttr: Int = 0
) : WebView(context, attrs, defStyleAttr) {
// TODO: project file details (Implement the next steps):
// 1) Progress logic - highlighting code is rather long operation
// 2) Lifecycle logic
// 3) Cache content logic - ?
// 4) Style logic - support two styles (default, darkula).
private var onCodeHighlightListener: OnCodeHighlightListener? = null
fun setOnCodeHighlightProgressLister(onCodeHighlightListener: OnCodeHighlightListener) {
this.onCodeHighlightListener = onCodeHighlightListener
}
init {
loadUrl(EMPTY_PAGE)
getSettings().apply {
settings.javaScriptEnabled = true
settings.builtInZoomControls = true
settings.setSupportZoom(true)
settings.displayZoomControls = false
settings.apply {
javaScriptEnabled = true
scrollBarStyle = View.SCROLLBARS_INSIDE_OVERLAY
setSupportZoom(true)
builtInZoomControls = true
displayZoomControls = false
}
webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
if (url != EMPTY_PAGE) {
// TODO: code highlight (Hide progress).
onCodeHighlightListener?.onCodeHighlightStarted()
}
}
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
super.onPageStarted(view, url, favicon)
override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url)
if (url != EMPTY_PAGE) {
// TODO: code highlight (Show progress).
onCodeHighlightListener?.onCodeHighlightFinished()
}
}
}
webChromeClient = object : WebChromeClient() {
override fun onProgressChanged(view: WebView, newProgress: Int) {
super.onProgressChanged(view, newProgress)
if (view.url != EMPTY_PAGE && newProgress == PROGRESS_PAGE_LOADED) {
onCodeHighlightListener?.onCodeHighlightFinished()
}
}
}
}
fun highlightRawCode(code: String) {
val jsPage = CodeHighlightJsPage.generatePage(code, "default.css")
fun highlightCode(rawFile: String) {
val jsPage = CodeHighlightJsPage.generatePage(rawFile, "default.css")
loadDataWithBaseURL("file:///android_asset/code_highlight/", jsPage, "text/html", "utf-8", null)
}
companion object {
private const val EMPTY_PAGE = "about:blank"
private const val PROGRESS_PAGE_LOADED = 100
}
interface OnCodeHighlightListener {
fun onCodeHighlightStarted()
fun onCodeHighlightFinished()
}
}
\ No newline at end of file
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