Skip to content

Commit ddd7f89

Browse files
author
Rohit Singh
committed
- Refactor unused imports and unwanted functions
- LinkPreview UI designed layout_link_preview.xml
1 parent e503e2a commit ddd7f89

File tree

11 files changed

+189
-212
lines changed

11 files changed

+189
-212
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
An Android Project with demo application, to fetch meta-data from url, like facebook, youtube and other websites.
44

5-
##Usage
5+
# Usage
66

77
Request metadata for url, it will execute in background thread, you will get an instance from LinkPreview
88

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:tools="http://schemas.android.com/tools">
44

5+
<uses-permission android:name="android.permission.INTERNET"/>
6+
57
<application
68
android:allowBackup="true"
79
android:dataExtractionRules="@xml/data_extraction_rules"

app/src/main/java/com/linkpreview/none/MainActivity.kt

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,49 @@ package com.linkpreview.none
22

33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.widget.Toast
8+
import androidx.core.view.isVisible
9+
import com.linkpreview.none.databinding.ActivityMainBinding
10+
import com.linkpreview.none.linkpreview.PreviewData
11+
import com.linkpreview.none.linkpreview.extension.isUrl
12+
import com.linkpreview.none.linkpreview.listener.LinkListener
513

614
class MainActivity : AppCompatActivity() {
15+
16+
lateinit var mBinding : ActivityMainBinding
17+
718
override fun onCreate(savedInstanceState: Bundle?) {
819
super.onCreate(savedInstanceState)
9-
setContentView(R.layout.activity_main)
20+
mBinding = ActivityMainBinding.inflate(LayoutInflater.from(this))
21+
setContentView(mBinding.root)
22+
setListener()
23+
24+
}
25+
26+
private fun setListener() {
27+
mBinding.idLinkPreview.loadListener = object : LinkListener {
28+
override fun onError() {
29+
Toast.makeText(this@MainActivity,"Error on Preview link",Toast.LENGTH_SHORT).show()
30+
mBinding.idLinkPreview.visibility = View.GONE
31+
}
32+
33+
override fun onSuccess(link: PreviewData) {
34+
mBinding.idLinkPreview.visibility = View.VISIBLE
35+
}
36+
37+
}
38+
39+
mBinding.btnPreviewLink.setOnClickListener {
40+
if (mBinding.edtLink.text.toString().isNotBlank() && mBinding.edtLink.text.toString()
41+
.isUrl()
42+
) {
43+
mBinding.idLinkPreview.parseTextForLink(mBinding.edtLink.text.toString())
44+
45+
}else{
46+
Toast.makeText(this@MainActivity,"Error on Parsing link",Toast.LENGTH_SHORT).show()
47+
}
48+
}
1049
}
1150
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.linkpreview.none.linkpreview.extension
2+
3+
import android.os.Build
4+
import android.widget.ImageView
5+
import coil.ImageLoader
6+
import coil.load
7+
import com.linkpreview.none.R
8+
9+
/**
10+
* Image extension to load image from URL or disk*/
11+
fun ImageView.loadCoil(
12+
url: String? = null,
13+
placeholder: Int = R.mipmap.ic_launcher,
14+
error: Int = placeholder
15+
) {
16+
17+
load(url) {
18+
crossfade(true)
19+
placeholder(placeholder)
20+
error(error)
21+
}
22+
}
23+

app/src/main/java/com/linkpreview/none/linkpreview/extension/LinkExtension.kt

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import java.net.URI
1919
@Suppress("BlockingMethodInNonBlockingContext")
2020
suspend fun LinkPreview.loadPreviewData(
2121
link: String,
22-
key: Int,
2322
listener: LinkListener?
2423
) = withContext(Dispatchers.Default) {
2524
try {
@@ -46,16 +45,13 @@ suspend fun LinkPreview.loadPreviewData(
4645

4746
PreviewData(siteName,doc.title(), chosen ?: "", link)
4847
} else {
49-
launch(Dispatchers.Main) { listener?.onError() }
5048
PreviewData("","", "", "")
5149
}
5250
} catch (e: IndexOutOfBoundsException) {
5351
e.printStackTrace()
54-
launch(Dispatchers.Main) { listener?.onError() }
5552
PreviewData("","", "", "")
5653
} catch (e: Exception) {
5754
e.printStackTrace()
58-
launch(Dispatchers.Main) { listener?.onError() }
5955
PreviewData("","", "", "")
6056
}
6157

@@ -93,69 +89,6 @@ fun Context.launchUrlWithCustomTab(uri: Uri) {
9389
}
9490
}
9591

96-
@Suppress("BlockingMethodInNonBlockingContext")
97-
suspend fun loadPreviewData(
98-
link: String,
99-
listener: LinkListener?
100-
) = withContext(Dispatchers.Default) {
101-
try {
102-
val result = try {
103-
val connection = Jsoup.connect(link).referrer("http://www.google.com")
104-
105-
val doc: Document = connection.get()
106-
val imageElements = doc.select("meta[property=og:image]")
107-
108-
//Get description from document object.
109-
val dataLink : Element? = doc.select("a").first()
110-
111-
val siteName: String = getHostName(link).toString()
112-
113-
114-
if (imageElements.size > 0) {
115-
var it = 0
116-
var chosen: String? = ""
117-
118-
while ((chosen == null || chosen.isEmpty()) && it < imageElements.size) {
119-
chosen = imageElements[it].attr("content")
120-
it += 1
121-
}
122-
123-
PreviewData(siteName,doc.title(), chosen ?: "", link)
124-
} else {
125-
launch(Dispatchers.Main) { listener?.onError() }
126-
PreviewData("","", "", "")
127-
}
128-
} catch (e: IndexOutOfBoundsException) {
129-
e.printStackTrace()
130-
launch(Dispatchers.Main) { listener?.onError() }
131-
PreviewData("","", "", "")
132-
} catch (e: Exception) {
133-
e.printStackTrace()
134-
launch(Dispatchers.Main) { listener?.onError() }
135-
PreviewData("","", "", "")
136-
}
137-
138-
launch(Dispatchers.Main) {
139-
try {
140-
if (result.isNotEmpty()) {
141-
listener?.onSuccess(result)
142-
} else {
143-
listener?.onError()
144-
}
145-
} catch (e: Exception) {
146-
e.printStackTrace()
147-
listener?.onError()
148-
} catch (e: IllegalArgumentException) {
149-
e.printStackTrace()
150-
listener?.onError()
151-
}
152-
}
153-
} catch (e: Exception) {
154-
listener?.onError()
155-
e.printStackTrace()
156-
}
157-
}
158-
15992
/*
16093
* get hostName */
16194
fun getHostName(baseUrl: String): CharSequence {

app/src/main/java/com/linkpreview/none/linkpreview/listener/CardListener.kt

Lines changed: 0 additions & 18 deletions
This file was deleted.

app/src/main/java/com/linkpreview/none/linkpreview/listener/LinkClickListener.kt

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)