Skip to content

Commit d13ac57

Browse files
authored
Merge pull request #20 from Mek101/cache_provider
Make the cache provider an injectable dependency
2 parents 58cf560 + 05b62e9 commit d13ac57

4 files changed

Lines changed: 40 additions & 28 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.kedia.ogparser
2+
3+
interface CacheProvider {
4+
suspend fun getOpenGraphResult(url: String): OpenGraphResult?
5+
suspend fun setOpenGraphResult(openGraphResult: OpenGraphResult, url: String)
6+
}

OGParser/src/main/java/com/kedia/ogparser/SharedPrefs.kt renamed to OGParser/src/main/java/com/kedia/ogparser/OpenGraphCacheProvider.kt

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import android.content.SharedPreferences
55
import android.util.Log
66
import androidx.preference.PreferenceManager
77

8-
class SharedPrefs(context: Context) {
8+
class OpenGraphCacheProvider(context: Context) : CacheProvider {
99

1010
private val pm: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
1111

@@ -65,7 +65,16 @@ class SharedPrefs(context: Context) {
6565
return pm.getString(TYPE + "_$link", "") ?: ""
6666
}
6767

68-
fun setOpenGraphResult(openGraphResult: OpenGraphResult, url: String) {
68+
private fun urlExists(title: String, description: String, image: String): Boolean {
69+
return title.isNotEmpty() &&
70+
title.equals("null").not() &&
71+
description.isNotEmpty() &&
72+
description.equals("null").not() &&
73+
image.isNotEmpty() &&
74+
image.equals("null").not()
75+
}
76+
77+
override suspend fun setOpenGraphResult(openGraphResult: OpenGraphResult, url: String) {
6978
setTitle(url, openGraphResult.title.toString())
7079
setDescription(url, openGraphResult.description.toString())
7180
setImage(url, openGraphResult.image.toString())
@@ -74,26 +83,18 @@ class SharedPrefs(context: Context) {
7483
setUrl(url, openGraphResult.url.toString())
7584
}
7685

77-
fun getOpenGraphResult(url: String): OpenGraphResult {
86+
override suspend fun getOpenGraphResult(url: String): OpenGraphResult? {
7887
val title = getTitle(url)
7988
val description = getDescription(url)
8089
val image = getImage(url)
90+
91+
if (!urlExists(title, description, image)) {
92+
return null
93+
}
94+
8195
val siteName = getSiteName(url)
8296
val type = getType(url)
8397
val url = getUrl(url)
8498
return OpenGraphResult(title, description, url, image, siteName, type)
8599
}
86-
87-
fun urlExists(url: String): Boolean {
88-
val title = getTitle(url)
89-
val description = getDescription(url)
90-
val image = getImage(url)
91-
return title.isNotEmpty() &&
92-
title.equals("null").not() &&
93-
description.isNotEmpty() &&
94-
description.equals("null").not() &&
95-
image.isNotEmpty() &&
96-
image.equals("null").not()
97-
}
98-
99-
}
100+
}

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ import kotlin.coroutines.CoroutineContext
88
class OpenGraphParser(
99
private val listener: OpenGraphCallback,
1010
private var showNullOnEmpty: Boolean = false,
11-
context: Context? = null
11+
private val cacheProvider: CacheProvider? = null
1212
) {
13-
14-
private val sharedPrefs: SharedPrefs? = context?.let { SharedPrefs(it) }
15-
1613
private var url: String = ""
1714

1815
private val AGENTS = mutableListOf(
@@ -52,15 +49,16 @@ class OpenGraphParser(
5249
if (!url.contains("http")) {
5350
url = "http://$url"
5451
}
55-
if (sharedPrefs?.urlExists(url) == true) {
56-
return@withContext sharedPrefs?.getOpenGraphResult(url)
52+
53+
cacheProvider?.getOpenGraphResult(url)?.let {
54+
return@withContext it
5755
}
5856

5957
AGENTS.forEach {
6058
openGraphResult = jsoupNetworkCall.callUrl(url, it)
6159
val isResultNull = checkNullParserResult(openGraphResult)
6260
if (!isResultNull) {
63-
openGraphResult?.let { sharedPrefs?.setOpenGraphResult(it, url) }
61+
openGraphResult?.let { cacheProvider?.setOpenGraphResult(it, url) }
6462
return@withContext openGraphResult
6563
}
6664
}
@@ -71,7 +69,7 @@ class OpenGraphParser(
7169
}
7270
return@withContext null
7371
}
74-
openGraphResult?.let { sharedPrefs?.setOpenGraphResult(it, url) }
72+
openGraphResult?.let { cacheProvider?.setOpenGraphResult(it, url) }
7573
return@withContext openGraphResult
7674
}
77-
}
75+
}

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,18 @@ import androidx.appcompat.app.AppCompatActivity
66
import com.kedia.ogparser.OpenGraphCallback
77
import com.kedia.ogparser.OpenGraphParser
88
import com.kedia.ogparser.OpenGraphResult
9+
import com.kedia.ogparser.OpenGraphCacheProvider
910
import kotlinx.android.synthetic.main.activity_main.*
1011

1112
class MainActivity : AppCompatActivity(), OpenGraphCallback {
1213

13-
private val openGraphParser by lazy { OpenGraphParser(this, showNullOnEmpty = true) }
14+
private val openGraphParser by lazy {
15+
OpenGraphParser(
16+
listener = this,
17+
showNullOnEmpty = true,
18+
cacheProvider = OpenGraphCacheProvider(this)
19+
)
20+
}
1421

1522
private val LINKS_TO_TEST = mutableListOf(
1623
"https://www.linkedin.com/posts/madhusmita-padhy_machinelearning-datascience-activity-6886390508722163712-yhQ0",
@@ -63,4 +70,4 @@ class MainActivity : AppCompatActivity(), OpenGraphCallback {
6370
LINKS_TO_TEST.removeFirstOrNull()
6471
}
6572
}
66-
}
73+
}

0 commit comments

Comments
 (0)