From 437aa0abec10b35b206820c0767c420a3635d9a2 Mon Sep 17 00:00:00 2001 From: aminecmi Date: Tue, 23 Aug 2022 22:03:33 +0200 Subject: [PATCH] Big code cleaning. --- .../android/HomeActivity.kt | 9 +- .../android/ReaderActivity.kt | 1 - .../android/adapters/ItemCardAdapter.kt | 11 +- .../android/adapters/ItemListAdapter.kt | 9 +- .../android/adapters/SourcesListAdapter.kt | 12 +-- .../android/fragments/ArticleFragment.kt | 6 +- .../android/model/AndroidIModelUtils.kt | 100 ++---------------- .../android/utils/ItemsUtils.kt | 29 ----- .../android/utils/LinksUtils.kt | 1 - .../android/utils/SizeUtils.kt | 9 -- .../readerforselfossv2/utils/DateUtils.kt | 1 - .../utils/ModelConverters.kt | 43 ++++++++ .../readerforselfossv2/model/SelfossModel.kt | 39 ++++++- .../readerforselfossv2/utils/DateUtils.kt | 3 - .../utils/ModelConverters.kt | 14 ++- .../utils/ModelConverters.kt | 22 ++++ .../utils/ModelConverters.kt | 22 ++++ 17 files changed, 171 insertions(+), 160 deletions(-) delete mode 100644 androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/ItemsUtils.kt delete mode 100644 androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/SizeUtils.kt diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt index a2496d5..722a8e4 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/HomeActivity.kt @@ -27,8 +27,6 @@ import bou.amine.apps.readerforselfossv2.android.adapters.ItemListAdapter import bou.amine.apps.readerforselfossv2.android.adapters.ItemsAdapter import bou.amine.apps.readerforselfossv2.android.background.LoadingWorker import bou.amine.apps.readerforselfossv2.android.databinding.ActivityHomeBinding -import bou.amine.apps.readerforselfossv2.android.model.getIcon -import bou.amine.apps.readerforselfossv2.android.model.getTitleDecoded import bou.amine.apps.readerforselfossv2.android.settings.SettingsActivity import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.themes.Toppings @@ -39,10 +37,7 @@ import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActiv import bou.amine.apps.readerforselfossv2.dao.ACTION import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.model.SelfossModel -import bou.amine.apps.readerforselfossv2.utils.ItemType -import bou.amine.apps.readerforselfossv2.utils.getHtmlDecoded -import bou.amine.apps.readerforselfossv2.utils.longHash -import bou.amine.apps.readerforselfossv2.utils.toView +import bou.amine.apps.readerforselfossv2.utils.* import com.ashokvarma.bottomnavigation.BottomNavigationBar import com.ashokvarma.bottomnavigation.BottomNavigationItem import com.ashokvarma.bottomnavigation.TextBadgeItem @@ -550,7 +545,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener, DIAwar } else { for (source in maybeSources) { val item = PrimaryDrawerItem().apply { - nameText = source.getTitleDecoded() + nameText = source.title.getHtmlDecoded() identifier = source.id.toLong() iconUrl = source.getIcon(repository.baseUrl) onDrawerItemClickListener = { _,_,_ -> diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt index 0d5e899..125940e 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/ReaderActivity.kt @@ -14,7 +14,6 @@ import bou.amine.apps.readerforselfossv2.android.databinding.ActivityReaderBindi import bou.amine.apps.readerforselfossv2.android.fragments.ArticleFragment import bou.amine.apps.readerforselfossv2.android.themes.AppColors import bou.amine.apps.readerforselfossv2.android.themes.Toppings -import bou.amine.apps.readerforselfossv2.android.utils.toggleStar import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.model.SelfossModel import com.ftinc.scoop.Scoop diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt index 7345564..a7b42d6 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemCardAdapter.kt @@ -17,6 +17,9 @@ import bou.amine.apps.readerforselfossv2.android.utils.glide.bitmapCenterCrop import bou.amine.apps.readerforselfossv2.android.utils.glide.circularBitmapDrawable import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.model.SelfossModel +import bou.amine.apps.readerforselfossv2.utils.getHtmlDecoded +import bou.amine.apps.readerforselfossv2.utils.getIcon +import bou.amine.apps.readerforselfossv2.utils.getThumbnail import com.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.util.ColorGenerator import com.bumptech.glide.Glide @@ -56,7 +59,7 @@ class ItemCardAdapter( val itm = items[position] binding.favButton.isSelected = itm.starred - binding.title.text = itm.getTitleDecoded() + binding.title.text = itm.title.getHtmlDecoded() binding.title.setOnTouchListener(LinkOnTouchListener()) @@ -79,13 +82,13 @@ class ItemCardAdapter( } if (itm.getIcon(repository.baseUrl).isEmpty()) { - val color = generator.getColor(itm.getSourceTitle()) + val color = generator.getColor(itm.title.getHtmlDecoded()) val drawable = TextDrawable .builder() .round() - .build(itm.getSourceTitle().toTextDrawableString(c), color) + .build(itm.title.getHtmlDecoded().toTextDrawableString(), color) binding.sourceImage.setImageDrawable(drawable) } else { c.circularBitmapDrawable(config, itm.getIcon(repository.baseUrl), binding.sourceImage) @@ -126,7 +129,7 @@ class ItemCardAdapter( binding.shareBtn.setOnClickListener { val item = items[bindingAdapterPosition] - c.shareLink(item.getLinkDecoded(), item.getTitleDecoded()) + c.shareLink(item.getLinkDecoded(), item.title.getHtmlDecoded()) } binding.browserBtn.setOnClickListener { diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt index 6f1272b..27839a4 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/ItemListAdapter.kt @@ -14,6 +14,9 @@ import bou.amine.apps.readerforselfossv2.android.utils.glide.bitmapCenterCrop import bou.amine.apps.readerforselfossv2.android.utils.glide.circularBitmapDrawable import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.model.SelfossModel +import bou.amine.apps.readerforselfossv2.utils.getHtmlDecoded +import bou.amine.apps.readerforselfossv2.utils.getIcon +import bou.amine.apps.readerforselfossv2.utils.getThumbnail import com.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.util.ColorGenerator import org.kodein.di.DI @@ -45,7 +48,7 @@ class ItemListAdapter( with(holder) { val itm = items[position] - binding.title.text = itm.getTitleDecoded() + binding.title.text = itm.title.getHtmlDecoded() binding.title.setOnTouchListener(LinkOnTouchListener()) @@ -56,13 +59,13 @@ class ItemListAdapter( if (itm.getThumbnail(repository.baseUrl).isEmpty()) { if (itm.getIcon(repository.baseUrl).isEmpty()) { - val color = generator.getColor(itm.getSourceTitle()) + val color = generator.getColor(itm.title.getHtmlDecoded()) val drawable = TextDrawable .builder() .round() - .build(itm.getSourceTitle().toTextDrawableString(c), color) + .build(itm.title.getHtmlDecoded().toTextDrawableString(), color) binding.itemImage.setImageDrawable(drawable) } else { diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt index 4904fa9..187af5a 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/adapters/SourcesListAdapter.kt @@ -10,13 +10,13 @@ import androidx.constraintlayout.widget.ConstraintLayout import androidx.recyclerview.widget.RecyclerView import bou.amine.apps.readerforselfossv2.android.R import bou.amine.apps.readerforselfossv2.android.databinding.SourceListItemBinding -import bou.amine.apps.readerforselfossv2.android.model.getIcon -import bou.amine.apps.readerforselfossv2.android.model.getTitleDecoded +import bou.amine.apps.readerforselfossv2.android.model.toTextDrawableString import bou.amine.apps.readerforselfossv2.android.utils.Config import bou.amine.apps.readerforselfossv2.android.utils.glide.circularBitmapDrawable -import bou.amine.apps.readerforselfossv2.android.utils.toTextDrawableString import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.model.SelfossModel +import bou.amine.apps.readerforselfossv2.utils.getHtmlDecoded +import bou.amine.apps.readerforselfossv2.utils.getIcon import com.amulyakhare.textdrawable.TextDrawable import com.amulyakhare.textdrawable.util.ColorGenerator import kotlinx.coroutines.CoroutineScope @@ -49,19 +49,19 @@ class SourcesListAdapter( config = Config() if (itm.getIcon(repository.baseUrl).isEmpty()) { - val color = generator.getColor(itm.getTitleDecoded()) + val color = generator.getColor(itm.title.getHtmlDecoded()) val drawable = TextDrawable .builder() .round() - .build(itm.getTitleDecoded().toTextDrawableString(c), color) + .build(itm.title.getHtmlDecoded().toTextDrawableString(), color) binding.itemImage.setImageDrawable(drawable) } else { c.circularBitmapDrawable(config, itm.getIcon(repository.baseUrl), binding.itemImage) } - binding.sourceTitle.text = itm.getTitleDecoded() + binding.sourceTitle.text = itm.title.getHtmlDecoded() } override fun getItemCount(): Int = items.size diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt index aecb5d1..7c9f602 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/fragments/ArticleFragment.kt @@ -32,6 +32,9 @@ import bou.amine.apps.readerforselfossv2.android.utils.glide.getBitmapInputStrea import bou.amine.apps.readerforselfossv2.android.utils.glide.loadMaybeBasicAuth import bou.amine.apps.readerforselfossv2.repository.Repository import bou.amine.apps.readerforselfossv2.model.SelfossModel +import bou.amine.apps.readerforselfossv2.utils.getHtmlDecoded +import bou.amine.apps.readerforselfossv2.utils.getImages +import bou.amine.apps.readerforselfossv2.utils.getThumbnail import bou.amine.apps.readerforselfossv2.utils.isEmptyOrNullOrNullString import com.bumptech.glide.Glide import com.bumptech.glide.load.engine.DiskCacheStrategy @@ -109,7 +112,7 @@ class ArticleFragment : Fragment(), DIAware { url = item.getLinkDecoded() contentText = item.content - contentTitle = item.getTitleDecoded() + contentTitle = item.title.getHtmlDecoded() contentImage = item.getThumbnail(repository.baseUrl) contentSource = item.sourceAndDateText(repository.dateUtils) allImages = item.getImages() @@ -123,7 +126,6 @@ class ArticleFragment : Fragment(), DIAware { typeface = try { ResourcesCompat.getFont(requireContext(), resId)!! } catch (e: java.lang.Exception) { - // ACRA.getErrorReporter().maybeHandleSilentException(Throwable("Font loading issue: ${e.message}"), requireContext()) // Just to be sure null } diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/model/AndroidIModelUtils.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/model/AndroidIModelUtils.kt index f146f2a..85c9e73 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/model/AndroidIModelUtils.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/model/AndroidIModelUtils.kt @@ -1,43 +1,12 @@ package bou.amine.apps.readerforselfossv2.android.model import android.content.Context -import android.net.Uri -import android.text.Html import android.webkit.URLUtil import bou.amine.apps.readerforselfossv2.model.SelfossModel +import bou.amine.apps.readerforselfossv2.utils.getImages import com.bumptech.glide.Glide import com.bumptech.glide.load.engine.DiskCacheStrategy import com.bumptech.glide.request.RequestOptions -import org.jsoup.Jsoup -import java.util.* - - -/** - * Items extension methods - */ -fun SelfossModel.Item.getIcon(baseUrl: String): String { - return constructUrl(baseUrl, "favicons", icon) -} - -fun SelfossModel.Item.getThumbnail(baseUrl: String): String { - return constructUrl(baseUrl, "thumbnails", thumbnail) -} - -fun SelfossModel.Item.getImages() : ArrayList { - val allImages = ArrayList() - - for ( image in Jsoup.parse(content).getElementsByTag("img")) { - val url = image.attr("src") - if (url.lowercase(Locale.US).contains(".jpg") || - url.lowercase(Locale.US).contains(".jpeg") || - url.lowercase(Locale.US).contains(".png") || - url.lowercase(Locale.US).contains(".webp")) - { - allImages.add(url) - } - } - return allImages -} fun SelfossModel.Item.preloadImages(context: Context) : Boolean { val imageUrls = this.getImages() @@ -60,66 +29,13 @@ fun SelfossModel.Item.preloadImages(context: Context) : Boolean { return true } -fun SelfossModel.Item.getTitleDecoded(): String { - return Html.fromHtml(title).toString() -} - -fun SelfossModel.Item.getSourceTitle(): String { - return Html.fromHtml(sourcetitle).toString() -} - -// TODO: maybe find a better way to handle these kind of urls -fun SelfossModel.Item.getLinkDecoded(): String { - var stringUrl: String - stringUrl = - if (link.startsWith("http://news.google.com/news/") || link.startsWith("https://news.google.com/news/")) { - if (link.contains("&url=")) { - link.substringAfter("&url=") - } else { - this.link.replace("&", "&") - } - } else { - this.link.replace("&", "&") +fun String.toTextDrawableString(): String { + val textDrawable = StringBuilder() + for (s in this.split(" ".toRegex()).filter { it.isNotEmpty() }.toTypedArray()) { + try { + textDrawable.append(s[0]) + } catch (e: StringIndexOutOfBoundsException) { } - - // handle :443 => https - if (stringUrl.contains(":443")) { - stringUrl = stringUrl.replace(":443", "").replace("http://", "https://") - } - - // handle url not starting with http - if (stringUrl.startsWith("//")) { - stringUrl = "http:$stringUrl" - } - - return stringUrl -} - - -/** - * Sources extension methods - */ - -fun SelfossModel.Source.getIcon(baseUrl: String): String { - return constructUrl(baseUrl, "favicons", icon) -} - -fun SelfossModel.Source.getTitleDecoded(): String { - return Html.fromHtml(title).toString() -} - - - -/** - * Common methods - */ -private fun constructUrl(baseUrl: String, path: String, file: String?): String { - return if (file == null || file == "null" || file.isEmpty()) { - "" - } else { - val baseUriBuilder = Uri.parse(baseUrl).buildUpon() - baseUriBuilder.appendPath(path).appendPath(file) - - baseUriBuilder.toString() } + return textDrawable.toString() } \ No newline at end of file diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/ItemsUtils.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/ItemsUtils.kt deleted file mode 100644 index 4a927c3..0000000 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/ItemsUtils.kt +++ /dev/null @@ -1,29 +0,0 @@ -package bou.amine.apps.readerforselfossv2.android.utils - -import android.content.Context -import bou.amine.apps.readerforselfossv2.android.model.getSourceTitle -import bou.amine.apps.readerforselfossv2.model.SelfossModel -import bou.amine.apps.readerforselfossv2.utils.DateUtils -import bou.amine.apps.readerforselfossv2.utils.parseRelativeDate - -fun String.toTextDrawableString(c: Context): String { - val textDrawable = StringBuilder() - for (s in this.split(" ".toRegex()).filter { it.isNotEmpty() }.toTypedArray()) { - try { - textDrawable.append(s[0]) - } catch (e: StringIndexOutOfBoundsException) { - } - } - return textDrawable.toString() -} - -fun SelfossModel.Item.sourceAndDateText(dateUtils: DateUtils): String { - val formattedDate = parseRelativeDate(dateUtils) - - return getSourceTitle() + formattedDate -} - -fun SelfossModel.Item.toggleStar(): SelfossModel.Item { - this.starred = !this.starred - return this -} \ No newline at end of file diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/LinksUtils.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/LinksUtils.kt index 08856a5..1fc608a 100644 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/LinksUtils.kt +++ b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/LinksUtils.kt @@ -18,7 +18,6 @@ import android.widget.Toast import androidx.browser.customtabs.CustomTabsIntent import bou.amine.apps.readerforselfossv2.android.R import bou.amine.apps.readerforselfossv2.android.ReaderActivity -import bou.amine.apps.readerforselfossv2.android.model.getLinkDecoded import bou.amine.apps.readerforselfossv2.android.utils.customtabs.CustomTabActivityHelper import bou.amine.apps.readerforselfossv2.model.SelfossModel import bou.amine.apps.readerforselfossv2.utils.toStringUriWithHttp diff --git a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/SizeUtils.kt b/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/SizeUtils.kt deleted file mode 100644 index 1bf44e3..0000000 --- a/androidApp/src/main/java/bou/amine/apps/readerforselfossv2/android/utils/SizeUtils.kt +++ /dev/null @@ -1,9 +0,0 @@ -package bou.amine.apps.readerforselfossv2.android.utils - -import android.content.res.Resources - -val Int.toPx: Int - get() = (this * Resources.getSystem().displayMetrics.density).toInt() - -val Int.toDp: Int - get() = (this / Resources.getSystem().displayMetrics.density).toInt() diff --git a/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/DateUtils.kt b/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/DateUtils.kt index e5fe1d2..064ff81 100644 --- a/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/DateUtils.kt +++ b/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/DateUtils.kt @@ -1,6 +1,5 @@ package bou.amine.apps.readerforselfossv2.utils -import android.annotation.SuppressLint import android.text.format.DateUtils import java.time.Instant import java.time.LocalDateTime diff --git a/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt b/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt index 2c9a1e8..b2c8672 100644 --- a/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt +++ b/shared/src/androidMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt @@ -1,8 +1,51 @@ package bou.amine.apps.readerforselfossv2.utils +import android.net.Uri import android.text.Html import bou.amine.apps.readerforselfossv2.model.SelfossModel +import org.jsoup.Jsoup +import java.util.* +import kotlin.collections.ArrayList actual fun String.getHtmlDecoded(): String { return Html.fromHtml(this).toString() +} + +actual fun SelfossModel.Item.getIcon(baseUrl: String): String { + return constructUrl(baseUrl, "favicons", icon) +} + +actual fun SelfossModel.Item.getThumbnail(baseUrl: String): String { + return constructUrl(baseUrl, "thumbnails", thumbnail) +} + +actual fun SelfossModel.Item.getImages(): ArrayList { + val allImages = ArrayList() + + for ( image in Jsoup.parse(content).getElementsByTag("img")) { + val url = image.attr("src") + if (url.lowercase(Locale.US).contains(".jpg") || + url.lowercase(Locale.US).contains(".jpeg") || + url.lowercase(Locale.US).contains(".png") || + url.lowercase(Locale.US).contains(".webp")) + { + allImages.add(url) + } + } + return allImages +} + +actual fun SelfossModel.Source.getIcon(baseUrl: String): String { + return constructUrl(baseUrl, "favicons", icon) +} + +actual fun constructUrl(baseUrl: String, path: String, file: String?): String { + return if (file == null || file == "null" || file.isEmpty()) { + "" + } else { + val baseUriBuilder = Uri.parse(baseUrl).buildUpon() + baseUriBuilder.appendPath(path).appendPath(file) + + baseUriBuilder.toString() + } } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt index fb01972..2531907 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/model/SelfossModel.kt @@ -1,5 +1,7 @@ package bou.amine.apps.readerforselfossv2.model +import bou.amine.apps.readerforselfossv2.utils.DateUtils +import bou.amine.apps.readerforselfossv2.utils.getHtmlDecoded import kotlinx.serialization.Serializable class SelfossModel { @@ -67,5 +69,40 @@ class SelfossModel { val link: String, val sourcetitle: String, val tags: List - ) + ) { + // TODO: maybe find a better way to handle these kind of urls + fun getLinkDecoded(): String { + var stringUrl: String + stringUrl = + if (link.startsWith("http://news.google.com/news/") || link.startsWith("https://news.google.com/news/")) { + if (link.contains("&url=")) { + link.substringAfter("&url=") + } else { + this.link.replace("&", "&") + } + } else { + this.link.replace("&", "&") + } + + // handle :443 => https + if (stringUrl.contains(":443")) { + stringUrl = stringUrl.replace(":443", "").replace("http://", "https://") + } + + // handle url not starting with http + if (stringUrl.startsWith("//")) { + stringUrl = "http:$stringUrl" + } + + return stringUrl + } + + fun sourceAndDateText(dateUtils: DateUtils): String = + this.title.getHtmlDecoded() + dateUtils.parseRelativeDate(this.datetime) + + fun toggleStar(): Item { + this.starred = !this.starred + return this + } + } } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/DateUtils.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/DateUtils.kt index 2c58342..95518ba 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/DateUtils.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/DateUtils.kt @@ -6,9 +6,6 @@ import bou.amine.apps.readerforselfossv2.model.SelfossModel fun SelfossModel.Item.parseDate(dateUtils: DateUtils): Long = dateUtils.parseDate(this.datetime) -fun SelfossModel.Item.parseRelativeDate(dateUtils: DateUtils): String = - dateUtils.parseRelativeDate(this.datetime) - expect class DateUtils(apiMajorVersion: Int) { fun parseDate(dateString: String): Long diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt index ba0a6c6..2751b66 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt @@ -1,3 +1,15 @@ package bou.amine.apps.readerforselfossv2.utils -expect fun String.getHtmlDecoded(): String \ No newline at end of file +import bou.amine.apps.readerforselfossv2.model.SelfossModel + +expect fun String.getHtmlDecoded(): String + +expect fun SelfossModel.Item.getIcon(baseUrl: String): String + +expect fun SelfossModel.Item.getThumbnail(baseUrl: String): String + +expect fun SelfossModel.Item.getImages(): ArrayList + +expect fun SelfossModel.Source.getIcon(baseUrl: String): String + +expect fun constructUrl(baseUrl: String, path: String, file: String?): String \ No newline at end of file diff --git a/shared/src/iosArm64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt b/shared/src/iosArm64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt index c149800..9f66d23 100644 --- a/shared/src/iosArm64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt +++ b/shared/src/iosArm64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt @@ -1,5 +1,27 @@ package bou.amine.apps.readerforselfossv2.utils +import bou.amine.apps.readerforselfossv2.model.SelfossModel + actual fun String.getHtmlDecoded(): String { TODO("Not yet implemented") +} + +actual fun SelfossModel.Item.getIcon(baseUrl: String): String { + TODO("Not yet implemented") +} + +actual fun SelfossModel.Item.getThumbnail(baseUrl: String): String { + TODO("Not yet implemented") +} + +actual fun SelfossModel.Item.getImages(): ArrayList { + TODO("Not yet implemented") +} + +actual fun SelfossModel.Source.getIcon(baseUrl: String): String { + TODO("Not yet implemented") +} + +actual fun constructUrl(baseUrl: String, path: String, file: String?): String { + TODO("Not yet implemented") } \ No newline at end of file diff --git a/shared/src/iosX64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt b/shared/src/iosX64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt index c149800..9f66d23 100644 --- a/shared/src/iosX64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt +++ b/shared/src/iosX64Main/kotlin/bou/amine/apps/readerforselfossv2/utils/ModelConverters.kt @@ -1,5 +1,27 @@ package bou.amine.apps.readerforselfossv2.utils +import bou.amine.apps.readerforselfossv2.model.SelfossModel + actual fun String.getHtmlDecoded(): String { TODO("Not yet implemented") +} + +actual fun SelfossModel.Item.getIcon(baseUrl: String): String { + TODO("Not yet implemented") +} + +actual fun SelfossModel.Item.getThumbnail(baseUrl: String): String { + TODO("Not yet implemented") +} + +actual fun SelfossModel.Item.getImages(): ArrayList { + TODO("Not yet implemented") +} + +actual fun SelfossModel.Source.getIcon(baseUrl: String): String { + TODO("Not yet implemented") +} + +actual fun constructUrl(baseUrl: String, path: String, file: String?): String { + TODO("Not yet implemented") } \ No newline at end of file