Using extension methods to clean the code a little.
This commit is contained in:
parent
5320f88230
commit
493b1b12b3
@ -67,7 +67,7 @@ class AddSourceActivity : AppCompatActivity() {
|
||||
|
||||
val config = Config(this)
|
||||
|
||||
if (config.baseUrl.isEmpty() || !isUrlValid(config.baseUrl)) {
|
||||
if (config.baseUrl.isEmpty() || !config.baseUrl.isUrlValid()) {
|
||||
mustLoginToAddSource()
|
||||
} else {
|
||||
|
||||
|
@ -197,7 +197,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
|
||||
ItemTouchHelper(simpleItemTouchCallback).attachToRecyclerView(mRecyclerView)
|
||||
|
||||
checkAndDisplayStoreApk(this@HomeActivity)
|
||||
this@HomeActivity.checkAndDisplayStoreApk()
|
||||
|
||||
}
|
||||
|
||||
@ -212,7 +212,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
editor = settings.edit()
|
||||
|
||||
if (BuildConfig.GITHUB_VERSION) {
|
||||
checkApkVersion(settings, editor, this@HomeActivity, mFirebaseRemoteConfig)
|
||||
this@HomeActivity.checkApkVersion(settings, editor, mFirebaseRemoteConfig)
|
||||
}
|
||||
|
||||
handleSharedPrefs()
|
||||
@ -317,7 +317,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
|
||||
drawer.addItem(
|
||||
PrimaryDrawerItem()
|
||||
.withName(tag.tag)
|
||||
.withIdentifier(longHash(tag.tag))
|
||||
.withIdentifier(tag.tag.longHash())
|
||||
.withIcon(gd)
|
||||
.withBadge("${tag.unread}")
|
||||
.withBadgeStyle(
|
||||
|
@ -60,7 +60,7 @@ class LoginActivity : AppCompatActivity() {
|
||||
if (settings.getString("url", "").isNotEmpty()) {
|
||||
goToMain()
|
||||
} else {
|
||||
checkAndDisplayStoreApk(this@LoginActivity)
|
||||
this@LoginActivity.checkAndDisplayStoreApk()
|
||||
}
|
||||
|
||||
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this)
|
||||
@ -132,7 +132,7 @@ class LoginActivity : AppCompatActivity() {
|
||||
var cancel = false
|
||||
var focusView: View? = null
|
||||
|
||||
if (!isUrlValid(url)) {
|
||||
if (!url.isUrlValid()) {
|
||||
mUrlView.error = getString(R.string.login_url_problem)
|
||||
focusView = mUrlView
|
||||
cancel = true
|
||||
|
@ -52,7 +52,7 @@ class ReaderActivity : DragDismissActivity() {
|
||||
val shareBtn: ImageButton = v.findViewById(R.id.shareBtn) as ImageButton
|
||||
|
||||
|
||||
val customTabsIntent = buildCustomTabsIntent(this@ReaderActivity)
|
||||
val customTabsIntent = this@ReaderActivity.buildCustomTabsIntent()
|
||||
mCustomTabActivityHelper = CustomTabActivityHelper()
|
||||
mCustomTabActivityHelper.bindCustomTabsService(this)
|
||||
|
||||
@ -73,7 +73,7 @@ class ReaderActivity : DragDismissActivity() {
|
||||
.into(image)
|
||||
|
||||
shareBtn.setOnClickListener {
|
||||
shareLink(response.body()!!.url, this@ReaderActivity)
|
||||
this@ReaderActivity.shareLink(response.body()!!.url)
|
||||
}
|
||||
|
||||
browserBtn.setOnClickListener {
|
||||
|
@ -95,7 +95,7 @@ class ItemCardAdapter(private val app: Activity,
|
||||
TextDrawable
|
||||
.builder()
|
||||
.round()
|
||||
.build(texDrawableFromSource(itm.sourcetitle), color)
|
||||
.build(itm.sourcetitle.toTextDrawableString(), color)
|
||||
holder.sourceImage.setImageDrawable(drawable)
|
||||
} else {
|
||||
|
||||
@ -216,25 +216,24 @@ class ItemCardAdapter(private val app: Activity,
|
||||
})
|
||||
|
||||
shareBtn.setOnClickListener {
|
||||
shareLink(items[adapterPosition].getLinkDecoded(), c)
|
||||
c.shareLink(items[adapterPosition].getLinkDecoded())
|
||||
}
|
||||
|
||||
browserBtn.setOnClickListener {
|
||||
openInBrowser(items[adapterPosition], c)
|
||||
c.openInBrowser(items[adapterPosition])
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleCustomTabActions() {
|
||||
val customTabsIntent = buildCustomTabsIntent(c)
|
||||
val customTabsIntent = c.buildCustomTabsIntent()
|
||||
helper.bindCustomTabsService(app)
|
||||
|
||||
mView.setOnClickListener {
|
||||
openItemUrl(items[adapterPosition],
|
||||
c.openItemUrl(items[adapterPosition],
|
||||
customTabsIntent,
|
||||
internalBrowser,
|
||||
articleViewer,
|
||||
app,
|
||||
c)
|
||||
app)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -230,29 +230,28 @@ class ItemListAdapter(private val app: Activity,
|
||||
})
|
||||
|
||||
shareBtn.setOnClickListener {
|
||||
shareLink(items[adapterPosition].getLinkDecoded(), c)
|
||||
c.shareLink(items[adapterPosition].getLinkDecoded())
|
||||
}
|
||||
|
||||
browserBtn.setOnClickListener {
|
||||
openInBrowser(items[adapterPosition], c)
|
||||
c.openInBrowser(items[adapterPosition])
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private fun handleCustomTabActions() {
|
||||
val customTabsIntent = buildCustomTabsIntent(c)
|
||||
val customTabsIntent = c.buildCustomTabsIntent()
|
||||
helper.bindCustomTabsService(app)
|
||||
|
||||
|
||||
if (!clickBehavior) {
|
||||
mView.setOnClickListener {
|
||||
openItemUrl(items[adapterPosition],
|
||||
c.openItemUrl(items[adapterPosition],
|
||||
customTabsIntent,
|
||||
internalBrowser,
|
||||
articleViewer,
|
||||
app,
|
||||
c)
|
||||
app)
|
||||
}
|
||||
mView.setOnLongClickListener {
|
||||
actionBarShowHide()
|
||||
@ -261,12 +260,11 @@ class ItemListAdapter(private val app: Activity,
|
||||
} else {
|
||||
mView.setOnClickListener { actionBarShowHide() }
|
||||
mView.setOnLongClickListener {
|
||||
openItemUrl(items[adapterPosition],
|
||||
c.openItemUrl(items[adapterPosition],
|
||||
customTabsIntent,
|
||||
internalBrowser,
|
||||
articleViewer,
|
||||
app,
|
||||
c)
|
||||
app)
|
||||
true
|
||||
}
|
||||
}
|
||||
|
@ -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.Sources
|
||||
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,
|
||||
@ -50,7 +50,7 @@ class SourcesListAdapter(private val app: Activity,
|
||||
TextDrawable
|
||||
.builder()
|
||||
.round()
|
||||
.build(texDrawableFromSource(itm.title), color)
|
||||
.build(itm.title.toTextDrawableString(), color)
|
||||
holder.sourceImage.setImageDrawable(drawable)
|
||||
} else {
|
||||
Glide
|
||||
|
@ -14,7 +14,7 @@ private fun constructUrl(config: Config?, path: String, file: String): String {
|
||||
val baseUriBuilder = Uri.parse(config!!.baseUrl).buildUpon()
|
||||
baseUriBuilder.appendPath(path).appendPath(file)
|
||||
|
||||
return if (isEmptyOrNullOrNullString(file)) ""
|
||||
return if (file.isEmptyOrNullOrNullString()) ""
|
||||
else baseUriBuilder.toString()
|
||||
}
|
||||
|
||||
|
@ -16,46 +16,42 @@ import apps.amine.bou.readerforselfoss.R
|
||||
import apps.amine.bou.readerforselfoss.api.selfoss.Item
|
||||
|
||||
|
||||
fun checkAndDisplayStoreApk(context: Context) = {
|
||||
fun isStoreVersion(context: Context): Boolean {
|
||||
var result = false
|
||||
fun Context.checkAndDisplayStoreApk() = {
|
||||
fun isStoreVersion(): Boolean =
|
||||
try {
|
||||
val installer = context.packageManager
|
||||
.getInstallerPackageName(context.packageName)
|
||||
result = !TextUtils.isEmpty(installer)
|
||||
val installer = this.packageManager
|
||||
.getInstallerPackageName(this.packageName)
|
||||
!TextUtils.isEmpty(installer)
|
||||
} catch (e: Throwable) {
|
||||
false
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
if (!isStoreVersion(context) && !BuildConfig.GITHUB_VERSION) {
|
||||
val alertDialog = AlertDialog.Builder(context).create()
|
||||
alertDialog.setTitle(context.getString(R.string.warning_version))
|
||||
alertDialog.setMessage(context.getString(R.string.text_version))
|
||||
if (!isStoreVersion() && !BuildConfig.GITHUB_VERSION) {
|
||||
val alertDialog = AlertDialog.Builder(this).create()
|
||||
alertDialog.setTitle(getString(R.string.warning_version))
|
||||
alertDialog.setMessage(getString(R.string.text_version))
|
||||
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
|
||||
{ dialog, _ -> dialog.dismiss() })
|
||||
alertDialog.show()
|
||||
} else Unit
|
||||
}
|
||||
|
||||
fun isUrlValid(url: String): Boolean {
|
||||
val baseUrl = HttpUrl.parse(url)
|
||||
fun String.isUrlValid(): Boolean {
|
||||
val baseUrl = HttpUrl.parse(this)
|
||||
var existsAndEndsWithSlash = false
|
||||
if (baseUrl != null) {
|
||||
val pathSegments = baseUrl.pathSegments()
|
||||
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 =
|
||||
str == null || str == "null" || str.isEmpty()
|
||||
fun String?.isEmptyOrNullOrNullString(): Boolean =
|
||||
this == null || this == "null" || this.isEmpty()
|
||||
|
||||
fun checkApkVersion(settings: SharedPreferences,
|
||||
fun Context.checkApkVersion(settings: SharedPreferences,
|
||||
editor: SharedPreferences.Editor,
|
||||
context: Context,
|
||||
mFirebaseRemoteConfig: FirebaseRemoteConfig) = {
|
||||
fun isThereAnUpdate() {
|
||||
val APK_LINK = "github_apk"
|
||||
@ -63,16 +59,16 @@ fun checkApkVersion(settings: SharedPreferences,
|
||||
val apkLink = mFirebaseRemoteConfig.getString(APK_LINK)
|
||||
val storedLink = settings.getString(APK_LINK, "")
|
||||
if (apkLink != storedLink && !apkLink.isEmpty()) {
|
||||
val alertDialog = AlertDialog.Builder(context).create()
|
||||
alertDialog.setTitle(context.getString(R.string.new_apk_available_title))
|
||||
alertDialog.setMessage(context.getString(R.string.new_apk_available_message))
|
||||
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, context.getString(R.string.new_apk_available_get)) { _, _ ->
|
||||
val alertDialog = AlertDialog.Builder(this).create()
|
||||
alertDialog.setTitle(getString(R.string.new_apk_available_title))
|
||||
alertDialog.setMessage(getString(R.string.new_apk_available_message))
|
||||
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.new_apk_available_get)) { _, _ ->
|
||||
editor.putString(APK_LINK, apkLink)
|
||||
editor.apply()
|
||||
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, _ ->
|
||||
editor.putString(APK_LINK, apkLink)
|
||||
editor.apply()
|
||||
@ -93,10 +89,10 @@ fun checkApkVersion(settings: SharedPreferences,
|
||||
}
|
||||
}
|
||||
|
||||
fun longHash(string: String): Long {
|
||||
fun String.longHash(): Long {
|
||||
var h = 98764321261L
|
||||
val l = string.length
|
||||
val chars = string.toCharArray()
|
||||
val l = this.length
|
||||
val chars = this.toCharArray()
|
||||
|
||||
for (i in 0..l - 1) {
|
||||
h = 31 * h + chars[i].toLong()
|
||||
@ -104,24 +100,24 @@ fun longHash(string: String): Long {
|
||||
return h
|
||||
}
|
||||
|
||||
fun handleLinksWithoutHttp(itemUrl: String) =
|
||||
if (!itemUrl.startsWith("https://") && !itemUrl.startsWith("http://"))
|
||||
"http://" + itemUrl
|
||||
fun String.toStringUriWithHttp() =
|
||||
if (!this.startsWith("https://") && !this.startsWith("http://"))
|
||||
"http://" + this
|
||||
else
|
||||
itemUrl
|
||||
this
|
||||
|
||||
fun shareLink(itemUrl: String, c: Context) {
|
||||
fun Context.shareLink(itemUrl: String) {
|
||||
val sendIntent = Intent()
|
||||
sendIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
sendIntent.action = Intent.ACTION_SEND
|
||||
sendIntent.putExtra(Intent.EXTRA_TEXT, handleLinksWithoutHttp(itemUrl))
|
||||
sendIntent.putExtra(Intent.EXTRA_TEXT, itemUrl.toStringUriWithHttp())
|
||||
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)
|
||||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
intent.data = Uri.parse(handleLinksWithoutHttp(i.getLinkDecoded()))
|
||||
c.startActivity(intent)
|
||||
intent.data = Uri.parse(i.getLinkDecoded().toStringUriWithHttp())
|
||||
startActivity(intent)
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
package apps.amine.bou.readerforselfoss.utils
|
||||
|
||||
|
||||
fun texDrawableFromSource(str: String): String {
|
||||
fun String.toTextDrawableString(): String {
|
||||
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])
|
||||
}
|
||||
return textDrawable.toString()
|
||||
|
@ -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()
|
||||
|
||||
// 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.resources.getColor(R.color.colorAccentDark))
|
||||
intentBuilder.setToolbarColor(resources.getColor(R.color.colorAccentDark))
|
||||
intentBuilder.setShowTitle(true)
|
||||
|
||||
|
||||
intentBuilder.setStartAnimations(c,
|
||||
intentBuilder.setStartAnimations(this,
|
||||
R.anim.slide_in_right,
|
||||
R.anim.slide_out_left)
|
||||
intentBuilder.setExitAnimations(c,
|
||||
intentBuilder.setExitAnimations(this,
|
||||
android.R.anim.slide_in_left,
|
||||
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)
|
||||
|
||||
val shareLabel = c.getString(R.string.label_share)
|
||||
val icon = BitmapFactory.decodeResource(c.resources,
|
||||
val shareLabel = this.getString(R.string.label_share)
|
||||
val icon = BitmapFactory.decodeResource(resources,
|
||||
R.drawable.ic_share_white_24dp)
|
||||
intentBuilder.setActionButton(icon, shareLabel, createPendingShareIntent(c))
|
||||
intentBuilder.setActionButton(icon, shareLabel, createPendingShareIntent)
|
||||
|
||||
return intentBuilder.build()
|
||||
}
|
||||
|
||||
fun openItemUrl(i: Item,
|
||||
fun Context.openItemUrl(i: Item,
|
||||
customTabsIntent: CustomTabsIntent,
|
||||
internalBrowser: Boolean,
|
||||
articleViewer: Boolean,
|
||||
app: Activity,
|
||||
c: Context) {
|
||||
app: Activity) {
|
||||
if (!internalBrowser) {
|
||||
val intent = Intent(Intent.ACTION_VIEW)
|
||||
intent.data = Uri.parse(i.getLinkDecoded())
|
||||
app.startActivity(intent)
|
||||
} else {
|
||||
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
|
||||
.setDragElasticity(DragDismissIntentBuilder.DragElasticity.NORMAL) // Larger elasticities will make it easier to dismiss.
|
||||
.build(intent)
|
||||
@ -78,7 +75,7 @@ fun openItemUrl(i: Item,
|
||||
) { _, uri ->
|
||||
val intent = Intent(Intent.ACTION_VIEW, uri)
|
||||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
c.startActivity(intent)
|
||||
startActivity(intent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user