Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update kotlin and other libraries and fix the code #982

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

android {
compileSdkVersion 28
compileSdkVersion 31

defaultConfig {
applicationId "org.wordpress.aztec"
Expand Down Expand Up @@ -45,7 +45,7 @@ dependencies {
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutinesVersion"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinCoroutinesVersion"

implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation "org.wordpress:utils:$wordpressUtilsVersion"

androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0', {
Expand Down
31 changes: 14 additions & 17 deletions app/src/main/kotlin/org/wordpress/aztec/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.os.Looper
import android.provider.MediaStore
import android.util.DisplayMetrics
import android.view.Gravity
Expand Down Expand Up @@ -248,15 +249,13 @@ open class MainActivity : AppCompatActivity(),

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK) {
var bitmap: Bitmap

when (requestCode) {
REQUEST_MEDIA_CAMERA_PHOTO -> {
// By default, BitmapFactory.decodeFile sets the bitmap's density to the device default so, we need
// to correctly set the input density to 160 ourselves.
val options = BitmapFactory.Options()
options.inDensity = DisplayMetrics.DENSITY_DEFAULT
bitmap = BitmapFactory.decodeFile(mediaPath, options)
val bitmap = BitmapFactory.decodeFile(mediaPath, options)
insertImageAndSimulateUpload(bitmap, mediaPath)
}
REQUEST_MEDIA_PHOTO -> {
Expand All @@ -266,7 +265,7 @@ open class MainActivity : AppCompatActivity(),
// to correctly set the input density to 160 ourselves.
val options = BitmapFactory.Options()
options.inDensity = DisplayMetrics.DENSITY_DEFAULT
bitmap = BitmapFactory.decodeStream(stream, null, options)
val bitmap = BitmapFactory.decodeStream(stream, null, options)

insertImageAndSimulateUpload(bitmap, mediaPath)
}
Expand All @@ -282,7 +281,7 @@ open class MainActivity : AppCompatActivity(),

override fun onThumbnailLoaded(drawable: Drawable?) {
val conf = Bitmap.Config.ARGB_8888 // see other conf types
bitmap = Bitmap.createBitmap(drawable!!.intrinsicWidth, drawable.intrinsicHeight, conf)
val bitmap = Bitmap.createBitmap(drawable!!.intrinsicWidth, drawable.intrinsicHeight, conf)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
Expand Down Expand Up @@ -369,11 +368,11 @@ open class MainActivity : AppCompatActivity(),
}
}

Handler().post(runnable)
Handler().postDelayed(runnable, 2000)
Handler().postDelayed(runnable, 4000)
Handler().postDelayed(runnable, 6000)
Handler().postDelayed(runnable, 8000)
Handler(Looper.getMainLooper()).post(runnable)
Handler(Looper.getMainLooper()).postDelayed(runnable, 2000)
Handler(Looper.getMainLooper()).postDelayed(runnable, 4000)
Handler(Looper.getMainLooper()).postDelayed(runnable, 6000)
Handler(Looper.getMainLooper()).postDelayed(runnable, 8000)

aztec.visualEditor.refreshText()
}
Expand Down Expand Up @@ -472,7 +471,7 @@ open class MainActivity : AppCompatActivity(),
aztec.initSourceEditorHistory()
}

invalidateOptionsHandler = Handler()
invalidateOptionsHandler = Handler(Looper.getMainLooper())
invalidateOptionsRunnable = Runnable { invalidateOptionsMenu() }
}

Expand Down Expand Up @@ -506,23 +505,21 @@ open class MainActivity : AppCompatActivity(),
}
}

override fun onRestoreInstanceState(savedInstanceState: Bundle?) {
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)

aztec.initSourceEditorHistory()

savedInstanceState?.let {
if (savedInstanceState.getBoolean("isMediaUploadDialogVisible")) {
showMediaUploadDialog()
}
if (savedInstanceState.getBoolean("isMediaUploadDialogVisible")) {
showMediaUploadDialog()
}
}

override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)

