Skip to content

Instantly share code, notes, and snippets.

View cmelchior's full-sized avatar

Christian Melchior cmelchior

View GitHub Profile
@cmelchior
cmelchior / titanic.csv
Last active July 10, 2024 10:09
Titanic Dataset used for testing Kotlin Notebooks
PassengerId Survived Pclass Name Sex Age SibSp Parch Ticket Fare Cabin Embarked
1 0 3 Braund, Mr. Owen Harris male 22 1 0 A/5 21171 7.25 S
2 1 1 Cumings, Mrs. John Bradley (Florence Briggs Thayer) female 38 1 0 PC 17599 71.2833 C85 C
3 1 3 Heikkinen, Miss. Laina female 26 0 0 STON/O2. 3101282 7.925 S
4 1 1 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35 1 0 113803 53.1 C123 S
5 0 3 Allen, Mr. William Henry male 35 0 0 373450 8.05 S
6 0 3 Moran, Mr. James male 0 0 330877 8.4583 Q
7 0 1 McCarthy, Mr. Timothy J male 54 0 0 17463 51.8625 E46 S
8 0 3 Palsson, Master. Gosta Leonard male 2 3 1 349909 21.075 S
9 1 3 Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) female 27 0 2 347742 11.1333 S
@cmelchior
cmelchior / Imager.kt
Created December 23, 2023 19:03
Show how to capture a snapshot from a screen built using Compose inside Kotlin Notebooks
package dk.ilios.jervis.ui
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@cmelchior
cmelchior / VersionPinning.kt
Created July 16, 2021 11:27
Realm Kotlin 0.40 Release Blog Post - Version Pinning
// BAD: Store a global managed object
MyApp.GLOBAL_OBJECT = realm.objects(Person::class).first()
// BETTER: Copy data out into an unmanaged object
val person = realm.objects(Person::class).first()
MyApp.GLOBAL_OBJECT = Person(person.name)
@cmelchior
cmelchior / VersionMismatch.kt
Created July 16, 2021 11:27
Realm Kotlin 0.40 Release Blog Post - Version Mismatch
// A write can now happen between two queries
val results1: RealmResults<Person> = realm.objects(Person::class)
val results2: RealmResults<Person> = realm.objects(Person::class)
// Resulting in subsequent queries not returning the same result
results1.version() != results2.version()
results1.size != results2.size
@cmelchior
cmelchior / ThreadsafeObservers.kt
Created July 16, 2021 11:25
Realm Kotlin 0.40 Release Blog Post - Threadsafe Observers
val jane = getJane()
CoroutineScope(Dispatchers.Main).launch {
// Run mapping/transform logic in the background
val flow: Flow<String> = jane.observe()
.filter { it.name.startsWith("Jane") }
.flowOn(Dispatchers.Unconfined)
// Before collecting on the UI thread
flow.collect {
@cmelchior
cmelchior / ObservingChanges.kt
Created July 16, 2021 11:22
Realm Kotlin 0.40 Release Blog Post - Observing Changes
val jane = getJane()
CoroutineScope(Dispatchers.Main).launch {
// Updates are observed using Kotlin Flow
val flow: Flow<Person> = jane.observe()
flow.collect {
// Listen to changes to the object
println(it.name)
}
}
@cmelchior
cmelchior / UpdatingData.kt
Created July 16, 2021 11:21
Realm Kotlin 0.40 Release Blog Post - Updating Data
CoroutineScope(Dispatchers.Main).launch {
// Create initial object
val jane = realm.write {
copyToRealm(Person("Jane"))
}
realm.write {
// Find latest version and update it
// Note, this always involves a null-check
// as another thread might have deleted the
@cmelchior
cmelchior / SuspendableWrite.kt
Created July 16, 2021 11:20
Realm Kotlin 0.40 Release Blog Post - Suspendable Write
CoroutineScope(Dispatchers.Main).launch {
// Write automatically happens on a background dispatcher
val jane = realm.write {
val unmanaged = Person("Jane")
// Add unmanaged objects
copyToRealm(unmanaged)
}
// Objects returned from writes are automatically frozen
@cmelchior
cmelchior / BlockingWrite.kt
Created July 16, 2021 11:19
Realm Kotlin 0.40 Release Blog Post - Blocking Write
val jane = realm.writeBlocking {
val unmanaged = Person("Jane")
copyToRealm(unmanaged)
}
@cmelchior
cmelchior / ClosingRealms.kt
Created July 16, 2021 11:18
Realm Kotlin 0.40 Release Blog Post - Closing Realms
// Close Realm to free native resources
realm.close()