-
Notifications
You must be signed in to change notification settings - Fork 121
Fix - Add missing hasChanges method to HTML editor #682
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
Changes from all commits
1f6b83d
31c2617
81067d4
003ac78
e871366
083459c
4405aaf
77a5a40
547cbd9
a528d57
e6afd51
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| package org.wordpress.aztec | ||
|
|
||
| import android.os.Parcel | ||
| import android.os.Parcelable | ||
| import java.security.MessageDigest | ||
| import java.security.NoSuchAlgorithmException | ||
| import java.util.Arrays | ||
|
|
||
| class AztecInitialContentHolder() : Parcelable { | ||
|
|
||
| enum class EditorHasChanges { | ||
| CHANGES, NO_CHANGES, UNKNOWN | ||
| } | ||
|
|
||
| interface EditorHasChangesInterface { | ||
| fun hasChanges(): AztecInitialContentHolder.EditorHasChanges | ||
| } | ||
|
|
||
| constructor(parcel : Parcel) : this() { | ||
| initialEditorContentParsedSHA256 = ByteArray(parcel.readInt()) | ||
| parcel.readByteArray(initialEditorContentParsedSHA256) | ||
| } | ||
|
|
||
| override fun writeToParcel(dest: Parcel?, flags: Int) { | ||
| dest?.writeInt(initialEditorContentParsedSHA256.size) | ||
| dest?.writeByteArray(initialEditorContentParsedSHA256) | ||
| } | ||
|
|
||
| override fun describeContents(): Int { | ||
| return 0 | ||
| } | ||
|
|
||
| companion object { | ||
| @JvmField | ||
| val CREATOR: Parcelable.Creator<AztecInitialContentHolder> = object : Parcelable.Creator<AztecInitialContentHolder> { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AS says this property
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tried to suppress with annotation, but no luck. There are other instances of it in Aztec without any annotation, so I left it without adding it. |
||
| override fun createFromParcel(`in`: Parcel): AztecInitialContentHolder { | ||
| return AztecInitialContentHolder(`in`) | ||
| } | ||
|
|
||
| override fun newArray(size: Int): Array<AztecInitialContentHolder?> { | ||
| return arrayOfNulls(size) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private var initialEditorContentParsedSHA256: ByteArray = ByteArray(0) | ||
|
|
||
| fun needToSetInitialValue(): Boolean { | ||
| return (initialEditorContentParsedSHA256.isEmpty() || Arrays.equals(initialEditorContentParsedSHA256, calculateSHA256(""))) | ||
| } | ||
|
|
||
| fun setInitialContent(source: String) { | ||
| try { | ||
| // Do not recalculate the hash if it's not the first call to `fromHTML`. | ||
| if (needToSetInitialValue()) { | ||
| initialEditorContentParsedSHA256 = calculateSHA256(source) | ||
| } | ||
| } catch (e: Throwable) { | ||
| // Do nothing here. calculateSHA256 -> NoSuchAlgorithmException | ||
| } | ||
| } | ||
|
|
||
| @Throws(NoSuchAlgorithmException::class) | ||
| private fun calculateSHA256(s: String): ByteArray { | ||
| val digest = MessageDigest.getInstance("SHA-256") | ||
| digest.update(s.toByteArray()) | ||
| return digest.digest() | ||
| } | ||
|
|
||
| fun hasChanges(source: String): EditorHasChanges { | ||
| if (!initialEditorContentParsedSHA256.isEmpty()) { | ||
| try { | ||
| if (Arrays.equals(initialEditorContentParsedSHA256, calculateSHA256(source))) { | ||
| return EditorHasChanges.NO_CHANGES | ||
| } | ||
| return EditorHasChanges.CHANGES | ||
| } catch (e: Throwable) { | ||
| // Do nothing here. calculateSHA256 -> NoSuchAlgorithmException | ||
| } | ||
| } | ||
| return EditorHasChanges.UNKNOWN | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call moving the
EditorHasChangesenum to theAztecInitialContentHolderclass 👍