Skip to content

Instantly share code, notes, and snippets.

Avatar

Andrew Orobator AOrobator

View GitHub Profile
View keybase.md

Keybase proof

I hereby claim:

To claim this, I am signing this object:

@AOrobator
AOrobator / OkHttp.kt
Created Jul 21, 2018
OkHttpExtensions
View OkHttp.kt
import okhttp3.mockwebserver.MockWebServer
import okhttp3.mockwebserver.RecordedRequest
import org.amshove.kluent.`should equal`
/**
* OkHttp3 extensions for syntactic sugar while testing
*/
enum class HttpMethod {
DELETE,
GET,
View RealmAction.kt
fun realmTransactionSync(transaction: (Realm) -> Unit) {
val realm = Realm.getDefaultInstance()
var canceled = false
realm.beginTransaction()
try {
transaction(realm)
} catch (e: Exception) {
e.printStackTrace()
realm.cancelTransaction()
View RealmSongRepository.kt
override fun scanSongIntoLibrary(filePath: String): SongId {
var newSongId: SongId = InvalidSongId
realmTransaction {
if (getSongForFilepath(it, filePath) == null) {
val song = Song {
id = Database.getNextId(it, Song::class.java)
newSongId = // get next ValidSongId
// Initialize title, artist, and album for the song
}
it.copyToRealm(song)
View RealmAction.kt
fun realmTransaction(onError: (Throwable) -> Unit = {}, transaction: (Realm) -> Unit) {
val realm = Realm.getDefaultInstance()
try {
realm.executeTransactionAsync { transaction(it) }
} catch (e: Exception) {
onError(e)
}
realm.close()
View RealmSongRepository.kt
class RealmSongRepository : SongRepository {
override fun addSongToLibrary(filePath: String): Single<SongId> {
return Single.fromCallable {
scanSongIntoLibrary(filePath)
}
.subscribeOn(io())
.observeOn(mainThread())
}
override fun scanSongIntoLibrary(filePath: String): SongId {
View FolderBrowsingPresenterTest.kt
@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,
View FolderBrowsingPresenter.kt
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)
}
View RealmSongRepository.kt
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
}
}
View SongId.kt
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)
You can’t perform that action at this time.