Skip to content

Commit

Permalink
Force Syntax Highlighting #68
Browse files Browse the repository at this point in the history
+ Fix glitch when switching between tabs
  • Loading branch information
massivemadness committed Feb 15, 2023
1 parent 800691c commit 7d79437
Show file tree
Hide file tree
Showing 27 changed files with 264 additions and 68 deletions.
9 changes: 9 additions & 0 deletions common-ui/src/main/res/drawable-anydpi/ic_marker.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#000000"
android:pathData="M18.5,1.15C17.97,1.15 17.46,1.34 17.07,1.73L11.26,7.55L16.91,13.2L22.73,7.39C23.5,6.61 23.5,5.35 22.73,4.56L19.89,1.73C19.5,1.34 19,1.15 18.5,1.15M10.3,8.5L4.34,14.46C3.56,15.24 3.56,16.5 4.36,17.31C3.14,18.54 1.9,19.77 0.67,21H6.33L7.19,20.14C7.97,20.9 9.22,20.89 10,20.12L15.95,14.16" />
</vector>
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ abstract class SyntaxHighlightEditText @JvmOverloads constructor(
syntaxHighlight()
}

open fun onLanguageChanged() = Unit
open fun onLanguageChanged() {
syntaxHighlight()
}

open fun onColorSchemeChanged() {
findResultStyleSpan = StyleSpan(color = colorScheme.findResultBackgroundColor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.blacksquircle.ui.feature.editor.data.utils
package com.blacksquircle.ui.feature.editor.ui.adapter

import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.blacksquircle.ui.feature.editor.data.utils
package com.blacksquircle.ui.feature.editor.ui.customview

enum class Panel {
DEFAULT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.blacksquircle.ui.feature.editor.data.utils
package com.blacksquircle.ui.feature.editor.ui.customview

import android.content.res.Configuration
import android.view.Menu
Expand Down Expand Up @@ -104,7 +104,7 @@ class ToolbarManager(
}

// Tools Menu
// R.id.action_error_checking -> listener.onErrorCheckingButton()
R.id.action_force_syntax -> listener.onForceSyntaxButton()
R.id.action_insert_color -> listener.onInsertColorButton()

// Overflow Menu
Expand Down Expand Up @@ -239,7 +239,7 @@ class ToolbarManager(
fun onPreviousResultButton()
fun onFindParamsChanged(params: FindParams)

// fun onErrorCheckingButton()
fun onForceSyntaxButton()
fun onInsertColorButton()

fun onUndoButton(): Boolean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2023 Squircle CE contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.blacksquircle.ui.feature.editor.ui.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.os.Bundle
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.activityViewModels
import androidx.navigation.fragment.navArgs
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.list.listItemsSingleChoice
import com.blacksquircle.ui.feature.editor.R
import com.blacksquircle.ui.feature.editor.ui.viewmodel.EditorIntent
import com.blacksquircle.ui.feature.editor.ui.viewmodel.EditorViewModel
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class ForceSyntaxDialog : DialogFragment() {

private val viewModel by activityViewModels<EditorViewModel>()
private val navArgs by navArgs<ForceSyntaxDialogArgs>()

@SuppressLint("CheckResult")
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return MaterialDialog(requireContext()).show {
title(R.string.dialog_title_force_syntax)

val languages = resources.getStringArray(R.array.language_name)
listItemsSingleChoice(
res = R.array.language_title,
initialSelection = languages.indexOf(navArgs.languageName),
waitForPositiveButton = false,
) { _, index, _ ->
val intent = EditorIntent.ForceSyntaxHighlighting(languages[index])
viewModel.obtainEvent(intent)
dismiss()
}
negativeButton(android.R.string.cancel)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ import com.blacksquircle.ui.editorkit.plugin.textscroller.textScroller
import com.blacksquircle.ui.editorkit.widget.TextScroller
import com.blacksquircle.ui.editorkit.widget.internal.UndoRedoEditText
import com.blacksquircle.ui.feature.editor.R
import com.blacksquircle.ui.feature.editor.data.utils.Panel
import com.blacksquircle.ui.feature.editor.data.utils.SettingsEvent
import com.blacksquircle.ui.feature.editor.data.utils.TabController
import com.blacksquircle.ui.feature.editor.data.utils.ToolbarManager
import com.blacksquircle.ui.feature.editor.databinding.FragmentEditorBinding
import com.blacksquircle.ui.feature.editor.ui.adapter.AutoCompleteAdapter
import com.blacksquircle.ui.feature.editor.ui.adapter.DocumentAdapter
import com.blacksquircle.ui.feature.editor.ui.adapter.TabController
import com.blacksquircle.ui.feature.editor.ui.customview.Panel
import com.blacksquircle.ui.feature.editor.ui.customview.ToolbarManager
import com.blacksquircle.ui.feature.editor.ui.viewmodel.EditorIntent
import com.blacksquircle.ui.feature.editor.ui.viewmodel.EditorViewEvent
import com.blacksquircle.ui.feature.editor.ui.viewmodel.EditorViewModel
Expand Down Expand Up @@ -93,7 +93,7 @@ class EditorFragment : Fragment(R.layout.fragment_editor),
}

private val onTabSelectedListener = object : TabAdapter.OnTabSelectedListener {
override fun onTabUnselected(position: Int) = saveFile()
override fun onTabUnselected(position: Int) = saveFile(local = false, unselected = true)
override fun onTabSelected(position: Int) {
viewModel.obtainEvent(EditorIntent.SelectTab(position))
}
Expand Down Expand Up @@ -157,7 +157,7 @@ class EditorFragment : Fragment(R.layout.fragment_editor),

override fun onPause() {
super.onPause()
saveFile()
saveFile(local = false, unselected = false)
}

override fun handleOnBackPressed(): Boolean {
Expand All @@ -178,6 +178,7 @@ class EditorFragment : Fragment(R.layout.fragment_editor),
tabAdapter.submitList(state.documents, state.position)
tabAdapter.setOnTabSelectedListener(onTabSelectedListener)
tabAdapter.setOnTabMovedListener(onTabMovedListener)
binding.editor.language = state.documents[state.position].language
toolbarManager.panel = state.panel
if (state.panel == Panel.DEFAULT) {
binding.editor.clearFindResultSpans()
Expand All @@ -203,7 +204,6 @@ class EditorFragment : Fragment(R.layout.fragment_editor),
binding.loadingBar.isVisible = false

binding.scroller.state = TextScroller.State.HIDDEN
binding.editor.language = state.content.documentModel.language
binding.editor.undoStack = state.content.undoStack
binding.editor.redoStack = state.content.redoStack
binding.editor.setTextContent(measurement)
Expand Down Expand Up @@ -275,7 +275,7 @@ class EditorFragment : Fragment(R.layout.fragment_editor),
}

override fun onSaveButton(): Boolean {
saveFile(local = true)
saveFile(local = true, unselected = false)
return true
}

Expand Down Expand Up @@ -316,7 +316,7 @@ class EditorFragment : Fragment(R.layout.fragment_editor),

override fun onPasteButton(): Boolean {
val position = tabAdapter.selectedPosition
if (binding.editor.hasPrimaryClip() && position > -1) {
if (position > -1 && binding.editor.hasPrimaryClip()) {
binding.editor.paste()
} else {
context?.showToast(R.string.message_nothing_to_paste)
Expand Down Expand Up @@ -388,6 +388,10 @@ class EditorFragment : Fragment(R.layout.fragment_editor),
binding.editor.find(params)
}

override fun onForceSyntaxButton() {
viewModel.obtainEvent(EditorIntent.ForceSyntax)
}

override fun onInsertColorButton() {
viewModel.obtainEvent(EditorIntent.ColorPicker)
}
Expand All @@ -413,10 +417,11 @@ class EditorFragment : Fragment(R.layout.fragment_editor),

// endregion TOOLBAR

private fun saveFile(local: Boolean = false) {
private fun saveFile(local: Boolean, unselected: Boolean) {
if (!binding.editor.isVisible) return
val action = EditorIntent.SaveFile(
local = local,
unselected = unselected,
text = binding.editor.text.toString(),
undoStack = binding.editor.undoStack.clone(),
redoStack = binding.editor.redoStack.clone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import com.blacksquircle.ui.core.ui.navigation.Screen

sealed class EditorScreen(route: String) : Screen<String>(route) {

class ForceSyntaxDialog(languageName: String) : EditorScreen(
route = "blacksquircle://editor/syntax?languageName=$languageName",
)
class CloseModifiedDialog(position: Int, fileName: String) : EditorScreen(
route = "blacksquircle://editor/close?position=$position&fileName=$fileName",
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ sealed class EditorIntent {
object ColorPicker : EditorIntent()
data class InsertColor(val color: Int) : EditorIntent()

object ModifyContent : EditorIntent()
object ForceSyntax : EditorIntent()
data class ForceSyntaxHighlighting(val languageName: String) : EditorIntent()

data class SaveFile(
val local: Boolean,
val unselected: Boolean,
val text: String,
val undoStack: UndoStack,
val redoStack: UndoStack,
Expand All @@ -50,6 +53,7 @@ sealed class EditorIntent {
val selectionEnd: Int,
) : EditorIntent()
data class SaveFileAs(val fileUri: Uri) : EditorIntent()
object ModifyContent : EditorIntent()

object PanelDefault : EditorIntent()
object PanelFind : EditorIntent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ package com.blacksquircle.ui.feature.editor.ui.viewmodel

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.blacksquircle.ui.core.data.factory.LanguageFactory
import com.blacksquircle.ui.core.data.storage.keyvalue.SettingsManager
import com.blacksquircle.ui.core.domain.resources.StringProvider
import com.blacksquircle.ui.core.ui.extensions.*
import com.blacksquircle.ui.core.ui.viewstate.ViewEvent
import com.blacksquircle.ui.feature.editor.R
import com.blacksquircle.ui.feature.editor.data.converter.DocumentConverter
import com.blacksquircle.ui.feature.editor.data.utils.Panel
import com.blacksquircle.ui.feature.editor.data.utils.SettingsEvent
import com.blacksquircle.ui.feature.editor.domain.model.DocumentContent
import com.blacksquircle.ui.feature.editor.domain.model.DocumentModel
import com.blacksquircle.ui.feature.editor.domain.model.DocumentParams
import com.blacksquircle.ui.feature.editor.domain.repository.DocumentRepository
import com.blacksquircle.ui.feature.editor.ui.customview.Panel
import com.blacksquircle.ui.feature.editor.ui.navigation.EditorScreen
import com.blacksquircle.ui.feature.editor.ui.viewstate.EditorViewState
import com.blacksquircle.ui.feature.editor.ui.viewstate.ToolbarViewState
Expand Down Expand Up @@ -90,9 +91,12 @@ class EditorViewModel @Inject constructor(
is EditorIntent.ColorPicker -> colorPicker()
is EditorIntent.InsertColor -> insertColor(event)

is EditorIntent.ModifyContent -> modifyContent()
is EditorIntent.ForceSyntax -> forceSyntax()
is EditorIntent.ForceSyntaxHighlighting -> forceSyntaxHighlighting(event)

is EditorIntent.SaveFile -> saveFile(event)
is EditorIntent.SaveFileAs -> saveFileAs(event)
is EditorIntent.ModifyContent -> modifyContent()

is EditorIntent.PanelDefault -> panelDefault()
is EditorIntent.PanelFind -> panelFind()
Expand Down Expand Up @@ -152,11 +156,12 @@ class EditorViewModel @Inject constructor(
currentJob?.cancel()
currentJob = viewModelScope.launch {
try {
_editorViewState.value = EditorViewState.Loading

val document = documents[event.position]
settingsManager.selectedUuid = document.uuid
refreshActionBar(event.position)

_editorViewState.value = EditorViewState.Loading
_editorViewState.value = EditorViewState.Content(
content = documentRepository.loadFile(document),
showKeyboard = settingsManager.extendedKeyboard,
Expand Down Expand Up @@ -328,16 +333,6 @@ class EditorViewModel @Inject constructor(
}
}

private fun modifyContent() {
if (selectedPosition > -1) {
val document = documents[selectedPosition]
if (!document.modified) {
documents[selectedPosition] = document.copy(modified = true)
refreshActionBar(selectedPosition)
}
}
}

private fun saveFile(event: EditorIntent.SaveFile) {
viewModelScope.launch {
try {
Expand Down Expand Up @@ -365,7 +360,7 @@ class EditorViewModel @Inject constructor(
text = event.text,
)
val params = DocumentParams(localStorage, true)
if (!localStorage) {
if (!localStorage && !event.unselected) {
_editorViewState.value = currentState.copy(content = content)
}
documentRepository.saveFile(content, params)
Expand Down Expand Up @@ -401,6 +396,49 @@ class EditorViewModel @Inject constructor(
}
}

private fun forceSyntax() {
viewModelScope.launch {
try {
if (selectedPosition > -1) {
val document = documents[selectedPosition]
val screen = EditorScreen.ForceSyntaxDialog(document.language.languageName)
_viewEvent.send(ViewEvent.Navigation(screen))
}
} catch (e: Exception) {
Timber.e(e, e.message)
_viewEvent.send(ViewEvent.Toast(e.message.orEmpty()))
}
}
}

private fun forceSyntaxHighlighting(event: EditorIntent.ForceSyntaxHighlighting) {
viewModelScope.launch {
try {
if (selectedPosition > -1) {
val document = documents[selectedPosition]
documents[selectedPosition] = document.copy(
language = LanguageFactory.fromName(event.languageName)
)
documentRepository.updateDocument(document)
refreshActionBar(selectedPosition)
}
} catch (e: Exception) {
Timber.e(e, e.message)
_viewEvent.send(ViewEvent.Toast(e.message.orEmpty()))
}
}
}

private fun modifyContent() {
if (selectedPosition > -1) {
val document = documents[selectedPosition]
if (!document.modified) {
documents[selectedPosition] = document.copy(modified = true)
refreshActionBar(selectedPosition)
}
}
}

private fun panelDefault() {
panel = Panel.DEFAULT
refreshActionBar(selectedPosition)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
package com.blacksquircle.ui.feature.editor.ui.viewstate

import com.blacksquircle.ui.core.ui.viewstate.ViewState
import com.blacksquircle.ui.feature.editor.data.utils.Panel
import com.blacksquircle.ui.feature.editor.domain.model.DocumentModel
import java.util.*
import com.blacksquircle.ui.feature.editor.ui.customview.Panel

sealed class ToolbarViewState : ViewState() {

Expand Down

0 comments on commit 7d79437

Please sign in to comment.