From e27d09c4f8cba6c5525fb823570546ae092e4fc5 Mon Sep 17 00:00:00 2001 From: Nick Mowen Date: Tue, 20 Nov 2018 11:28:22 -0700 Subject: [PATCH 1/2] Made link parsing much more efficient with regex and updated libraries --- .idea/misc.xml | 2 +- README.md | 2 +- linkpreview/build.gradle | 8 +++---- .../linkpreview/extension/UrlExtension.kt | 12 +++++++++- .../mowen/linkpreview/view/LinkPreview.kt | 23 ++++++++++--------- 5 files changed, 29 insertions(+), 18 deletions(-) diff --git a/.idea/misc.xml b/.idea/misc.xml index 26a9819..197c30e 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -35,7 +35,7 @@ - + diff --git a/README.md b/README.md index a19efba..2e17cda 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ On your module's `build.gradle` file add this statement to the `dependencies` se ```groovy dependencies { - implementation 'com.nick.mowen.linkpreview:linkpreview:2.0' + implementation 'com.nick.mowen.linkpreview:linkpreview:2.1' } ``` diff --git a/linkpreview/build.gradle b/linkpreview/build.gradle index 2ac6734..45ec828 100644 --- a/linkpreview/build.gradle +++ b/linkpreview/build.gradle @@ -9,8 +9,8 @@ android { defaultConfig { minSdkVersion 19 targetSdkVersion 28 - versionCode 15 - versionName "2.0" + versionCode 16 + versionName "2.1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { @@ -31,7 +31,7 @@ dependencies { implementation 'androidx.constraintlayout:constraintlayout:1.1.3' //Other - implementation 'org.jsoup:jsoup:1.11.2' + implementation 'org.jsoup:jsoup:1.11.3' //noinspection GradleDependency implementation 'com.github.bumptech.glide:glide:3.8.0' @@ -54,7 +54,7 @@ ext { publishedGroupId = 'com.nick.mowen.linkpreview' artifact = 'linkpreview' - libraryVersion = '2.0' + libraryVersion = '2.1' libraryDescription = 'A convenient view that shows a clickable preview of a link' siteUrl = 'https://github.com/NickM-27/LinkPreview' gitUrl = 'https://github.com/NickM-27/LinkPreview.git' diff --git a/linkpreview/src/main/java/com/nick/mowen/linkpreview/extension/UrlExtension.kt b/linkpreview/src/main/java/com/nick/mowen/linkpreview/extension/UrlExtension.kt index 80d19a3..4657ff9 100644 --- a/linkpreview/src/main/java/com/nick/mowen/linkpreview/extension/UrlExtension.kt +++ b/linkpreview/src/main/java/com/nick/mowen/linkpreview/extension/UrlExtension.kt @@ -3,4 +3,14 @@ package com.nick.mowen.linkpreview.extension /** * Checks whether a string is a url by using Regex */ -fun String.isUrl(): Boolean = this.matches(Regex("https?://(www\\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_+.~#?&/=]*)")) \ No newline at end of file +fun String.isUrl(): Boolean = this.matches(Regex(REGEX_URL)) + +/** + * Parses the link from the full string + */ +fun String.parseUrl(): String = REGEX_URL.toRegex().find(this)?.value ?: "" + +/** + * Regex pattern that matches to standard urls + */ +private const val REGEX_URL = "https?://(www\\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_+.~#?&/=]*)" \ No newline at end of file diff --git a/linkpreview/src/main/java/com/nick/mowen/linkpreview/view/LinkPreview.kt b/linkpreview/src/main/java/com/nick/mowen/linkpreview/view/LinkPreview.kt index 814b784..a4268b0 100644 --- a/linkpreview/src/main/java/com/nick/mowen/linkpreview/view/LinkPreview.kt +++ b/linkpreview/src/main/java/com/nick/mowen/linkpreview/view/LinkPreview.kt @@ -13,10 +13,7 @@ import androidx.core.net.toUri import com.bumptech.glide.Glide import com.nick.mowen.linkpreview.ImageType import com.nick.mowen.linkpreview.R -import com.nick.mowen.linkpreview.extension.addLink -import com.nick.mowen.linkpreview.extension.isUrl -import com.nick.mowen.linkpreview.extension.loadImage -import com.nick.mowen.linkpreview.extension.loadLinkMap +import com.nick.mowen.linkpreview.extension.* import com.nick.mowen.linkpreview.listener.LinkClickListener import com.nick.mowen.linkpreview.listener.LinkListener import kotlinx.coroutines.GlobalScope @@ -49,7 +46,11 @@ open class LinkPreview : FrameLayout, View.OnClickListener { bindViews(context) } - constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) { + constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super( + context, + attrs, + defStyle + ) { bindViews(context) } @@ -164,28 +165,28 @@ open class LinkPreview : FrameLayout, View.OnClickListener { * @return if a link was found in the text */ fun parseTextForLink(text: String): Boolean { - when { + return when { text.contains("youtube") && text.contains("v=") -> { val id = text.split("v=")[1].split(" ")[0] url = "https://www.youtube.com/watch?v=$id" setText() - return true + true } text.contains("youtu.be") -> { val id = text.split("be/")[1].split(" ")[0] url = "https://www.youtube.com/watch?v=$id" setText() - return true + true } text.contains("http") -> { - text.split(" ").filter { it.matches(Regex("https?://(www\\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_+.~#?&/=]*)")) }.forEach { url = it } + url = text.parseUrl() setText() - return true + true } else -> { imageType = ImageType.NONE visibility = View.GONE - return false + false } } } From 4d6cb1151029940b0cfc4c53e26887a9b6d31949 Mon Sep 17 00:00:00 2001 From: Nick Mowen Date: Thu, 31 Jan 2019 16:41:00 -0700 Subject: [PATCH 2/2] Added option for showing or hiding view initially --- .idea/assetWizardSettings.xml | 47 ------------------- .idea/codeStyles/codeStyleConfig.xml | 5 -- .idea/dictionaries/nickmowen.xml | 7 --- .idea/misc.xml | 40 ++-------------- .idea/vcs.xml | 2 +- build.gradle | 4 +- linkpreview/build.gradle | 10 ++-- .../linkpreview/binding/BindingAdapter.kt | 4 +- .../mowen/linkpreview/view/LinkPreview.kt | 7 ++- sample/build.gradle | 4 +- 10 files changed, 22 insertions(+), 108 deletions(-) delete mode 100644 .idea/assetWizardSettings.xml delete mode 100644 .idea/codeStyles/codeStyleConfig.xml delete mode 100644 .idea/dictionaries/nickmowen.xml diff --git a/.idea/assetWizardSettings.xml b/.idea/assetWizardSettings.xml deleted file mode 100644 index fc3261a..0000000 --- a/.idea/assetWizardSettings.xml +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml deleted file mode 100644 index a55e7a1..0000000 --- a/.idea/codeStyles/codeStyleConfig.xml +++ /dev/null @@ -1,5 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/dictionaries/nickmowen.xml b/.idea/dictionaries/nickmowen.xml deleted file mode 100644 index 63733f2..0000000 --- a/.idea/dictionaries/nickmowen.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - mozilla - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 197c30e..af0bbdd 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,41 +1,11 @@ - - - + + + + - + diff --git a/.idea/vcs.xml b/.idea/vcs.xml index 94a25f7..35eb1dd 100644 --- a/.idea/vcs.xml +++ b/.idea/vcs.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/build.gradle b/build.gradle index c5e5e38..895b190 100644 --- a/build.gradle +++ b/build.gradle @@ -1,13 +1,13 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.3.10' + ext.kotlin_version = '1.3.20' repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.3.0-beta04' + classpath 'com.android.tools.build:gradle:3.3.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" //For Library diff --git a/linkpreview/build.gradle b/linkpreview/build.gradle index 45ec828..75bf038 100644 --- a/linkpreview/build.gradle +++ b/linkpreview/build.gradle @@ -9,8 +9,8 @@ android { defaultConfig { minSdkVersion 19 targetSdkVersion 28 - versionCode 16 - versionName "2.1" + versionCode 17 + versionName "2.2" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { @@ -43,8 +43,8 @@ dependencies { //Testing libraries testImplementation 'org.jetbrains.kotlin:kotlin-test-junit:1.2.31' - androidTestImplementation 'androidx.test:runner:1.1.0' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' + androidTestImplementation 'androidx.test:runner:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' } ext { @@ -54,7 +54,7 @@ ext { publishedGroupId = 'com.nick.mowen.linkpreview' artifact = 'linkpreview' - libraryVersion = '2.1' + libraryVersion = '2.2' libraryDescription = 'A convenient view that shows a clickable preview of a link' siteUrl = 'https://github.com/NickM-27/LinkPreview' gitUrl = 'https://github.com/NickM-27/LinkPreview.git' diff --git a/linkpreview/src/main/java/com/nick/mowen/linkpreview/binding/BindingAdapter.kt b/linkpreview/src/main/java/com/nick/mowen/linkpreview/binding/BindingAdapter.kt index a059aff..c3a8c5d 100644 --- a/linkpreview/src/main/java/com/nick/mowen/linkpreview/binding/BindingAdapter.kt +++ b/linkpreview/src/main/java/com/nick/mowen/linkpreview/binding/BindingAdapter.kt @@ -7,7 +7,5 @@ object BindingAdapter { @BindingAdapter("parsedLink") @JvmStatic - fun setParsedLink(view: LinkPreview, link: String) { - view.parseTextForLink(link) - } + fun setParsedLink(view: LinkPreview, link: String) = view.parseTextForLink(link) } \ No newline at end of file diff --git a/linkpreview/src/main/java/com/nick/mowen/linkpreview/view/LinkPreview.kt b/linkpreview/src/main/java/com/nick/mowen/linkpreview/view/LinkPreview.kt index a4268b0..c6af312 100644 --- a/linkpreview/src/main/java/com/nick/mowen/linkpreview/view/LinkPreview.kt +++ b/linkpreview/src/main/java/com/nick/mowen/linkpreview/view/LinkPreview.kt @@ -10,6 +10,7 @@ import android.widget.ImageView import android.widget.TextView import androidx.browser.customtabs.CustomTabsIntent import androidx.core.net.toUri +import androidx.core.view.isGone import com.bumptech.glide.Glide import com.nick.mowen.linkpreview.ImageType import com.nick.mowen.linkpreview.R @@ -35,6 +36,8 @@ open class LinkPreview : FrameLayout, View.OnClickListener { var clickListener: LinkClickListener? = null /** Optional click listener to override click behavior */ var articleColor: Int = Color.CYAN + /** Set whether or not to default to hidden while loading preview */ + var hideWhileLoading = false /** Color of the Chrome CustomTab that is launched on view click */ @@ -68,7 +71,9 @@ open class LinkPreview : FrameLayout, View.OnClickListener { if (isInEditMode) return - visibility = View.GONE + if (hideWhileLoading) + isGone = true + image = findViewById(R.id.preview_image) text = findViewById(R.id.preview_text) setOnClickListener(this) diff --git a/sample/build.gradle b/sample/build.gradle index 014f7a9..6f00575 100644 --- a/sample/build.gradle +++ b/sample/build.gradle @@ -44,6 +44,6 @@ dependencies { //Testing Libraries testImplementation 'org.jetbrains.kotlin:kotlin-test-junit:1.2.31' - androidTestImplementation 'androidx.test:runner:1.1.0-beta02' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-beta02' + androidTestImplementation 'androidx.test:runner:1.1.1' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1' }