Kotlin Notes: Difference between revisions

From James's Wiki
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
=== Custom Listeners ===
=== Custom Listeners ===
===== Define the listener =====
===== Define the listener =====
<syntaxhighlight lang="kotlin">
  var onDismissListener: (() -> Unit)? = null
  var onDismissListener: (() -> Unit)? = null
 
</syntaxhighlight>
===== Invoke the listener =====
===== Invoke the listener =====
<syntaxhighlight lang="kotlin">
  onDismissListener?.invoke()
  onDismissListener?.invoke()
 
</syntaxhighlight>
===== Use the listener in another class =====  
===== Use the listener in another class =====  
<syntaxhighlight lang="kotlin">
  dialogRapidShortTimePicker.onDismissListener = {
  dialogRapidShortTimePicker.onDismissListener = {
       Timber.d("Dialog dismissed from helper")
       Timber.d("Dialog dismissed from helper")
       dismiss()
       dismiss()
  }
  }
</syntaxhighlight>


=== Interfaces for callbacks ===
=== Interfaces for callbacks ===
Line 17: Line 21:
==== Code in HomeFragment ====
==== Code in HomeFragment ====
At the type we define the Interface used for the listener
At the type we define the Interface used for the listener
 
<syntaxhighlight lang="kotlin">
  enum class DialogTypes {
  enum class DialogTypes {
     RAPID_SHORT_TIME,
     RAPID_SHORT_TIME,
Line 45: Line 49:
     }
     }
  }
  }
</syntaxhighlight>
==== In the class that we want to call the listener from ====
==== In the class that we want to call the listener from ====
<syntaxhighlight lang="kotlin">
  class SomeClass {
  class SomeClass {
     var listener: DialogListener?=null
     var listener: DialogListener?=null
 
...
//other code
...
  override fun onSingleTapConfirmed(event: MotionEvent): Boolean {
  override fun onSingleTapConfirmed(event: MotionEvent): Boolean {
         val x = event.x.toInt()
         val x = event.x.toInt()
Line 54: Line 62:
         Timber.v("onSingleTapConfirmed: $x, $y")
         Timber.v("onSingleTapConfirmed: $x, $y")
  ...
  ...
  other code
  //other code
  ...
  ...
  segments[index].preTripSegment.durationClickArea.contains(x, y) -> {
  segments[index].preTripSegment.durationClickArea.contains(x, y) -> {
Line 60: Line 68:
     listener?.openDialog(DialogTypes.RAPID_SHORT_TIME)
     listener?.openDialog(DialogTypes.RAPID_SHORT_TIME)
  }
  }
</syntaxhighlight>

Latest revision as of 11:15, 2 March 2025

Custom Listeners

Define the listener
 var onDismissListener: (() -> Unit)? = null
Invoke the listener
 onDismissListener?.invoke()
Use the listener in another class
 dialogRapidShortTimePicker.onDismissListener = {
      Timber.d("Dialog dismissed from helper")
      dismiss()
 }

Interfaces for callbacks

Here is an interface used to open dialogs from HomeFragment since dialogs must be opened from a fragment or activity

Code in HomeFragment

At the type we define the Interface used for the listener

 enum class DialogTypes {
     RAPID_SHORT_TIME,
     TIME_INPUT_HOUR
 }
 
 interface DialogListener {
     fun openDialog(type: DialogTypes)
 }
 
 class HomeFragment : Fragment(), LocationProvider, DialogListener {
 
 //other code
 ...
 override fun openDialog(type: DialogTypes) {
        Timber.i("openDialog type: $type")
        when (type) {
            DialogTypes.RAPID_SHORT_TIME -> {
                val dialogRapidShortTimeHelper = DialogRapidShortTimeHelper()
                dialogRapidShortTimeHelper.show(childFragmentManager, "dialog")
            }
            DialogTypes.TIME_INPUT_HOUR -> {
                val dialogTimeInputHourHelper = DialogTimeInputHourHelper()
                dialogTimeInputHourHelper.show(childFragmentManager, "dialog")
            }
        }
    }
 }

In the class that we want to call the listener from

 class SomeClass {
     var listener: DialogListener?=null
 ...
 //other code
 ...
 override fun onSingleTapConfirmed(event: MotionEvent): Boolean {
        val x = event.x.toInt()
        val y = event.y.toInt()
        Timber.v("onSingleTapConfirmed: $x, $y")
 ...
 //other code
 ...
 segments[index].preTripSegment.durationClickArea.contains(x, y) -> {
     //this is the part that performs the callback
     listener?.openDialog(DialogTypes.RAPID_SHORT_TIME)
 }