This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val song: Song = ... | |
| val playlist: Playlist = ... | |
| playlistRepository.addSongToPlaylist(song.typedId, playlist.typedId) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data class SongId(id: Long) | |
| val Song.typedId: SongId = SongId(this.id) | |
| data class PlaylistId(id: Long) | |
| val Playlist.typedId: PlaylistId = PlaylistId(this.id) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Compiler doesn’t like this. Red squigglies everywhere. | |
| playlistRepository.addSongToPlaylist(songId = PlaylistId(420L), playlistId = SongId(9001L)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface PlaylistRepository: Repository { | |
| fun addSongToPlaylist(songId: SongId, playlistId: PlaylistId) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| data class SongId(id: Long) | |
| data class PlaylistId(id: Long) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val playlist: Playlist = ... | |
| val song: Song = ... | |
| // This is a bug, but the compiler allows it! | |
| playlistRepository.addSongToPlaylist(songId = playlist.id, playlistId = song.id) |