Skip to content

Instantly share code, notes, and snippets.

val song: Song = ...
val playlist: Playlist = ...
playlistRepository.addSongToPlaylist(song.typedId, playlist.typedId)
data class SongId(id: Long)
val Song.typedId: SongId = SongId(this.id)
data class PlaylistId(id: Long)
val Playlist.typedId: PlaylistId = PlaylistId(this.id)
// Compiler doesn’t like this. Red squigglies everywhere.
playlistRepository.addSongToPlaylist(songId = PlaylistId(420L), playlistId = SongId(9001L))
interface PlaylistRepository: Repository {
fun addSongToPlaylist(songId: SongId, playlistId: PlaylistId)
}
data class SongId(id: Long)
data class PlaylistId(id: Long)
val playlist: Playlist = ...
val song: Song = ...
// This is a bug, but the compiler allows it!
playlistRepository.addSongToPlaylist(songId = playlist.id, playlistId = song.id)
interface PlaylistRepository: Repository {
fun addSongToPlaylist(songId: Long, playlistId: Long)
}
@AOrobator
AOrobator / ConstellationDetailActivity.kt
Last active December 18, 2016 23:59
Implementing App Shortcuts - Enabling/Disabling Shortcuts
@TargetApi(N_MR1)
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.enable_shortcut -> shortcutAction {
it.enableShortcuts(listOf(constellation.name))
alertUser(R.string.shortcut_enabled)
}
R.id.disable_shortcut -> shortcutAction {
it.disableShortcuts(listOf(constellation.name))
alertUser(R.string.shortcut_disabled)
@AOrobator
AOrobator / ShortcutHelper.kt
Last active December 18, 2016 23:55
Implementing App Shortcuts - Updating your shortcuts
/**
* Sets the new Shortcut List by providing the three most visited
* constellations
* */
@TargetApi(N_MR1)
fun updateShortcuts(shortcutManager: ShortcutManager) {
shortcutManager.dynamicShortcuts =
Constellation
.values()
.sortedWith(compareBy { -getConstellationVisitedCount(it) })
@AOrobator
AOrobator / ShortcutHelper.kt
Last active December 6, 2016 01:27
Implementing App Shortcuts - ShortcutActions
inline fun shortcutAction(action: (ShortcutManager) -> Unit): Unit {
if (SDK_INT >= N_MR1) {
val shortcutManager = APP_CONTEXT.getSystemService(ShortcutManager::class.java)
action(shortcutManager)
}
}