Skip to content

Instantly share code, notes, and snippets.

@AOrobator
AOrobator / ConstellationDetailActivity.kt
Last active December 6, 2016 01:22
Implementing App Shortcuts - Tracking usage pt. 1
shortcutAction {
trackShortcutUsed(it, constellation)
}
@AOrobator
AOrobator / ShortcutHelper.kt
Last active December 6, 2016 01:22
Implementing App Shortcuts - Tracking usage pt. 2
@TargetApi(N_MR1)
fun trackShortcutUsed(shortcutManager: ShortcutManager, constellation: Constellation) {
shortcutManager.reportShortcutUsed(constellation.name)
val sharedPrefs = PreferenceManager.getDefaultSharedPreferences(APP_CONTEXT)
val seenCount = sharedPrefs.getInt(constellation.name, 0)
sharedPrefs
.edit()
.putInt(constellation.name, seenCount + 1)
.apply()
updateShortcuts(shortcutManager)
@AOrobator
AOrobator / RestoreShortcuts.java
Last active December 5, 2016 00:34
Implementing App Shortcuts - Restoring shortcuts
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ShortcutManager shortcutManager = getSystemService(ShortcutManager.class);
if (shortcutManager.getDynamicShortcuts().size() == 0) {
// Application restored. Need to re-publish dynamic shortcuts.
if (shortcutManager.getPinnedShortcuts().size() > 0) {
// Pinned shortcuts have been restored. Use
// updateShortcuts(List) to make sure they
// contain up-to-date information.
interface PlaylistRepository: Repository {
fun addSongToPlaylist(songId: Long, playlistId: Long)
}
val playlist: Playlist = ...
val song: Song = ...
// This is a bug, but the compiler allows it!
playlistRepository.addSongToPlaylist(songId = playlist.id, playlistId = song.id)
data class SongId(id: Long)
data class PlaylistId(id: Long)
interface PlaylistRepository: Repository {
fun addSongToPlaylist(songId: SongId, playlistId: PlaylistId)
}
// Compiler doesn’t like this. Red squigglies everywhere.
playlistRepository.addSongToPlaylist(songId = PlaylistId(420L), playlistId = SongId(9001L))
data class SongId(id: Long)
val Song.typedId: SongId = SongId(this.id)
data class PlaylistId(id: Long)
val Playlist.typedId: PlaylistId = PlaylistId(this.id)
val song: Song = ...
val playlist: Playlist = ...
playlistRepository.addSongToPlaylist(song.typedId, playlist.typedId)