Skip to content

Commit

Permalink
Release 3.3.1 (#36)
Browse files Browse the repository at this point in the history
* Add open message joiner function

* Update text extraction

* Fix background tint

* Rename logger file

* Test codecov

* Remove coverage

* Enhancement/swipe (#35)

* Merge swipe onPostCreate with swipe onCreate

* Update samples and docs

* Add deprecated method to maintain compatibility

* Replace exception with illegal argument

* Check if parent exists before configurations in searchview
  • Loading branch information
AllanWang authored Aug 12, 2017
1 parent 02e1dbc commit 10617f0
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ open class KauLogger(val tag: String) {

open var enabled = true
open var showPrivateText = false
open var messageJoiner: (msg: String, privMsg: String) -> String = { msg, privMsg -> "$msg: $privMsg" }

/**
* Filter pass-through to decide what we wish to log
Expand Down Expand Up @@ -43,11 +44,14 @@ open class KauLogger(val tag: String) {
= enabled && filter(priority)

protected open fun logImpl(priority: Int, message: String?, privateMessage: String?, t: Throwable?) {
var text = message ?: ""
if (showPrivateText && privateMessage != null)
text += "\n-\t$privateMessage"
if (t != null) Log.e(tag, text, t)
else if (text.isNotBlank()) Log.println(priority, tag, text)
val text = if (showPrivateText) {
if (message == null) privateMessage
else if (privateMessage == null) message
else messageJoiner(message, privateMessage)
} else message

if (t != null) Log.e(tag, text ?: "Error", t)
else if (!text.isNullOrBlank()) Log.println(priority, tag, text)
}

open fun v(text: String?, privateText: String? = null) = log(Log.VERBOSE, text, privateText)
Expand Down
44 changes: 32 additions & 12 deletions core/src/main/kotlin/ca/allanwang/kau/swipe/SwipeBackHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import ca.allanwang.kau.logging.KL
import ca.allanwang.kau.swipe.SwipeBackHelper.onDestroy
import java.util.*

internal class SwipeBackException(message: String = "You Should call kauSwipeOnCreate() first") : RuntimeException(message)

/**
* Singleton to hold our swipe stack
* All activity pages held with strong references, so it is crucial to call
Expand All @@ -30,14 +28,10 @@ internal object SwipeBackHelper {
else -> R.anim.kau_slide_in_top
}
activity.overridePendingTransition(startAnimation, 0)
page.onPostCreate()
KL.v("KauSwipe onCreate ${activity.localClassName}")
}

fun onPostCreate(activity: Activity) {
this[activity]?.onPostCreate() ?: throw SwipeBackException()
KL.v("KauSwipe onPostCreate ${activity.localClassName}")
}

fun onDestroy(activity: Activity) {
val page: SwipeBackPage? = this[activity]
pageStack.kauRemoveIf { it.activityRef.get() == null || it === page }
Expand All @@ -55,17 +49,43 @@ internal object SwipeBackHelper {
}

/**
* The following are the activity bindings to add an activity to the stack
* onCreate, onPostCreate, and onDestroy are mandatory
* finish is there as a helper method to animate the transaction
* The creation binder, which adds the swipe functionality to an activity.
* Call this during [Activity.onCreate] after all views are added.
*
* Preferably, this should be the last line in the onCreate method.
* Note that this will also capture your statusbar color and nav bar color,
* so be sure to assign those beforehand if at all.
*
* Lastly, don't forget to call [kauSwipeOnDestroy] as well when the activity is destroyed.
*/
fun Activity.kauSwipeOnCreate(builder: SwipeBackContract.() -> Unit = {}) = SwipeBackHelper.onCreate(this, builder)

fun Activity.kauSwipeOnPostCreate() = SwipeBackHelper.onPostCreate(this)
/**
* Deprecated as onPostCreate seems unreliable.
* We will instead initialize everything during [kauSwipeOnCreate]
*/
@Deprecated(level = DeprecationLevel.WARNING, message = "All functionality has been moved to kauSwipeOnCreate")
fun Activity.kauSwipeOnPostCreate() {
}

/**
* The unbinder, which removes our layouts, releases our weak references, and cleans our page stack
* Call this during [Activity.onDestroy]
*
* Given that our references are held weakly, we allow failures and missing pages to pass silently
* as they should be destroyed anyways.
*
* Don't forget to call [kauSwipeOnCreate] when the activity is created to enable swipe functionality.
*/
fun Activity.kauSwipeOnDestroy() = SwipeBackHelper.onDestroy(this)
fun Activity.kauSwipeFinish() = SwipeBackHelper.finish(this)

/**
* Helper function for activities to animate the finish transaction with a pseudo swipe
* The activity will automatically be finished afterwards
*/
fun Activity.kauSwipeFinish() = SwipeBackHelper.finish(this)

/*
* Constants used for the swipe edge flags
*/
const val SWIPE_EDGE_LEFT = ViewDragHelper.EDGE_LEFT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ internal class SwipeBackLayout @JvmOverloads constructor(context: Context, attrs
*/
set(value) {
if (value !in arrayOf(SWIPE_EDGE_TOP, SWIPE_EDGE_BOTTOM, SWIPE_EDGE_LEFT, SWIPE_EDGE_RIGHT))
throw SwipeBackException("Edge flag is not valid; use one of the SWIPE_EDGE_* values")
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)
Expand Down
18 changes: 14 additions & 4 deletions core/src/main/kotlin/ca/allanwang/kau/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@ import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.Handler
import android.os.Looper
import android.support.annotation.IntRange
import ca.allanwang.kau.R
import ca.allanwang.kau.logging.KL
import java.math.RoundingMode
import java.text.DecimalFormat

Expand All @@ -30,13 +28,25 @@ annotation class KauUtils
get() = this * Resources.getSystem().displayMetrics.density

@KauUtils inline val Int.dpToPx: Int
get() = (this * Resources.getSystem().displayMetrics.density).toInt()
get() = toFloat().dpToPx.toInt()

@KauUtils inline val Float.pxToDp: Float
get() = this / Resources.getSystem().displayMetrics.density

@KauUtils inline val Int.pxToDp: Int
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
get() = toFloat().pxToDp.toInt()

@KauUtils inline val Float.dpToSp: Float
get() = this * Resources.getSystem().displayMetrics.scaledDensity

@KauUtils inline val Int.dpToSp: Int
get() = toFloat().dpToSp.toInt()

@KauUtils inline val Float.spToDp: Float
get() = this / Resources.getSystem().displayMetrics.scaledDensity

@KauUtils inline val Int.spToDp: Int
get() = toFloat().spToDp.toInt()

/**
* Converts minute value to string
Expand Down
5 changes: 5 additions & 0 deletions docs/Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v3.3.1
* :core: Open up all logger functions
* :core: Deprecate kauSwipeOnPostCreate and move functionality to onCreate
* :searchview: Fix background tint

## v3.3.0
* :core: Create debounce methods
* :core: Create zip methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import ca.allanwang.kau.permissions.kauRequestPermissions
import ca.allanwang.kau.swipe.SWIPE_EDGE_LEFT
import ca.allanwang.kau.swipe.kauSwipeOnCreate
import ca.allanwang.kau.swipe.kauSwipeOnDestroy
import ca.allanwang.kau.swipe.kauSwipeOnPostCreate
import ca.allanwang.kau.utils.fullLinearRecycler
import ca.allanwang.kau.utils.startActivitySlideOut
import ca.allanwang.kau.utils.toast
Expand Down Expand Up @@ -49,14 +48,9 @@ class AnimActivity : KauBaseActivity() {
}
}

override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState)
kauSwipeOnPostCreate()
}

override fun onDestroy() {
super.onDestroy()
kauSwipeOnDestroy()
super.onDestroy()
}

override fun onBackPressed() {
Expand Down
13 changes: 4 additions & 9 deletions sample/src/main/kotlin/ca/allanwang/kau/sample/SwipeActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ class SwipeActivity : KauBaseActivity() {
it.setOnClickListener { startActivityWithEdge(swipeEdge) }
}
val flag = intent.getIntExtra(SWIPE_EDGE, -1)
kauSwipeOnCreate {
edgeFlag = flag
}
toolbar.title = when (flag) {
SWIPE_EDGE_LEFT -> "Left Edge Swipe"
SWIPE_EDGE_RIGHT -> "Right Edge Swipe"
Expand All @@ -57,16 +54,14 @@ class SwipeActivity : KauBaseActivity() {
val bg = headerColor.darken(0.2f)
container.setBackgroundColor(bg)
navigationBarColor = bg
}

override fun onPostCreate(savedInstanceState: Bundle?) {
super.onPostCreate(savedInstanceState)
kauSwipeOnPostCreate()
kauSwipeOnCreate {
edgeFlag = flag
}
}

override fun onDestroy() {
super.onDestroy()
kauSwipeOnDestroy()
super.onDestroy()
}

override fun onBackPressed() {
Expand Down
11 changes: 8 additions & 3 deletions sample/src/main/res/xml/kau_changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@
<item text="" />
-->

<version title="v3.3.1"/>
<item text=":core: Open up all logger functions" />
<item text=":core: Deprecate kauSwipeOnPostCreate and move functionality to onCreate" />
<item text=":searchview: Fix background tint" />
<item text="" />
<item text="" />
<item text="" />

<version title="v3.3.0"/>
<item text=":core: Create debounce methods" />
<item text=":core: Create zip methods" />
Expand All @@ -14,9 +22,6 @@
<item text=":kpref-activity: [Breaking] Removed sliding toolbar and use normal toolbar title" />
<item text=":kpref-activity: Remove :core-ui: dependency" />
<item text=":searchview: [Breaking] remove reactive dependencies and stick with basic callbacks" />
<item text="" />
<item text="" />
<item text="" />

<version title="v3.2.5"/>
<item text=":core: Fix FAQ background" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.support.transition.ChangeBounds
import android.support.transition.TransitionManager
import android.support.transition.TransitionSet
import android.support.v7.widget.AppCompatEditText
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextWatcher
Expand Down Expand Up @@ -153,7 +152,7 @@ class SearchView @JvmOverloads constructor(
}
if (SearchItem.backgroundColor != backgroundColor) {
SearchItem.backgroundColor = backgroundColor
tintForeground(backgroundColor)
tintBackground(backgroundColor)
}
val icons = mutableListOf(navIcon to iconNav, clearIcon to iconClear)
val extra = extraIcon
Expand Down Expand Up @@ -291,11 +290,12 @@ class SearchView @JvmOverloads constructor(
*/
fun bind(menu: Menu, @IdRes id: Int, @ColorInt menuIconColor: Int = Color.WHITE, config: Configs.() -> Unit = {}): SearchView {
config(config)
menuItem = menu.findItem(id) ?: throw IllegalArgumentException("Menu item with given id doesn't exist")
if (menuItem!!.icon == null) menuItem!!.icon = GoogleMaterial.Icon.gmd_search.toDrawable(context, 18, menuIconColor)
val menuItem = menu.findItem(id) ?: throw IllegalArgumentException("Menu item with given id doesn't exist")
if (menuItem.icon == null) menuItem.icon = GoogleMaterial.Icon.gmd_search.toDrawable(context, 18, menuIconColor)
card.gone()
menuItem!!.setOnMenuItemClickListener { configureCoords(it); revealOpen(); true }
menuItem.setOnMenuItemClickListener { configureCoords(it); revealOpen(); true }
shadow.setOnClickListener { revealClose() }
this.menuItem = menuItem
return this
}

Expand All @@ -310,6 +310,7 @@ class SearchView @JvmOverloads constructor(
}

private fun configureCoords(item: MenuItem) {
if (parent !is ViewGroup) return
val view = parentViewGroup.findViewById<View>(item.itemId) ?: return
val locations = IntArray(2)
view.getLocationOnScreen(locations)
Expand Down

0 comments on commit 10617f0

Please sign in to comment.