Skip to content

Commit

Permalink
Convert SAM interfaces to Kotlin 1.4 functional interfaces
Browse files Browse the repository at this point in the history
And deprecate SAM conversion helper functions
  • Loading branch information
Maxr1998 committed Aug 30, 2020
1 parent 6ab688c commit 593615e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ open class Preference(key: String) : AbstractPreference(key) {
/**
* Can be set to [Preference.preBindListener]
*/
interface OnPreBindListener {
fun interface OnPreBindListener {
/**
* Called before [Preference.bindViews], allows you to set data right before the [Preference][preference]
* is bound to a view.
Expand All @@ -306,7 +306,7 @@ open class Preference(key: String) : AbstractPreference(key) {
fun onPreBind(preference: Preference, holder: PreferencesAdapter.ViewHolder)
}

interface OnClickListener {
fun interface OnClickListener {
/**
* Notified when the connected [Preference] is clicked
*
Expand Down Expand Up @@ -450,7 +450,7 @@ class PreferenceScreen private constructor(builder: Builder) : Preference(builde
}
}

interface Appendable {
fun interface Appendable {
fun addPreferenceItem(p: Preference)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package de.Maxr1998.modernpreferences.helpers
import android.content.Context
import de.Maxr1998.modernpreferences.Preference
import de.Maxr1998.modernpreferences.PreferenceScreen
import de.Maxr1998.modernpreferences.PreferencesAdapter
import de.Maxr1998.modernpreferences.preferences.*

// PreferenceScreen DSL functions
Expand Down Expand Up @@ -84,30 +83,43 @@ inline fun CollapsePreference.subScreen(key: String = "", block: PreferenceScree
}

// Listener helpers
@Deprecated(
"Helper callback function was replaced by native SAM-interface support since Kotlin 1.4.\n" +
"Directly assign callback to clickListener instead."
)
inline fun Preference.onClicked(crossinline callback: (Preference) -> Boolean) {
clickListener = object : Preference.OnClickListener {
override fun onClick(preference: Preference, holder: PreferencesAdapter.ViewHolder) =
callback(preference)
}
}

inline fun Preference.onClickView(crossinline callback: (Preference, PreferencesAdapter.ViewHolder) -> Boolean) {
clickListener = object : Preference.OnClickListener {
override fun onClick(preference: Preference, holder: PreferencesAdapter.ViewHolder) =
callback(preference, holder)
}
}

inline fun TwoStatePreference.onChange(crossinline callback: (TwoStatePreference, PreferencesAdapter.ViewHolder?, Boolean) -> Boolean) {
checkedChangeListener = object : TwoStatePreference.OnCheckedChangeListener {
override fun onCheckedChanged(preference: TwoStatePreference, holder: PreferencesAdapter.ViewHolder?, checked: Boolean) =
callback(preference, holder, checked)
}
}

inline fun SeekBarPreference.onSeek(crossinline callback: (SeekBarPreference, PreferencesAdapter.ViewHolder?, Int) -> Boolean) {
seekListener = object : SeekBarPreference.OnSeekListener {
override fun onSeek(preference: SeekBarPreference, holder: PreferencesAdapter.ViewHolder?, value: Int) =
callback(preference, holder, value)
}
clickListener = Preference.OnClickListener { preference, _ -> callback(preference) }
}

@Deprecated(
"Helper callback function was replaced by native SAM-interface support since Kotlin 1.4",
ReplaceWith(
"clickListener = Preference.OnClickListener(callback)",
"de.Maxr1998.modernpreferences.Preference"
)
)
fun Preference.onClickView(callback: Preference.OnClickListener) {
clickListener = callback
}

@Deprecated(
"Helper callback function was replaced by native SAM-interface support since Kotlin 1.4",
ReplaceWith(
"checkedChangeListener = TwoStatePreference.OnCheckedChangeListener(callback)",
"de.Maxr1998.modernpreferences.preferences.TwoStatePreference"
)
)
fun TwoStatePreference.onChange(callback: TwoStatePreference.OnCheckedChangeListener) {
checkedChangeListener = callback
}

@Deprecated(
"Helper callback function was replaced by native SAM-interface support since Kotlin 1.4",
ReplaceWith(
"seekListener = SeekBarPreference.OnSeekListener(callback)",
"de.Maxr1998.modernpreferences.preferences.SeekBarPreference"
)
)
fun SeekBarPreference.onSeek(callback: SeekBarPreference.OnSeekListener) {
seekListener = callback
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class SeekBarPreference(key: String) : Preference(key) {
private fun calcRaw(value: Int) = (value - min) / step
private fun calcValue(raw: Int) = min + raw * step

interface OnSeekListener {
fun interface OnSeekListener {
/**
* Notified when the [value][SeekBarPreference.value] of the connected [SeekBarPreference] changes.
* This is called *before* the change gets persisted, which can be prevented by returning false.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ abstract class TwoStatePreference(key: String) : StatefulPreference(key) {
updateState(holder, !checkedInternal)
}

interface OnCheckedChangeListener {
fun interface OnCheckedChangeListener {
/**
* Notified when the [checked][TwoStatePreference.checked] state of the connected [TwoStatePreference] changes.
* This is called before the change gets persisted and can be prevented by returning false.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import android.content.Context
import android.graphics.drawable.BitmapDrawable
import android.util.Log
import android.widget.Toast
import de.Maxr1998.modernpreferences.Preference
import de.Maxr1998.modernpreferences.helpers.*
import de.Maxr1998.modernpreferences.preferences.SeekBarPreference
import java.util.*

object Common {
Expand Down Expand Up @@ -63,7 +65,7 @@ object Common {
pref("dependent") {
title = "Toggle the switch above"
dependency = "switch"
onClickView { _, holder ->
clickListener = Preference.OnClickListener { _, holder ->
Toast.makeText(holder.itemView.context, "Preference was clicked!", Toast.LENGTH_SHORT).show()
false
}
Expand Down Expand Up @@ -107,7 +109,7 @@ object Common {
default = 3

// Callback listener
onSeek { _, _, i ->
seekListener = SeekBarPreference.OnSeekListener { _, _, i ->
Log.d("Preferences", "SeekBar changed to $i")
true
}
Expand Down

0 comments on commit 593615e

Please sign in to comment.