Skip to content

Instantly share code, notes, and snippets.

View andreyfomenkov's full-sized avatar
🏠
High Voltage - The Game

Andrey Fomenkov andreyfomenkov

🏠
High Voltage - The Game
View GitHub Profile
@andreyfomenkov
andreyfomenkov / WeatherActivity.kt
Created April 26, 2021 16:13
Dispose a resource [A]
class WeatherActivity : AppCompatActivity() {
private val thermometer = Thermometer.getInstance()
private var disposable: Disposable? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
disposable = thermometer
.observeTemperature()
.subscribe { celsius -> /* Display in a TextView */ }
@andreyfomenkov
andreyfomenkov / WeatherActivity.kt
Created December 20, 2020 18:16
Leaked subscription
class WeatherActivity : AppCompatActivity() {
private val thermometer = Thermometer.getInstance()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
thermometer
.observeTemperature()
.subscribe { celsius -> /* Display in a TextView */ }
}
class WeatherActivity : AppCompatActivity() {
private val thermometer = Thermometer.getInstance()
private var disposable: Disposable? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
disposable = thermometer // Save the result of subscribe() method
.observeTemperature()
.subscribe { celsius -> /* Display in a TextView */ }
@andreyfomenkov
andreyfomenkov / Sample.kt
Last active December 20, 2020 18:09
Leaking subscription
class Thermometer {
fun observeTemperature(): Observable<Int>
}
// ...
val thermometer = Thermometer.getInstance()
// ...
thermometer
.observeTemperature()
.subscribe { /* Handle value */ } // Subscribed, but not disposed afterwards!
@andreyfomenkov
andreyfomenkov / MyApplication.kt
Last active December 18, 2020 18:26
Meanwhile in Application class
fun registerReceiver() {
val filter = IntentFilter()
filter.addAction("build-rx-report") // Action from report.sh
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val result = RxDisposableWatcher.probe()
val report = HtmlReportBuilder(result).build()
val file = File(getExternalFilesDir(null), "report.html")
@andreyfomenkov
andreyfomenkov / report.sh
Last active December 23, 2020 14:16
Shell script to send broadcast event, pull report and display in a browser
#!/bin/bash
# Replace APPLICATION_ID, REPORT_DEVICE_LOCATION
# and REPORT_DESKTOP_LOCATION with your values
BROADCAST_ACTION="build-rx-report" # Intent filter action for BroadcastReceiver
APPLICATION_ID=com.myapplication # RENAME
SLEEP_BEFORE_PULL=3 # Give a time (in seconds) to generate & save HTML report
# Location in Android device SD card
REPORT_DEVICE_LOCATION=/storage/emulated/0/Android/data/$APPLICATION_ID/files/report.html
REPORT_DESKTOP_LOCATION=~/report.html # Location on desktop
// ... DECOMPILED CODE GOES ABOVE ... //
@NotNull
public String toString() {
return "ABC-1234567890";
}
// ... DECOMPILED CODE GOES BELOW ... //
@andreyfomenkov
andreyfomenkov / User.kt
Last active February 15, 2018 12:54
User data class with .toString() method overriding
data class User(val firstName: String,
val lastName: String,
val age: Int) {
override fun toString() = "ABC-1234567890"
}
@andreyfomenkov
andreyfomenkov / decompile_generated.java
Last active February 16, 2018 11:53
Decompiled .toString() method generated by Kotlin compiler
// ... DECOMPILED CODE GOES ABOVE ... //
public String toString() {
return "User(firstName=" + this.firstName + ", lastName=" + this.lastName + ", age=" + this.age + ")";
}
// ... DECOMPILED CODE GOES BELOW ... //
@andreyfomenkov
andreyfomenkov / User.kt
Last active February 14, 2018 12:14
Simple Kotlin data class
data class User(val firstName: String,
val lastName: String,
val age: Int)