-
Notifications
You must be signed in to change notification settings - Fork 7
Add toolbar formatting options to the new rich html editor #1996
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
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
89baa4e
Redo the tooolbar layout
LunarX e788305
Connect format option buttons to their respective action
LunarX 12071f2
Disable toolbar formatting options when the webview is unfocused
LunarX d2f4567
Coherent naming across editor format action methods
LunarX 5d9af99
Correctly build the toolbar content so it's easy to swap between the …
LunarX 9c1f0c5
Add content descriptions to the formatting options buttons
LunarX 9fa8954
Remove send button and divider from the formatting options toolbar
LunarX 539b8dc
Add insert link dialog
LunarX 4285859
Use common dialog title
LunarX 9eea30b
Move link inside edition toolbar so it can be displayed as activated …
LunarX b620a10
Remove prefilled default values until we actually handle them properly
LunarX efb21df
Focus the editor if the format toolbar is opened
LunarX 8a86e25
Add url validation
LunarX b09d440
Fix url error end icon padding
LunarX cfde8f4
Correctly reset the insert dialog edit text states when opening the d…
LunarX 3a3ccac
Fix insert dialog paddings
LunarX 56362d8
Automatically focus url field when opening insert link dialog
LunarX 51d8bf0
Simplify the synchronizing of display text and url to avoid unnecessa…
LunarX b3a225c
Clean some code before opening the PR for reviews
LunarX 9232b21
Add missing RCore import
KevinBoulongne b4c7a55
Only set `urlEditText.doOnTextChanged { … }` listener once
KevinBoulongne 4e98e05
Extract PROTOCOL_SEPARATOR to companion object
KevinBoulongne 250cee4
Oneline validate method for link insertion
KevinBoulongne cfb8e26
Fix wrongly renamed view
LunarX 7d08669
Remove unnecessary var
LunarX e362cce
Remove useless new line
LunarX 4c19040
Refactor updateEditorVisibility with better method and argument name
LunarX 1214937
Add missing coma
LunarX 31b0075
Fix comment
LunarX 7755c24
Simplify editor focus handling
LunarX b518377
Apply suggestions from code review
LunarX File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
app/src/main/java/com/infomaniak/mail/ui/newMessage/InsertLinkDialog.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* Infomaniak Mail - Android | ||
* Copyright (C) 2024 Infomaniak Network SA | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.infomaniak.mail.ui.newMessage | ||
|
||
import android.content.Context | ||
import android.content.DialogInterface | ||
import android.util.Patterns | ||
import androidx.appcompat.app.AlertDialog | ||
import androidx.core.widget.addTextChangedListener | ||
import androidx.core.widget.doOnTextChanged | ||
import com.google.android.material.dialog.MaterialAlertDialogBuilder | ||
import com.infomaniak.lib.core.utils.context | ||
import com.infomaniak.lib.core.utils.showKeyboard | ||
import com.infomaniak.mail.R | ||
import com.infomaniak.mail.databinding.DialogInsertLinkBinding | ||
import com.infomaniak.mail.ui.alertDialogs.BaseAlertDialog | ||
import com.infomaniak.mail.utils.extensions.trimmedText | ||
import dagger.hilt.android.qualifiers.ActivityContext | ||
import dagger.hilt.android.scopes.ActivityScoped | ||
import javax.inject.Inject | ||
import com.infomaniak.lib.core.R as RCore | ||
|
||
@ActivityScoped | ||
class InsertLinkDialog @Inject constructor( | ||
@ActivityContext private val activityContext: Context, | ||
) : BaseAlertDialog(activityContext) { | ||
|
||
val binding: DialogInsertLinkBinding by lazy { DialogInsertLinkBinding.inflate(activity.layoutInflater) } | ||
private var addLink: ((String, String) -> Unit)? = null | ||
|
||
override val alertDialog: AlertDialog = with(binding) { | ||
showDisplayNamePreview() | ||
|
||
MaterialAlertDialogBuilder(context) | ||
.setView(root) | ||
.setPositiveButton(R.string.buttonConfirm, null) | ||
.setNegativeButton(RCore.string.buttonCancel, null) | ||
.create() | ||
.also { | ||
it.setOnShowListener { dialog -> | ||
urlEditText.showKeyboard() | ||
resetDialogState() | ||
setConfirmButtonListener(dialog) | ||
} | ||
urlEditText.doOnTextChanged { _, _, _, _ -> | ||
urlLayout.setError(null) | ||
} | ||
} | ||
} | ||
|
||
override fun resetCallbacks() { | ||
addLink = null | ||
} | ||
|
||
fun show(defaultDisplayNameValue: String = "", defaultUrlValue: String = "", addLinkCallback: (String, String) -> Unit) { | ||
binding.apply { | ||
displayNameEditText.setText(defaultDisplayNameValue) | ||
urlEditText.setText(defaultUrlValue) | ||
} | ||
|
||
addLink = addLinkCallback | ||
alertDialog.show() | ||
} | ||
|
||
// Pre-fills the display name with the url's content if the fields contain the same value. | ||
private fun showDisplayNamePreview() = with(binding) { | ||
displayNameEditText.setTextColor(activityContext.getColor(R.color.tertiaryTextColor)) | ||
|
||
var areInputsSynced = false | ||
urlEditText.addTextChangedListener( | ||
beforeTextChanged = { text, _, _, _ -> | ||
areInputsSynced = text.toString() == displayNameEditText.text.toString() | ||
}, | ||
onTextChanged = { text, _, _, _ -> | ||
if (areInputsSynced || displayNameEditText.text.isNullOrBlank()) displayNameEditText.setText(text) | ||
} | ||
) | ||
|
||
displayNameEditText.setOnFocusChangeListener { _, hasFocus -> | ||
val textColor = activityContext.getColor(if (hasFocus) R.color.primaryTextColor else R.color.tertiaryTextColor) | ||
displayNameEditText.setTextColor(textColor) | ||
} | ||
} | ||
|
||
private fun resetDialogState() = with(binding) { | ||
urlLayout.setError(null) | ||
displayNameLayout.placeholderText = null | ||
} | ||
|
||
private fun setConfirmButtonListener(dialog: DialogInterface) = with(binding) { | ||
(dialog as AlertDialog).getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { | ||
val url = addMissingHttpsProtocol(urlEditText.trimmedText) | ||
|
||
if (validate(url)) { | ||
val displayName = (displayNameEditText.text?.takeIf { it.isNotBlank() } ?: urlEditText.text).toString().trim() | ||
addLink?.invoke(displayName, url) | ||
|
||
dialog.dismiss() | ||
} else { | ||
urlLayout.setError(activityContext.getString(R.string.snackbarInvalidUrl)) | ||
} | ||
} | ||
} | ||
|
||
private fun addMissingHttpsProtocol(link: String): String { | ||
val protocolEndIndex = link.indexOf(PROTOCOL_SEPARATOR) | ||
val isProtocolSpecified = protocolEndIndex > 0 // If there is a specified protocol and it is at least 1 char long | ||
|
||
if (isProtocolSpecified) return link | ||
|
||
val strippedUserInput = if (protocolEndIndex == -1) link else link.substring(PROTOCOL_SEPARATOR.length) | ||
|
||
return "https://$strippedUserInput" | ||
} | ||
|
||
private fun validate(userUrlInput: String): Boolean = Patterns.WEB_URL.matcher(userUrlInput).matches() | ||
|
||
companion object { | ||
private const val PROTOCOL_SEPARATOR = "://" | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.