Skip to content

Instantly share code, notes, and snippets.

View cmelchior's full-sized avatar

Christian Melchior cmelchior

View GitHub Profile
@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 / SerializeToJson.java
Last active September 20, 2022 04:30
Serialize Realm objects to JSON using GSON
// GSON can parse the data.
//
// Deserialization:
// Note there is a bug in GSON 2.3.1 that can cause it to StackOverflow when working with RealmObjects.
// To work around this, use the ExclusionStrategy below or downgrade to 1.7.1
// See more here: https://code.google.com/p/google-gson/issues/detail?id=440
//
// Serialization:
// <Type>RealmProxy objects are created by the Realm annotation processor. They are used to control
// access to the actual data instead of storing them in fields and it is therefore them we need to register a
@cmelchior
cmelchior / CustomTypeAdapter.java
Created April 9, 2015 06:35
Realm, GSON and primitive JSON arrays
// Make a custom Gson instance, with a custom TypeAdapter for each wrapper object.
// In this instance we only have RealmList<RealmInt> as a a wrapper for RealmList<Integer>
Type token = new TypeToken<RealmList<RealmInt>>(){}.getType();
Gson gson = new GsonBuilder()
.setExclusionStrategies(new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
return f.getDeclaringClass().equals(RealmObject.class);
}
@cmelchior
cmelchior / github_issue_rankings.rb
Last active March 7, 2022 18:58
Small ruby script that will rank all Github issues in a repo according to which is the most popular
#!/usr/bin/ruby -w
#
# This script will analyze a Github repo and try to rank all open issues with regard to popularity.
# WARNING: This script will run quite a few HTTP requests against Github to do this analysis. At least 1 pr. issue.
# The current limit on Github is 5000 requests pr. hour: https://developer.github.com/v3/rate_limit/
#
# Usage: ./ruby github-issue-rankings.rb <github_user/repo> <github_api_access_token>
# Example: ruby github_issue_rankings.rb realm/realm-java $GITHUB_ISSUE_RANKINGS_ACCESS_TOKEN
#
# The algorithm for ranking issues are the following:
@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