Skip to content

Commit 3a89d0f

Browse files
authored
Merge pull request #28 from NickM-27/develop
3.1
2 parents c1d3d68 + c7d0288 commit 3a89d0f

File tree

17 files changed

+591
-166
lines changed

17 files changed

+591
-166
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ On your module's `build.gradle` file add this statement to the `dependencies` se
1818

1919
```groovy
2020
dependencies {
21-
implementation 'com.nick.mowen.linkpreview:linkpreview:3.0'
21+
implementation 'com.nick.mowen.linkpreview:linkpreview:3.2'
2222
}
2323
```
2424

@@ -91,6 +91,17 @@ preview.clickListener = object : LinkClickListener {
9191
}
9292
```
9393

94+
### Data Binding
95+
96+
LinkPreview supports data binding commands so the view can be customized in xml
97+
```xml
98+
<com.nick.mowen.linkpreview.view.LinkPreview
99+
android:id="@+id/preview"
100+
android:layout_width="wrap_content"
101+
android:layout_height="wrap_content"
102+
app:parsedLink="@{data.string}"/>
103+
```
104+
94105
Appications using LinkPreview
95106
---
96107
Icon | Application

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4-
ext.kotlin_version = '1.3.41'
4+
ext.kotlin_version = '1.3.50'
55
repositories {
66
google()
77
jcenter()
88
}
99
dependencies {
10-
classpath 'com.android.tools.build:gradle:3.5.0-beta05'
10+
classpath 'com.android.tools.build:gradle:3.5.1'
1111
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1212

1313
// For Library

linkpreview/build.gradle

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ android {
99
defaultConfig {
1010
minSdkVersion 19
1111
targetSdkVersion 29
12-
versionCode 19
13-
versionName "2.4"
12+
versionCode 23
13+
versionName "3.2"
14+
vectorDrawables.useSupportLibrary = true
1415
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1516
}
1617
buildTypes {
@@ -23,8 +24,11 @@ android {
2324
exclude 'META-INF/atomicfu.kotlin_module'
2425
}
2526
compileOptions {
26-
sourceCompatibility 1.8
27-
targetCompatibility 1.8
27+
sourceCompatibility JavaVersion.VERSION_1_8
28+
targetCompatibility JavaVersion.VERSION_1_8
29+
}
30+
kotlinOptions {
31+
jvmTarget = "1.8"
2832
}
2933
dataBinding {
3034
enabled = true
@@ -34,21 +38,19 @@ android {
3438
dependencies {
3539

3640
// AndroidX
37-
implementation 'androidx.core:core-ktx:1.0.2'
41+
implementation 'com.google.android.material:material:1.1.0-beta01'
42+
implementation 'androidx.core:core-ktx:1.1.0'
3843
implementation 'androidx.browser:browser:1.0.0'
3944
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
4045

4146
// Other
4247
implementation 'org.jsoup:jsoup:1.12.1'
43-
implementation 'com.github.bumptech.glide:glide:4.9.0'
48+
implementation 'io.coil-kt:coil:0.7.0'
4449

4550
// Kotlin
46-
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
47-
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.2'
48-
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.2'
49-
50-
// Annotation Processors
51-
kapt 'com.github.bumptech.glide:compiler:4.9.0'
51+
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
52+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'
53+
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'
5254

5355
// Testing Libraries
5456
testImplementation 'org.jetbrains.kotlin:kotlin-test-junit:1.2.31'
@@ -63,7 +65,7 @@ ext {
6365

6466
publishedGroupId = 'com.nick.mowen.linkpreview'
6567
artifact = 'linkpreview'
66-
libraryVersion = '3.0'
68+
libraryVersion = '3.2'
6769
libraryDescription = 'A convenient view that shows a clickable preview of a link'
6870
siteUrl = 'https://github.com/NickM-27/LinkPreview'
6971
gitUrl = 'https://github.com/NickM-27/LinkPreview.git'
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.nick.mowen.linkpreview
2+
3+
import androidx.annotation.Keep
4+
5+
@Keep
6+
data class CardData(val title: String, val imageUrl: String, val baseUrl: String) {
7+
8+
fun isEmpty(): Boolean = title.isEmpty() && imageUrl.isEmpty() && baseUrl.isEmpty()
9+
10+
fun isNotEmpty(): Boolean = !isEmpty()
11+
}
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
package com.nick.mowen.linkpreview.binding
22

3+
import android.widget.ImageView
34
import androidx.databinding.BindingAdapter
5+
import coil.api.load
46
import com.nick.mowen.linkpreview.view.LinkPreview
7+
import kotlinx.coroutines.Dispatchers
8+
import kotlinx.coroutines.GlobalScope
9+
import kotlinx.coroutines.launch
510

611
object BindingAdapter {
712

813
@BindingAdapter("parsedLink")
914
@JvmStatic
10-
fun setParsedLink(view: LinkPreview, link: String) = view.parseTextForLink(link)
15+
fun LinkPreview.setParsedLink(link: String) = parseTextForLink(link)
16+
17+
@BindingAdapter("imageUrl")
18+
@JvmStatic
19+
fun ImageView.setImageUrl(imageUrl: String?) {
20+
imageUrl ?: return
21+
load(imageUrl) {
22+
crossfade(true)
23+
}
24+
}
1125
}

linkpreview/src/main/java/com/nick/mowen/linkpreview/extension/ImageExtension.kt

Lines changed: 0 additions & 67 deletions
This file was deleted.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package com.nick.mowen.linkpreview.extension
2+
3+
import android.util.Log
4+
import android.view.View
5+
import com.nick.mowen.linkpreview.CardData
6+
import com.nick.mowen.linkpreview.listener.CardListener
7+
import com.nick.mowen.linkpreview.listener.LinkListener
8+
import com.nick.mowen.linkpreview.view.LinkCardView
9+
import com.nick.mowen.linkpreview.view.LinkPreview
10+
import kotlinx.coroutines.Dispatchers
11+
import kotlinx.coroutines.launch
12+
import kotlinx.coroutines.withContext
13+
import org.jsoup.Jsoup
14+
import org.jsoup.nodes.Document
15+
16+
suspend fun LinkPreview.loadImage(
17+
link: String,
18+
linkMap: HashMap<Int, String>,
19+
key: Int,
20+
listener: LinkListener?
21+
) = withContext(Dispatchers.Default) {
22+
try {
23+
val result = try {
24+
val connection = Jsoup.connect(link).userAgent("Mozilla")
25+
val doc: Document = connection.get()
26+
val imageElements = doc.select("meta[property=og:image]")
27+
28+
if (imageElements.size > 0) {
29+
var it = 0
30+
var chosen: String? = ""
31+
32+
while ((chosen == null || chosen.isEmpty()) && it < imageElements.size) {
33+
chosen = imageElements[it].attr("content")
34+
it += 1
35+
}
36+
37+
chosen
38+
} else {
39+
linkMap[key] = "Fail"
40+
launch(Dispatchers.Main) { listener?.onError() }
41+
""
42+
}
43+
} catch (e: IndexOutOfBoundsException) {
44+
e.printStackTrace()
45+
linkMap[key] = "Fail"
46+
launch(Dispatchers.Main) { listener?.onError() }
47+
""
48+
} catch (e: Exception) {
49+
e.printStackTrace()
50+
linkMap[key] = "Fail"
51+
launch(Dispatchers.Main) { listener?.onError() }
52+
""
53+
}
54+
55+
launch(Dispatchers.Main) {
56+
try {
57+
if (result != null && result.isNotEmpty()) {
58+
setImageData(result)
59+
listener?.onSuccess(result)
60+
} else {
61+
Log.d("Article Request", "Image url is empty")
62+
visibility = View.GONE
63+
listener?.onError()
64+
}
65+
} catch (e: Exception) {
66+
e.printStackTrace()
67+
listener?.onError()
68+
} catch (e: IllegalArgumentException) {
69+
e.printStackTrace()
70+
listener?.onError()
71+
}
72+
}
73+
} catch (e: Exception) {
74+
e.printStackTrace()
75+
}
76+
}
77+
78+
suspend fun LinkCardView.loadCardData(
79+
link: String,
80+
linkMap: HashMap<Int, String>,
81+
key: Int,
82+
listener: CardListener?
83+
) = withContext(Dispatchers.Default) {
84+
try {
85+
val result = try {
86+
val connection = Jsoup.connect(link).userAgent("Mozilla")
87+
val doc: Document = connection.get()
88+
val imageElements = doc.select("meta[property=og:image]")
89+
90+
if (imageElements.size > 0) {
91+
var it = 0
92+
var chosen: String? = ""
93+
94+
while ((chosen == null || chosen.isEmpty()) && it < imageElements.size) {
95+
chosen = imageElements[it].attr("content")
96+
it += 1
97+
}
98+
99+
CardData(doc.title(), chosen ?: "", link)
100+
} else {
101+
linkMap[key] = "Fail"
102+
launch(Dispatchers.Main) { listener?.onError() }
103+
CardData("", "", "")
104+
}
105+
} catch (e: IndexOutOfBoundsException) {
106+
e.printStackTrace()
107+
linkMap[key] = "Fail"
108+
launch(Dispatchers.Main) { listener?.onError() }
109+
CardData("", "", "")
110+
} catch (e: Exception) {
111+
e.printStackTrace()
112+
linkMap[key] = "Fail"
113+
launch(Dispatchers.Main) { listener?.onError() }
114+
CardData("", "", "")
115+
}
116+
117+
launch(Dispatchers.Main) {
118+
try {
119+
if (result.isNotEmpty()) {
120+
setCardData(result)
121+
listener?.onSuccess(result)
122+
} else {
123+
Log.d("Article Request", "Image url is empty")
124+
visibility = View.GONE
125+
listener?.onError()
126+
}
127+
} catch (e: Exception) {
128+
e.printStackTrace()
129+
listener?.onError()
130+
} catch (e: IllegalArgumentException) {
131+
e.printStackTrace()
132+
listener?.onError()
133+
}
134+
}
135+
} catch (e: Exception) {
136+
e.printStackTrace()
137+
}
138+
}

linkpreview/src/main/java/com/nick/mowen/linkpreview/extension/LinkMapExtension.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ fun Context.loadLinkMap(): HashMap<Int, String> {
2020
return map
2121
}
2222

23+
/**
24+
* Clears previously saved links
25+
*/
26+
fun Context.clearMap() {
27+
getSharedPreferences(Constants.MAP_PREFERENCES, Context.MODE_PRIVATE).edit { clear() }
28+
}
29+
2330
/**
2431
* Adds hashed user URL to set and creates link to preference that holds image url
2532
*

0 commit comments

Comments
 (0)