Last active
October 6, 2022 14:23
-
-
Save AlexZhukovich/9d4047a1b0dbd8280545d86703380697 to your computer and use it in GitHub Desktop.
Android ProcessLifecycleOwner
by example - source code for article https://alexzh.com/2019/08/19/android-processlifecycleowner-by-example/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Analytics { | |
private var startSessionTimestamp: Long = -1 | |
private val reporters = mutableListOf<AnalyticsReporter>() | |
fun addReporter(reporter: AnalyticsReporter) { | |
reporters.add(reporter) | |
} | |
fun startSession() { | |
startSessionTimestamp = Date().time | |
} | |
fun stopSession() { | |
reportSession() | |
sendAllEvents() | |
startSessionTimestamp = -1 | |
} | |
private fun reportSession() { | |
reporters.forEach {reporter -> | |
val currentTime = Date().time | |
// we should check if session was started and stopped correctly | |
val sessionTime = (currentTime - startSessionTimestamp) / 1000 | |
reporter.report("Session time: $sessionTime sec" ) | |
} | |
} | |
private fun sendAllEvents() { | |
reporters.forEach {reporter -> | |
reporter.sendAllEvents() | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface AnalyticsReporter { | |
fun report(event: String) | |
fun sendAllEvents() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationObserver(val analytics: Analytics) : LifecycleObserver { | |
@OnLifecycleEvent(Lifecycle.Event.ON_START) | |
fun onForeground() { | |
analytics.startSession() | |
} | |
@OnLifecycleEvent(Lifecycle.Event.ON_STOP) | |
fun onBackground() { | |
analytics.stopSession() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
... | |
implementation "androidx.lifecycle:lifecycle-runtime:2.0.0" | |
implementation "androidx.lifecycle:lifecycle-extensions:2.0.0" | |
annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.0.0" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LogReporter : AnalyticsReporter { | |
private val events = mutableListOf<String>() | |
override fun report(event: String) { | |
events.add(event) | |
} | |
override fun sendAllEvents() { | |
events.forEach { event -> | |
Log.d(this.javaClass.simpleName, event) | |
} | |
events.clear() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MapNotesApp : Application() { | |
override fun onCreate() { | |
super.onCreate() | |
... | |
val analytics = Analytics() | |
analytics.addReporter(LogReporter()) | |
ProcessLifecycleOwner | |
.get() | |
.lifecycle | |
.addObserver(ApplicationObserver(analytics)) | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment