From 81518f74fa76b6c2274a0e02ad4169f5d7f61e59 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 8 Oct 2018 01:06:44 -0400 Subject: Update translations (#169) * New translations strings_about.xml (Polish) * New translations strings_commons.xml (Hebrew) * New translations strings_commons.xml (Tagalog) * New translations strings_commons.xml (Swedish) * New translations strings_commons.xml (Serbian (Cyrillic)) * New translations strings_commons.xml (Russian) * New translations strings_commons.xml (Romanian) * New translations strings_commons.xml (Portuguese, Brazilian) * New translations strings_commons.xml (Portuguese) * New translations strings_commons.xml (Norwegian) * New translations strings_commons.xml (Korean) * New translations strings_commons.xml (Japanese) * New translations strings_commons.xml (Italian) * New translations strings_commons.xml (Hungarian) * New translations strings_commons.xml (Greek) * New translations strings_commons.xml (German) * New translations strings_commons.xml (Galician) * New translations strings_commons.xml (French) * New translations strings_commons.xml (Finnish) * New translations strings_commons.xml (English) * New translations strings_commons.xml (Dutch) * New translations strings_commons.xml (Czech) * New translations strings_commons.xml (Chinese Traditional) * New translations strings_commons.xml (Chinese Simplified) * New translations strings_commons.xml (Catalan) * New translations strings_commons.xml (Arabic) * New translations strings_commons.xml (Afrikaans) * New translations strings_colorpicker.xml (Ukrainian) * New translations strings_commons.xml (Ukrainian) * New translations strings_mediapicker.xml (Ukrainian) * New translations strings_mediapicker.xml (Tagalog) * New translations strings_mediapicker.xml (Swedish) * New translations strings_mediapicker.xml (Serbian (Cyrillic)) * New translations strings_mediapicker.xml (Russian) * New translations strings_mediapicker.xml (Romanian) * New translations strings_mediapicker.xml (Portuguese, Brazilian) * New translations strings_mediapicker.xml (Portuguese) * New translations strings_mediapicker.xml (Norwegian) * New translations strings_mediapicker.xml (Korean) * New translations strings_mediapicker.xml (Japanese) * New translations strings_mediapicker.xml (Italian) * New translations strings_mediapicker.xml (Hungarian) * New translations strings_commons.xml (Vietnamese) * New translations strings_mediapicker.xml (Hebrew) * New translations strings_mediapicker.xml (Greek) * New translations strings_mediapicker.xml (German) * New translations strings_mediapicker.xml (Galician) * New translations strings_mediapicker.xml (French) * New translations strings_mediapicker.xml (Finnish) * New translations strings_mediapicker.xml (English) * New translations strings_mediapicker.xml (Dutch) * New translations strings_mediapicker.xml (Czech) * New translations strings_mediapicker.xml (Chinese Traditional) * New translations strings_mediapicker.xml (Chinese Simplified) * New translations strings_mediapicker.xml (Catalan) * New translations strings_mediapicker.xml (Arabic) * New translations strings_mediapicker.xml (Afrikaans) * New translations strings_colorpicker.xml (Tagalog) * New translations strings_mediapicker.xml (Polish) * New translations strings_about.xml (English) * New translations strings_about.xml (Portuguese, Brazilian) * New translations strings_about.xml (Portuguese) * New translations strings_about.xml (Norwegian) * New translations strings_about.xml (Korean) * New translations strings_about.xml (Japanese) * New translations strings_about.xml (Italian) * New translations strings_about.xml (Hungarian) * New translations strings_about.xml (Hebrew) * New translations strings_about.xml (Greek) * New translations strings_about.xml (German) * New translations strings_about.xml (Galician) * New translations strings_about.xml (French) * New translations strings_about.xml (Finnish) * New translations strings_about.xml (Dutch) * New translations strings_about.xml (Russian) * New translations strings_about.xml (Czech) * New translations strings_about.xml (Chinese Simplified) * New translations strings_about.xml (Catalan) * New translations strings_about.xml (Arabic) * New translations strings_about.xml (Afrikaans) * New translations strings_mediapicker.xml (Danish) * New translations strings_commons.xml (Danish) * New translations strings_colorpicker.xml (Danish) * New translations strings_about.xml (Danish) * New translations strings_mediapicker.xml (Spanish) * New translations strings_commons.xml (Spanish) * New translations strings_about.xml (Spanish) * New translations strings_about.xml (Chinese Traditional) * New translations strings_about.xml (Romanian) * New translations strings_about.xml (Serbian (Cyrillic)) * New translations strings_colorpicker.xml (Swedish) * New translations strings_colorpicker.xml (Serbian (Cyrillic)) * New translations strings_colorpicker.xml (Russian) * New translations strings_colorpicker.xml (Romanian) * New translations strings_colorpicker.xml (Portuguese) * New translations strings_colorpicker.xml (Japanese) * New translations strings_colorpicker.xml (Hebrew) * New translations strings_colorpicker.xml (Greek) * New translations strings_about.xml (Swedish) * New translations strings_colorpicker.xml (Arabic) * New translations strings_about.xml (Tagalog) * New translations strings_about.xml (Ukrainian) * New translations strings_about.xml (Vietnamese) * New translations strings_colorpicker.xml (Afrikaans) * New translations strings_colorpicker.xml (Catalan) * New translations strings_colorpicker.xml (Chinese Traditional) * New translations strings_colorpicker.xml (Czech) * New translations strings_colorpicker.xml (Dutch) * New translations strings_colorpicker.xml (English) * New translations strings_colorpicker.xml (Finnish) * New translations strings_mediapicker.xml (Vietnamese) * New translations strings_commons.xml (Danish) * New translations strings_about.xml (Swedish) * New translations strings_about.xml (Ukrainian) * New translations strings_colorpicker.xml (Swedish) * New translations strings_colorpicker.xml (Ukrainian) * New translations strings_commons.xml (Swedish) * New translations strings_commons.xml (Ukrainian) * New translations strings_mediapicker.xml (Swedish) * New translations strings_mediapicker.xml (Ukrainian) * Delete empty files * Optimize all source files --- .../ca/allanwang/kau/kotlin/LazyResettable.kt | 6 +-- .../main/kotlin/ca/allanwang/kau/kotlin/Utils.kt | 2 +- .../kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt | 6 ++- .../ca/allanwang/kau/kpref/KPrefSingleDelegate.kt | 3 +- .../allanwang/kau/permissions/PermissionManager.kt | 9 ++-- .../ca/allanwang/kau/permissions/Permissions.kt | 6 +-- .../ca/allanwang/kau/swipe/SwipeBackHelper.kt | 3 +- .../kotlin/ca/allanwang/kau/swipe/SwipeBackPage.kt | 3 +- .../kotlin/ca/allanwang/kau/utils/ColorUtils.kt | 12 ++--- .../kotlin/ca/allanwang/kau/utils/FileUtils.kt | 3 +- .../kotlin/ca/allanwang/kau/utils/FontUtils.kt | 3 +- .../ca/allanwang/kau/utils/NotificationUtils.kt | 3 +- .../kotlin/ca/allanwang/kau/utils/ViewUtils.kt | 51 ++++++++++++++-------- core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt | 3 +- 14 files changed, 65 insertions(+), 48 deletions(-) (limited to 'core/src/main/kotlin') diff --git a/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt b/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt index ceeaa30..979f7a7 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kotlin/LazyResettable.kt @@ -14,7 +14,8 @@ internal object UNINITIALIZED fun lazyResettable(initializer: () -> T): LazyResettable = LazyResettable(initializer) class LazyResettable(private val initializer: () -> T, lock: Any? = null) : ILazyResettable, Serializable { - @Volatile private var _value: Any = UNINITIALIZED + @Volatile + private var _value: Any = UNINITIALIZED private val lock = lock ?: this override fun invalidate() { @@ -69,8 +70,7 @@ class LazyResettableRegistry : ILazyResettableRegistry { var lazyRegistry: MutableList> = mutableListOf() - override fun lazy(initializer: () -> T): LazyResettable - = add(lazyResettable(initializer)) + override fun lazy(initializer: () -> T): LazyResettable = add(lazyResettable(initializer)) override fun add(resettable: LazyResettable): LazyResettable { lazyRegistry.add(resettable) diff --git a/core/src/main/kotlin/ca/allanwang/kau/kotlin/Utils.kt b/core/src/main/kotlin/ca/allanwang/kau/kotlin/Utils.kt index db28914..1ba1de6 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kotlin/Utils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kotlin/Utils.kt @@ -3,4 +3,4 @@ package ca.allanwang.kau.kotlin /** * Created by Allan Wang on 07/04/18. */ -inline fun javaClass(): Class = T::class.java \ No newline at end of file +inline fun javaClass(): Class = T::class.java \ No newline at end of file diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt index 33ba807..60d03ad 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt @@ -26,7 +26,8 @@ class KPrefDelegate internal constructor( private object UNINITIALIZED - @Volatile private var _value: Any = UNINITIALIZED + @Volatile + private var _value: Any = UNINITIALIZED private val lock = lock ?: this init { @@ -89,5 +90,6 @@ class KPrefDelegate internal constructor( } class KPrefException(message: String) : IllegalAccessException(message) { - constructor(element: Any?) : this("Invalid type in pref cache: ${element?.javaClass?.simpleName ?: "null"}") + constructor(element: Any?) : this("Invalid type in pref cache: ${element?.javaClass?.simpleName + ?: "null"}") } \ No newline at end of file diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefSingleDelegate.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefSingleDelegate.kt index 6525305..ed30a44 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefSingleDelegate.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefSingleDelegate.kt @@ -14,7 +14,8 @@ fun KPref.kprefSingle(key: String) = KPrefSingleDelegate(key, this) */ class KPrefSingleDelegate internal constructor(private val key: String, private val pref: KPref, lock: Any? = null) : ILazyResettable { - @Volatile private var _value: Boolean? = null + @Volatile + private var _value: Boolean? = null private val lock = lock ?: this init { diff --git a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt index 3b15e0b..36456ec 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt @@ -29,7 +29,8 @@ internal object PermissionManager { */ private val manifestPermission = lazyContext> { try { - it.packageManager.getPackageInfo(it.packageName, PackageManager.GET_PERMISSIONS)?.requestedPermissions ?: emptyArray() + it.packageManager.getPackageInfo(it.packageName, PackageManager.GET_PERMISSIONS)?.requestedPermissions + ?: emptyArray() } catch (e: Exception) { emptyArray() } @@ -47,7 +48,8 @@ internal object PermissionManager { } else KL.d { "Request is postponed since another one is still in progress; did you remember to override onRequestPermissionsResult?" } } - @Synchronized private fun requestPermissions(context: Context, permissions: Array) { + @Synchronized + private fun requestPermissions(context: Context, permissions: Array) { permissions.forEach { if (!manifestPermission(context).contains(it)) { KL.e { "Requested permission $it is not stated in the manifest" } @@ -55,7 +57,8 @@ internal object PermissionManager { //we'll let the request pass through so it can be denied and so the callback can be triggered } } - val activity = (context as? Activity) ?: throw KauException("Context is not an instance of an activity; cannot request permissions") + val activity = (context as? Activity) + ?: throw KauException("Context is not an instance of an activity; cannot request permissions") KL.i { "Requesting permissions ${permissions.contentToString()}" } ActivityCompat.requestPermissions(activity, permissions, 1) } diff --git a/core/src/main/kotlin/ca/allanwang/kau/permissions/Permissions.kt b/core/src/main/kotlin/ca/allanwang/kau/permissions/Permissions.kt index 4de6695..248e484 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/permissions/Permissions.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/permissions/Permissions.kt @@ -22,8 +22,7 @@ import android.support.annotation.RequiresApi /** * Hook that should be added inside all [Activity.onRequestPermissionsResult] so that the Permission manager can handle the responses */ -fun Activity.kauOnRequestPermissionsResult(permissions: Array, grantResults: IntArray) - = PermissionManager.onRequestPermissionsResult(this, permissions, grantResults) +fun Activity.kauOnRequestPermissionsResult(permissions: Array, grantResults: IntArray) = PermissionManager.onRequestPermissionsResult(this, permissions, grantResults) /** * Request a permission with a callback @@ -32,8 +31,7 @@ fun Activity.kauOnRequestPermissionsResult(permissions: Array, grant * The [callback] returns [granted], which is true if all permissions are granted * [deniedPerm] is the first denied permission, if granted is false */ -fun Context.kauRequestPermissions(vararg permissions: String, callback: (granted: Boolean, deniedPerm: String?) -> Unit) - = PermissionManager(this, permissions, callback) +fun Context.kauRequestPermissions(vararg permissions: String, callback: (granted: Boolean, deniedPerm: String?) -> Unit) = PermissionManager(this, permissions, callback) /** * See http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous for a diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt index 41cd2e0..018d7c7 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt @@ -16,8 +16,7 @@ internal object SwipeBackHelper { private val pageStack = Stack() - private operator fun get(activity: Activity): SwipeBackPage? - = pageStack.firstOrNull { it.activityRef.get() === activity } + private operator fun get(activity: Activity): SwipeBackPage? = pageStack.firstOrNull { it.activityRef.get() === activity } fun onCreate(activity: Activity, builder: SwipeBackContract.() -> Unit = {}) { val page = this[activity] ?: pageStack.push(SwipeBackPage(activity).apply { builder() }) diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackPage.kt b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackPage.kt index 7b6654d..4dba622 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackPage.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackPage.kt @@ -40,7 +40,8 @@ internal class SwipeBackPage(activity: Activity) : SwipeBackContractInternal by } private fun handleLayout() { - val activity = activityRef.get() ?: return KL.v { "KauSwipe activity ref gone during handleLayout" } + val activity = activityRef.get() + ?: return KL.v { "KauSwipe activity ref gone during handleLayout" } if (swipeEnabled) swipeBackLayout.attachToActivity(activity) else swipeBackLayout.removeFromActivity(activity) } diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt index d0e1f8f..236f2ca 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt @@ -94,12 +94,10 @@ fun Int.blendWith(@ColorInt color: Int, @FloatRange(from = 0.0, to = 1.0) ratio: } @ColorInt -fun Int.withAlpha(@IntRange(from = 0L, to = 255L) alpha: Int): Int - = Color.argb(alpha, Color.red(this), Color.green(this), Color.blue(this)) +fun Int.withAlpha(@IntRange(from = 0L, to = 255L) alpha: Int): Int = Color.argb(alpha, Color.red(this), Color.green(this), Color.blue(this)) @ColorInt -fun Int.withMinAlpha(@IntRange(from = 0L, to = 255L) alpha: Int): Int - = Color.argb(Math.max(alpha, Color.alpha(this)), Color.red(this), Color.green(this), Color.blue(this)) +fun Int.withMinAlpha(@IntRange(from = 0L, to = 255L) alpha: Int): Int = Color.argb(Math.max(alpha, Color.alpha(this)), Color.red(this), Color.green(this), Color.blue(this)) @ColorInt fun Int.lighten(@FloatRange(from = 0.0, to = 1.0) factor: Float = 0.1f): Int { @@ -116,12 +114,10 @@ fun Int.darken(@FloatRange(from = 0.0, to = 1.0) factor: Float = 0.1f): Int { } @ColorInt -fun Int.colorToBackground(@FloatRange(from = 0.0, to = 1.0) factor: Float = 0.1f): Int - = if (isColorDark) darken(factor) else lighten(factor) +fun Int.colorToBackground(@FloatRange(from = 0.0, to = 1.0) factor: Float = 0.1f): Int = if (isColorDark) darken(factor) else lighten(factor) @ColorInt -fun Int.colorToForeground(@FloatRange(from = 0.0, to = 1.0) factor: Float = 0.1f): Int - = if (isColorDark) lighten(factor) else darken(factor) +fun Int.colorToForeground(@FloatRange(from = 0.0, to = 1.0) factor: Float = 0.1f): Int = if (isColorDark) lighten(factor) else darken(factor) @Throws(IllegalArgumentException::class) fun String.toColor(): Int { diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/FileUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/FileUtils.kt index b97f4aa..bfbc009 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/FileUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/FileUtils.kt @@ -6,5 +6,4 @@ import java.io.InputStream /** * Created by Allan Wang on 2017-08-04. */ -fun File.copyFromInputStream(inputStream: InputStream) - = inputStream.use { input -> outputStream().use { output -> input.copyTo(output) } } \ No newline at end of file +fun File.copyFromInputStream(inputStream: InputStream) = inputStream.use { input -> outputStream().use { output -> input.copyTo(output) } } \ No newline at end of file diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/FontUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/FontUtils.kt index 05073c7..1db7694 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/FontUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/FontUtils.kt @@ -17,7 +17,8 @@ object FontUtils { context.applicationContext.assets, "fonts/$font.ttf") sTypefaceCache.put(font, tf) } - return sTypefaceCache.get(font) ?: throw IllegalArgumentException("Font error; typeface does not exist at assets/fonts$font.ttf") + return sTypefaceCache.get(font) + ?: throw IllegalArgumentException("Font error; typeface does not exist at assets/fonts$font.ttf") } } diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/NotificationUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/NotificationUtils.kt index 23a8370..1eb0076 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/NotificationUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/NotificationUtils.kt @@ -7,5 +7,4 @@ import android.support.v4.app.NotificationManagerCompat /** * Created by Allan Wang on 2017-08-04. */ -fun Context.cancelNotification(notifId: Int) - = NotificationManagerCompat.from(this).cancel(notifId) \ No newline at end of file +fun Context.cancelNotification(notifId: Int) = NotificationManagerCompat.from(this).cancel(notifId) \ No newline at end of file diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt index 9a1e9b0..4e020bc 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt @@ -23,7 +23,6 @@ import android.view.ViewGroup import android.view.inputmethod.InputMethodManager import android.widget.EditText import android.widget.ImageView -import android.widget.TextView import ca.allanwang.kau.ui.createSimpleRippleDrawable import com.mikepenz.iconics.IconicsDrawable import com.mikepenz.iconics.typeface.IIcon @@ -33,34 +32,47 @@ import com.mikepenz.iconics.typeface.IIcon * Created by Allan Wang on 2017-05-31. */ -@KauUtils inline fun T.visible(): T { +@KauUtils +inline fun T.visible(): T { visibility = View.VISIBLE return this } -@KauUtils inline fun T.invisible(): T { +@KauUtils +inline fun T.invisible(): T { visibility = View.INVISIBLE return this } -@KauUtils inline fun T.gone(): T { +@KauUtils +inline fun T.gone(): T { visibility = View.GONE return this } -@KauUtils inline fun T.invisibleIf(invisible: Boolean): T = if (invisible) invisible() else visible() +@KauUtils +inline fun T.invisibleIf(invisible: Boolean): T = if (invisible) invisible() else visible() -@KauUtils inline fun T.visibleIf(visible: Boolean): T = if (visible) visible() else gone() +@KauUtils +inline fun T.visibleIf(visible: Boolean): T = if (visible) visible() else gone() -@KauUtils inline fun T.goneIf(gone: Boolean): T = visibleIf(!gone) +@KauUtils +inline fun T.goneIf(gone: Boolean): T = visibleIf(!gone) -@KauUtils inline val View.isVisible: Boolean get() = visibility == View.VISIBLE +@KauUtils +inline val View.isVisible: Boolean + get() = visibility == View.VISIBLE -@KauUtils inline val View.isInvisible: Boolean get() = visibility == View.INVISIBLE +@KauUtils +inline val View.isInvisible: Boolean + get() = visibility == View.INVISIBLE -@KauUtils inline val View.isGone: Boolean get() = visibility == View.GONE +@KauUtils +inline val View.isGone: Boolean + get() = visibility == View.GONE -@KauUtils inline fun View.setBackgroundColorRes(@ColorRes color: Int) = setBackgroundColor(context.color(color)) +@KauUtils +inline fun View.setBackgroundColorRes(@ColorRes color: Int) = setBackgroundColor(context.color(color)) fun View.snackbar(text: String, duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {}): Snackbar { val snackbar = Snackbar.make(this, text, duration) @@ -69,8 +81,7 @@ fun View.snackbar(text: String, duration: Int = Snackbar.LENGTH_LONG, builder: S return snackbar } -fun View.snackbar(@StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {}) - = snackbar(context.string(textId), duration, builder) +fun View.snackbar(@StringRes textId: Int, duration: Int = Snackbar.LENGTH_LONG, builder: Snackbar.() -> Unit = {}) = snackbar(context.string(textId), duration, builder) @KauUtils fun ImageView.setIcon(icon: IIcon?, sizeDp: Int = 24, @ColorInt color: Int = Color.WHITE, builder: IconicsDrawable.() -> Unit = {}) { @@ -78,7 +89,9 @@ fun ImageView.setIcon(icon: IIcon?, sizeDp: Int = 24, @ColorInt color: Int = Col setImageDrawable(icon.toDrawable(context, sizeDp = sizeDp, color = color, builder = builder)) } -@KauUtils inline val FloatingActionButton.isHidden get() = !isShown +@KauUtils +inline val FloatingActionButton.isHidden + get() = !isShown fun FloatingActionButton.showIf(show: Boolean) = if (show) show() else hide() @@ -133,7 +146,8 @@ fun View.setMargin(margin: Int) = setMargins(margin, KAU_ALL) * Base margin setter * returns true if setting is successful, false otherwise */ -@KauUtils private fun View.setMargins(margin: Int, flag: Int): Boolean { +@KauUtils +private fun View.setMargins(margin: Int, flag: Int): Boolean { val p = (layoutParams as? ViewGroup.MarginLayoutParams) ?: return false p.setMargins( if (flag and KAU_LEFT > 0) margin else p.leftMargin, @@ -189,7 +203,8 @@ fun View.setPadding(padding: Int) = setPadding(padding, KAU_ALL) /** * Base padding setter */ -@KauUtils private fun View.setPadding(padding: Int, flag: Int) { +@KauUtils +private fun View.setPadding(padding: Int, flag: Int) { setPadding( if (flag and KAU_LEFT > 0) padding else paddingLeft, if (flag and KAU_TOP > 0) padding else paddingTop, @@ -217,7 +232,9 @@ fun View.setRippleBackground(@ColorInt foregroundColor: Int, @ColorInt backgroun background = createSimpleRippleDrawable(foregroundColor, backgroundColor) } -@KauUtils inline val View.parentViewGroup: ViewGroup get() = parent as ViewGroup +@KauUtils +inline val View.parentViewGroup: ViewGroup + get() = parent as ViewGroup inline val EditText.value: String get() = text.toString().trim() diff --git a/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt b/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt index f804b70..7caf5d6 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt @@ -47,7 +47,8 @@ fun Context.kauParseFaq( } 1 -> { items.add(FaqItem(items.size + 1, - question ?: throw IllegalArgumentException("KAU FAQ answer found without a question"), + question + ?: throw IllegalArgumentException("KAU FAQ answer found without a question"), Html.fromHtml(parser.text.replace("\n", if (parseNewLine) "
" else "")))) question = null flag = -1 -- cgit v1.2.3 From 19020070cc073c81bfe397454f2314d37a78ed31 Mon Sep 17 00:00:00 2001 From: Allan Wang Date: Mon, 8 Oct 2018 01:46:36 -0400 Subject: Enhancement/kpref (#170) * Deprecate kpref double * Update kpref tests and migration * Fix kpref file * Update changelog * Replace changelog angle brackets --- .../kotlin/ca/allanwang/kau/kpref/KPrefTest.kt | 26 +++++--- .../kotlin/ca/allanwang/kau/email/EmailBuilder.kt | 7 ++- .../kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt | 69 +++++++++++----------- .../ca/allanwang/kau/kpref/KPrefTransaction.kt | 64 ++++++++++++++++++++ docs/Migration.md | 15 ++++- sample/src/main/res/xml/kau_changelog.xml | 4 +- 6 files changed, 137 insertions(+), 48 deletions(-) create mode 100644 core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefTransaction.kt (limited to 'core/src/main/kotlin') diff --git a/core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt b/core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt index e806a3f..2a9263a 100644 --- a/core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt +++ b/core/src/androidTest/kotlin/ca/allanwang/kau/kpref/KPrefTest.kt @@ -16,40 +16,46 @@ import kotlin.test.assertTrue */ @RunWith(AndroidJUnit4::class) @MediumTest -class ChangelogTest { +class KPrefTest { lateinit var pref: TestPref class TestPref : KPref() { + init { initialize(InstrumentationRegistry.getTargetContext(), "kpref_test_${System.currentTimeMillis()}") } - var one: Int by kpref("one", 1) + var one by kpref("one", 1) + + var two by kpref("two", 2f) - var `true`: Boolean by kpref("true", true) + var `true` by kpref("true", true) - var hello: String by kpref("hello", "hello") + var hello by kpref("hello", "hello") - var set: StringSet by kpref("set", setOf("po", "ta", "to")) + var set by kpref("set", setOf("po", "ta", "to")) - val oneShot: Boolean by kprefSingle("asdf") + val oneShot by kprefSingle("asdf") } @Before fun init() { pref = TestPref() + pref.sp.edit().clear().commit() } @Test fun getDefaults() { assertEquals(1, pref.one) + assertEquals(2f, pref.two) assertEquals(true, pref.`true`) assertEquals("hello", pref.hello) assertEquals(3, pref.set.size) assertTrue(pref.set.contains("po")) assertTrue(pref.set.contains("ta")) assertTrue(pref.set.contains("to")) + assertEquals(0, pref.sp.all.size, "Defaults should not be set automatically") } @Test @@ -60,6 +66,7 @@ class ChangelogTest { pref.hello = "goodbye" assertEquals("goodbye", pref.hello) assertEquals(pref.hello, pref.sp.getString("hello", "hello")) + assertEquals(2, pref.sp.all.size) } @SuppressLint("CommitPrefEdits") @@ -67,12 +74,13 @@ class ChangelogTest { fun reset() { pref.one = 2 assertEquals(2, pref.one) + assertEquals(6, pref.prefMap.size, "Prefmap does not have all elements") pref.reset() //only invalidates our lazy delegate; doesn't change the actual pref - assertEquals(2, pref.one) + assertEquals(2, pref.one, "Kpref did not properly fetch from shared prefs") pref.sp.edit().putInt("one", -1).commit() - assertEquals(2, pref.one) //our lazy delegate still retains the old value + assertEquals(2, pref.one, "Lazy kpref should still retain old value") pref.reset() - assertEquals(-1, pref.one) //back in sync with sp + assertEquals(-1, pref.one, "Kpref did not refetch from shared prefs upon reset") } 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 804eacb..dbdcf09 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt @@ -69,9 +69,14 @@ class EmailBuilder(val email: String, val subject: String) { if (appInfo) { try { val appInfo = context.packageManager.getPackageInfo(context.packageName, 0) + val versionCode = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { + appInfo.longVersionCode.toString() + } else { + appInfo.versionCode.toString() + } emailBuilder.append("\nApp: ").append(context.packageName) .append("\nApp Version Name: ").append(appInfo.versionName) - .append("\nApp Version Code: ").append(appInfo.versionCode).append("\n") + .append("\nApp Version Code: ").append(versionCode).append("\n") } catch (e: PackageManager.NameNotFoundException) { KL.e { "EmailBuilder packageInfo not found" } } diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt index 60d03ad..b80479e 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefDelegate.kt @@ -3,15 +3,29 @@ package ca.allanwang.kau.kpref import ca.allanwang.kau.kotlin.ILazyResettable -fun KPref.kpref(key: String, fallback: Boolean, postSetter: (value: Boolean) -> Unit = {}) = KPrefDelegate(key, fallback, this, postSetter) -fun KPref.kpref(key: String, fallback: Double, postSetter: (value: Float) -> Unit = {}) = KPrefDelegate(key, fallback.toFloat(), this, postSetter) -fun KPref.kpref(key: String, fallback: Float, postSetter: (value: Float) -> Unit = {}) = KPrefDelegate(key, fallback, this, postSetter) -fun KPref.kpref(key: String, fallback: Int, postSetter: (value: Int) -> Unit = {}) = KPrefDelegate(key, fallback, this, postSetter) -fun KPref.kpref(key: String, fallback: Long, postSetter: (value: Long) -> Unit = {}) = KPrefDelegate(key, fallback, this, postSetter) -fun KPref.kpref(key: String, fallback: Set, postSetter: (value: Set) -> Unit = {}) = KPrefDelegate(key, StringSet(fallback), this, postSetter) -fun KPref.kpref(key: String, fallback: String, postSetter: (value: String) -> Unit = {}) = KPrefDelegate(key, fallback, this, postSetter) +fun KPref.kpref(key: String, fallback: Boolean, postSetter: (value: Boolean) -> Unit = {}) = + KPrefDelegate(key, fallback, this, KPrefBooleanTransaction, postSetter) -class StringSet(set: Collection) : LinkedHashSet(set) +fun KPref.kpref(key: String, fallback: Float, postSetter: (value: Float) -> Unit = {}) = + KPrefDelegate(key, fallback, this, KPrefFloatTransaction, postSetter) + +@Deprecated("Double is not supported in SharedPreferences; cast to float yourself", + ReplaceWith("kpref(key, fallback.toFloat(), postSetter)"), + DeprecationLevel.WARNING) +fun KPref.kpref(key: String, fallback: Double, postSetter: (value: Float) -> Unit = {}) = + kpref(key, fallback.toFloat(), postSetter) + +fun KPref.kpref(key: String, fallback: Int, postSetter: (value: Int) -> Unit = {}) = + KPrefDelegate(key, fallback, this, KPrefIntTransaction, postSetter) + +fun KPref.kpref(key: String, fallback: Long, postSetter: (value: Long) -> Unit = {}) = + KPrefDelegate(key, fallback, this, KPrefLongTransaction, postSetter) + +fun KPref.kpref(key: String, fallback: Set, postSetter: (value: Set) -> Unit = {}) = + KPrefDelegate(key, fallback, this, KPrefSetTransaction, postSetter) + +fun KPref.kpref(key: String, fallback: String, postSetter: (value: String) -> Unit = {}) = + KPrefDelegate(key, fallback, this, KPrefStringTransaction, postSetter) /** * Created by Allan Wang on 2017-06-07. @@ -20,20 +34,24 @@ class StringSet(set: Collection) : LinkedHashSet(set) * Contains a unique key for the shared preference as well as a nonnull fallback item * Also contains an optional mutable postSetter that will be called every time a new value is given */ -class KPrefDelegate internal constructor( - private val key: String, private val fallback: T, private val pref: KPref, private var postSetter: (value: T) -> Unit = {}, lock: Any? = null +class KPrefDelegate internal constructor( + private val key: String, + private val fallback: T, + private val pref: KPref, + private val transaction: KPrefTransaction, + private var postSetter: (value: T) -> Unit = {} ) : ILazyResettable { private object UNINITIALIZED @Volatile - private var _value: Any = UNINITIALIZED - private val lock = lock ?: this + private var _value: Any? = UNINITIALIZED + private val lock = this init { if (pref.prefMap.containsKey(key)) throw KPrefException("$key is already used elsewhere in preference ${pref.PREFERENCE_NAME}") - pref.prefMap.put(key, this@KPrefDelegate) + pref.prefMap[key] = this@KPrefDelegate } override fun invalidate() { @@ -53,15 +71,7 @@ class KPrefDelegate internal constructor( @Suppress("UNCHECKED_CAST") _v2 as T } else { - _value = when (fallback) { - is Boolean -> pref.sp.getBoolean(key, fallback) - is Float -> pref.sp.getFloat(key, fallback) - is Int -> pref.sp.getInt(key, fallback) - is Long -> pref.sp.getLong(key, fallback) - is StringSet -> StringSet(pref.sp.getStringSet(key, fallback)) - is String -> pref.sp.getString(key, fallback) - else -> throw KPrefException(fallback) - } + _value = transaction.get(pref.sp, key, fallback) @Suppress("UNCHECKED_CAST") _value as T } @@ -75,21 +85,10 @@ class KPrefDelegate internal constructor( operator fun setValue(any: Any, property: kotlin.reflect.KProperty<*>, t: T) { _value = t val editor = pref.sp.edit() - when (t) { - is Boolean -> editor.putBoolean(key, t) - is Float -> editor.putFloat(key, t) - is Int -> editor.putInt(key, t) - is Long -> editor.putLong(key, t) - is StringSet -> editor.putStringSet(key, t) - is String -> editor.putString(key, t) - else -> throw KPrefException(t) - } + transaction.set(editor, key, t) editor.apply() postSetter(t) } } -class KPrefException(message: String) : IllegalAccessException(message) { - constructor(element: Any?) : this("Invalid type in pref cache: ${element?.javaClass?.simpleName - ?: "null"}") -} \ No newline at end of file +class KPrefException(message: String) : IllegalAccessException(message) \ No newline at end of file diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefTransaction.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefTransaction.kt new file mode 100644 index 0000000..773d208 --- /dev/null +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefTransaction.kt @@ -0,0 +1,64 @@ +package ca.allanwang.kau.kpref + +import android.content.SharedPreferences + +internal interface KPrefTransaction { + fun get(prefs: SharedPreferences, key: String, fallback: T): T + fun set(editor: SharedPreferences.Editor, key: String, data: T) +} + +internal object KPrefBooleanTransaction : KPrefTransaction { + override fun get(prefs: SharedPreferences, key: String, fallback: Boolean) = + prefs.getBoolean(key, fallback) + + override fun set(editor: SharedPreferences.Editor, key: String, data: Boolean) { + editor.putBoolean(key, data) + } +} + +internal object KPrefIntTransaction : KPrefTransaction { + override fun get(prefs: SharedPreferences, key: String, fallback: Int) = + prefs.getInt(key, fallback) + + override fun set(editor: SharedPreferences.Editor, key: String, data: Int) { + editor.putInt(key, data) + } +} + +internal object KPrefLongTransaction : KPrefTransaction { + override fun get(prefs: SharedPreferences, key: String, fallback: Long) = + prefs.getLong(key, fallback) + + override fun set(editor: SharedPreferences.Editor, key: String, data: Long) { + editor.putLong(key, data) + } +} + +internal object KPrefFloatTransaction : KPrefTransaction { + override fun get(prefs: SharedPreferences, key: String, fallback: Float) = + prefs.getFloat(key, fallback) + + override fun set(editor: SharedPreferences.Editor, key: String, data: Float) { + editor.putFloat(key, data) + } +} + +internal object KPrefStringTransaction : KPrefTransaction { + override fun get(prefs: SharedPreferences, key: String, fallback: String) = + prefs.getString(key, fallback) + + override fun set(editor: SharedPreferences.Editor, key: String, data: String) { + editor.putString(key, data) + } +} + +internal object KPrefSetTransaction : KPrefTransaction> { + override fun get(prefs: SharedPreferences, key: String, fallback: Set) = + prefs.getStringSet(key, fallback)!! + + override fun set(editor: SharedPreferences.Editor, key: String, data: Set) { + editor.putStringSet(key, data) + } +} + + diff --git a/docs/Migration.md b/docs/Migration.md index 721cd0d..ff89f87 100644 --- a/docs/Migration.md +++ b/docs/Migration.md @@ -6,9 +6,22 @@ Below are some highlights on major refactoring/breaking changes Along with the update to support Android Studio 3.1, a lot of changes have occurred with other dependencies and with lint. -* Resource ids can be negatives due to the bit overflow. Instead, `INVALID_ID` has been introduced to signify an unset or invalid id. +## Invalid Resource Id Equality + +Resource ids can be negatives due to the bit overflow. +Instead, `INVALID_ID` has been introduced to signify an unset or invalid id. Methods such as `Context.string(id, fallback)` now check against `INVALID_ID` through equality rather than using an inequality to address this. +## Deprecate Kotterknife + +Kotterknife has been deprecated in favour of `kotlin-android-extensions`. +See [official docs](https://kotlinlang.org/docs/tutorials/android-plugin.html#view-binding). + +## Update KPref + +KPref has been slightly refactored internally. +Preferences backed by `StringSet` can now go back to `Set` + # v3.6.0 ## startActivity diff --git a/sample/src/main/res/xml/kau_changelog.xml b/sample/src/main/res/xml/kau_changelog.xml index a5d0ea9..f6ec799 100644 --- a/sample/src/main/res/xml/kau_changelog.xml +++ b/sample/src/main/res/xml/kau_changelog.xml @@ -10,9 +10,9 @@ - + - + -- cgit v1.2.3