diff options
author | Allan Wang <me@allanwang.ca> | 2019-09-22 21:47:12 -0700 |
---|---|---|
committer | Allan Wang <me@allanwang.ca> | 2019-09-22 21:47:12 -0700 |
commit | 53ee262302fadfb3c42e951145511ba2c588e227 (patch) | |
tree | 9ca12a913a42895b6cb5961003d2c88e28399f37 /core/src/main/kotlin | |
parent | 00ba1bcf7a756252baa808e62a0916fd18e95946 (diff) | |
parent | bfba246d84aa6a22bc02837f9fcbd97780867d10 (diff) | |
download | kau-53ee262302fadfb3c42e951145511ba2c588e227.tar.gz kau-53ee262302fadfb3c42e951145511ba2c588e227.tar.bz2 kau-53ee262302fadfb3c42e951145511ba2c588e227.zip |
Merge dev
Diffstat (limited to 'core/src/main/kotlin')
23 files changed, 175 insertions, 90 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 78661b1..5ae8376 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/email/EmailBuilder.kt @@ -104,8 +104,9 @@ class EmailBuilder(val email: String, val subject: String) { if (packages.isNotEmpty()) { emailBuilder.append("\n") packages.forEach { - if (context.isAppInstalled(it.packageName)) + if (context.isAppInstalled(it.packageName)) { emailBuilder.append(String.format("\n%s is installed", it.appName)) + } } } diff --git a/core/src/main/kotlin/ca/allanwang/kau/kotlin/Streams.kt b/core/src/main/kotlin/ca/allanwang/kau/kotlin/Streams.kt index 65d15a7..582b0cd 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kotlin/Streams.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kotlin/Streams.kt @@ -26,8 +26,11 @@ package ca.allanwang.kau.kotlin */ inline fun <T, C : MutableIterable<T>> C.kauRemoveIf(filter: (item: T) -> Boolean): C { val iter = iterator() - while (iter.hasNext()) - if (filter(iter.next())) iter.remove() + while (iter.hasNext()) { + if (filter(iter.next())) { + iter.remove() + } + } return this } @@ -38,7 +41,9 @@ inline fun <T, C : MutableIterable<T>> C.kauRemoveIf(filter: (item: T) -> Boolea inline fun <T : Any> Iterator<T>.firstOrNull(predicate: (T) -> Boolean): T? { while (hasNext()) { val data = next() - if (predicate(data)) return data + if (predicate(data)) { + return data + } } return null } diff --git a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBuilder.kt b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBuilder.kt index 4dd3012..8f6d0c5 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBuilder.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefBuilder.kt @@ -1,3 +1,18 @@ +/* + * Copyright 2019 Allan Wang + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package ca.allanwang.kau.kpref interface KPrefBuilder { @@ -72,4 +87,4 @@ object KPrefBuilderInMemory : KPrefBuilder { KPrefDelegateInMemory(key, fallback, this, postSetter) override fun KPref.kprefSingle(key: String) = KPrefSingleDelegateInMemory(key, this) -}
\ 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 ef59e78..ae1f855 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefSingleDelegate.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/kpref/KPrefSingleDelegate.kt @@ -110,4 +110,4 @@ class KPrefSingleDelegateInMemory internal constructor( override fun isInitialized(): Boolean = _value != null override fun toString(): String = if (isInitialized()) value.toString() else "Lazy kPref $key not initialized yet." -}
\ No newline at end of file +} diff --git a/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt b/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt index 4562b00..9410c4c 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/logging/KauLogger.kt @@ -64,16 +64,18 @@ open class KauLogger( } inline fun log(priority: Int, message: () -> Any?, t: Throwable? = null) { - if (shouldLog(priority)) + if (shouldLog(priority)) { logImpl(priority, message()?.toString(), t) + } } open fun logImpl(priority: Int, message: String?, t: Throwable?) { val msg = message ?: "null" - if (t != null) + if (t != null) { Log.e(tag, msg, t) - else + } else { Log.println(priority, tag, msg) + } } /** @@ -113,8 +115,11 @@ class KauLoggerExtension(val tag: String, val logger: KauLogger) { inline fun log(priority: Int, message: () -> Any?, t: Throwable? = null) = logger.log(priority, { val msg = message()?.toString() - if (msg == null) null - else "$tag: $msg" + if (msg == null) { + null + } else { + "$tag: $msg" + } }, t) inline fun checkThread(id: Int) { 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 96adfa3..b549523 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/permissions/PermissionManager.kt @@ -82,7 +82,7 @@ internal object PermissionManager { if (!manifestPermission(context).contains(it)) { KL.e { "Requested permission $it is not stated in the manifest" } context.toast("$it is not in the manifest") - //we'll let the request pass through so it can be denied and so the callback can be triggered + // we'll let the request pass through so it can be denied and so the callback can be triggered } } val activity = (context as? Activity) 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 3c90b05..ee4f5ef 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/permissions/Permissions.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/permissions/Permissions.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018 Allan Wang + * Copyright 2019 Allan Wang * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -76,6 +76,8 @@ const val PERMISSION_READ_CALL_LOG = Manifest.permission.READ_CALL_LOG const val PERMISSION_WRITE_CALL_LOG = Manifest.permission.WRITE_CALL_LOG const val PERMISSION_ADD_VOICEMAIL = Manifest.permission.ADD_VOICEMAIL const val PERMISSION_USE_SIP = Manifest.permission.USE_SIP +@Deprecated(level = DeprecationLevel.WARNING, message = "Permission is deprecated") +@Suppress("DEPRECATION") const val PERMISSION_PROCESS_OUTGOING_CALLS = Manifest.permission.PROCESS_OUTGOING_CALLS @RequiresApi(Build.VERSION_CODES.KITKAT_WATCH) diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/RelativeSlider.kt b/core/src/main/kotlin/ca/allanwang/kau/swipe/RelativeSlider.kt index 0b1dd88..9f23b63 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/RelativeSlider.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/RelativeSlider.kt @@ -37,7 +37,7 @@ internal class RelativeSlider(var curPage: SwipeBackPage) : SwipeListener { * Set offset of previous page based on the edge flag and percentage scrolled */ override fun onScroll(percent: Float, px: Int, edgeFlag: Int) { - if (offset == 0f) return //relative slider is not enabled + if (offset == 0f) return // relative slider is not enabled val page = SwipeBackHelper.getPrePage(curPage) ?: return if (percent == 0f) { page.swipeBackLayout.x = 0f 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 90e6e6d..a8c71a4 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt @@ -62,7 +62,7 @@ internal object SwipeBackHelper { fun finish(activity: Activity) = this[activity]?.scrollToFinishActivity() internal fun getPrePage(page: SwipeBackPage): SwipeBackPage? { - //clean invalid pages + // clean invalid pages pageStack.kauRemoveIf { it.activityRef.get() == null } return pageStack.getOrNull(pageStack.indexOf(page) - 1) } diff --git a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt index c5b64e3..5f462b4 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt @@ -52,7 +52,9 @@ internal class SwipeBackLayout @JvmOverloads constructor( */ override var scrollThreshold = DEFAULT_SCROLL_THRESHOLD set(value) { - if (value >= 1.0f || value <= 0f) throw IllegalArgumentException("Threshold value should be between 0.0 and 1.0") + if (value >= 1.0f || value <= 0f) { + throw IllegalArgumentException("Threshold value should be between 0.0 and 1.0") + } field = value } @@ -130,8 +132,9 @@ internal class SwipeBackLayout @JvmOverloads constructor( * We will verify that only one axis is used at a time */ set(value) { - if (value !in arrayOf(SWIPE_EDGE_TOP, SWIPE_EDGE_BOTTOM, SWIPE_EDGE_LEFT, SWIPE_EDGE_RIGHT)) + if (value !in arrayOf(SWIPE_EDGE_TOP, SWIPE_EDGE_BOTTOM, SWIPE_EDGE_LEFT, SWIPE_EDGE_RIGHT)) { throw IllegalArgumentException("Edge flag is not valid; use one of the SWIPE_EDGE_* values") + } field = value horizontal = edgeFlag == SWIPE_EDGE_LEFT || edgeFlag == SWIPE_EDGE_RIGHT dragHelper.setEdgeTrackingEnabled(value) @@ -162,7 +165,9 @@ internal class SwipeBackLayout @JvmOverloads constructor( init { dragHelper = ViewDragHelper.create(this, ViewDragCallback()) - //allow touch from anywhere on the screen + val density = resources.displayMetrics.density + val minVel = MIN_FLING_VELOCITY * density + // allow touch from anywhere on the screen edgeSize = Math.max(resources.displayMetrics.widthPixels, resources.displayMetrics.heightPixels) edgeFlag = edgeFlag sensitivity = 0.3f @@ -193,8 +198,9 @@ internal class SwipeBackLayout @JvmOverloads constructor( val iter = listeners.iterator() while (iter.hasNext()) { val l = iter.next().get() - if (l == null || l == listener) + if (l == null || l == listener) { iter.remove() + } } } @@ -206,10 +212,11 @@ internal class SwipeBackLayout @JvmOverloads constructor( val iter = listeners.iterator() while (iter.hasNext()) { val l = iter.next().get() - if (l == null) + if (l == null) { iter.remove() - else if (l == listener) + } else if (l == listener) { return true + } } return false } @@ -240,6 +247,7 @@ internal class SwipeBackLayout @JvmOverloads constructor( dragHelper.shouldInterceptTouchEvent(event) } catch (e: Exception) { false + } } @@ -360,7 +368,7 @@ internal class SwipeBackLayout @JvmOverloads constructor( super.onViewPositionChanged(changedView, left, top, dx, dy) val contentView = contentViewRef.get() ?: return KL.e { "KauSwipe cannot change view position as contentView is null; is onPostCreate called?" } - //make sure that we are using the proper axis + // make sure that we are using the proper axis scrollPercent = Math.abs( if (horizontal) left.toFloat() / contentView.width else (top.toFloat() / contentView.height) @@ -388,7 +396,7 @@ internal class SwipeBackLayout @JvmOverloads constructor( override fun onViewReleased(releasedChild: View, xvel: Float, yvel: Float) { var result = Pair(0, 0) if (scrollPercent <= scrollThreshold) { - //threshold not met; check velocities + // threshold not met; check velocities if ((edgeFlag == SWIPE_EDGE_LEFT && xvel > MIN_FLING_VELOCITY) || (edgeFlag == SWIPE_EDGE_RIGHT && xvel < -MIN_FLING_VELOCITY) || (edgeFlag == SWIPE_EDGE_TOP && yvel > MIN_FLING_VELOCITY) || @@ -396,7 +404,7 @@ internal class SwipeBackLayout @JvmOverloads constructor( ) result = exitCaptureOffsets(edgeFlag, releasedChild) } else { - //threshold met; fling to designated side + // threshold met; fling to designated side result = exitCaptureOffsets(edgeFlag, releasedChild) } dragHelper.settleCapturedViewAt(result.first, result.second) diff --git a/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt b/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt index a46e6c5..bb4cd88 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/ui/ProgressAnimator.kt @@ -94,7 +94,9 @@ class ProgressAnimator private constructor() : ValueAnimator() { * @return [condition] */ private fun ProgressAction.runIf(condition: Boolean, progress: Float): Boolean { - if (condition) this(progress) + if (condition) { + this(progress) + } return condition } @@ -170,7 +172,9 @@ class ProgressAnimator private constructor() : ValueAnimator() { fun withDisposableEndAction(action: ProgressDisposableRunnable) = endActions.add(action) fun reset() { - if (isRunning) cancel() + if (isRunning) { + cancel() + } animators.clear() startActions.clear() cancelActions.clear() diff --git a/core/src/main/kotlin/ca/allanwang/kau/ui/views/CollapsibleViewDelegate.kt b/core/src/main/kotlin/ca/allanwang/kau/ui/views/CollapsibleViewDelegate.kt index b2a0d27..2a057cb 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/ui/views/CollapsibleViewDelegate.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/ui/views/CollapsibleViewDelegate.kt @@ -62,15 +62,22 @@ class CollapsibleViewDelegate : CollapsibleView { override var expansion = 0f set(value) { - if (value == field) return + if (value == field) { + return + } var v = value - if (v > 1) v = 1f - else if (v < 0) v = 0f + if (v > 1) { + v = 1f + } else if (v < 0) { + v = 0f + } stateHolder = - if (v == 0f) KAU_COLLAPSED - else if (v == 1f) KAU_EXPANDED - else if (v - field < 0) KAU_COLLAPSING - else KAU_EXPANDING + when { + v == 0f -> KAU_COLLAPSED + v == 1f -> KAU_EXPANDED + v - field < 0 -> KAU_COLLAPSING + else -> KAU_EXPANDING + } field = v view?.goneIf(state == KAU_COLLAPSED) view?.requestLayout() @@ -89,8 +96,11 @@ class CollapsibleViewDelegate : CollapsibleView { override fun resetCollapsibleAnimation() { animator?.cancel() animator = null - if (stateHolder == KAU_COLLAPSING) stateHolder = KAU_COLLAPSED - else if (stateHolder == KAU_EXPANDING) stateHolder = KAU_EXPANDED + if (stateHolder == KAU_COLLAPSING) { + stateHolder = KAU_COLLAPSED + } else if (stateHolder == KAU_EXPANDING) { + stateHolder = KAU_EXPANDED + } } override fun getCollapsibleDimension(): Pair<Int, Int> { @@ -116,8 +126,14 @@ class CollapsibleViewDelegate : CollapsibleView { override fun collapse(animate: Boolean) = setExpanded(false, animate) override fun setExpanded(expand: Boolean) = setExpanded(expand, true) override fun setExpanded(expand: Boolean, animate: Boolean) { - if (expand == expanded) return //state already matches + if (expand == expanded) { + return // state already matches + } val target = if (expand) 1f else 0f - if (animate) animateSize(target) else expansion = target + if (animate) { + animateSize(target) + } else { + expansion = target + } } } diff --git a/core/src/main/kotlin/ca/allanwang/kau/ui/views/MeasureSpecDelegate.kt b/core/src/main/kotlin/ca/allanwang/kau/ui/views/MeasureSpecDelegate.kt index 6481306..728ba7b 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/ui/views/MeasureSpecDelegate.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/ui/views/MeasureSpecDelegate.kt @@ -95,7 +95,9 @@ class MeasureSpecDelegate : MeasureSpecContract { private val parentFrame = Rect() override fun initAttrs(context: Context, attrs: AttributeSet?) { - if (attrs == null) return + if (attrs == null) { + return + } val styledAttrs = context.obtainStyledAttributes(attrs, R.styleable.MeasureSpecDelegate) relativeWidth = styledAttrs.getFloat(R.styleable.MeasureSpecDelegate_relativeWidth, relativeWidth) relativeHeight = styledAttrs.getFloat(R.styleable.MeasureSpecDelegate_relativeHeight, relativeHeight) @@ -113,21 +115,25 @@ class MeasureSpecDelegate : MeasureSpecContract { view.parentViewGroup.getWindowVisibleDisplayFrame(parentFrame) var width = View.MeasureSpec.getSize(widthMeasureSpec).toFloat() var height = View.MeasureSpec.getSize(heightMeasureSpec).toFloat() - //first cycle - relative to parent - if (relativeHeightToParent > 0) + // first cycle - relative to parent + if (relativeHeightToParent > 0) { height = relativeHeightToParent * parentFrame.height() - if (relativeWidthToParent > 0) + } + if (relativeWidthToParent > 0) { width = relativeWidthToParent * parentFrame.width() - //second cycle - relative to each other - if (relativeHeight > 0) + } + // second cycle - relative to each other + if (relativeHeight > 0) { height = relativeHeight * width - else if (relativeWidth > 0) + } else if (relativeWidth > 0) { width = relativeWidth * height - //third cycle - relative to each other - if (postRelativeHeight > 0) + } + // third cycle - relative to each other + if (postRelativeHeight > 0) { height = postRelativeHeight * width - else if (postRelativeWidth > 0) + } else if (postRelativeWidth > 0) { width = postRelativeWidth * height + } return Pair(width.measureSpec, height.measureSpec) } diff --git a/core/src/main/kotlin/ca/allanwang/kau/ui/views/RippleCanvas.kt b/core/src/main/kotlin/ca/allanwang/kau/ui/views/RippleCanvas.kt index 176b9ea..a972447 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/ui/views/RippleCanvas.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/ui/views/RippleCanvas.kt @@ -102,11 +102,12 @@ class RippleCanvas @JvmOverloads constructor( ripple.radius = animation.animatedValue as Float invalidate() } - if (callback != null) + if (callback != null) { animator.addListener(object : AnimatorListenerAdapter() { override fun onAnimationCancel(animation: Animator?) = callback() override fun onAnimationEnd(animation: Animator?) = callback() }) + } animator.start() } diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt index 21141f1..279bcd3 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt @@ -85,7 +85,7 @@ inline fun Activity.restart(intentBuilder: Intent.() -> Unit = {}) { i.putExtras(oldExtras) i.intentBuilder() startActivity(i) - overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_fade_out) //No transitions + overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_fade_out) // No transitions finish() overridePendingTransition(R.anim.kau_fade_in, R.anim.kau_fade_out) } @@ -125,7 +125,9 @@ inline var Activity.statusBarColor: Int get() = if (buildIsLollipopAndUp) window.statusBarColor else Color.BLACK @SuppressLint("NewApi") set(value) { - if (buildIsLollipopAndUp) window.statusBarColor = value + if (buildIsLollipopAndUp) { + window.statusBarColor = value + } } inline var Activity.statusBarLight: Boolean 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 3de0297..ab7b341 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ColorUtils.kt @@ -157,7 +157,7 @@ fun String.toColor(): Int { return Color.parseColor(toParse) } -//Get ColorStateList +// Get ColorStateList fun Context.colorStateList(@ColorInt color: Int): ColorStateList { val disabledColor = color.adjustAlpha(0.3f) return ColorStateList( 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 d002fb8..cf68066 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt @@ -80,22 +80,27 @@ inline fun <T : Activity> Context.startActivity( intentBuilder: Intent.() -> Unit = {} ) { val intent = Intent(this, clazz) - if (clearStack) intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK) + if (clearStack) { + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK) + } intent.intentBuilder() val bundle = Bundle() bundle.bundleBuilder() - startActivity(intent, if (bundle.isEmpty) null else bundle) - if (clearStack && this is Activity) finish() + startActivity(intent, bundle.takeIf { !it.isEmpty }) + if (clearStack && this is Activity) { + finish() + } } fun Context.startPlayStoreLink(@StringRes packageIdRes: Int) = startPlayStoreLink(string(packageIdRes)) fun Context.startPlayStoreLink(packageId: String) { val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$packageId")) - if (intent.resolveActivity(packageManager) != null) + if (intent.resolveActivity(packageManager) != null) { startActivity(intent) - else + } else { toast("Cannot resolve play store", log = true) + } } /** @@ -105,15 +110,16 @@ 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)) - if (browserIntent.resolveActivity(packageManager) != null) + if (browserIntent.resolveActivity(packageManager) != null) { startActivity(browserIntent) - else + } else { toast("Cannot resolve browser", log = true) + } } fun Context.startLink(@StringRes url: Int) = startLink(string(url)) -//Toast helpers +// Toast helpers inline fun View.toast(@StringRes id: Int, duration: Int = Toast.LENGTH_LONG, log: Boolean = false) = context.toast(id, duration, log) @@ -130,7 +136,7 @@ inline fun Context.toast(text: String, duration: Int = Toast.LENGTH_LONG, log: B const val INVALID_ID = 0 -//Resource retrievers +// Resource retrievers inline fun Context.string(@StringRes id: Int): String = getString(id) inline fun Context.string(@StringRes id: Int, fallback: String?): String? = @@ -161,7 +167,7 @@ inline fun Context.animation(@AnimRes id: Int) = AnimationUtils.loadAnimation(th inline fun Context.plural(@PluralsRes id: Int, quantity: Number) = resources.getQuantityString(id, quantity.toInt(), quantity.toInt()) -//Attr retrievers +// Attr retrievers fun Context.resolveColor(@AttrRes attr: Int, @ColorInt fallback: Int = 0): Int { val a = theme.obtainStyledAttributes(intArrayOf(attr)) try { @@ -240,19 +246,22 @@ fun Context.hasPermission(permissions: String) = !buildIsMarshmallowAndUp || Con fun Context.copyToClipboard(text: String?, label: String = "Copied Text", showToast: Boolean = true) { val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager clipboard.setPrimaryClip(ClipData.newPlainText(label, text ?: "")) - if (showToast) toast(R.string.kau_text_copied) + if (showToast) { + toast(R.string.kau_text_copied) + } } fun Context.shareText(text: String?) { - if (text == null) return toast("Share text is null") + text ?: return toast("Share text is null") val intent = Intent(Intent.ACTION_SEND) intent.type = "text/plain" intent.putExtra(Intent.EXTRA_TEXT, text) val chooserIntent = Intent.createChooser(intent, string(R.string.kau_share)) - if (chooserIntent.resolveActivity(packageManager) != null) + if (chooserIntent.resolveActivity(packageManager) != null) { startActivity(chooserIntent) - else + } else { toast("Cannot resolve activity to share text", log = true) + } } /** diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/IIconUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/IIconUtils.kt index 8b40352..dc7b214 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/IIconUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/IIconUtils.kt @@ -16,11 +16,11 @@ package ca.allanwang.kau.utils import android.content.Context -import android.content.res.ColorStateList import android.graphics.Color import android.graphics.drawable.Drawable import androidx.annotation.ColorInt import com.mikepenz.iconics.IconicsDrawable +import com.mikepenz.iconics.dsl.iconicsDrawable import com.mikepenz.iconics.typeface.IIcon /** @@ -32,10 +32,9 @@ fun IIcon.toDrawable( sizeDp: Int = 24, @ColorInt color: Int = Color.WHITE, builder: IconicsDrawable.() -> Unit = {} -): Drawable { - val state = ColorStateList.valueOf(color) - val icon = IconicsDrawable(c).icon(this).color(state) - if (sizeDp > 0) icon.sizeDp(sizeDp) - icon.builder() - return icon -} +): Drawable = c.iconicsDrawable(this) { + this.color = colorInt(color) + if (sizeDp > 0) { + size = sizeDp(sizeDp) + } +}.apply(builder) diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt index 8765c69..fac9421 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/Kotterknife.kt @@ -116,11 +116,11 @@ private inline val Dialog.viewFinder: Dialog.(Int) -> View? private inline val DialogFragment.viewFinder: DialogFragment.(Int) -> View? get() = { dialog.findViewById(it) } private inline val SupportDialogFragment.viewFinder: SupportDialogFragment.(Int) -> View? - get() = { dialog.findViewById(it) } + get() = { dialog?.findViewById(it) } private inline val Fragment.viewFinder: Fragment.(Int) -> View? - get() = { view!!.findViewById(it) } + get() = { view?.findViewById(it) } private inline val SupportFragment.viewFinder: SupportFragment.(Int) -> View? - get() = { view!!.findViewById(it) } + get() = { view?.findViewById(it) } private inline val ViewHolder.viewFinder: ViewHolder.(Int) -> View? get() = { itemView.findViewById(it) } @@ -246,7 +246,7 @@ private fun <T, V : View> requiredResettable(ids: IntArray, finder: T.(Int) -> V private fun <T, V : View> optionalResettable(ids: IntArray, finder: T.(Int) -> View?) = LazyResettable { t: T, _ -> ids.map { t.finder(it) as V? }.filterNotNull() } -//Like Kotterknife's lazy delegate but is resettable +// Like Kotterknife's lazy delegate but is resettable private class LazyResettable<in T, out V>(initializer: (T, KProperty<*>) -> V) : Lazy<T, V>(initializer) { override fun getValue(thisRef: T, property: KProperty<*>): V { KotterknifeRegistry.register(thisRef!!, this) diff --git a/core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt index 4055847..0bd534e 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/PackageUtils.kt @@ -54,13 +54,13 @@ fun Context.isAppEnabled(packageName: String): Boolean { @KauUtils fun Context.showAppInfo(packageName: String) { try { - //Open the specific App Info page: + // Open the specific App Info page: val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) intent.data = Uri.parse("package:$packageName") startActivity(intent) } catch (e: ActivityNotFoundException) { e.printStackTrace() - //Open the generic Apps page: + // Open the generic Apps page: val intent = Intent(Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS) startActivity(intent) } 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 186d125..e4ec19c 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ViewUtils.kt @@ -105,7 +105,7 @@ fun ImageView.setIcon( @ColorInt color: Int = Color.WHITE, builder: IconicsDrawable.() -> Unit = {} ) { - if (icon == null) return + icon ?: return setImageDrawable(icon.toDrawable(context, sizeDp = sizeDp, color = color, builder = builder)) } @@ -274,7 +274,9 @@ fun Context.fullLinearRecycler(rvAdapter: RecyclerView.Adapter<*>? = null, confi layoutManager = LinearLayoutManager(this@fullLinearRecycler) layoutParams = RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.MATCH_PARENT) - if (rvAdapter != null) adapter = rvAdapter + if (rvAdapter != null) { + adapter = rvAdapter + } configs() } @@ -296,8 +298,9 @@ inline fun <T : ImageView> T.fadeScaleTransition( minScale: Float = 0.7f, crossinline action: T.() -> Unit ) { - if (!isVisible) action() - else { + if (!isVisible) { + action() + } else { var transitioned = false ValueAnimator.ofFloat(1.0f, 0.0f, 1.0f).apply { this.duration = duration @@ -323,12 +326,17 @@ fun FloatingActionButton.hideOnDownwardsScroll(recycler: RecyclerView) { recycler.addOnScrollListener(object : RecyclerView.OnScrollListener() { override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { - if (newState == RecyclerView.SCROLL_STATE_IDLE && !isShown) show() + if (newState == RecyclerView.SCROLL_STATE_IDLE && !isShown) { + show() + } } override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { - if (dy > 0 && isShown) hide() - else if (dy < 0 && isHidden) show() + if (dy > 0 && isShown) { + hide() + } else if (dy < 0 && isHidden) { + show() + } } }) } diff --git a/core/src/main/kotlin/ca/allanwang/kau/xml/Changelog.kt b/core/src/main/kotlin/ca/allanwang/kau/xml/Changelog.kt index 9adcbf4..0872b68 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/xml/Changelog.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/xml/Changelog.kt @@ -84,8 +84,9 @@ internal fun parse(context: Context, @XmlRes xmlRes: Int): List<Pair<String, Cha context.resources.getXml(xmlRes).use { parser: XmlResourceParser -> var eventType = parser.eventType while (eventType != XmlPullParser.END_DOCUMENT) { - if (eventType == XmlPullParser.START_TAG) + if (eventType == XmlPullParser.START_TAG) { ChangelogType.values.any { it.add(parser, items) } + } eventType = parser.next() } } @@ -104,9 +105,12 @@ internal enum class ChangelogType(val tag: String, val attr: String, @LayoutRes * Returns true if tag matches; false otherwise */ fun add(parser: XmlResourceParser, list: MutableList<Pair<String, ChangelogType>>): Boolean { - if (parser.name != tag) return false - if (parser.getAttributeValue(null, attr).isNotBlank()) + if (parser.name != tag) { + return false + } + if (parser.getAttributeValue(null, attr).isNotBlank()) { list.add(Pair(parser.getAttributeValue(null, attr), this)) + } return true } } 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 73d7d6c..afde735 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/xml/FAQ.kt @@ -43,7 +43,7 @@ fun Context.kauParseFaq( resources.getXml(xmlRes).use { parser: XmlResourceParser -> var eventType = parser.eventType var question: Spanned? = null - var flag = -1 //-1, 0, 1 -> invalid, question, answer + var flag = -1 // -1, 0, 1 -> invalid, question, answer while (eventType != XmlPullParser.END_DOCUMENT) { if (eventType == XmlPullParser.START_TAG) { flag = when (parser.name) { |