diff options
Diffstat (limited to 'core/src/main')
12 files changed, 129 insertions, 61 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/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/swipe/SwipeBackLayout.kt b/core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackLayout.kt index 1f564ce..0fb0046 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) @@ -197,8 +200,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() + } } } @@ -210,10 +214,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 } @@ -239,12 +244,14 @@ internal class SwipeBackLayout @JvmOverloads constructor( } override fun onInterceptTouchEvent(event: MotionEvent): Boolean { - if (!swipeEnabled || disallowIntercept) return false - try { - return dragHelper.shouldInterceptTouchEvent(event) - } catch (e: Exception) { + if (!swipeEnabled || disallowIntercept) { return false } + return try { + dragHelper.shouldInterceptTouchEvent(event) + } catch (e: Exception) { + false + } } @SuppressLint("ClickableViewAccessibility") 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..4631aec 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..8800c2d 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) @@ -114,20 +116,24 @@ class MeasureSpecDelegate : MeasureSpecContract { var width = View.MeasureSpec.getSize(widthMeasureSpec).toFloat() var height = View.MeasureSpec.getSize(heightMeasureSpec).toFloat() //first cycle - relative to parent - if (relativeHeightToParent > 0) + 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) + 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) + 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..3b593a6 100644 --- a/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt +++ b/core/src/main/kotlin/ca/allanwang/kau/utils/ActivityUtils.kt @@ -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/ContextUtils.kt b/core/src/main/kotlin/ca/allanwang/kau/utils/ContextUtils.kt index d002fb8..2f552ed 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,10 +110,11 @@ 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)) @@ -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/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 } } |