Using extension methods to clean the code a little.

This commit is contained in:
Amine 2017-06-14 21:10:53 +02:00
parent 5320f88230
commit 493b1b12b3
11 changed files with 78 additions and 88 deletions

View File

@ -67,7 +67,7 @@ class AddSourceActivity : AppCompatActivity() {
val config = Config(this) val config = Config(this)
if (config.baseUrl.isEmpty() || !isUrlValid(config.baseUrl)) { if (config.baseUrl.isEmpty() || !config.baseUrl.isUrlValid()) {
mustLoginToAddSource() mustLoginToAddSource()
} else { } else {

View File

@ -197,7 +197,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
ItemTouchHelper(simpleItemTouchCallback).attachToRecyclerView(mRecyclerView) ItemTouchHelper(simpleItemTouchCallback).attachToRecyclerView(mRecyclerView)
checkAndDisplayStoreApk(this@HomeActivity) this@HomeActivity.checkAndDisplayStoreApk()
} }
@ -212,7 +212,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
editor = settings.edit() editor = settings.edit()
if (BuildConfig.GITHUB_VERSION) { if (BuildConfig.GITHUB_VERSION) {
checkApkVersion(settings, editor, this@HomeActivity, mFirebaseRemoteConfig) this@HomeActivity.checkApkVersion(settings, editor, mFirebaseRemoteConfig)
} }
handleSharedPrefs() handleSharedPrefs()
@ -317,7 +317,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
drawer.addItem( drawer.addItem(
PrimaryDrawerItem() PrimaryDrawerItem()
.withName(tag.tag) .withName(tag.tag)
.withIdentifier(longHash(tag.tag)) .withIdentifier(tag.tag.longHash())
.withIcon(gd) .withIcon(gd)
.withBadge("${tag.unread}") .withBadge("${tag.unread}")
.withBadgeStyle( .withBadgeStyle(

View File

@ -60,7 +60,7 @@ class LoginActivity : AppCompatActivity() {
if (settings.getString("url", "").isNotEmpty()) { if (settings.getString("url", "").isNotEmpty()) {
goToMain() goToMain()
} else { } else {
checkAndDisplayStoreApk(this@LoginActivity) this@LoginActivity.checkAndDisplayStoreApk()
} }
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this) mFirebaseAnalytics = FirebaseAnalytics.getInstance(this)
@ -132,7 +132,7 @@ class LoginActivity : AppCompatActivity() {
var cancel = false var cancel = false
var focusView: View? = null var focusView: View? = null
if (!isUrlValid(url)) { if (!url.isUrlValid()) {
mUrlView.error = getString(R.string.login_url_problem) mUrlView.error = getString(R.string.login_url_problem)
focusView = mUrlView focusView = mUrlView
cancel = true cancel = true

View File

@ -52,7 +52,7 @@ class ReaderActivity : DragDismissActivity() {
val shareBtn: ImageButton = v.findViewById(R.id.shareBtn) as ImageButton val shareBtn: ImageButton = v.findViewById(R.id.shareBtn) as ImageButton
val customTabsIntent = buildCustomTabsIntent(this@ReaderActivity) val customTabsIntent = this@ReaderActivity.buildCustomTabsIntent()
mCustomTabActivityHelper = CustomTabActivityHelper() mCustomTabActivityHelper = CustomTabActivityHelper()
mCustomTabActivityHelper.bindCustomTabsService(this) mCustomTabActivityHelper.bindCustomTabsService(this)
@ -73,7 +73,7 @@ class ReaderActivity : DragDismissActivity() {
.into(image) .into(image)
shareBtn.setOnClickListener { shareBtn.setOnClickListener {
shareLink(response.body()!!.url, this@ReaderActivity) this@ReaderActivity.shareLink(response.body()!!.url)
} }
browserBtn.setOnClickListener { browserBtn.setOnClickListener {

View File

@ -95,7 +95,7 @@ class ItemCardAdapter(private val app: Activity,
TextDrawable TextDrawable
.builder() .builder()
.round() .round()
.build(texDrawableFromSource(itm.sourcetitle), color) .build(itm.sourcetitle.toTextDrawableString(), color)
holder.sourceImage.setImageDrawable(drawable) holder.sourceImage.setImageDrawable(drawable)
} else { } else {
@ -216,25 +216,24 @@ class ItemCardAdapter(private val app: Activity,
}) })
shareBtn.setOnClickListener { shareBtn.setOnClickListener {
shareLink(items[adapterPosition].getLinkDecoded(), c) c.shareLink(items[adapterPosition].getLinkDecoded())
} }
browserBtn.setOnClickListener { browserBtn.setOnClickListener {
openInBrowser(items[adapterPosition], c) c.openInBrowser(items[adapterPosition])
} }
} }
private fun handleCustomTabActions() { private fun handleCustomTabActions() {
val customTabsIntent = buildCustomTabsIntent(c) val customTabsIntent = c.buildCustomTabsIntent()
helper.bindCustomTabsService(app) helper.bindCustomTabsService(app)
mView.setOnClickListener { mView.setOnClickListener {
openItemUrl(items[adapterPosition], c.openItemUrl(items[adapterPosition],
customTabsIntent, customTabsIntent,
internalBrowser, internalBrowser,
articleViewer, articleViewer,
app, app)
c)
} }
} }
} }

View File

@ -230,29 +230,28 @@ class ItemListAdapter(private val app: Activity,
}) })
shareBtn.setOnClickListener { shareBtn.setOnClickListener {
shareLink(items[adapterPosition].getLinkDecoded(), c) c.shareLink(items[adapterPosition].getLinkDecoded())
} }
browserBtn.setOnClickListener { browserBtn.setOnClickListener {
openInBrowser(items[adapterPosition], c) c.openInBrowser(items[adapterPosition])
} }
} }
private fun handleCustomTabActions() { private fun handleCustomTabActions() {
val customTabsIntent = buildCustomTabsIntent(c) val customTabsIntent = c.buildCustomTabsIntent()
helper.bindCustomTabsService(app) helper.bindCustomTabsService(app)
if (!clickBehavior) { if (!clickBehavior) {
mView.setOnClickListener { mView.setOnClickListener {
openItemUrl(items[adapterPosition], c.openItemUrl(items[adapterPosition],
customTabsIntent, customTabsIntent,
internalBrowser, internalBrowser,
articleViewer, articleViewer,
app, app)
c)
} }
mView.setOnLongClickListener { mView.setOnLongClickListener {
actionBarShowHide() actionBarShowHide()
@ -261,12 +260,11 @@ class ItemListAdapter(private val app: Activity,
} else { } else {
mView.setOnClickListener { actionBarShowHide() } mView.setOnClickListener { actionBarShowHide() }
mView.setOnLongClickListener { mView.setOnLongClickListener {
openItemUrl(items[adapterPosition], c.openItemUrl(items[adapterPosition],
customTabsIntent, customTabsIntent,
internalBrowser, internalBrowser,
articleViewer, articleViewer,
app, app)
c)
true true
} }
} }

View File

@ -25,7 +25,7 @@ import apps.amine.bou.readerforselfoss.R
import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi import apps.amine.bou.readerforselfoss.api.selfoss.SelfossApi
import apps.amine.bou.readerforselfoss.api.selfoss.Sources import apps.amine.bou.readerforselfoss.api.selfoss.Sources
import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse import apps.amine.bou.readerforselfoss.api.selfoss.SuccessResponse
import apps.amine.bou.readerforselfoss.utils.texDrawableFromSource import apps.amine.bou.readerforselfoss.utils.toTextDrawableString
class SourcesListAdapter(private val app: Activity, class SourcesListAdapter(private val app: Activity,
@ -50,7 +50,7 @@ class SourcesListAdapter(private val app: Activity,
TextDrawable TextDrawable
.builder() .builder()
.round() .round()
.build(texDrawableFromSource(itm.title), color) .build(itm.title.toTextDrawableString(), color)
holder.sourceImage.setImageDrawable(drawable) holder.sourceImage.setImageDrawable(drawable)
} else { } else {
Glide Glide

View File

@ -14,7 +14,7 @@ private fun constructUrl(config: Config?, path: String, file: String): String {
val baseUriBuilder = Uri.parse(config!!.baseUrl).buildUpon() val baseUriBuilder = Uri.parse(config!!.baseUrl).buildUpon()
baseUriBuilder.appendPath(path).appendPath(file) baseUriBuilder.appendPath(path).appendPath(file)
return if (isEmptyOrNullOrNullString(file)) "" return if (file.isEmptyOrNullOrNullString()) ""
else baseUriBuilder.toString() else baseUriBuilder.toString()
} }

View File

@ -16,46 +16,42 @@ import apps.amine.bou.readerforselfoss.R
import apps.amine.bou.readerforselfoss.api.selfoss.Item import apps.amine.bou.readerforselfoss.api.selfoss.Item
fun checkAndDisplayStoreApk(context: Context) = { fun Context.checkAndDisplayStoreApk() = {
fun isStoreVersion(context: Context): Boolean { fun isStoreVersion(): Boolean =
var result = false
try { try {
val installer = context.packageManager val installer = this.packageManager
.getInstallerPackageName(context.packageName) .getInstallerPackageName(this.packageName)
result = !TextUtils.isEmpty(installer) !TextUtils.isEmpty(installer)
} catch (e: Throwable) { } catch (e: Throwable) {
false
} }
return result if (!isStoreVersion() && !BuildConfig.GITHUB_VERSION) {
} val alertDialog = AlertDialog.Builder(this).create()
alertDialog.setTitle(getString(R.string.warning_version))
if (!isStoreVersion(context) && !BuildConfig.GITHUB_VERSION) { alertDialog.setMessage(getString(R.string.text_version))
val alertDialog = AlertDialog.Builder(context).create()
alertDialog.setTitle(context.getString(R.string.warning_version))
alertDialog.setMessage(context.getString(R.string.text_version))
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
{ dialog, _ -> dialog.dismiss() }) { dialog, _ -> dialog.dismiss() })
alertDialog.show() alertDialog.show()
} else Unit } else Unit
} }
fun isUrlValid(url: String): Boolean { fun String.isUrlValid(): Boolean {
val baseUrl = HttpUrl.parse(url) val baseUrl = HttpUrl.parse(this)
var existsAndEndsWithSlash = false var existsAndEndsWithSlash = false
if (baseUrl != null) { if (baseUrl != null) {
val pathSegments = baseUrl.pathSegments() val pathSegments = baseUrl.pathSegments()
existsAndEndsWithSlash = "" == pathSegments[pathSegments.size - 1] existsAndEndsWithSlash = "" == pathSegments[pathSegments.size - 1]
} }
return Patterns.WEB_URL.matcher(url).matches() && existsAndEndsWithSlash return Patterns.WEB_URL.matcher(this).matches() && existsAndEndsWithSlash
} }
fun isEmptyOrNullOrNullString(str: String?): Boolean = fun String?.isEmptyOrNullOrNullString(): Boolean =
str == null || str == "null" || str.isEmpty() this == null || this == "null" || this.isEmpty()
fun checkApkVersion(settings: SharedPreferences, fun Context.checkApkVersion(settings: SharedPreferences,
editor: SharedPreferences.Editor, editor: SharedPreferences.Editor,
context: Context,
mFirebaseRemoteConfig: FirebaseRemoteConfig) = { mFirebaseRemoteConfig: FirebaseRemoteConfig) = {
fun isThereAnUpdate() { fun isThereAnUpdate() {
val APK_LINK = "github_apk" val APK_LINK = "github_apk"
@ -63,16 +59,16 @@ fun checkApkVersion(settings: SharedPreferences,
val apkLink = mFirebaseRemoteConfig.getString(APK_LINK) val apkLink = mFirebaseRemoteConfig.getString(APK_LINK)
val storedLink = settings.getString(APK_LINK, "") val storedLink = settings.getString(APK_LINK, "")
if (apkLink != storedLink && !apkLink.isEmpty()) { if (apkLink != storedLink && !apkLink.isEmpty()) {
val alertDialog = AlertDialog.Builder(context).create() val alertDialog = AlertDialog.Builder(this).create()
alertDialog.setTitle(context.getString(R.string.new_apk_available_title)) alertDialog.setTitle(getString(R.string.new_apk_available_title))
alertDialog.setMessage(context.getString(R.string.new_apk_available_message)) alertDialog.setMessage(getString(R.string.new_apk_available_message))
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, context.getString(R.string.new_apk_available_get)) { _, _ -> alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.new_apk_available_get)) { _, _ ->
editor.putString(APK_LINK, apkLink) editor.putString(APK_LINK, apkLink)
editor.apply() editor.apply()
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(apkLink)) val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(apkLink))
context.startActivity(browserIntent) startActivity(browserIntent)
} }
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, context.getString(R.string.new_apk_available_no), alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, getString(R.string.new_apk_available_no),
{ dialog, _ -> { dialog, _ ->
editor.putString(APK_LINK, apkLink) editor.putString(APK_LINK, apkLink)
editor.apply() editor.apply()
@ -93,10 +89,10 @@ fun checkApkVersion(settings: SharedPreferences,
} }
} }
fun longHash(string: String): Long { fun String.longHash(): Long {
var h = 98764321261L var h = 98764321261L
val l = string.length val l = this.length
val chars = string.toCharArray() val chars = this.toCharArray()
for (i in 0..l - 1) { for (i in 0..l - 1) {
h = 31 * h + chars[i].toLong() h = 31 * h + chars[i].toLong()
@ -104,24 +100,24 @@ fun longHash(string: String): Long {
return h return h
} }
fun handleLinksWithoutHttp(itemUrl: String) = fun String.toStringUriWithHttp() =
if (!itemUrl.startsWith("https://") && !itemUrl.startsWith("http://")) if (!this.startsWith("https://") && !this.startsWith("http://"))
"http://" + itemUrl "http://" + this
else else
itemUrl this
fun shareLink(itemUrl: String, c: Context) { fun Context.shareLink(itemUrl: String) {
val sendIntent = Intent() val sendIntent = Intent()
sendIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK sendIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
sendIntent.action = Intent.ACTION_SEND sendIntent.action = Intent.ACTION_SEND
sendIntent.putExtra(Intent.EXTRA_TEXT, handleLinksWithoutHttp(itemUrl)) sendIntent.putExtra(Intent.EXTRA_TEXT, itemUrl.toStringUriWithHttp())
sendIntent.type = "text/plain" sendIntent.type = "text/plain"
c.startActivity(Intent.createChooser(sendIntent, c.getString(R.string.share)).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)) startActivity(Intent.createChooser(sendIntent, getString(R.string.share)).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
} }
fun openInBrowser(i: Item, c: Context) { fun Context.openInBrowser(i: Item) {
val intent = Intent(Intent.ACTION_VIEW) val intent = Intent(Intent.ACTION_VIEW)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.data = Uri.parse(handleLinksWithoutHttp(i.getLinkDecoded())) intent.data = Uri.parse(i.getLinkDecoded().toStringUriWithHttp())
c.startActivity(intent) startActivity(intent)
} }

View File

@ -1,9 +1,9 @@
package apps.amine.bou.readerforselfoss.utils package apps.amine.bou.readerforselfoss.utils
fun texDrawableFromSource(str: String): String { fun String.toTextDrawableString(): String {
val textDrawable = StringBuilder() val textDrawable = StringBuilder()
for (s in str.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) { for (s in this.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()) {
textDrawable.append(s[0]) textDrawable.append(s[0])
} }
return textDrawable.toString() return textDrawable.toString()

View File

@ -17,56 +17,53 @@ import apps.amine.bou.readerforselfoss.utils.customtabs.CustomTabActivityHelper
fun buildCustomTabsIntent(c: Context): CustomTabsIntent { fun Context.buildCustomTabsIntent(): CustomTabsIntent {
val actionIntent = Intent(Intent.ACTION_SEND)
actionIntent.type = "text/plain"
val createPendingShareIntent: PendingIntent = PendingIntent.getActivity(this, 0, actionIntent, 0)
fun createPendingShareIntent(c: Context): PendingIntent {
val actionIntent = Intent(Intent.ACTION_SEND)
actionIntent.type = "text/plain"
return PendingIntent.getActivity(
c, 0, actionIntent, 0)
}
val intentBuilder = CustomTabsIntent.Builder() val intentBuilder = CustomTabsIntent.Builder()
// TODO: change to primary when it's possible to customize custom tabs title color // TODO: change to primary when it's possible to customize custom tabs title color
//intentBuilder.setToolbarColor(c.getResources().getColor(R.color.colorPrimary)); //intentBuilder.setToolbarColor(c.getResources().getColor(R.color.colorPrimary));
intentBuilder.setToolbarColor(c.resources.getColor(R.color.colorAccentDark)) intentBuilder.setToolbarColor(resources.getColor(R.color.colorAccentDark))
intentBuilder.setShowTitle(true) intentBuilder.setShowTitle(true)
intentBuilder.setStartAnimations(c, intentBuilder.setStartAnimations(this,
R.anim.slide_in_right, R.anim.slide_in_right,
R.anim.slide_out_left) R.anim.slide_out_left)
intentBuilder.setExitAnimations(c, intentBuilder.setExitAnimations(this,
android.R.anim.slide_in_left, android.R.anim.slide_in_left,
android.R.anim.slide_out_right) android.R.anim.slide_out_right)
val closeicon = BitmapFactory.decodeResource(c.resources, R.drawable.ic_close_white_24dp) val closeicon = BitmapFactory.decodeResource(resources, R.drawable.ic_close_white_24dp)
intentBuilder.setCloseButtonIcon(closeicon) intentBuilder.setCloseButtonIcon(closeicon)
val shareLabel = c.getString(R.string.label_share) val shareLabel = this.getString(R.string.label_share)
val icon = BitmapFactory.decodeResource(c.resources, val icon = BitmapFactory.decodeResource(resources,
R.drawable.ic_share_white_24dp) R.drawable.ic_share_white_24dp)
intentBuilder.setActionButton(icon, shareLabel, createPendingShareIntent(c)) intentBuilder.setActionButton(icon, shareLabel, createPendingShareIntent)
return intentBuilder.build() return intentBuilder.build()
} }
fun openItemUrl(i: Item, fun Context.openItemUrl(i: Item,
customTabsIntent: CustomTabsIntent, customTabsIntent: CustomTabsIntent,
internalBrowser: Boolean, internalBrowser: Boolean,
articleViewer: Boolean, articleViewer: Boolean,
app: Activity, app: Activity) {
c: Context) {
if (!internalBrowser) { if (!internalBrowser) {
val intent = Intent(Intent.ACTION_VIEW) val intent = Intent(Intent.ACTION_VIEW)
intent.data = Uri.parse(i.getLinkDecoded()) intent.data = Uri.parse(i.getLinkDecoded())
app.startActivity(intent) app.startActivity(intent)
} else { } else {
if (articleViewer) { if (articleViewer) {
val intent = Intent(c, ReaderActivity::class.java) val intent = Intent(this, ReaderActivity::class.java)
DragDismissIntentBuilder(c) DragDismissIntentBuilder(this)
.setFullscreenOnTablets(true) // defaults to false, tablets will have padding on each side .setFullscreenOnTablets(true) // defaults to false, tablets will have padding on each side
.setDragElasticity(DragDismissIntentBuilder.DragElasticity.NORMAL) // Larger elasticities will make it easier to dismiss. .setDragElasticity(DragDismissIntentBuilder.DragElasticity.NORMAL) // Larger elasticities will make it easier to dismiss.
.build(intent) .build(intent)
@ -78,7 +75,7 @@ fun openItemUrl(i: Item,
) { _, uri -> ) { _, uri ->
val intent = Intent(Intent.ACTION_VIEW, uri) val intent = Intent(Intent.ACTION_VIEW, uri)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
c.startActivity(intent) startActivity(intent)
} }
} }
} }