mirror of
https://github.com/sudoxnym/habitica-android.git
synced 2026-07-13 17:51:57 +00:00
Create reusable methods in Task class to handle async markdown parsing
This commit is contained in:
parent
941ce87fd9
commit
487a924582
2 changed files with 44 additions and 0 deletions
|
|
@ -8,6 +8,7 @@ import com.habitrpg.android.habitica.R
|
|||
import com.habitrpg.android.habitica.models.Tag
|
||||
import com.habitrpg.android.habitica.models.user.Stats
|
||||
import com.habitrpg.android.habitica.ui.helpers.MarkdownParser
|
||||
import io.reactivex.functions.Consumer
|
||||
import io.realm.RealmList
|
||||
import io.realm.RealmObject
|
||||
import io.realm.annotations.Ignore
|
||||
|
|
@ -191,6 +192,36 @@ open class Task : RealmObject, Parcelable {
|
|||
|
||||
}
|
||||
|
||||
fun markdownText(callback: (CharSequence) -> Unit): CharSequence {
|
||||
if (this.parsedText != null) {
|
||||
return this.parsedText as CharSequence
|
||||
}
|
||||
|
||||
MarkdownParser.parseMarkdownAsync(this.text, Consumer { parsedText ->
|
||||
this.parsedText = parsedText
|
||||
callback(parsedText)
|
||||
})
|
||||
|
||||
return this.text
|
||||
}
|
||||
|
||||
fun markdownNotes(callback: (CharSequence) -> Unit): CharSequence? {
|
||||
if (this.parsedNotes != null) {
|
||||
return this.parsedNotes as CharSequence
|
||||
}
|
||||
|
||||
if (this.notes?.isEmpty() == true) {
|
||||
return null
|
||||
}
|
||||
|
||||
MarkdownParser.parseMarkdownAsync(this.notes, Consumer { parsedText ->
|
||||
this.parsedNotes = parsedText
|
||||
callback(parsedText)
|
||||
})
|
||||
|
||||
return this.notes
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other == null) {
|
||||
return false
|
||||
|
|
|
|||
|
|
@ -7,6 +7,11 @@ import com.commonsware.cwac.anddown.AndDown
|
|||
import net.pherth.android.emoji_library.EmojiParser
|
||||
|
||||
import android.text.Html.FROM_HTML_MODE_LEGACY
|
||||
import com.habitrpg.android.habitica.helpers.RxErrorHandler
|
||||
import io.reactivex.Single
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.functions.Consumer
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
|
||||
/**
|
||||
* @author data5tream
|
||||
|
|
@ -35,6 +40,14 @@ object MarkdownParser {
|
|||
return output
|
||||
}
|
||||
|
||||
fun parseMarkdownAsync(input: String?, onSuccess: Consumer<CharSequence>) {
|
||||
Single.just(input)
|
||||
.map { this.parseMarkdown(it) }
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(onSuccess, RxErrorHandler.handleEmptyError())
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts stylized CharSequence into markdown
|
||||
*
|
||||
|
|
|
|||
Loading…
Reference in a new issue