From 246ec2c3ac5e3047e8cb29ba5892f097ec8f45b6 Mon Sep 17 00:00:00 2001 From: Amine Date: Tue, 13 Jun 2017 21:34:48 +0200 Subject: [PATCH] Should fix #27 --- .../bou/readerforselfoss/ReaderActivity.kt | 9 ++----- .../adapters/ItemCardAdapter.kt | 18 ++++---------- .../adapters/ItemListAdapter.kt | 18 ++++---------- .../bou/readerforselfoss/utils/AppUtils.kt | 24 ++++++++++++++++++- 4 files changed, 34 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt index 71b49b0..751d1f9 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt @@ -22,7 +22,7 @@ import apps.amine.bou.readerforselfoss.api.mercury.MercuryApi import apps.amine.bou.readerforselfoss.api.mercury.ParsedContent import apps.amine.bou.readerforselfoss.utils.buildCustomTabsIntent import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper - +import apps.amine.bou.readerforselfoss.utils.shareLink class ReaderActivity : DragDismissActivity() { @@ -73,12 +73,7 @@ class ReaderActivity : DragDismissActivity() { .into(image) shareBtn.setOnClickListener { - val sendIntent = Intent() - sendIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK - sendIntent.action = Intent.ACTION_SEND - sendIntent.putExtra(Intent.EXTRA_TEXT, response.body()!!.url) - sendIntent.type = "text/plain" - startActivity(Intent.createChooser(sendIntent, getString(R.string.share)).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) + shareLink(response.body()!!.url, this@ReaderActivity) } browserBtn.setOnClickListener { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt index fd62ac2..c09e19f 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemCardAdapter.kt @@ -2,10 +2,8 @@ package apps.amine.bou.readerforselfoss.adapters import android.app.Activity import android.content.Context -import android.content.Intent import android.graphics.Bitmap import android.graphics.Color -import android.net.Uri import android.support.constraint.ConstraintLayout import android.support.design.widget.Snackbar import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory @@ -39,7 +37,9 @@ import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse import apps.amine.bou.readerforselfoss.utils.buildCustomTabsIntent import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper +import apps.amine.bou.readerforselfoss.utils.openInBrowser import apps.amine.bou.readerforselfoss.utils.openItemUrl +import apps.amine.bou.readerforselfoss.utils.shareLink class ItemCardAdapter(private val app: Activity, private val items: ArrayList, @@ -221,21 +221,11 @@ class ItemCardAdapter(private val app: Activity, }) shareBtn.setOnClickListener { - val i = items[adapterPosition] - val sendIntent = Intent() - sendIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK - sendIntent.action = Intent.ACTION_SEND - sendIntent.putExtra(Intent.EXTRA_TEXT, i.getLinkDecoded()) - sendIntent.type = "text/plain" - c.startActivity(Intent.createChooser(sendIntent, c.getString(R.string.share)).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) + shareLink(items[adapterPosition].getLinkDecoded(), c) } browserBtn.setOnClickListener { - val i = items[adapterPosition] - val intent = Intent(Intent.ACTION_VIEW) - intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK - intent.data = Uri.parse(i.getLinkDecoded()) - c.startActivity(intent) + openInBrowser(items[adapterPosition], c) } } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt index 938353b..3a0fb4e 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/adapters/ItemListAdapter.kt @@ -38,8 +38,9 @@ import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse import apps.amine.bou.readerforselfoss.utils.buildCustomTabsIntent import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper +import apps.amine.bou.readerforselfoss.utils.openInBrowser import apps.amine.bou.readerforselfoss.utils.openItemUrl - +import apps.amine.bou.readerforselfoss.utils.shareLink class ItemListAdapter(private val app: Activity, @@ -229,21 +230,12 @@ class ItemListAdapter(private val app: Activity, }) shareBtn.setOnClickListener { - val i = items[adapterPosition] - val sendIntent = Intent() - sendIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK - sendIntent.action = Intent.ACTION_SEND - sendIntent.putExtra(Intent.EXTRA_TEXT, i.getLinkDecoded()) - sendIntent.type = "text/plain" - c.startActivity(Intent.createChooser(sendIntent, c.getString(R.string.share)).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) + shareLink(items[adapterPosition].getLinkDecoded(), c) } browserBtn.setOnClickListener { - val i = items[adapterPosition] - val intent = Intent(Intent.ACTION_VIEW) - intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK - intent.data = Uri.parse(i.getLinkDecoded()) - c.startActivity(intent) + openInBrowser(items[adapterPosition], c) + } } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/AppUtils.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/AppUtils.kt index 03e5c0f..b0daeab 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/AppUtils.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/AppUtils.kt @@ -13,7 +13,7 @@ import okhttp3.HttpUrl import apps.amine.bou.readerforselfoss.BuildConfig import apps.amine.bou.readerforselfoss.R - +import apps.amine.bou.readerforselfoss.api.selfoss.Item fun checkAndDisplayStoreApk(context: Context) = { @@ -102,4 +102,26 @@ fun longHash(string: String): Long { h = 31 * h + chars[i].toLong() } return h +} + +fun handleLinksWithoutHttp(itemUrl: String) = + if (!itemUrl.startsWith("https://") && !itemUrl.startsWith("http://")) + "http://" + itemUrl + else + itemUrl + +fun shareLink(itemUrl: String, c: Context) { + val sendIntent = Intent() + sendIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK + sendIntent.action = Intent.ACTION_SEND + sendIntent.putExtra(Intent.EXTRA_TEXT, handleLinksWithoutHttp(itemUrl)) + sendIntent.type = "text/plain" + c.startActivity(Intent.createChooser(sendIntent, c.getString(R.string.share)).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) +} + +fun openInBrowser(i: Item, c: Context) { + val intent = Intent(Intent.ACTION_VIEW) + intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK + intent.data = Uri.parse(handleLinksWithoutHttp(i.getLinkDecoded())) + c.startActivity(intent) } \ No newline at end of file