aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/kotlin/ca
diff options
context:
space:
mode:
Diffstat (limited to 'core/src/main/kotlin/ca')
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt6
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt17
-rw-r--r--core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt48
3 files changed, 45 insertions, 26 deletions
diff --git a/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt b/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt
index 88a0945..8c6acff 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt
@@ -13,6 +13,7 @@ import ca.allanwang.kau.logging.KL
import ca.allanwang.kau.utils.installerPackageName
import ca.allanwang.kau.utils.isAppInstalled
import ca.allanwang.kau.utils.string
+import ca.allanwang.kau.utils.toast
/**
@@ -76,7 +77,10 @@ class EmailBuilder(val email: String, val subject: String) {
emailBuilder.append("\n").append(footer)
intent.putExtra(Intent.EXTRA_TEXT, emailBuilder.toString())
- context.startActivity(Intent.createChooser(intent, context.resources.getString(R.string.kau_send_via)))
+ if (intent.resolveActivity(context.packageManager) != null)
+ context.startActivity(Intent.createChooser(intent, context.resources.getString(R.string.kau_send_via)))
+ else
+ context.toast("Cannot resolve email activity", log = true)
}
}
diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt
index be16c7c..fbb8c7d 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt
@@ -3,6 +3,7 @@ package ca.allanwang.kau.kpref
import android.content.Context
import android.content.SharedPreferences
import ca.allanwang.kau.kotlin.ILazyResettable
+import ca.allanwang.kau.logging.KL
/**
* Created by Allan Wang on 2017-06-07.
@@ -22,15 +23,13 @@ import ca.allanwang.kau.kotlin.ILazyResettable
*/
open class KPref {
- lateinit private var c: Context
- lateinit internal var PREFERENCE_NAME: String
- private var initialized = false
+ lateinit var PREFERENCE_NAME: String
+ lateinit var sp: SharedPreferences
fun initialize(c: Context, preferenceName: String) {
- if (initialized) throw KPrefException("KPref object $preferenceName has already been initialized; please only do so once")
- initialized = true
- this.c = c.applicationContext
PREFERENCE_NAME = preferenceName
+ sp = c.applicationContext.getSharedPreferences(preferenceName, Context.MODE_PRIVATE)
+ KL.d("Shared Preference $preferenceName has been initialized")
val toDelete = deleteKeys()
if (toDelete.isNotEmpty()) {
val edit = sp.edit()
@@ -39,12 +38,6 @@ open class KPref {
}
}
- //todo hide this
- val sp: SharedPreferences by lazy {
- if (!initialized) throw KPrefException("KPref object has not yet been initialized; please initialize it with a context and preference name")
- c.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
- }
-
internal val prefMap: MutableMap<String, ILazyResettable<*>> = mutableMapOf()
fun reset() {
diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
index d561c8a..a72c7dd 100644
--- a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
+++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
@@ -77,7 +77,11 @@ fun Context.startActivitySlideOut(clazz: Class<out Activity>, clearStack: Boolea
fun Context.startPlayStoreLink(@StringRes packageIdRes: Int) = startPlayStoreLink(string(packageIdRes))
fun Context.startPlayStoreLink(packageId: String) {
- startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$packageId")))
+ val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$packageId"))
+ if (intent.resolveActivity(packageManager) != null)
+ startActivity(intent)
+ else
+ toast("Cannot resolve play store", log = true)
}
/**
@@ -87,22 +91,24 @@ fun Context.startPlayStoreLink(packageId: String) {
fun Context.startLink(vararg url: String?) {
val link = url.firstOrNull { !it.isNullOrBlank() } ?: return
val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(link))
- startActivity(browserIntent)
+ if (browserIntent.resolveActivity(packageManager) != null)
+ startActivity(browserIntent)
+ else
+ toast("Cannot resolve browser", log = true)
}
-fun Context.startLink(@StringRes url: Int) {
- val browserIntent = Intent(Intent.ACTION_VIEW, Uri.parse(string(url)))
- startActivity(browserIntent)
-}
+fun Context.startLink(@StringRes url: Int) = startLink(string(url))
//Toast helpers
-inline fun View.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_LONG) = context.toast(id, duration)
+inline fun View.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) = context.toast(id, duration, log)
+
+inline fun Context.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) = toast(this.string(id), duration, log)
-inline fun Context.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_LONG) = toast(this.string(id), duration)
+inline fun View.toast(text: String, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) = context.toast(text, duration, log)
-inline fun View.toast(text: String, duration: Int = Toast.LENGTH_LONG) = context.toast(text, duration)
-inline fun Context.toast(text: String, duration: Int = Toast.LENGTH_LONG) {
+inline fun Context.toast(text: String, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) {
Toast.makeText(this, text, duration).show()
+ if (log) KL.i("Toast: $text")
}
//Resource retrievers
@@ -163,6 +169,10 @@ fun Context.resolveString(@AttrRes attr: Int, fallback: String = ""): String {
inline fun Context.materialDialog(action: MaterialDialog.Builder.() -> Unit): MaterialDialog {
val builder = MaterialDialog.Builder(this)
builder.action()
+ if (isFinishing) {
+ KL.d("Material Dialog triggered from finishing context; did not show")
+ return builder.build()
+ }
return builder.show()
}
@@ -192,9 +202,21 @@ fun Context.copyToClipboard(text: String?, label: String = "Copied Text", showTo
}
fun Context.shareText(text: String?) {
- if (text == null) return toast(R.string.kau_text_is_null)
+ if (text == null) return toast("Share text is null")
val intent = Intent(Intent.ACTION_SEND)
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_TEXT, text)
- startActivity(Intent.createChooser(intent, string(R.string.kau_share)))
-} \ No newline at end of file
+ val chooserIntent = Intent.createChooser(intent, string(R.string.kau_share))
+ if (chooserIntent.resolveActivity(packageManager) != null)
+ startActivity(chooserIntent)
+ else
+ toast("Cannot resolve activity to share text", log = true)
+}
+
+/**
+ * Check if given context is finishing.
+ * This is a wrapper to check if it's both an activity and finishing
+ * As of now, it is only checked when tied to an activity
+ */
+inline val Context.isFinishing: Boolean
+ get() = (this as? Activity)?.isFinishing ?: false \ No newline at end of file