Skip to content

Commit

Permalink
Merge branch 'main' into Serializable_extensions
Browse files Browse the repository at this point in the history
# Conflicts:
#	app/src/main/java/org/wikipedia/descriptions/DescriptionEditActivity.kt
  • Loading branch information
Isira-Seneviratne committed Jan 18, 2024
2 parents cb8b39f + ac6025d commit 60b1249
Show file tree
Hide file tree
Showing 76 changed files with 1,040 additions and 471 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ object JavaScriptActionHandler {
}

fun scrollToAnchor(anchorLink: String): String {
val anchor = if (anchorLink.contains("#")) anchorLink.substring(anchorLink.indexOf("#") + 1) else anchorLink
val anchor = anchorLink.substringAfter('#')
return "var el = document.getElementById('$anchor');" +
"window.scrollTo(0, el.offsetTop - (screen.height / 2));" +
"setTimeout(function(){ el.style.backgroundColor='#fc3';" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import org.wikipedia.page.Namespace
import org.wikipedia.page.PageTitle
import org.wikipedia.page.linkpreview.LinkPreviewDialog
import org.wikipedia.readinglist.database.ReadingList
import org.wikipedia.util.*
import org.wikipedia.util.ResourceUtil
import org.wikipedia.util.StringUtil
import org.wikipedia.views.DrawableItemDecoration
import org.wikipedia.views.PageItemView
import org.wikipedia.views.WikiErrorView
Expand Down Expand Up @@ -138,7 +139,7 @@ class CategoryActivity : BaseActivity() {
startActivity(newIntent(this, title))
} else {
val entry = HistoryEntry(title, HistoryEntry.SOURCE_CATEGORY)
ExclusiveBottomSheetPresenter.show(supportFragmentManager, LinkPreviewDialog.newInstance(entry, null))
ExclusiveBottomSheetPresenter.show(supportFragmentManager, LinkPreviewDialog.newInstance(entry))
}
}

Expand Down
8 changes: 3 additions & 5 deletions app/src/main/java/org/wikipedia/dataclient/Service.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ interface Service {
)
suspend fun fullTextSearch(
@Query("gsrsearch") searchTerm: String?,
@Query("gsroffset") gsrOffset: String?,
@Query("gsrlimit") gsrLimit: Int,
@Query("continue") cont: String?
@Query("gsroffset") gsrOffset: Int?
): MwQueryResponse

@GET(MW_API_PREFIX + "action=query&list=allusers&auwitheditsonly=1")
Expand All @@ -65,10 +64,9 @@ interface Service {
"&gsrnamespace=6&iiurlwidth=" + PREFERRED_THUMB_SIZE
)
suspend fun fullTextSearchCommons(
@Query("gsrsearch") searchTerm: String?,
@Query("gsroffset") gsrOffset: String?,
@Query("gsrsearch") searchTerm: String,
@Query("gsrlimit") gsrLimit: Int,
@Query("continue") cont: String?
@Query("gsroffset") gsrOffset: Int?,
): MwQueryResponse