if (mediaUploadDialog != null && mediaUploadDialog!!.isShowing) {
outState?.putBoolean("isMediaUploadDialogVisible", true)
outState.putBoolean("isMediaUploadDialogVisible", true)
}
}

Expand Down
16 changes: 10 additions & 6 deletions aztec/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ plugins {
}

android {
compileSdkVersion 28
buildscript {
ext.commonTargetSdkVersion = 30
ext.commonMinSdkVersion = 21
}
compileSdkVersion 31

defaultConfig {
minSdkVersion commonMinSdkVersion
Expand Down Expand Up @@ -54,17 +58,17 @@ dependencies {
implementation "org.jsoup:jsoup:$jSoupVersion"

implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.android.material:material:1.6.0'

implementation "org.wordpress:utils:$wordpressUtilsVersion"

testImplementation "junit:junit:$jUnitVersion"
testImplementation "org.robolectric:robolectric:$robolectricVersion"
testImplementation 'androidx.test:core:1.2.0'
testImplementation 'androidx.test:core:1.4.0'

androidTestImplementation 'androidx.test:core:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:core:1.4.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0', {
exclude group: 'com.android.support', module: 'support-annotations'
}
Expand Down
20 changes: 12 additions & 8 deletions aztec/src/main/kotlin/org/wordpress/aztec/AztecText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,9 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
history.inputLast = InstanceStateUtils.readAndPurgeTempInstance<String>(INPUT_LAST_KEY, "", savedState.state)
visibility = customState.getInt(VISIBILITY_KEY)

initialEditorContentParsedSHA256 = customState.getByteArray(RETAINED_INITIAL_HTML_PARSED_SHA256_KEY)
customState.getByteArray(RETAINED_INITIAL_HTML_PARSED_SHA256_KEY)?.let {
initialEditorContentParsedSHA256 = it
}
val retainedHtml = InstanceStateUtils.readAndPurgeTempInstance<String>(RETAINED_HTML_KEY, "", savedState.state)
fromHtml(retainedHtml)

Expand Down Expand Up @@ -884,9 +886,9 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
return false
}

override fun onSaveInstanceState(): Parcelable {
override fun onSaveInstanceState(): Parcelable? {
val superState = super.onSaveInstanceState()
val savedState = SavedState(superState)
val savedState = superState?.let { SavedState(it) }
val bundle = Bundle()
InstanceStateUtils.writeTempInstance(context, externalLogger, HISTORY_LIST_KEY, ArrayList<String>(history.historyList), bundle)
bundle.putInt(HISTORY_CURSOR_KEY, history.historyCursor)
Expand Down Expand Up @@ -919,7 +921,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown

bundle.putBoolean(IS_MEDIA_ADDED_KEY, isMediaAdded)

savedState.state = bundle
savedState?.state = bundle
return savedState
}

Expand All @@ -929,7 +931,9 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown
constructor(superState: Parcelable) : super(superState)

constructor(parcel: Parcel) : super(parcel) {
state = parcel.readBundle(javaClass.classLoader)
parcel.readBundle(javaClass.classLoader)?.let {
state = it
}
}

override fun writeToParcel(out: Parcel, flags: Int) {
Expand Down Expand Up @@ -1799,8 +1803,8 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown

val html = Format.removeSourceEditorFormatting(parser.toHtml(output), isInCalypsoMode, isInGutenbergMode)

val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as android.content.ClipboardManager
clipboard.primaryClip = ClipData.newHtmlText("aztec", output.toString(), html)
val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
clipboard.setPrimaryClip(ClipData.newHtmlText("aztec", output.toString(), html))
}

// copied from TextView with some changes
Expand Down Expand Up @@ -1987,7 +1991,7 @@ open class AztecText : AppCompatEditText, TextWatcher, UnknownHtmlSpan.OnUnknown

unknownBlockSpanStart = text.getSpanStart(unknownHtmlSpan)
blockEditorDialog = builder.create()
blockEditorDialog!!.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
blockEditorDialog!!.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
blockEditorDialog!!.show()
}

Expand Down
26 changes: 14 additions & 12 deletions aztec/src/main/kotlin/org/wordpress/aztec/History.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,21 @@ class History(val historyEnabled: Boolean, val historySize: Int) {
}

fun beforeTextChanged(editText: EditText) {
if (historyEnabled && !historyWorking) {
mainHandler.removeCallbacks(historyRunnable)
if (!textChangedPending) {
textChangedPending = true
historyRunnable?.text =
when (editText) {
is AztecText -> editText.toFormattedHtml()
is SourceViewEditText -> editText.text.toString()
else -> ""
}
historyRunnable?.editText = editText
historyRunnable?.let { runnable ->
if (historyEnabled && !historyWorking) {
mainHandler.removeCallbacks(runnable)
if (!textChangedPending) {
textChangedPending = true
runnable.text =
when (editText) {
is AztecText -> editText.toFormattedHtml()
is SourceViewEditText -> editText.text.toString()
else -> ""
}
runnable.editText = editText
}
mainHandler.postDelayed(runnable, historyThrottleTime)
}
mainHandler.postDelayed(historyRunnable, historyThrottleTime)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ open class SourceViewEditText : AppCompatEditText, TextWatcher {
visibility = customState.getInt("visibility")
val retainedContent = InstanceStateUtils.readAndPurgeTempInstance<String>(RETAINED_CONTENT_KEY, "", savedState.state)
setText(retainedContent)
initialEditorContentParsedSHA256 = customState.getByteArray(AztecText.RETAINED_INITIAL_HTML_PARSED_SHA256_KEY)
customState.getByteArray(AztecText.RETAINED_INITIAL_HTML_PARSED_SHA256_KEY)?.let {
initialEditorContentParsedSHA256 = it
}
}

// Do not include the content of the editor when saving state to bundle.
Expand All @@ -109,12 +111,12 @@ open class SourceViewEditText : AppCompatEditText, TextWatcher {
return false
}

override fun onSaveInstanceState(): Parcelable {
override fun onSaveInstanceState(): Parcelable? {
val bundle = Bundle()
bundle.putByteArray(org.wordpress.aztec.AztecText.RETAINED_INITIAL_HTML_PARSED_SHA256_KEY,
bundle.putByteArray(AztecText.RETAINED_INITIAL_HTML_PARSED_SHA256_KEY,
initialEditorContentParsedSHA256)
InstanceStateUtils.writeTempInstance(context, null, RETAINED_CONTENT_KEY, text.toString(), bundle)
val superState = super.onSaveInstanceState()
val superState = super.onSaveInstanceState() ?: return null
val savedState = SavedState(superState)
bundle.putInt("visibility", visibility)
savedState.state = bundle
Expand All @@ -127,7 +129,9 @@ open class SourceViewEditText : AppCompatEditText, TextWatcher {
constructor(superState: Parcelable) : super(superState)

constructor(parcel: Parcel) : super(parcel) {
state = parcel.readBundle(javaClass.classLoader)
parcel.readBundle(javaClass.classLoader)?.let {
state = it
}
}

override fun writeToParcel(out: Parcel, flags: Int) {
Expand Down Expand Up @@ -265,7 +269,7 @@ open class SourceViewEditText : AppCompatEditText, TextWatcher {
val str: String

if (withCursorTag) {
val withCursor = StringBuffer(text)
val withCursor = StringBuffer(text.toString())
if (!isCursorInsideTag()) {
withCursor.insert(selectionEnd, "<aztec_cursor></aztec_cursor>")
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class AztecCodeSpan(override var attributes: AztecAttributes = AztecAttributes()
configureTextPaint(tp)
}

override fun updateMeasureState(tp: TextPaint?) {
override fun updateMeasureState(tp: TextPaint) {
configureTextPaint(tp)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ open class AztecPreformatSpan(
}
}

override fun drawBackground(canvas: Canvas, paint: Paint, left: Int, right: Int, top: Int, baseline: Int, bottom: Int, text: CharSequence?, start: Int, end: Int, lnum: Int) {
override fun drawBackground(canvas: Canvas, paint: Paint, left: Int, right: Int, top: Int, baseline: Int, bottom: Int, text: CharSequence, start: Int, end: Int, lnum: Int) {
val color = paint.color
val alpha: Int = (preformatStyle.preformatBackgroundAlpha * 255).toInt()
paint.color = Color.argb(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface IAztecNestable {
fun getNestingLevelAt(spanned: Spanned, index: Int, nextIndex: Int = index): Int {
return spanned.getSpans(index, nextIndex, IAztecNestable::class.java)
.filter { spanned.getSpanEnd(it) != index || index == 0 || spanned[index - 1] != Constants.NEWLINE }
.maxBy { it.nestingLevel }?.nestingLevel ?: 0
.maxByOrNull { it.nestingLevel }?.nestingLevel ?: 0
}

fun getMinNestingLevelAt(spanned: Spanned, index: Int, nextIndex: Int = index): Int {
Expand All @@ -21,7 +21,7 @@ interface IAztecNestable {
.filter { spanned.getSpanStart(it) <= index && spanned.getSpanEnd(it) >= nextIndex &&
(spanned.getSpanStart(it) != index || spanned.getSpanEnd(it) != nextIndex) }
.filter { index != nextIndex || spanned.getSpanStart(it) != index && spanned.getSpanEnd(it) != index }
.minBy { it.nestingLevel }?.nestingLevel ?: 0
.minByOrNull { it.nestingLevel }?.nestingLevel ?: 0
}

fun pushDeeper(spannable: Spannable, start: Int, end: Int, fromLevel: Int = 0, pushBy: Int = 1): List<SpanWrapper<IAztecNestable>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,12 @@ class AztecToolbar : FrameLayout, IAztecToolbar, OnMenuItemClickListener {
isMediaToolbarVisible = restoredState.getBoolean("isMediaToolbarVisible")
setAdvancedState()
setupMediaToolbar()
editorContentParsedSHA256LastSwitch = restoredState.getByteArray(RETAINED_EDITOR_HTML_PARSED_SHA256_KEY)
sourceContentParsedSHA256LastSwitch = restoredState.getByteArray(RETAINED_SOURCE_HTML_PARSED_SHA256_KEY)
restoredState.getByteArray(RETAINED_EDITOR_HTML_PARSED_SHA256_KEY)?.let { editorContentParsedSHA256LastSwitch = it }
restoredState.getByteArray(RETAINED_SOURCE_HTML_PARSED_SHA256_KEY)?.let { sourceContentParsedSHA256LastSwitch = it }
}

override fun onSaveInstanceState(): Parcelable {
val superState = super.onSaveInstanceState()
override fun onSaveInstanceState(): Parcelable? {
val superState = super.onSaveInstanceState() ?: return null
val savedState = SourceViewEditText.SavedState(superState)
val bundle = Bundle()
bundle.putBoolean("isSourceVisible", sourceEditor?.visibility == View.VISIBLE)
Expand Down
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ allprojects {
}
}
google()
jcenter()
mavenCentral()
}
}

Expand Down Expand Up @@ -46,13 +46,13 @@ subprojects {

ext {
gradlePluginVersion = '3.3.1'
kotlinCoroutinesVersion = '1.1.0'
kotlinCoroutinesVersion = '1.3.8'
tagSoupVersion = '1.2.1'
glideVersion = '4.10.0'
glideVersion = '4.12.0'
picassoVersion = '2.5.2'
robolectricVersion = '4.4'
jUnitVersion = '4.12'
jSoupVersion = '1.11.3'
robolectricVersion = '4.6.1'
jUnitVersion = '4.13.2'
jSoupVersion = '1.13.1'
wordpressUtilsVersion = 'trunk-1ed207c03d2242b6fc3d74f9e388e9163cbc82a6'
espressoVersion = '3.0.1'
commonTargetSdkVersion = 30
Expand Down
2 changes: 1 addition & 1 deletion glide-loader/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

android {
compileSdkVersion 28
compileSdkVersion 31

defaultConfig {
minSdkVersion commonMinSdkVersion
Expand Down
Loading