diff options
author | Allan Wang <me@allanwang.ca> | 2019-09-08 01:25:23 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-09-08 01:25:23 -0700 |
commit | c6140c1008b042376bab328780f1686b75a9f839 (patch) | |
tree | 7a4578d02c9d93f922b721610124d445f7209ed3 /core | |
parent | a9407288fad0d30e71e2f6862b66533e043f9311 (diff) | |
parent | ff2a6237d1412b400644650f12389fefe73480c8 (diff) | |
download | kau-c6140c1008b042376bab328780f1686b75a9f839.tar.gz kau-c6140c1008b042376bab328780f1686b75a9f839.tar.bz2 kau-c6140c1008b042376bab328780f1686b75a9f839.zip |
Merge pull request #221 from AllanWang/android-readability
Android readability
Diffstat (limited to 'core')
13 files changed, 146 insertions, 78 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 400f60a..921cd1f 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 b00d951..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 61e6f30..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) @@ -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 fe210b2..279bcd3 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 62e5716..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,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 } } diff --git a/core/src/main/res-public/values/public.xml b/core/src/main/res-public/values/public.xml index 443f32e..211c539 100644 --- a/core/src/main/res-public/values/public.xml +++ b/core/src/main/res-public/values/public.xml @@ -1,25 +1,20 @@ <resources xmlns:tools='http://schemas.android.com/tools' tools:ignore='ResourceName'>
<!-- AUTO-GENERATED FILE. DO NOT MODIFY. public.xml is generated by the generatepublicxml gradle task --> + <public name='kau_slide_in_top' type='anim' /> <public name='kau_slide_in_left' type='anim' /> - <public name='kau_slide_out_left' type='anim' /> + <public name='kau_slide_out_right' type='anim' /> <public name='kau_slide_out_right_top' type='anim' /> + <public name='kau_fade_in' type='anim' /> + <public name='kau_slide_out_top' type='anim' /> + <public name='kau_slide_out_bottom' type='anim' /> <public name='kau_fade_out' type='anim' /> + <public name='kau_slide_out_left' type='anim' /> <public name='kau_slide_out_left_top' type='anim' /> - <public name='kau_fade_in' type='anim' /> <public name='kau_slide_in_bottom' type='anim' /> <public name='kau_slide_in_right' type='anim' /> - <public name='kau_slide_in_top' type='anim' /> - <public name='kau_slide_out_top' type='anim' /> - <public name='kau_slide_out_bottom' type='anim' /> - <public name='kau_slide_out_right' type='anim' /> - <public name='kau_enter_slide_right' type='transition' /> - <public name='kau_exit_slide_right' type='transition' /> - <public name='kau_exit_slide_top' type='transition' /> - <public name='kau_enter_slide_bottom' type='transition' /> - <public name='kau_enter_slide_left' type='transition' /> - <public name='kau_exit_slide_left' type='transition' /> - <public name='kau_enter_slide_top' type='transition' /> - <public name='kau_exit_slide_bottom' type='transition' /> + <public name='kau_transparent' type='drawable' /> + <public name='kau_selectable_white' type='drawable' /> + <public name='kau_shadow_overlay' type='color' /> <public name='kau_activity_horizontal_margin' type='dimen' /> <public name='kau_activity_vertical_margin' type='dimen' /> <public name='kau_dialog_margin' type='dimen' /> @@ -105,7 +100,6 @@ <public name='kau_bullet_point' type='string' /> <public name='Kau' type='style' /> <public name='Kau.Translucent' type='style' /> - <public name='kau_shadow_overlay' type='color' /> <public name='KauFadeIn' type='style' /> <public name='KauFadeInFadeOut' type='style' /> <public name='KauSlideInRight' type='style' /> @@ -113,6 +107,12 @@ <public name='KauSlideInFadeOut' type='style' /> <public name='KauSlideInSlideOutRight' type='style' /> <public name='KauSlideInSlideOutBottom' type='style' /> - <public name='kau_transparent' type='drawable' /> - <public name='kau_selectable_white' type='drawable' /> + <public name='kau_enter_slide_bottom' type='transition' /> + <public name='kau_enter_slide_top' type='transition' /> + <public name='kau_exit_slide_bottom' type='transition' /> + <public name='kau_exit_slide_top' type='transition' /> + <public name='kau_enter_slide_right' type='transition' /> + <public name='kau_exit_slide_right' type='transition' /> + <public name='kau_exit_slide_left' type='transition' /> + <public name='kau_enter_slide_left' type='transition' /> </resources>
\ No newline at end of file |