Skip to content

Commit

Permalink
Remove listeners in favor of lambdas
Browse files Browse the repository at this point in the history
  • Loading branch information
Jawnnypoo committed Nov 10, 2017
1 parent 3dd89da commit 396647d
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 45 deletions.
10 changes: 4 additions & 6 deletions app/src/main/java/com/commit451/lifeline/sample/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import android.app.Application
import android.widget.Toast

import com.commit451.lifeline.Lifeline
import com.commit451.lifeline.OnBackgroundedListener
import com.commit451.lifeline.OnForegroundedListener

/**
* Here is where you would initialize [com.commit451.lifeline.Lifeline]
Expand All @@ -15,13 +13,13 @@ class App : Application() {
override fun onCreate() {
super.onCreate()
Lifeline.init(this)
Lifeline.register(OnBackgroundedListener {
Lifeline.registerOnBackgroundedListener {
Toast.makeText(this@App, "On backgrounded", Toast.LENGTH_SHORT)
.show()
})
Lifeline.register(OnForegroundedListener {
}
Lifeline.registerOnForegroundedListener {
Toast.makeText(this@App, "On foregrounded", Toast.LENGTH_SHORT)
.show()
})
}
}
}

This file was deleted.

21 changes: 12 additions & 9 deletions lifeline/src/main/java/com/commit451/lifeline/Lifeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,40 +93,40 @@ object Lifeline {
*/
fun init(application: Application) {
lifecycleHandler = TrackedLifecycleCallbacks()
application.registerComponentCallbacks(BackgroundComponentCallbacks2(InternalBackgroundListener {
application.registerComponentCallbacks(BackgroundComponentCallbacks2({
for (listener in onBackgroundedListeners) {
listener.onBackgrounded()
listener.invoke()
}
}))
application.registerActivityLifecycleCallbacks(lifecycleHandler)
}

fun register(listener: OnBackgroundedListener) {
fun registerOnBackgroundedListener(listener: OnBackgroundedListener) {
onBackgroundedListeners.add(listener)
}

fun unregister(listener: OnBackgroundedListener) {
fun unregisterOnBackgroundedListener(listener: OnBackgroundedListener) {
onBackgroundedListeners.remove(listener)
}

fun register(listener: OnForegroundedListener) {
fun registerOnForegroundedListener(listener: OnForegroundedListener) {
onForegroundedListeners.add(listener)
}

fun unregister(listener: OnForegroundedListener) {
fun unregisterOnForegroundedListener(listener: OnForegroundedListener) {
onForegroundedListeners.remove(listener)
}

private fun lifecycleHandler(): TrackedLifecycleCallbacks {
return lifecycleHandler ?: throw IllegalStateException("You need to call init() before accessing Lifeline")
}

private class BackgroundComponentCallbacks2(private val listener: InternalBackgroundListener) : ComponentCallbacks2 {
private class BackgroundComponentCallbacks2(private val onBackgrounded: () -> Unit) : ComponentCallbacks2 {

override fun onTrimMemory(level: Int) {
if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
// We're in the Background
listener.onBackgrounded()
onBackgrounded.invoke()
}
}

Expand Down Expand Up @@ -166,7 +166,7 @@ object Lifeline {
timeSpentOutsideApp = System.currentTimeMillis() - timeStartedPause
timeStartedPause = 0
for (listener in onForegroundedListeners) {
listener.onForegrounded()
listener.invoke()
}
}
currentVisibleActivityRef = WeakReference(activity)
Expand All @@ -191,3 +191,6 @@ object Lifeline {
}
}
}

typealias OnBackgroundedListener = () -> Unit
typealias OnForegroundedListener = () -> Unit

This file was deleted.

This file was deleted.

0 comments on commit 396647d

Please sign in to comment.