mirror of
https://github.com/anyproto/anytype-kotlin.git
synced 2025-06-10 10:00:44 +09:00
Refact | Holder highlight in separate class (#711)
This commit is contained in:
parent
83fab86bf3
commit
0224b6c8f6
6 changed files with 112 additions and 97 deletions
|
@ -0,0 +1,102 @@
|
|||
package com.agileburo.anytype.core_ui.features.editor.holders
|
||||
|
||||
import android.text.Editable
|
||||
import android.view.View
|
||||
import androidx.core.view.updateLayoutParams
|
||||
import com.agileburo.anytype.core_ui.R
|
||||
import com.agileburo.anytype.core_ui.common.Markup
|
||||
import com.agileburo.anytype.core_ui.common.isLinksPresent
|
||||
import com.agileburo.anytype.core_ui.features.page.BlockView
|
||||
import com.agileburo.anytype.core_ui.features.page.BlockViewHolder
|
||||
import com.agileburo.anytype.core_ui.features.page.ListenerType
|
||||
import com.agileburo.anytype.core_ui.features.page.TextHolder
|
||||
import com.agileburo.anytype.core_ui.menu.ContextMenuType
|
||||
import com.agileburo.anytype.core_ui.tools.DefaultSpannableFactory
|
||||
import com.agileburo.anytype.core_ui.tools.DefaultTextWatcher
|
||||
import com.agileburo.anytype.core_ui.widgets.text.EditorLongClickListener
|
||||
import com.agileburo.anytype.core_ui.widgets.text.TextInputWidget
|
||||
import com.agileburo.anytype.core_utils.ext.dimen
|
||||
import kotlinx.android.synthetic.main.item_block_highlight.view.*
|
||||
|
||||
class Highlight(
|
||||
view: View,
|
||||
onMarkupActionClicked: (Markup.Type, IntRange) -> Unit
|
||||
) : BlockViewHolder(view), TextHolder, BlockViewHolder.IndentableHolder {
|
||||
|
||||
override val content: TextInputWidget = itemView.highlightContent
|
||||
override val root: View = itemView
|
||||
private val indent = itemView.highlightIndent
|
||||
private val container = itemView.highlightBlockContentContainer
|
||||
|
||||
init {
|
||||
content.setSpannableFactory(DefaultSpannableFactory())
|
||||
setup(onMarkupActionClicked, ContextMenuType.HIGHLIGHT)
|
||||
}
|
||||
|
||||
fun bind(
|
||||
item: BlockView.Highlight,
|
||||
onTextChanged: (String, Editable) -> Unit,
|
||||
onFocusChanged: (String, Boolean) -> Unit,
|
||||
onSelectionChanged: (String, IntRange) -> Unit,
|
||||
clicked: (ListenerType) -> Unit
|
||||
) {
|
||||
//indentize(item)
|
||||
|
||||
if (item.mode == BlockView.Mode.READ) {
|
||||
enableReadOnlyMode()
|
||||
setBlockText(text = item.text, markup = item, clicked = clicked)
|
||||
} else {
|
||||
enableEditMode()
|
||||
setLinksClickable(item)
|
||||
setBlockText(text = item.text, markup = item, clicked = clicked)
|
||||
if (item.isFocused) setCursor(item)
|
||||
setFocus(item)
|
||||
with(content) {
|
||||
clearTextWatchers()
|
||||
setOnFocusChangeListener { _, hasFocus ->
|
||||
onFocusChanged(item.id, hasFocus)
|
||||
}
|
||||
addTextChangedListener(
|
||||
DefaultTextWatcher { text ->
|
||||
onTextChanged(item.id, text)
|
||||
}
|
||||
)
|
||||
setOnLongClickListener(
|
||||
EditorLongClickListener(
|
||||
t = item.id,
|
||||
click = { onBlockLongClick(itemView, it, clicked) }
|
||||
)
|
||||
)
|
||||
selectionWatcher = {
|
||||
onSelectionChanged(item.id, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun select(item: BlockView.Selectable) {
|
||||
container.isSelected = item.isSelected
|
||||
}
|
||||
|
||||
override fun indentize(item: BlockView.Indentable) {
|
||||
indent.updateLayoutParams {
|
||||
width = item.indent * dimen(R.dimen.indent)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getMentionImageSizeAndPadding(): Pair<Int, Int> = with(itemView) {
|
||||
Pair(
|
||||
first = resources.getDimensionPixelSize(R.dimen.mention_span_image_size_default),
|
||||
second = resources.getDimensionPixelSize(R.dimen.mention_span_image_padding_default)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be set before @[setBlockText]!
|
||||
*/
|
||||
private fun setLinksClickable(block: BlockView.Highlight) {
|
||||
if (block.marks.isLinksPresent()) {
|
||||
content.setLinksClickable()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -349,7 +349,7 @@ class BlockAdapter(
|
|||
)
|
||||
}
|
||||
HOLDER_HIGHLIGHT -> {
|
||||
BlockViewHolder.Highlight(
|
||||
Highlight(
|
||||
view = inflater.inflate(
|
||||
R.layout.item_block_highlight,
|
||||
parent,
|
||||
|
@ -469,7 +469,7 @@ class BlockAdapter(
|
|||
clicked = onClickListener
|
||||
)
|
||||
}
|
||||
is BlockViewHolder.Highlight -> {
|
||||
is Highlight -> {
|
||||
holder.processChangePayload(
|
||||
payloads = payloads.typeOf(),
|
||||
item = blocks[position],
|
||||
|
@ -788,7 +788,7 @@ class BlockAdapter(
|
|||
clicked = onClickListener
|
||||
)
|
||||
}
|
||||
is BlockViewHolder.Highlight -> {
|
||||
is Highlight -> {
|
||||
holder.bind(
|
||||
item = blocks[position] as BlockView.Highlight,
|
||||
onTextChanged = onTextChanged,
|
||||
|
|
|
@ -2,11 +2,9 @@ package com.agileburo.anytype.core_ui.features.page
|
|||
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.net.Uri
|
||||
import android.text.Editable
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.FrameLayout
|
||||
import androidx.core.view.updateLayoutParams
|
||||
import androidx.core.view.updatePadding
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.agileburo.anytype.core_ui.BuildConfig
|
||||
|
@ -17,8 +15,6 @@ import com.agileburo.anytype.core_ui.extensions.color
|
|||
import com.agileburo.anytype.core_ui.features.page.BlockViewDiffUtil.Companion.SELECTION_CHANGED
|
||||
import com.agileburo.anytype.core_ui.features.page.BlockViewDiffUtil.Payload
|
||||
import com.agileburo.anytype.core_ui.menu.ContextMenuType
|
||||
import com.agileburo.anytype.core_ui.tools.DefaultSpannableFactory
|
||||
import com.agileburo.anytype.core_ui.tools.DefaultTextWatcher
|
||||
import com.agileburo.anytype.core_ui.widgets.text.EditorLongClickListener
|
||||
import com.agileburo.anytype.core_ui.widgets.text.TextInputWidget
|
||||
import com.agileburo.anytype.core_utils.const.MimeTypes
|
||||
|
@ -35,7 +31,6 @@ import com.google.android.exoplayer2.util.Util
|
|||
import kotlinx.android.synthetic.main.item_block_bookmark.view.*
|
||||
import kotlinx.android.synthetic.main.item_block_bookmark_error.view.*
|
||||
import kotlinx.android.synthetic.main.item_block_file.view.*
|
||||
import kotlinx.android.synthetic.main.item_block_highlight.view.*
|
||||
import kotlinx.android.synthetic.main.item_block_picture.view.*
|
||||
import kotlinx.android.synthetic.main.item_block_text.view.*
|
||||
import kotlinx.android.synthetic.main.item_block_video.view.*
|
||||
|
@ -597,89 +592,6 @@ open class BlockViewHolder(view: View) : RecyclerView.ViewHolder(view) {
|
|||
}
|
||||
}
|
||||
|
||||
class Highlight(
|
||||
view: View,
|
||||
onMarkupActionClicked: (Markup.Type, IntRange) -> Unit
|
||||
) : BlockViewHolder(view), TextHolder, IndentableHolder {
|
||||
|
||||
override val content: TextInputWidget = itemView.highlightContent
|
||||
override val root: View = itemView
|
||||
private val indent = itemView.highlightIndent
|
||||
private val container = itemView.highlightBlockContentContainer
|
||||
|
||||
init {
|
||||
content.setSpannableFactory(DefaultSpannableFactory())
|
||||
setup(onMarkupActionClicked, ContextMenuType.HIGHLIGHT)
|
||||
}
|
||||
|
||||
fun bind(
|
||||
item: BlockView.Highlight,
|
||||
onTextChanged: (String, Editable) -> Unit,
|
||||
onFocusChanged: (String, Boolean) -> Unit,
|
||||
onSelectionChanged: (String, IntRange) -> Unit,
|
||||
clicked: (ListenerType) -> Unit
|
||||
) {
|
||||
//indentize(item)
|
||||
|
||||
if (item.mode == BlockView.Mode.READ) {
|
||||
enableReadOnlyMode()
|
||||
setBlockText(text = item.text, markup = item, clicked = clicked)
|
||||
} else {
|
||||
enableEditMode()
|
||||
setLinksClickable(item)
|
||||
setBlockText(text = item.text, markup = item, clicked = clicked)
|
||||
if (item.isFocused) setCursor(item)
|
||||
setFocus(item)
|
||||
with(content) {
|
||||
clearTextWatchers()
|
||||
setOnFocusChangeListener { _, hasFocus ->
|
||||
onFocusChanged(item.id, hasFocus)
|
||||
}
|
||||
addTextChangedListener(
|
||||
DefaultTextWatcher { text ->
|
||||
onTextChanged(item.id, text)
|
||||
}
|
||||
)
|
||||
setOnLongClickListener(
|
||||
EditorLongClickListener(
|
||||
t = item.id,
|
||||
click = { onBlockLongClick(itemView, it, clicked) }
|
||||
)
|
||||
)
|
||||
selectionWatcher = {
|
||||
onSelectionChanged(item.id, it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun select(item: BlockView.Selectable) {
|
||||
container.isSelected = item.isSelected
|
||||
}
|
||||
|
||||
override fun indentize(item: BlockView.Indentable) {
|
||||
indent.updateLayoutParams {
|
||||
width = item.indent * dimen(R.dimen.indent)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getMentionImageSizeAndPadding(): Pair<Int, Int> = with(itemView) {
|
||||
Pair(
|
||||
first = resources.getDimensionPixelSize(R.dimen.mention_span_image_size_default),
|
||||
second = resources.getDimensionPixelSize(R.dimen.mention_span_image_padding_default)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Should be set before @[setBlockText]!
|
||||
*/
|
||||
private fun setLinksClickable(block: BlockView.Highlight) {
|
||||
if (block.marks.isLinksPresent()) {
|
||||
content.setLinksClickable()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun onBlockLongClick(root: View, target: String, clicked: (ListenerType) -> Unit) {
|
||||
val rect = PopupExtensions.calculateRectInWindow(root)
|
||||
val dimensions = BlockDimensions(
|
||||
|
|
|
@ -2484,7 +2484,7 @@ class BlockAdapterTest {
|
|||
|
||||
adapter.onBindViewHolder(holder, 0)
|
||||
|
||||
check(holder is BlockViewHolder.Highlight)
|
||||
check(holder is Highlight)
|
||||
|
||||
assertEquals(
|
||||
expected = InputType.TYPE_NULL,
|
||||
|
@ -2528,7 +2528,7 @@ class BlockAdapterTest {
|
|||
|
||||
adapter.onBindViewHolder(holder, 0)
|
||||
|
||||
check(holder is BlockViewHolder.Highlight)
|
||||
check(holder is Highlight)
|
||||
|
||||
// Testing
|
||||
|
||||
|
@ -2582,7 +2582,7 @@ class BlockAdapterTest {
|
|||
|
||||
adapter.onBindViewHolder(holder, 0)
|
||||
|
||||
check(holder is BlockViewHolder.Highlight)
|
||||
check(holder is Highlight)
|
||||
|
||||
assertEquals(
|
||||
expected = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS,
|
||||
|
@ -2624,7 +2624,7 @@ class BlockAdapterTest {
|
|||
|
||||
adapter.onBindViewHolder(holder, 0)
|
||||
|
||||
check(holder is BlockViewHolder.Highlight)
|
||||
check(holder is Highlight)
|
||||
|
||||
// Testing
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ import androidx.recyclerview.widget.RecyclerView
|
|||
import androidx.test.core.app.ApplicationProvider
|
||||
import com.agileburo.anytype.core_ui.common.Markup
|
||||
import com.agileburo.anytype.core_ui.common.Span
|
||||
import com.agileburo.anytype.core_ui.features.editor.holders.Highlight
|
||||
import com.agileburo.anytype.core_ui.features.page.BlockAdapter
|
||||
import com.agileburo.anytype.core_ui.features.page.BlockView
|
||||
import com.agileburo.anytype.core_ui.features.page.BlockViewHolder
|
||||
|
@ -53,7 +54,7 @@ class HighlightingBlockTest {
|
|||
|
||||
adapter.onBindViewHolder(holder, 0)
|
||||
|
||||
check(holder is BlockViewHolder.Highlight)
|
||||
check(holder is Highlight)
|
||||
|
||||
// Testing
|
||||
|
||||
|
|
|
@ -196,7 +196,7 @@ class BlockAdapterCursorBindingTest {
|
|||
|
||||
adapter.onBindViewHolder(holder, 0)
|
||||
|
||||
check(holder is BlockViewHolder.Highlight)
|
||||
check(holder is Highlight)
|
||||
|
||||
// Testing
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue