Skip to content

Commit

Permalink
Merge pull request #28 from NickM-27/develop
Browse files Browse the repository at this point in the history
3.1
  • Loading branch information
NickM-27 authored Oct 9, 2019
2 parents c1d3d68 + c7d0288 commit 3a89d0f
Show file tree
Hide file tree
Showing 17 changed files with 591 additions and 166 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:3.0'
implementation 'com.nick.mowen.linkpreview:linkpreview:3.2'
}
```

Expand Down Expand Up @@ -91,6 +91,17 @@ preview.clickListener = object : LinkClickListener {
}
```

### Data Binding

LinkPreview supports data binding commands so the view can be customized in xml
```xml
<com.nick.mowen.linkpreview.view.LinkPreview
android:id="@+id/preview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:parsedLink="@{data.string}"/>
```

Appications using LinkPreview
---
Icon | Application
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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.41'
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0-beta05'
classpath 'com.android.tools.build:gradle:3.5.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// For Library
Expand Down
28 changes: 15 additions & 13 deletions linkpreview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ android {
defaultConfig {
minSdkVersion 19
targetSdkVersion 29
versionCode 19
versionName "2.4"
versionCode 23
versionName "3.2"
vectorDrawables.useSupportLibrary = true
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand All @@ -23,8 +24,11 @@ android {
exclude 'META-INF/atomicfu.kotlin_module'
}
compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
dataBinding {
enabled = true
Expand All @@ -34,21 +38,19 @@ android {
dependencies {

// AndroidX
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'com.google.android.material:material:1.1.0-beta01'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.browser:browser:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

// Other
implementation 'org.jsoup:jsoup:1.12.1'
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'io.coil-kt:coil:0.7.0'

// Kotlin
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.2'

// Annotation Processors
kapt 'com.github.bumptech.glide:compiler:4.9.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2'

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

publishedGroupId = 'com.nick.mowen.linkpreview'
artifact = 'linkpreview'
libraryVersion = '3.0'
libraryVersion = '3.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'
Expand Down
11 changes: 11 additions & 0 deletions linkpreview/src/main/java/com/nick/mowen/linkpreview/CardData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.nick.mowen.linkpreview

import androidx.annotation.Keep

@Keep
data class CardData(val title: String, val imageUrl: String, val baseUrl: String) {

fun isEmpty(): Boolean = title.isEmpty() && imageUrl.isEmpty() && baseUrl.isEmpty()

fun isNotEmpty(): Boolean = !isEmpty()
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
package com.nick.mowen.linkpreview.binding

import android.widget.ImageView
import androidx.databinding.BindingAdapter
import coil.api.load
import com.nick.mowen.linkpreview.view.LinkPreview
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

object BindingAdapter {

@BindingAdapter("parsedLink")
@JvmStatic
fun setParsedLink(view: LinkPreview, link: String) = view.parseTextForLink(link)
fun LinkPreview.setParsedLink(link: String) = parseTextForLink(link)

@BindingAdapter("imageUrl")
@JvmStatic
fun ImageView.setImageUrl(imageUrl: String?) {
imageUrl ?: return
load(imageUrl) {
crossfade(true)
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package com.nick.mowen.linkpreview.extension

import android.util.Log
import android.view.View
import com.nick.mowen.linkpreview.CardData
import com.nick.mowen.linkpreview.listener.CardListener
import com.nick.mowen.linkpreview.listener.LinkListener
import com.nick.mowen.linkpreview.view.LinkCardView
import com.nick.mowen.linkpreview.view.LinkPreview
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jsoup.Jsoup
import org.jsoup.nodes.Document

suspend fun LinkPreview.loadImage(
link: String,
linkMap: HashMap<Int, String>,
key: Int,
listener: LinkListener?
) = withContext(Dispatchers.Default) {
try {
val result = try {
val connection = Jsoup.connect(link).userAgent("Mozilla")
val doc: Document = connection.get()
val imageElements = doc.select("meta[property=og:image]")

if (imageElements.size > 0) {
var it = 0
var chosen: String? = ""

while ((chosen == null || chosen.isEmpty()) && it < imageElements.size) {
chosen = imageElements[it].attr("content")
it += 1
}

chosen
} else {
linkMap[key] = "Fail"
launch(Dispatchers.Main) { listener?.onError() }
""
}
} catch (e: IndexOutOfBoundsException) {
e.printStackTrace()
linkMap[key] = "Fail"
launch(Dispatchers.Main) { listener?.onError() }
""
} catch (e: Exception) {
e.printStackTrace()
linkMap[key] = "Fail"
launch(Dispatchers.Main) { listener?.onError() }
""
}

launch(Dispatchers.Main) {
try {
if (result != null && result.isNotEmpty()) {
setImageData(result)
listener?.onSuccess(result)
} else {
Log.d("Article Request", "Image url is empty")
visibility = View.GONE
listener?.onError()
}
} catch (e: Exception) {
e.printStackTrace()
listener?.onError()
} catch (e: IllegalArgumentException) {
e.printStackTrace()
listener?.onError()
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}

suspend fun LinkCardView.loadCardData(
link: String,
linkMap: HashMap<Int, String>,
key: Int,
listener: CardListener?
) = withContext(Dispatchers.Default) {
try {
val result = try {
val connection = Jsoup.connect(link).userAgent("Mozilla")
val doc: Document = connection.get()
val imageElements = doc.select("meta[property=og:image]")

if (imageElements.size > 0) {
var it = 0
var chosen: String? = ""

while ((chosen == null || chosen.isEmpty()) && it < imageElements.size) {
chosen = imageElements[it].attr("content")
it += 1
}

CardData(doc.title(), chosen ?: "", link)
} else {
linkMap[key] = "Fail"
launch(Dispatchers.Main) { listener?.onError() }
CardData("", "", "")
}
} catch (e: IndexOutOfBoundsException) {
e.printStackTrace()
linkMap[key] = "Fail"
launch(Dispatchers.Main) { listener?.onError() }
CardData("", "", "")
} catch (e: Exception) {
e.printStackTrace()
linkMap[key] = "Fail"
launch(Dispatchers.Main) { listener?.onError() }
CardData("", "", "")
}

launch(Dispatchers.Main) {
try {
if (result.isNotEmpty()) {
setCardData(result)
listener?.onSuccess(result)
} else {
Log.d("Article Request", "Image url is empty")
visibility = View.GONE
listener?.onError()
}
} catch (e: Exception) {
e.printStackTrace()
listener?.onError()
} catch (e: IllegalArgumentException) {
e.printStackTrace()
listener?.onError()
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ fun Context.loadLinkMap(): HashMap<Int, String> {
return map
}

/**
* Clears previously saved links
*/
fun Context.clearMap() {
getSharedPreferences(Constants.MAP_PREFERENCES, Context.MODE_PRIVATE).edit { clear() }
}

/**
* Adds hashed user URL to set and creates link to preference that holds image url
*
Expand Down
Loading

0 comments on commit 3a89d0f

Please sign in to comment.