From aac69e28b429ab762cf6c24ed0ca945d494ad7c2 Mon Sep 17 00:00:00 2001 From: Filip Date: Tue, 30 Jan 2018 00:16:53 +0100 Subject: [PATCH] Fatal bug fixes --- app/build.gradle | 4 +-- .../viewholders/PMMessageViewHolder.kt | 10 ++----- .../mainnavigation/MainNavigationActivity.kt | 1 - .../ui/widgets/entry/EntryPresenter.kt | 2 ++ .../ui/widgets/entry/EntryWidget.kt | 10 ++----- .../ui/widgets/entry/comment/CommentWidget.kt | 8 ++---- .../ui/widgets/link/LinkWidget.kt | 15 ++++++++--- .../textview/BetterLinkMovementMethod.java | 26 ++++++++++++++++-- .../utils/textview/SpoilerAwareLinkSpan.kt | 2 +- .../utils/textview/TextViewExtensions.kt | 27 ++++++++++++++++--- .../wykop_link_handler/WykopLinkHandler.kt | 2 ++ 11 files changed, 73 insertions(+), 34 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index e539a8ba9..723ee71a4 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -28,8 +28,8 @@ android { applicationId "io.github.feelfreelinux.wykopmobilny" minSdkVersion 17 targetSdkVersion 27 - versionCode 24 - versionName "0.5.3.1" + versionCode 25 + versionName "0.5.3.2" def credentialsPropertiesFile = rootProject.file("credentials.properties") def credentialsProperties = new Properties() diff --git a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/adapters/viewholders/PMMessageViewHolder.kt b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/adapters/viewholders/PMMessageViewHolder.kt index 2c8520346..eba4225d7 100755 --- a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/adapters/viewholders/PMMessageViewHolder.kt +++ b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/adapters/viewholders/PMMessageViewHolder.kt @@ -10,7 +10,6 @@ import io.github.feelfreelinux.wykopmobilny.models.dataclass.PMMessage import io.github.feelfreelinux.wykopmobilny.ui.modules.NewNavigatorApi import io.github.feelfreelinux.wykopmobilny.utils.SettingsPreferencesApi import io.github.feelfreelinux.wykopmobilny.utils.getActivityContext -import io.github.feelfreelinux.wykopmobilny.utils.textview.URLClickedListener import io.github.feelfreelinux.wykopmobilny.utils.textview.prepareBody import io.github.feelfreelinux.wykopmobilny.utils.toPrettyDate import io.github.feelfreelinux.wykopmobilny.utils.wykop_link_handler.WykopLinkHandlerApi @@ -19,15 +18,10 @@ import kotlinx.android.synthetic.main.pmmessage_sent_layout.view.* class PMMessageViewHolder(val view: View, val linkHandlerApi: WykopLinkHandlerApi, val settingsPreferencesApi: SettingsPreferencesApi, - val navigatorApi: NewNavigatorApi) : RecyclerView.ViewHolder(view), URLClickedListener { - - override fun handleUrl(url: String) { - linkHandlerApi.handleUrl(url) - } + val navigatorApi: NewNavigatorApi) : RecyclerView.ViewHolder(view) { fun bindView(message: PMMessage) { flipMessage(message.isSentFromUser) - view.apply { val prettyDate = message.date.toPrettyDate() date.text = prettyDate @@ -36,7 +30,7 @@ class PMMessageViewHolder(val view: View, date.text = context.getString(R.string.date_with_user_app, prettyDate, message.app) } - body.prepareBody(message.body, this@PMMessageViewHolder) + body.prepareBody(message.body, { linkHandlerApi.handleUrl(it) }) embedImage.forceDisableMinimizedMode = true embedImage.setEmbed(message.embed, settingsPreferencesApi, navigatorApi) } diff --git a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/modules/mainnavigation/MainNavigationActivity.kt b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/modules/mainnavigation/MainNavigationActivity.kt index 6ecbd046b..716f69538 100755 --- a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/modules/mainnavigation/MainNavigationActivity.kt +++ b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/modules/mainnavigation/MainNavigationActivity.kt @@ -142,7 +142,6 @@ class MainNavigationActivity : BaseActivity(), MainNavigationView, NavigationVie .setButtonDoNotShowAgain(R.string.do_not_show_again) .setButtonUpdate(R.string.update) .start() - if (settingsApi.showNotifications) { // Schedules notification service WykopNotificationsJob.schedule(settingsApi) diff --git a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/entry/EntryPresenter.kt b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/entry/EntryPresenter.kt index f23452d62..f7722f358 100755 --- a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/entry/EntryPresenter.kt +++ b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/entry/EntryPresenter.kt @@ -6,6 +6,7 @@ import io.github.feelfreelinux.wykopmobilny.base.Schedulers import io.github.feelfreelinux.wykopmobilny.models.dataclass.Entry import io.github.feelfreelinux.wykopmobilny.ui.modules.NewNavigatorApi import io.github.feelfreelinux.wykopmobilny.utils.ClipboardHelperApi +import io.github.feelfreelinux.wykopmobilny.utils.printout import io.github.feelfreelinux.wykopmobilny.utils.textview.removeHtml import io.github.feelfreelinux.wykopmobilny.utils.wykop_link_handler.WykopLinkHandlerApi @@ -82,6 +83,7 @@ class EntryPresenter(val schedulers: Schedulers, } fun handleLink(url : String) { + printout("DDD") linkHandler.handleUrl(url, false) } diff --git a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/entry/EntryWidget.kt b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/entry/EntryWidget.kt index 356a93512..b05c6ea3b 100755 --- a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/entry/EntryWidget.kt +++ b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/entry/EntryWidget.kt @@ -21,14 +21,13 @@ import io.github.feelfreelinux.wykopmobilny.utils.api.getGroupColor import io.github.feelfreelinux.wykopmobilny.utils.appendNewSpan import io.github.feelfreelinux.wykopmobilny.utils.getActivityContext import io.github.feelfreelinux.wykopmobilny.utils.isVisible -import io.github.feelfreelinux.wykopmobilny.utils.textview.URLClickedListener import io.github.feelfreelinux.wykopmobilny.utils.textview.prepareBody import io.github.feelfreelinux.wykopmobilny.utils.usermanager.UserManagerApi import kotlinx.android.synthetic.main.dialog_voters.view.* import kotlinx.android.synthetic.main.entry_layout.view.* import kotlinx.android.synthetic.main.entry_menu_bottomsheet.view.* -class EntryWidget(context: Context, attrs: AttributeSet) : CardView(context, attrs), EntryView, URLClickedListener { +class EntryWidget(context: Context, attrs: AttributeSet) : CardView(context, attrs), EntryView { private lateinit var userManagerApi: UserManagerApi private lateinit var presenter: EntryPresenter @@ -115,8 +114,7 @@ class EntryWidget(context: Context, attrs: AttributeSet) : CardView(context, att private fun setupBody() { if (entry.body.isNotEmpty()) { entryContentTextView.isVisible = true - entryContentTextView.prepareBody(entry.body, this) - if (shouldEnableClickListener) entryContentTextView.setOnClickListener { presenter.openDetails(entry.embed?.isRevealed ?: false) } + entryContentTextView.prepareBody(entry.body, { presenter.handleLink(it) }, { if (shouldEnableClickListener) presenter.openDetails(true) }) } else entryContentTextView.isVisible = false if (entry.survey != null) { @@ -129,10 +127,6 @@ class EntryWidget(context: Context, attrs: AttributeSet) : CardView(context, att survey.setSurvey(surveyData, userManagerApi) } - override fun handleUrl(url: String) { - presenter.handleLink(url) - } - override fun showErrorDialog(e: Throwable) = context.showExceptionDialog(e) diff --git a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/entry/comment/CommentWidget.kt b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/entry/comment/CommentWidget.kt index dc8a2dba5..49ed0d544 100755 --- a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/entry/comment/CommentWidget.kt +++ b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/entry/comment/CommentWidget.kt @@ -29,7 +29,7 @@ import kotlinx.android.synthetic.main.dialog_voters.view.* import kotlinx.android.synthetic.main.entry_comment_layout.view.* import kotlinx.android.synthetic.main.entry_comment_menu_bottomsheet.view.* -class CommentWidget : CardView, CommentView, URLClickedListener { +class CommentWidget : CardView, CommentView { constructor(context: Context) : super(context) constructor(context: Context, attrs: AttributeSet) : super(context, attrs) @@ -103,14 +103,10 @@ class CommentWidget : CardView, CommentView, URLClickedListener { replyTextView.setOnClickListener { addReceiver() } if (comment.body.isNotEmpty()) { entryContentTextView.isVisible = true - entryContentTextView.prepareBody(comment.body, this) + entryContentTextView.prepareBody(comment.body, { presenter.handleLink(it) }) } else entryContentTextView.isVisible = false } - override fun handleUrl(url: String) { - presenter.handleLink(url) - } - fun addReceiver() { addReceiverListener?.invoke(comment.author) } diff --git a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/link/LinkWidget.kt b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/link/LinkWidget.kt index b630be35d..4f8c44c20 100644 --- a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/link/LinkWidget.kt +++ b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/ui/widgets/link/LinkWidget.kt @@ -65,11 +65,20 @@ class LinkWidget(context: Context, attrs: AttributeSet) : CardView(context, attr private fun setupHeader() { dateTextView.text = link.date.toPrettyDate() hotBadgeStrip.isVisible = link.isHot - avatarView.setAuthor(link.author!!) + val author = link.author + if (author != null) { + avatarView.isVisible = true + userTextView.isVisible = true + avatarView.setAuthor(link.author!!) + userTextView.text = link.author!!.nick + userTextView.setTextColor(context.getGroupColor(link.author!!.group)) + } else { + avatarView.isVisible = false + userTextView.isVisible = false + } dateTextView.text = link.date.toPrettyDate() urlTextView.text = URL(link.sourceUrl).host.removePrefix("www.") - userTextView.text = link.author!!.nick - userTextView.setTextColor(context.getGroupColor(link.author!!.group)) + tagsTextView.prepareBody(link.tags.convertToTagsHtml(), this) } diff --git a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/textview/BetterLinkMovementMethod.java b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/textview/BetterLinkMovementMethod.java index e02a147bf..cea836979 100755 --- a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/textview/BetterLinkMovementMethod.java +++ b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/textview/BetterLinkMovementMethod.java @@ -19,6 +19,8 @@ import android.view.Window; import android.widget.TextView; +import static io.github.feelfreelinux.wykopmobilny.utils.LogHelperKt.printout; + /** * Handles URL clicks on TextViews. Unlike the default implementation, this: *

@@ -35,6 +37,7 @@ public class BetterLinkMovementMethod extends LinkMovementMethod { private static final int LINKIFY_NONE = -2; private OnLinkClickListener onLinkClickListener; + private OnTextClickListener onTextClickListener; private OnLinkLongClickListener onLinkLongClickListener; private final RectF touchedLineBounds = new RectF(); private boolean isUrlHighlighted; @@ -52,6 +55,10 @@ public interface OnLinkClickListener { boolean onClick(TextView textView, String url); } + public interface OnTextClickListener { + void onClick(); + } + public interface OnLinkLongClickListener { /** * @param textView The TextView on which a long-click was registered. @@ -166,6 +173,16 @@ public BetterLinkMovementMethod setOnLinkClickListener(OnLinkClickListener click return this; } + public BetterLinkMovementMethod setOnTextClickListener(OnTextClickListener clickListener) { + if (this == singleInstance) { + throw new UnsupportedOperationException("Setting a click listener on the instance returned by getInstance() is not supported to avoid memory " + + "leaks. Please use newInstance() or any of the linkify() methods instead."); + } + + this.onTextClickListener = clickListener; + return this; + } + /** * Set a listener that will get called whenever any link is clicked on the TextView. */ @@ -240,8 +257,12 @@ public void onTimerReached() { case MotionEvent.ACTION_UP: // Register a click only if the touch started and ended on the same URL. - if (!wasLongPressRegistered && touchStartedOverALink && clickableSpanUnderTouch == clickableSpanUnderTouchOnActionDown) { - dispatchUrlClick(textView, clickableSpanUnderTouch); + if (!wasLongPressRegistered) { + if (touchStartedOverALink && clickableSpanUnderTouch == clickableSpanUnderTouchOnActionDown) { + dispatchUrlClick(textView, clickableSpanUnderTouch); + } else { + onTextClickListener.onClick(); + } } cleanupOnTouchUp(textView); @@ -386,6 +407,7 @@ protected void dispatchUrlClick(TextView textView, ClickableSpan clickableSpan) boolean handled = onLinkClickListener != null && onLinkClickListener.onClick(textView, clickableSpanWithText.text()); if (!handled) { + printout("AAAAA"); // Let Android handle this click. clickableSpanWithText.span().onClick(textView); } diff --git a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/textview/SpoilerAwareLinkSpan.kt b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/textview/SpoilerAwareLinkSpan.kt index b7c53ffa5..35a156f81 100755 --- a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/textview/SpoilerAwareLinkSpan.kt +++ b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/textview/SpoilerAwareLinkSpan.kt @@ -7,7 +7,7 @@ interface URLClickedListener { fun handleUrl(url : String) } -class SpoilerAwareLinkSpan(val url : String) : URLSpan(url) { +class ClickableSpanNoUnderline(val url : String) : URLSpan(url) { override fun updateDrawState(ds: TextPaint) { super.updateDrawState(ds) ds.isUnderlineText = false diff --git a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/textview/TextViewExtensions.kt b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/textview/TextViewExtensions.kt index c0c06f1a4..0ca4e3dfa 100755 --- a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/textview/TextViewExtensions.kt +++ b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/textview/TextViewExtensions.kt @@ -11,7 +11,6 @@ fun TextView.prepareBody(html: String, listener : URLClickedListener) { val method = BetterLinkMovementMethod.linkifyHtml(this) method.setOnLinkClickListener({ textView, url -> - textView.isEnabled = false if (url.startsWith("spoiler:")) { val text = url.substringAfter("spoiler:") context.createAlertBuilder().apply { @@ -21,7 +20,29 @@ fun TextView.prepareBody(html: String, listener : URLClickedListener) { create().show() } } else listener.handleUrl(url) - textView.isEnabled = true - false + true + }) +} + +fun TextView.prepareBody(html: String, urlClickListener : (String) -> Unit, clickListener : (() -> Unit)? = null) { + text = SpannableStringBuilder(html.toSpannable()) + val method = BetterLinkMovementMethod.linkifyHtml(this) + clickListener?.let { + method.setOnTextClickListener { + clickListener() + } + } + method.setOnLinkClickListener({ + _, url -> + if (url.startsWith("spoiler:")) { + val text = url.substringAfter("spoiler:") + context.createAlertBuilder().apply { + setTitle("Spoiler") + setMessage(URLDecoder.decode(text, "UTF-8")) + setPositiveButton(android.R.string.ok, null) + create().show() + } + } else urlClickListener(url) + true }) } \ No newline at end of file diff --git a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/wykop_link_handler/WykopLinkHandler.kt b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/wykop_link_handler/WykopLinkHandler.kt index 61cfad3e7..f3f0c65da 100755 --- a/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/wykop_link_handler/WykopLinkHandler.kt +++ b/app/src/main/kotlin/io/github/feelfreelinux/wykopmobilny/utils/wykop_link_handler/WykopLinkHandler.kt @@ -9,6 +9,7 @@ import io.github.feelfreelinux.wykopmobilny.ui.modules.links.linkdetails.LinkDet import io.github.feelfreelinux.wykopmobilny.ui.modules.mikroblog.entry.EntryActivity import io.github.feelfreelinux.wykopmobilny.ui.modules.pm.conversation.ConversationActivity import io.github.feelfreelinux.wykopmobilny.ui.modules.tag.TagActivity +import io.github.feelfreelinux.wykopmobilny.utils.printout import io.github.feelfreelinux.wykopmobilny.utils.wykop_link_handler.linkparser.ConversationLinkParser import io.github.feelfreelinux.wykopmobilny.utils.wykop_link_handler.linkparser.EntryLinkParser import io.github.feelfreelinux.wykopmobilny.utils.wykop_link_handler.linkparser.LinkParser @@ -69,6 +70,7 @@ class WykopLinkHandler(val context: Activity, private val navigatorApi: NewNavig } private fun handleLink(url: String, refreshNotifications : Boolean) { + val intent = getLinkIntent(url, context) if (intent != null) { if (refreshNotifications) context.startActivityForResult(intent, NewNavigator.STARTED_FROM_NOTIFICATIONS_CODE)