Skip to content

Instantly share code, notes, and snippets.

@AOrobator
AOrobator / FolderBrowsingPresenterTest.kt
Created April 7, 2018 17:00
Tests don't cover all bugs
@Test
fun `When unscanned song clicked, song is scanned into library`() {
val presenter = FolderBrowsingPresenter(queue, MockSongRepository())
val target: FolderBrowsingPresenter.Target = mock()
presenter.attach(target)
presenter.onUnscannedSongClicked(
DirectoryItemSong(
name = "01 Get You.m4a",
lastModifiedTime = 1234L,
@AOrobator
AOrobator / FolderBrowsingPresenter.kt
Created April 7, 2018 16:21
OnUnscannedSongClicked
fun onUnscannedSongClicked(song: DirectoryItemSong) {
val songFile = File(song.path)
if (songFile.extension in supportedFormats) {
target?.showLoading()
songRepo
.addSongToLibrary(song.path)
.subscribe {
target?.hideLoading()
target?.playSong(it)
}
class RealmSongRepository: SongRepository {
override fun getSongById(songId: SongId) : Song? = when (songId) {
is ValidSongId -> getSongFromRealm(songId.id) // smart cast to ValidSongId
InvalidSongId -> null
// No else statement required because the compiler knows we’ve covered all cases
}
}
sealed class SongId
data class ValidSongId(id: Long) : SongId()
object InvalidSongId : SongId()
val Song.typedId: SongId = if (this.id == -1L) InvalidSongId else SongId(this.id)
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)