Skip to content

Commit

Permalink
- Fix ClickableSpan crash. closes #3
Browse files Browse the repository at this point in the history
- Code refactor
- Update README
  • Loading branch information
YahiaAngelo committed Oct 31, 2020
1 parent a851945 commit ac9719d
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 51 deletions.
6 changes: 3 additions & 3 deletions MarkdownEditText/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ext {
siteUrl = 'https://github.com/YahiaAngelo/MarkdownEditText'
gitUrl = 'https://github.com/YahiaAngelo/MarkdownEditText.git'

libraryVersion = '1.1.0'
libraryVersion = '1.1.1'

developerId = 'yahiaangelo'
developerName = 'Yahia Mostafa'
Expand All @@ -42,7 +42,7 @@ android {
minSdkVersion 23
targetSdkVersion 29
versionCode 1
versionName "1.1.0"
versionName "1.1.1"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
Expand Down Expand Up @@ -70,7 +70,7 @@ android {
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
testImplementation 'junit:junit:4.13'
implementation 'com.google.android.material:material:1.2.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,38 +111,46 @@ class MarkdownEditText : AppCompatEditText {
if (stop) {
clearTextWatchers()
} else {
when(textStyle){
TextStyle.UNORDERED_LIST -> triggerUnOrderedListStyle(stop)
TextStyle.ORDERED_LIST -> triggerOrderedListStyle(stop)
TextStyle.TASKS_LIST -> triggerTasksListStyle(stop)
else-> {
if (isSelectionStyling) {
styliseText(textStyle, selectionStart, selectionEnd)
isSelectionStyling = false
} else {
textWatcher = object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}

override fun beforeTextChanged(
s: CharSequence?,
start: Int,
count: Int,
after: Int
) {
}

if (isSelectionStyling) {
styliseText(textStyle, selectionStart, selectionEnd)
isSelectionStyling = false
} else {
textWatcher = object : TextWatcher {
override fun afterTextChanged(s: Editable?) {}

override fun beforeTextChanged(
s: CharSequence?,
start: Int,
count: Int,
after: Int
) {
}
override fun onTextChanged(
s: CharSequence?,
start: Int,
before: Int,
count: Int
) {

override fun onTextChanged(
s: CharSequence?,
start: Int,
before: Int,
count: Int
) {
if (before < count) {
styliseText(textStyle, start)
}
}

if (before < count) {
styliseText(textStyle, start)
}
addTextWatcher(textWatcher!!)
}

}
addTextWatcher(textWatcher!!)
}



}


Expand Down Expand Up @@ -360,13 +368,18 @@ class MarkdownEditText : AppCompatEditText {

text?.setSpan(object : ClickableSpan() {
override fun onClick(widget: View) {
val spanStart = text?.getSpanStart(taskSpan)
val spanEnd = text?.getSpanEnd(taskSpan)
taskSpan.isDone = !taskSpan.isDone
text!!.setSpan(
taskSpan,
start,
end,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
if (spanStart != null && spanEnd != null){
text!!.setSpan(
taskSpan,
spanStart,
spanEnd,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
)
}

}

override fun updateDrawState(ds: TextPaint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class MarkdownStylesBar @JvmOverloads constructor(
private var stylesBarAdapter: StylesBarAdapter
private var styleButtons: ArrayList<StyleButton>
private var recyclerView: RecyclerView = RecyclerView(context)
var stylesList: Array<MarkdownEditText.TextStyle> = MarkdownEditText.TextStyle.values()
set(value) {
field = value
updateStyles()
}
var markdownEditText: MarkdownEditText? = null
set(value) {
stylesBarAdapter.markdownEditText = value
Expand All @@ -26,12 +31,7 @@ class MarkdownStylesBar @JvmOverloads constructor(
val linearLayoutManager = LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)
recyclerView.layoutManager = linearLayoutManager
styleButtons = ArrayList()
styleButtons.add(StyleButton(R.drawable.ic_format_bold, R.id.style_button_bold))
styleButtons.add(StyleButton(R.drawable.ic_format_italic, R.id.style_button_italic))
styleButtons.add(StyleButton(R.drawable.ic_format_strikethrough, R.id.style_button_strike))
styleButtons.add(StyleButton(R.drawable.ic_format_list_bulleted, R.id.style_button_unordered_list))
styleButtons.add(StyleButton(R.drawable.ic_format_list_numbered, R.id.style_button_ordered_list))
styleButtons.add(StyleButton(R.drawable.ic_check_box, R.id.style_button_task_list))
setStylesButtons()
stylesBarAdapter = StylesBarAdapter(styleButtons)
recyclerView.adapter = stylesBarAdapter

Expand All @@ -42,7 +42,27 @@ class MarkdownStylesBar @JvmOverloads constructor(
}
a.recycle()
addView(recyclerView)


}

private fun updateStyles(){
styleButtons.clear()
setStylesButtons()
stylesBarAdapter.setStyles(styleButtons)
}

private fun setStylesButtons(){
for (style in stylesList){
when(style){
MarkdownEditText.TextStyle.BOLD -> styleButtons.add(StyleButton(R.drawable.ic_format_bold, R.id.style_button_bold))
MarkdownEditText.TextStyle.ITALIC -> styleButtons.add(StyleButton(R.drawable.ic_format_italic, R.id.style_button_italic))
MarkdownEditText.TextStyle.STRIKE -> styleButtons.add(StyleButton(R.drawable.ic_format_strikethrough, R.id.style_button_strike))
MarkdownEditText.TextStyle.UNORDERED_LIST -> styleButtons.add(StyleButton(R.drawable.ic_format_list_bulleted, R.id.style_button_unordered_list))
MarkdownEditText.TextStyle.ORDERED_LIST -> styleButtons.add(StyleButton(R.drawable.ic_format_list_numbered, R.id.style_button_ordered_list))
MarkdownEditText.TextStyle.TASKS_LIST -> styleButtons.add(StyleButton(R.drawable.ic_check_box, R.id.style_button_task_list))
else ->{}
}
}
}

fun getViewWithId(id: Int): View{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.yahiaangelo.markdownedittext.MarkdownEditText
import com.yahiaangelo.markdownedittext.R
import com.yahiaangelo.markdownedittext.model.StyleButton

class StylesBarAdapter(private val buttonsList: ArrayList<StyleButton>) : RecyclerView.Adapter<StylesBarAdapter.MyViewHolder>() {
class StylesBarAdapter(private var buttonsList: ArrayList<StyleButton>) : RecyclerView.Adapter<StylesBarAdapter.MyViewHolder>() {

private var selectedButton: MaterialButton? = null
var buttonsColor: ColorStateList? = null
Expand All @@ -27,6 +27,11 @@ class StylesBarAdapter(private val buttonsList: ArrayList<StyleButton>) : Recycl
return buttonsList.size
}

fun setStyles(newButtonsList: ArrayList<StyleButton>){
buttonsList = newButtonsList
notifyDataSetChanged()
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val styleButtonModel = buttonsList[position]
if (buttonsColor != null){
Expand Down Expand Up @@ -65,11 +70,16 @@ class StylesBarAdapter(private val buttonsList: ArrayList<StyleButton>) : Recycl
MarkdownEditText.TextStyle.STRIKE,
!button.isChecked
)
R.id.style_button_unordered_list -> markdownEditText!!.triggerUnOrderedListStyle(
R.id.style_button_unordered_list -> markdownEditText!!.triggerStyle(
MarkdownEditText.TextStyle.UNORDERED_LIST,
!button.isChecked
)
R.id.style_button_ordered_list -> markdownEditText!!.triggerOrderedListStyle(!button.isChecked)
R.id.style_button_task_list -> markdownEditText!!.triggerTasksListStyle(!button.isChecked)
R.id.style_button_ordered_list -> markdownEditText!!.triggerStyle(
MarkdownEditText.TextStyle.ORDERED_LIST,
!button.isChecked)
R.id.style_button_task_list -> markdownEditText!!.triggerStyle(
MarkdownEditText.TextStyle.TASKS_LIST,
!button.isChecked)
}

}
Expand Down
3 changes: 2 additions & 1 deletion MarkdownEditText/src/main/res/layout/styles_bar_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
android:id="@+id/style_button"
android:checkable="true"
app:iconGravity="textStart"
android:layout_marginEnd="12dp"
android:layout_marginEnd="6dp"
android:layout_marginStart="6dp"
app:backgroundTint="@color/style_button_color"
app:iconPadding="0dp"
tools:icon="@drawable/ic_format_italic"
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A native Rich text editor for android based on [Markwon](https://github.com/noti
### Adding the depencency
Add the dependency to your app build.gradle file:
```
implementation 'com.yahiaangelo.markdownedittext:markdownedittext:1.1.0'
implementation 'com.yahiaangelo.markdownedittext:markdownedittext:1.1.1'
```
### XML
```xml
Expand All @@ -40,6 +40,11 @@ implementation 'com.yahiaangelo.markdownedittext:markdownedittext:1.1.0'
val stylesBar = findViewById<MarkdownStylesBar>(R.id.stylesbar)
markdownEditText.setStylesBar(stylesBar)
```
#### Customize default Styles bar :
```Kotlin
//Select specific Styles to show
stylesbar.stylesList = arrayOf(MarkdownEditText.TextStyle.BOLD, MarkdownEditText.TextStyle.ITALIC)
```
---

### Available styles:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Jun 30 11:55:45 EET 2020
#Sun Oct 18 21:40:57 EET 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.SpannableStringBuilder
import com.yahiaangelo.markdownedittext.MarkdownEditText
import com.yahiaangelo.markdownedittext.MarkdownStylesBar
import com.yahiaangelo.markdownedittext.sample.databinding.ActivityMainBinding
import kotlinx.android.synthetic.main.activity_main.view.*

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -21,5 +19,7 @@ class MainActivity : AppCompatActivity() {
}
return@setOnMenuItemClickListener true
}
//Select specific Styles to show
//binding.stylesbar.stylesList = arrayOf(MarkdownEditText.TextStyle.BOLD, MarkdownEditText.TextStyle.ITALIC)
}
}

0 comments on commit ac9719d

Please sign in to comment.