-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from NickM-27/develop
3.1
- Loading branch information
Showing
17 changed files
with
591 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
linkpreview/src/main/java/com/nick/mowen/linkpreview/CardData.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
16 changes: 15 additions & 1 deletion
16
linkpreview/src/main/java/com/nick/mowen/linkpreview/binding/BindingAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
67 changes: 0 additions & 67 deletions
67
linkpreview/src/main/java/com/nick/mowen/linkpreview/extension/ImageExtension.kt
This file was deleted.
Oops, something went wrong.
138 changes: 138 additions & 0 deletions
138
linkpreview/src/main/java/com/nick/mowen/linkpreview/extension/LinkExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.