Skip to content

Commit 165d81f

Browse files
authored
Merge pull request #7 from Priyansh-Kedia/caching
added caching
2 parents 9b6b6df + 3e50750 commit 165d81f

6 files changed

Lines changed: 131 additions & 11 deletions

File tree

OGParser/build.gradle

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,17 @@ dependencies {
4545
// OpenGraph
4646
implementation 'org.jsoup:jsoup:1.10.3'
4747

48+
// Preference AndroidX
49+
def preference_version = "1.1.1"
50+
implementation "androidx.preference:preference-ktx:$preference_version"
51+
52+
//ktx
53+
implementation 'androidx.core:core-ktx:1.6.0'
54+
4855
//Architecture
4956
def lifecycle_version = '2.2.0'
5057

5158
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.9'
5259
// implementation "androidx.lifecycle:lifecycle-extensions:${lifecycle_version}"
5360
// implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:${lifecycle_version}"
54-
}
61+
}

OGParser/src/main/java/com/kedia/ogparser/OpenGraphParser.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package com.kedia.ogparser
22

3-
import android.util.Log
3+
import android.content.Context
44
import kotlinx.coroutines.*
55
import org.jsoup.Jsoup
66
import kotlin.coroutines.CoroutineContext
77

88

99
class OpenGraphParser(
1010
private val listener: OpenGraphCallback,
11-
private var showNullOnEmpty: Boolean = false
11+
private var showNullOnEmpty: Boolean = false,
12+
private val context: Context? = null
1213
) {
1314

15+
private val sharedPrefs: SharedPrefs? = context?.let { SharedPrefs(it) }
16+
1417
private var url: String = ""
1518

1619
private val AGENT = "Mozilla"
@@ -52,7 +55,9 @@ class OpenGraphParser(
5255
if (!url.contains("http")) {
5356
url = "http://$url"
5457
}
55-
58+
if (sharedPrefs?.urlExists(url) == true) {
59+
return@withContext sharedPrefs?.getOpenGraphResult(url)
60+
}
5661
openGraphResult = OpenGraphResult()
5762
try {
5863
val response = Jsoup.connect(url)
@@ -108,7 +113,7 @@ class OpenGraphParser(
108113
}
109114
return@withContext null
110115
}
111-
116+
openGraphResult?.let { sharedPrefs?.setOpenGraphResult(it, url) }
112117
return@withContext openGraphResult
113118
}
114119
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.kedia.ogparser
2+
3+
import android.content.Context
4+
import android.content.SharedPreferences
5+
import androidx.preference.PreferenceManager
6+
7+
class SharedPrefs(context: Context) {
8+
9+
private val pm: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
10+
11+
private val OG_PARSER = "Og_Parser"
12+
private val TITLE = OG_PARSER + "_title"
13+
private val DESCRIPTION = OG_PARSER + "_description"
14+
private val URL = OG_PARSER + "_url"
15+
private val IMAGE = OG_PARSER + "_image"
16+
private val SITE_NAME = OG_PARSER + "_site_name"
17+
private val TYPE = OG_PARSER + "_type"
18+
19+
fun setTitle(link: String, title: String) {
20+
pm.edit().putString(TITLE + "_$link", title).apply()
21+
}
22+
23+
fun getTitle(link: String): String {
24+
return pm.getString(TITLE + "_$link", "") ?: ""
25+
}
26+
27+
fun setDescription(link: String, description: String) {
28+
pm.edit().putString(DESCRIPTION + "_$link", description).apply()
29+
}
30+
31+
fun getDescription(link: String): String {
32+
return pm.getString(DESCRIPTION + "_$link", "") ?: ""
33+
}
34+
35+
fun setUrl(link: String, url: String) {
36+
pm.edit().putString(URL + "_$link", url).apply()
37+
}
38+
39+
fun getUrl(link: String): String {
40+
return pm.getString(URL + "_$link", "") ?: ""
41+
}
42+
43+
fun setImage(link: String, image: String) {
44+
pm.edit().putString(IMAGE + "_$link", image).apply()
45+
}
46+
47+
fun getImage(link: String): String {
48+
return pm.getString(IMAGE + "_$link", "") ?: ""
49+
}
50+
51+
fun setSiteName(link: String, siteName: String) {
52+
pm.edit().putString(SITE_NAME + "_$link", siteName).apply()
53+
}
54+
55+
fun getSiteName(link: String): String {
56+
return pm.getString(SITE_NAME + "_$link", "") ?: ""
57+
}
58+
59+
fun setType(link: String, type: String) {
60+
pm.edit().putString(TYPE + "_$link", type).apply()
61+
}
62+
63+
fun getType(link: String): String {
64+
return pm.getString(TYPE + "_$link", "") ?: ""
65+
}
66+
67+
fun setOpenGraphResult(openGraphResult: OpenGraphResult, url: String) {
68+
setTitle(url, openGraphResult.title.toString())
69+
setDescription(url, openGraphResult.description.toString())
70+
setImage(url, openGraphResult.image.toString())
71+
setSiteName(url, openGraphResult.siteName.toString())
72+
setType(url, openGraphResult.type.toString())
73+
setUrl(url, openGraphResult.url.toString())
74+
}
75+
76+
fun getOpenGraphResult(url: String): OpenGraphResult {
77+
val title = getTitle(url)
78+
val description = getDescription(url)
79+
val image = getImage(url)
80+
val siteName = getSiteName(url)
81+
val type = getType(url)
82+
val url = getUrl(url)
83+
return OpenGraphResult(title, description, url, image, siteName, type)
84+
}
85+
86+
fun urlExists(url: String): Boolean {
87+
val title = getTitle(url)
88+
val description = getDescription(url)
89+
val image = getImage(url)
90+
91+
return title.isNotEmpty() && description.isNotEmpty() && image.isNotEmpty()
92+
}
93+
94+
}

app/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,4 @@ dependencies {
4545
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
4646
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
4747

48-
4948
}

app/src/main/java/com/kedia/opengraphpreview/MainActivity.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,20 @@ class MainActivity : AppCompatActivity(), OpenGraphCallback {
1414
super.onCreate(savedInstanceState)
1515
setContentView(R.layout.activity_main)
1616

17-
val openGraphParser = OpenGraphParser(this)
17+
val openGraphParser = OpenGraphParser(this, context = this)
1818
openGraphParser.parse("https://www.youtube.com")
19+
20+
button.setOnClickListener {
21+
openGraphParser.parse(tview.text.toString())
22+
}
1923
}
2024

2125
override fun onPostResponse(openGraphResult: OpenGraphResult) {
22-
tview.text = openGraphResult.toString()
26+
tview.setText(openGraphResult.toString())
2327
}
2428

2529
override fun onError(error: String) {
2630
Log.e("TAG!!!!", "$error")
27-
tview.text = error
31+
// tview.text = error
2832
}
2933
}

app/src/main/res/layout/activity_main.xml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,25 @@
66
android:layout_height="match_parent"
77
tools:context=".MainActivity">
88

9-
<TextView
9+
<EditText
1010
android:id="@+id/tview"
1111
android:layout_width="wrap_content"
1212
android:layout_height="wrap_content"
13-
android:text="Hello World!"
13+
android:hint="Hello World!"
1414
app:layout_constraintBottom_toBottomOf="parent"
1515
app:layout_constraintLeft_toLeftOf="parent"
1616
app:layout_constraintRight_toRightOf="parent"
1717
app:layout_constraintTop_toTopOf="parent" />
1818

19+
<Button
20+
android:id="@+id/button"
21+
android:layout_width="wrap_content"
22+
android:layout_height="wrap_content"
23+
android:text="Button"
24+
app:layout_constraintBottom_toBottomOf="parent"
25+
app:layout_constraintEnd_toEndOf="parent"
26+
app:layout_constraintStart_toStartOf="parent"
27+
app:layout_constraintTop_toBottomOf="@+id/tview" />
28+
29+
1930
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)