Skip to content

Commit

Permalink
v3.4.1 (#63)
Browse files Browse the repository at this point in the history
* Check browser intent before launching (#54)

* Update changelog

* fix/misc (#55)

* Add kapt plugin

* Fix kau vector

* Debug lintRelease

* Revert debug

* Update dependencies

* Check context finishing state before showing dialog (#61)

* Keep copy of shared pref rather than application context (#60)

* Keep copy of shared pref rather than application context

* Add back preference name

* Add resolver checks (#62)
  • Loading branch information
AllanWang authored Sep 23, 2017
1 parent f3e7098 commit a647df3
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 36 deletions.
6 changes: 5 additions & 1 deletion core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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


/**
Expand Down Expand Up @@ -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)
}
}

Expand Down
17 changes: 5 additions & 12 deletions core/src/main/kotlin/ca/allanwang/kau/kpref/KPref.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
Expand All @@ -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() {
Expand Down
48 changes: 35 additions & 13 deletions core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand All @@ -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
Expand Down Expand Up @@ -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()
}

Expand Down Expand Up @@ -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)))
}
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
2 changes: 1 addition & 1 deletion core/src/main/res-public/values/strings_commons.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Most resources are verbatim and x represents a formatted item
<string name="kau_no">No</string>
<string name="kau_no_results_found">No Results Found</string>
<string name="kau_none">None</string>
<string name="kau_ok">@android:string/ok</string>
<string name="kau_ok">OK</string>
<string name="kau_play_store">Play Store</string>
<string name="kau_rate">Rate</string>
<string name="kau_report_bug">Report A Bug</string>
Expand Down
4 changes: 0 additions & 4 deletions core/src/main/res/values/strings.xml

This file was deleted.

1 change: 1 addition & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* :searchview: Ensure reveals are called on the UI thread
* :searchview: Check that searchview has a parent before checking card state
* :mediapicker: Reuse request manager from activity
* :kpref-activity: Add bounds to text item

## v3.3.2
* :kpref-activity: Add visibility toggle to Core contract. Items can override this to show/hide given preferences based on boolean callbacks.
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@ CORE_MIN_SDK=19
MIN_SDK=21
TARGET_SDK=26
BUILD_TOOLS=26.0.1
ANDROID_SUPPORT_LIBS=26.0.1
ANDROID_SUPPORT_LIBS=26.1.0

VERSION_NAME=3.4.0

KOTLIN=1.1.4-2
KOTLIN=1.1.4-3
ABOUT_LIBRARIES=5.9.7
ANKO=0.10.1
BLURRY=2.1.1
CONSTRAINT_LAYOUT=1.1.0-beta1
FAST_ADAPTER=2.6.3
FAST_ADAPTER_COMMONS=2.6.3
GLIDE=4.0.0
GLIDE=4.1.1
ICONICS=2.9.3
IICON_GOOGLE=3.0.1.1
MATERIAL_DIALOG=0.9.4.7
Expand Down
1 change: 1 addition & 0 deletions mediapicker/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ext.kauSubModuleMinSdk = project.CORE_MIN_SDK

apply from: '../android-lib.gradle'
apply plugin: 'kotlin-kapt'

dependencies {
implementation project(':core-ui')
Expand Down
4 changes: 2 additions & 2 deletions sample/src/main/res/drawable/kau.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
<path
android:fillColor="#00a9ff"
android:fillType="evenOdd"
android:pathData="M42.8336 42.7538l19-42.75 31.67656 57H57.0836zM0 .08894h27L0 28.6213z" />
android:pathData="M42.8336 42.7538l19-42.75 31.67656 57H57.0836zM0 0.08894h27L0 28.6213z" />
<path
android:fillColor="#ff8900"
android:fillType="evenOdd"
android:pathData="M28.5 .0038 H57l-57 57v-26.882z" />
android:pathData="M28.5 0.0038 H57l-57 57v-26.882z" />
<path
android:fillColor="#00a9ff"
android:fillType="evenOdd"
Expand Down

0 comments on commit a647df3

Please sign in to comment.