Skip to content

Commit

Permalink
Add clickable links in EditFragment
Browse files Browse the repository at this point in the history
Closes #12

Also fix "checked item header" not correctly aligned in RTL layout
  • Loading branch information
maltaisn committed Jul 30, 2021
1 parent 40e4f66 commit 1416dd2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## v1.4.0
- Added a label attribute to hide all notes with that label in active & archive destinations.
The notes are still visible in the trash and in the label's destination.
- Added clickable links for website & email in edit screen.
- Added Spanish translation
- Show keyboard when note is converted to text or list.
- Fixed bug allowing to create two labels with the same name, and blank labels.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ package com.maltaisn.notes.ui.edit.adapter
import android.text.Editable
import android.text.TextWatcher
import android.text.format.DateUtils
import android.text.method.LinkMovementMethod
import android.text.style.CharacterStyle
import android.text.util.Linkify
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import androidx.core.text.getSpans
import androidx.core.text.util.LinkifyCompat
import androidx.core.view.isInvisible
import androidx.core.widget.doOnTextChanged
import androidx.core.widget.addTextChangedListener
import androidx.core.widget.doAfterTextChanged
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.chip.Chip
import com.maltaisn.notes.hideKeyboard
Expand Down Expand Up @@ -95,6 +99,7 @@ class EditTitleViewHolder(binding: ItemEditTitleBinding, callback: EditAdapter.C
override fun setFocus(pos: Int) {
titleEdt.requestFocus()
titleEdt.setSelection(pos)
titleEdt.showKeyboard()
}
}

Expand All @@ -106,6 +111,13 @@ class EditContentViewHolder(binding: ItemEditContentBinding, callback: EditAdapt
init {
contentEdt.addTextChangedListener(BulletTextWatcher())
contentEdt.addTextChangedListener(clearSpansTextWatcher)
contentEdt.doAfterTextChanged { editable ->
// Add new links
LinkifyCompat.addLinks(editable ?: return@doAfterTextChanged,
Linkify.WEB_URLS or Linkify.EMAIL_ADDRESSES)
}
contentEdt.movementMethod = LinkMovementMethod.getInstance() // Clickable links

contentEdt.setOnClickListener {
callback.onNoteClickedToEdit()
}
Expand Down Expand Up @@ -157,16 +169,24 @@ class EditItemViewHolder(binding: ItemEditItemBinding, callback: EditAdapter.Cal

itemEdt.addTextChangedListener(clearSpansTextWatcher)

itemEdt.doOnTextChanged { _, _, _, count ->
// This is used to detect when user enters line breaks into the input, so the
// item can be split into multiple items. When user enters a single line break,
// selection is set at the beginning of new item. On paste, i.e. when more than one
// character is entered, selection is set at the end of last new item.
val pos = bindingAdapterPosition
if (pos != RecyclerView.NO_POSITION) {
callback.onNoteItemChanged(pos, count > 1)
}
}
itemEdt.addTextChangedListener(
beforeTextChanged = { _, _, _, _ -> },
onTextChanged = { _, _, _, count ->
// This is used to detect when user enters line breaks into the input, so the
// item can be split into multiple items. When user enters a single line break,
// selection is set at the beginning of new item. On paste, i.e. when more than one
// character is entered, selection is set at the end of last new item.
val pos = bindingAdapterPosition
if (pos != RecyclerView.NO_POSITION) {
callback.onNoteItemChanged(pos, count > 1)
}
},
afterTextChanged = { editable ->
// Add new links
LinkifyCompat.addLinks(editable ?: return@addTextChangedListener,
Linkify.WEB_URLS or Linkify.EMAIL_ADDRESSES)
})
itemEdt.movementMethod = LinkMovementMethod.getInstance() // Clickable links
itemEdt.setOnFocusChangeListener { _, hasFocus ->
// Only show delete icon for currently focused item.
deleteImv.isInvisible = !hasFocus
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/item_edit_content.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@
android:inputType="textMultiLine|textLongMessage|textCapSentences|textAutoCorrect"
android:textAppearance="?textAppearanceBody1"
android:textSize="15sp"
android:autoLink="web|email"
android:linksClickable="true"
/>
4 changes: 3 additions & 1 deletion app/src/main/res/layout/item_edit_header.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/title_txv"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="8dp"
android:paddingStart="40dp"
android:paddingEnd="16dp"
android:maxLines="1"
android:ellipsize="end"
android:textAppearance="@style/TextAppearance.MaterialComponents.Body2"
android:textColor="@color/material_on_background_emphasis_medium"
android:textStyle="bold"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/item_edit_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
android:textDirection="anyRtl"
android:textColor="@color/item_edit_item_text_color"
android:textSize="15sp"
android:autoLink="web|email"
android:linksClickable="true"
tools:ignore="LabelFor"
tools:text="Mauris rutrum massa placerat felis pharetra tristique. Nulla sed urna a dui vehicula euismod."
/>
Expand Down

0 comments on commit 1416dd2

Please sign in to comment.