CorePlugin hasExplicitMovementMethodPlugin configuration

This commit is contained in:
Dimitry Ivanov 2020-05-28 13:03:50 +03:00
parent 03770cfe2d
commit 8e332712fe
4 changed files with 65 additions and 4 deletions

View File

@ -1,8 +1,17 @@
# Changelog # Changelog
# $nap; # $nap;
#### Added
* `core` - `MovementMethodPlugin.none()`, `MovementMethodPlugin.link()` factory methods
* `core` - `CorePlugin` `hasExplicitMovementMethodPlugin` configuration method to **not** add implicit `LinkMovementMethod` on `afterSetText`
#### Changed
* `html` - `SimpleTagHandler` visits children tags if supplied tag is block one ([#235]) * `html` - `SimpleTagHandler` visits children tags if supplied tag is block one ([#235])
#### Deprecated
* `core` - `MovementMethodPlugin.create()` use explicit `MovementMethodPlugin.link()` instead
[#235]: https://github.com/noties/Markwon/issues/235 [#235]: https://github.com/noties/Markwon/issues/235
# 4.4.0 # 4.4.0

View File

@ -115,9 +115,22 @@ public class CorePlugin extends AbstractMarkwonPlugin {
// @since 4.0.0 // @since 4.0.0
private final List<OnTextAddedListener> onTextAddedListeners = new ArrayList<>(0); private final List<OnTextAddedListener> onTextAddedListeners = new ArrayList<>(0);
// @since $nap;
private boolean hasExplicitMovementMethodPlugin;
protected CorePlugin() { protected CorePlugin() {
} }
/**
* @since $nap;
*/
@SuppressWarnings("UnusedReturnValue")
@NonNull
public CorePlugin hasExplicitMovementMethodPlugin(boolean hasExplicitMovementMethodPlugin) {
this.hasExplicitMovementMethodPlugin = hasExplicitMovementMethodPlugin;
return this;
}
/** /**
* Can be useful to post-process text added. For example for auto-linking capabilities. * Can be useful to post-process text added. For example for auto-linking capabilities.
* *
@ -188,7 +201,8 @@ public class CorePlugin extends AbstractMarkwonPlugin {
// let's ensure that there is a movement method applied // let's ensure that there is a movement method applied
// we do it `afterSetText` so any user-defined movement method won't be // we do it `afterSetText` so any user-defined movement method won't be
// replaced (it should be done in `beforeSetText` or manually on a TextView) // replaced (it should be done in `beforeSetText` or manually on a TextView)
if (textView.getMovementMethod() == null) { // @since $nap; we additionally check if we should apply _implicit_ movement method
if (!hasExplicitMovementMethodPlugin && textView.getMovementMethod() == null) {
textView.setMovementMethod(LinkMovementMethod.getInstance()); textView.setMovementMethod(LinkMovementMethod.getInstance());
} }
} }

View File

@ -6,8 +6,10 @@ import android.text.method.MovementMethod;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.noties.markwon.AbstractMarkwonPlugin; import io.noties.markwon.AbstractMarkwonPlugin;
import io.noties.markwon.core.CorePlugin;
/** /**
* @since 3.0.0 * @since 3.0.0
@ -19,26 +21,62 @@ public class MovementMethodPlugin extends AbstractMarkwonPlugin {
* Uses Android system LinkMovementMethod as default * Uses Android system LinkMovementMethod as default
* *
* @see #create(MovementMethod) * @see #create(MovementMethod)
* @see #link()
* @deprecated $nap; use {@link #link()}
*/ */
@NonNull @NonNull
@Deprecated
public static MovementMethodPlugin create() { public static MovementMethodPlugin create() {
return create(LinkMovementMethod.getInstance()); return create(LinkMovementMethod.getInstance());
} }
/**
* @since $nap;
*/
@NonNull
public static MovementMethodPlugin link() {
return create(LinkMovementMethod.getInstance());
}
/**
* Special {@link MovementMethodPlugin} that is <strong>not</strong> applying a MovementMethod on a TextView
* implicitly
*
* @since $nap;
*/
@NonNull
public static MovementMethodPlugin none() {
return new MovementMethodPlugin(null);
}
@NonNull @NonNull
public static MovementMethodPlugin create(@NonNull MovementMethod movementMethod) { public static MovementMethodPlugin create(@NonNull MovementMethod movementMethod) {
return new MovementMethodPlugin(movementMethod); return new MovementMethodPlugin(movementMethod);
} }
@Nullable
private final MovementMethod movementMethod; private final MovementMethod movementMethod;
/**
* Since $nap; change to be <em>nullable</em>
*/
@SuppressWarnings("WeakerAccess") @SuppressWarnings("WeakerAccess")
MovementMethodPlugin(@NonNull MovementMethod movementMethod) { MovementMethodPlugin(@Nullable MovementMethod movementMethod) {
this.movementMethod = movementMethod; this.movementMethod = movementMethod;
} }
@Override
public void configure(@NonNull Registry registry) {
registry.require(CorePlugin.class)
.hasExplicitMovementMethodPlugin(true);
}
@Override @Override
public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) { public void beforeSetText(@NonNull TextView textView, @NonNull Spanned markdown) {
textView.setMovementMethod(movementMethod); // @since $nap; check for equality
final MovementMethod current = textView.getMovementMethod();
if (current != movementMethod) {
textView.setMovementMethod(movementMethod);
}
} }
} }

View File

@ -39,6 +39,7 @@ import io.noties.markwon.image.destination.ImageDestinationProcessorRelativeToAb
import io.noties.markwon.image.file.FileSchemeHandler; import io.noties.markwon.image.file.FileSchemeHandler;
import io.noties.markwon.image.network.OkHttpNetworkSchemeHandler; import io.noties.markwon.image.network.OkHttpNetworkSchemeHandler;
import io.noties.markwon.image.svg.SvgMediaDecoder; import io.noties.markwon.image.svg.SvgMediaDecoder;
import io.noties.markwon.movement.MovementMethodPlugin;
import io.noties.markwon.recycler.MarkwonAdapter; import io.noties.markwon.recycler.MarkwonAdapter;
import io.noties.markwon.recycler.SimpleEntry; import io.noties.markwon.recycler.SimpleEntry;
import io.noties.markwon.recycler.table.TableEntry; import io.noties.markwon.recycler.table.TableEntry;
@ -82,7 +83,6 @@ public class RecyclerActivity extends Activity {
@NonNull @NonNull
private static Markwon markwon(@NonNull Context context) { private static Markwon markwon(@NonNull Context context) {
return Markwon.builder(context) return Markwon.builder(context)
.usePlugin(CorePlugin.create())
.usePlugin(ImagesPlugin.create(plugin -> { .usePlugin(ImagesPlugin.create(plugin -> {
plugin plugin
.addSchemeHandler(FileSchemeHandler.createWithAssets(context)) .addSchemeHandler(FileSchemeHandler.createWithAssets(context))