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+ }
0 commit comments