@GET(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ open class MwQueryResponse : MwResponse() {

@Serializable
class Continuation {
val sroffset = 0
val gsroffset = 0
val gpsoffset = 0
val sroffset: Int? = null
val gsroffset: Int? = null
val gpsoffset: Int? = null
@SerialName("continue") val continuation: String? = null
@SerialName("uccontinue") val ucContinuation: String? = null
@SerialName("rccontinue") val rcContinuation: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class OfflineCacheInterceptor : Interceptor {
val name = line.substring(0, pos).trim()
val value = line.substring(pos + 1).trim()
builder.header(name, value)
if (name.lowercase(Locale.getDefault()) == "content-type") {
if (name.equals("content-type", true)) {
contentType = value
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package org.wikipedia.descriptions
import android.content.Context
import android.net.Uri
import android.text.SpannableString
import android.text.Spanned
import android.text.TextUtils
import android.text.method.LinkMovementMethod
import android.text.style.BulletSpan
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ScrollView
import androidx.core.text.buildSpannedString
import androidx.core.text.inSpans
import org.wikipedia.R
import org.wikipedia.WikipediaApp
import org.wikipedia.databinding.ViewDescriptionEditRevertHelpBinding
Expand All @@ -31,14 +32,18 @@ class DescriptionEditRevertHelpView constructor(context: Context, attrs: Attribu
.replace(":revertSubtitle".toRegex(), context.getString(R.string.description_edit_revert_subtitle))
.replace(":revertIntro".toRegex(), context.getString(R.string.description_edit_revert_intro))
.replace(":revertHistory".toRegex(), context.getString(R.string.description_edit_revert_history, getHistoryUri(qNumber))))
val gapWidth = DimenUtil.roundedDpToPx(8f)
val revertReason1 = SpannableString(StringUtil.fromHtml(context.getString(R.string.description_edit_revert_reason1, context.getString(R.string.wikidata_description_guide_url))))
val revertReason2 = SpannableString(StringUtil.fromHtml(context.getString(R.string.description_edit_revert_reason2)))
revertReason1.setSpan(BulletSpan(gapWidth), 0, revertReason1.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
revertReason2.setSpan(BulletSpan(gapWidth), 0, revertReason2.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
val revertReason1 = createReason(context.getString(R.string.description_edit_revert_reason1,
context.getString(R.string.wikidata_description_guide_url)))
val revertReason2 = createReason(context.getString(R.string.description_edit_revert_reason2))
binding.helpText.text = SpannableString(TextUtils.expandTemplate(helpStr, revertReason1, revertReason2))
}

private fun createReason(htmlString: String) = buildSpannedString {
inSpans(BulletSpan(DimenUtil.roundedDpToPx(8f))) {
append(StringUtil.fromHtml(htmlString))
}
}

private fun getHistoryUri(qNumber: String): Uri {
return Uri.Builder()
.scheme(WikipediaApp.instance.wikiSite.scheme())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ class ArticleEditDetailsFragment : Fragment(), WatchlistExpiryDialog.Callback, M
startActivity(FilePageActivity.newIntent(requireContext(), viewModel.pageTitle))
} else {
ExclusiveBottomSheetPresenter.show(childFragmentManager, LinkPreviewDialog.newInstance(
HistoryEntry(viewModel.pageTitle, HistoryEntry.SOURCE_EDIT_DIFF_DETAILS), null))
HistoryEntry(viewModel.pageTitle, HistoryEntry.SOURCE_EDIT_DIFF_DETAILS)))
}
}
binding.newerIdButton.setOnClickListener {
Expand Down
84 changes: 38 additions & 46 deletions app/src/main/java/org/wikipedia/diff/DiffUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import android.text.style.StrikethroughSpan
import android.text.style.StyleSpan
import android.view.ViewGroup
import androidx.core.graphics.ColorUtils
import androidx.core.text.buildSpannedString
import androidx.core.text.inSpans
import androidx.core.text.set
import androidx.recyclerview.widget.RecyclerView
import org.wikipedia.R
import org.wikipedia.dataclient.restbase.DiffResponse
Expand All @@ -31,10 +34,10 @@ object DiffUtil {
if (it.lineNumber > lastItem!!.lineEnd) {
lastItem!!.lineEnd = it.lineNumber
}
val str = SpannableStringBuilder(lastItem!!.parsedText)
str.append("\n")
str.append(item.parsedText)
lastItem!!.parsedText = str
lastItem!!.parsedText = buildSpannedString {
appendLine(lastItem!!.parsedText)
append(item.parsedText)
}
} else {
items.add(item)
lastItem = item
Expand Down Expand Up @@ -81,54 +84,43 @@ object DiffUtil {
}

private fun createSpannableDiffText(context: Context, diff: DiffResponse.DiffItem): CharSequence {
val spannableString = SpannableStringBuilder(diff.text.ifEmpty { "\n" })
if (diff.text.isEmpty()) {
spannableString.setSpan(EmptyLineSpan(ResourceUtil.getThemedColor(context, android.R.attr.colorBackground),
ResourceUtil.getThemedColor(context, R.attr.placeholder_color)), 0, spannableString.length, 0)
return spannableString
}
when (diff.type) {
DiffResponse.DIFF_TYPE_LINE_ADDED -> {
updateDiffTextDecor(context, spannableString, true, 0, diff.text.length)
}
DiffResponse.DIFF_TYPE_LINE_REMOVED -> {
updateDiffTextDecor(context, spannableString, false, 0, diff.text.length)
}
DiffResponse.DIFF_TYPE_PARAGRAPH_MOVED_FROM -> {
updateDiffTextDecor(context, spannableString, false, 0, diff.text.length)
}
DiffResponse.DIFF_TYPE_PARAGRAPH_MOVED_TO -> {
updateDiffTextDecor(context, spannableString, true, 0, diff.text.length)
}
}
if (diff.highlightRanges.isNotEmpty()) {
for (highlightRange in diff.highlightRanges) {
val indices = StringUtil.utf8Indices(diff.text)
val highlightRangeStart = indices[highlightRange.start].coerceIn(0, diff.text.length)
val highlightRangeEnd = (indices.getOrElse(highlightRange.start + highlightRange.length) { indices.last() + 1 }).coerceIn(0, diff.text.length)
return buildSpannedString {
if (diff.text.isEmpty()) {
inSpans(EmptyLineSpan(ResourceUtil.getThemedColor(context, android.R.attr.colorBackground),
ResourceUtil.getThemedColor(context, R.attr.placeholder_color))) {
appendLine()
}
} else {
append(diff.text)

when (diff.type) {
DiffResponse.DIFF_TYPE_LINE_ADDED, DiffResponse.DIFF_TYPE_PARAGRAPH_MOVED_TO -> {
updateDiffTextDecor(context, true, 0, diff.text.length)
}
DiffResponse.DIFF_TYPE_LINE_REMOVED, DiffResponse.DIFF_TYPE_PARAGRAPH_MOVED_FROM -> {
updateDiffTextDecor(context, false, 0, diff.text.length)
}
}

for (highlightRange in diff.highlightRanges) {
val indices = StringUtil.utf8Indices(diff.text)
val highlightRangeStart = indices[highlightRange.start].coerceIn(0, diff.text.length)
val highlightRangeEnd = (indices.getOrElse(highlightRange.start + highlightRange.length) { indices.last() + 1 }).coerceIn(0, diff.text.length)
val isAddition = highlightRange.type == DiffResponse.HIGHLIGHT_TYPE_ADD

if (highlightRange.type == DiffResponse.HIGHLIGHT_TYPE_ADD) {
updateDiffTextDecor(context, spannableString, true, highlightRangeStart, highlightRangeEnd)
} else {
updateDiffTextDecor(context, spannableString, false, highlightRangeStart, highlightRangeEnd)
updateDiffTextDecor(context, isAddition, highlightRangeStart, highlightRangeEnd)
}
}
}
return spannableString
}

private fun updateDiffTextDecor(context: Context, spannableText: SpannableStringBuilder, isAddition: Boolean, start: Int, end: Int) {
val boldStyle = StyleSpan(Typeface.BOLD)
val foregroundAddedColor = ForegroundColorSpan(ResourceUtil.getThemedColor(context, R.attr.primary_color))
val foregroundRemovedColor = ForegroundColorSpan(ResourceUtil.getThemedColor(context, R.attr.primary_color))
spannableText.setSpan(BackgroundColorSpan(ColorUtils.setAlphaComponent(ResourceUtil.getThemedColor(context,
if (isAddition) R.attr.success_color else R.attr.destructive_color), 48)), start, end, 0)
spannableText.setSpan(boldStyle, start, end, 0)
if (isAddition) {
spannableText.setSpan(foregroundAddedColor, start, end, 0)
} else {
spannableText.setSpan(foregroundRemovedColor, start, end, 0)
spannableText.setSpan(StrikethroughSpan(), start, end, 0)
private fun SpannableStringBuilder.updateDiffTextDecor(context: Context, isAddition: Boolean, start: Int, end: Int) {
this[start, end] = BackgroundColorSpan(ColorUtils.setAlphaComponent(ResourceUtil.getThemedColor(context,
if (isAddition) R.attr.success_color else R.attr.destructive_color), 48))
this[start, end] = StyleSpan(Typeface.BOLD)
this[start, end] = ForegroundColorSpan(ResourceUtil.getThemedColor(context, R.attr.primary_color))
if (!isAddition) {
this[start, end] = StrikethroughSpan()
}
}
}
16 changes: 4 additions & 12 deletions app/src/main/java/org/wikipedia/edit/FindInEditorActionProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import android.view.View
import org.wikipedia.edit.richtext.SyntaxHighlighter
import org.wikipedia.util.DeviceUtil
import org.wikipedia.util.DimenUtil
import org.wikipedia.util.StringUtil
import org.wikipedia.views.FindInPageActionProvider
import org.wikipedia.views.FindInPageActionProvider.FindInPageListener
import java.util.*

class FindInEditorActionProvider(private val scrollView: View,
private val textView: SyntaxHighlightableEditText,
Expand Down Expand Up @@ -59,21 +59,13 @@ class FindInEditorActionProvider(private val scrollView: View,
}

override fun onSearchTextChanged(text: String?) {
searchQuery = if (text.isNullOrEmpty()) null else text
searchQuery = text?.ifEmpty { null }
currentResultIndex = 0
resultPositions.clear()

searchQuery?.let {
val searchTextLower = it.lowercase(Locale.getDefault())
val textLower = textView.text.toString().lowercase(Locale.getDefault())
var position = 0
do {
position = textLower.indexOf(searchTextLower, position)
if (position >= 0) {
resultPositions.add(position)
position += searchTextLower.length
}
} while (position >= 0)
resultPositions += it.toRegex(StringUtil.SEARCH_REGEX_OPTIONS).findAll(textView.text)
.map { it.range.first }
}
scrollToCurrentResult()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SyntaxHighlightViewAdapter(
}

override fun onPreviewLink(title: String) {
val dialog = LinkPreviewDialog.newInstance(HistoryEntry(PageTitle(title, pageTitle.wikiSite), HistoryEntry.SOURCE_INTERNAL_LINK), null)
val dialog = LinkPreviewDialog.newInstance(HistoryEntry(PageTitle(title, pageTitle.wikiSite), HistoryEntry.SOURCE_INTERNAL_LINK))
ExclusiveBottomSheetPresenter.show(activity.supportFragmentManager, dialog)
editText.post {
dialog.dialog?.setOnDismissListener {
Expand Down
67 changes: 16 additions & 51 deletions app/src/main/java/org/wikipedia/edit/WikiTextKeyboardView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,34 +90,27 @@ class WikiTextKeyboardView constructor(context: Context, attrs: AttributeSet?) :
binding.wikitextButtonPreviewLink.setOnClickListener {
editText?.inputConnection?.let { inputConnection ->
var title: String? = null
val selection = inputConnection.getSelectedText(0)
if (!selection.isNullOrEmpty() && !selection.toString().contains("[[")) {
title = trimPunctuation(selection.toString())
val selection = inputConnection.getSelectedText(0)?.toString()
if (!selection.isNullOrEmpty() && "[[" !in selection) {
title = selection.trim('.', ',', ';', '?', '!')
} else {
val before: String
val after: String
if (selection != null && selection.length > 1) {
val selectionStr = selection.toString()
before = selectionStr.substring(0, selectionStr.length / 2)
after = selectionStr.substring(selectionStr.length / 2)
val (before, after) = if (selection != null && selection.length > 1) {
selection.substring(0, selection.length / 2) to
selection.substring(selection.length / 2)
} else {
val peekLength = 64
before = inputConnection.getTextBeforeCursor(peekLength, 0).toString()
after = inputConnection.getTextAfterCursor(peekLength, 0).toString()
inputConnection.getTextBeforeCursor(peekLength, 0)?.toString() to
inputConnection.getTextAfterCursor(peekLength, 0)?.toString()
}

if (before.isNotEmpty() && after.isNotEmpty()) {
var str = before + after
val i1 = lastIndexOf(before, "[[")
val i2 = after.indexOf("]]") + before.length
if (i1 >= 0 && i2 > 0 && i2 > i1) {
str = str.substring(i1 + 2, i2).trim()
if (str.isNotEmpty()) {
if (str.contains("|")) {
str = str.split("\\|".toRegex()).toTypedArray()[0]
}
title = str
}
if (!before.isNullOrEmpty() && !after.isNullOrEmpty()) {
val str = before + after
val i1 = before.lastIndexOf("[[")
if (i1 >= 0) {
title = str.substring(i1 + 2).substringBefore("]]")
.trim()
.splitToSequence('|')
.firstOrNull()?.ifEmpty { null }
}
}
}
Expand Down Expand Up @@ -173,7 +166,6 @@ class WikiTextKeyboardView constructor(context: Context, attrs: AttributeSet?) :
}

companion object {

fun toggleSyntaxAroundCurrentSelection(editText: EditText?, ic: InputConnection, prefix: String, suffix: String) {
editText?.let {
if (it.selectionStart == it.selectionEnd) {
Expand Down Expand Up @@ -201,32 +193,5 @@ class WikiTextKeyboardView constructor(context: Context, attrs: AttributeSet?) :
}
}
}

@Suppress("SameParameterValue")
fun lastIndexOf(str: String, subStr: String): Int {
var index = -1
var a = 0
while (a < str.length) {
val i = str.indexOf(subStr, a)
if (i >= 0) {
index = i
a = i + 1
} else {
break
}
}
return index
}

fun trimPunctuation(str: String): String {
var newStr = str
while (newStr.startsWith(".") || newStr.startsWith(",") || newStr.startsWith(";") || newStr.startsWith("?") || newStr.startsWith("!")) {
newStr = newStr.substring(1)
}
while (newStr.endsWith(".") || newStr.endsWith(",") || newStr.endsWith(";") || newStr.endsWith("?") || newStr.endsWith("!")) {
newStr = newStr.substring(0, newStr.length - 1)
}
return newStr
}
}
}
Loading

0 comments on commit 60b1249

Please sign in to comment.