Added IDataProvider.appendToPlaylist()

This commit is contained in:
casey langen 2017-11-06 01:00:09 -08:00
parent a2fa7cd890
commit aba807b00f
5 changed files with 54 additions and 7 deletions

View File

@ -9,6 +9,7 @@ import android.os.Bundle
import android.os.Handler
import android.support.v4.app.DialogFragment
import android.support.v7.app.AlertDialog
import android.util.Log
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem

View File

@ -30,5 +30,7 @@ interface IDataProvider {
fun getCategoryValues(type: String, filter: String = ""): Observable<List<ICategoryValue>>
fun appendToPlaylist(playlistId: Long, categoryType: String = "", categoryId: Long = -1, filter: String = "", offset: Long = -1): Observable<Boolean>
val state: State
}

View File

@ -159,6 +159,37 @@ class RemoteDataProvider(private val service: WebSocketService) : IDataProvider
.compose(applySchedulers())
}
override fun appendToPlaylist(playlistId: Long, categoryType: String, categoryId: Long, filter: String, offset: Long): Observable<Boolean> {
val type = if (categoryType.isNotEmpty() && categoryId > 0)
Messages.Request.QueryTracksByCategory else Messages.Request.QueryTracks
val suboptions = JSONObject()
if (type == Messages.Request.QueryTracksByCategory) {
suboptions.put(Messages.Key.CATEGORY, categoryType)
suboptions.put(Messages.Key.ID, categoryId)
}
if (filter.isNotEmpty()) {
suboptions.put(Messages.Key.FILTER, filter)
}
val subquery = JSONObject()
.put(Messages.Key.TYPE, type.toString())
.put(Messages.Key.OPTIONS, suboptions)
val message = SocketMessage.Builder
.request(Messages.Request.AppendToPlaylist)
.addOption(Messages.Key.PLAYLIST_ID, playlistId)
.addOption(Messages.Key.OFFSET, offset)
.addOption(Messages.Key.SUBQUERY, subquery)
.build()
return service.observe(message, client)
.flatMap<Boolean> { socketMessage -> isSuccessful(socketMessage) }
.compose(applySchedulers())
}
override fun observeState(): Observable<Pair<IDataProvider.State, IDataProvider.State>> {
return connectionStatePublisher.compose(applySchedulers())
}
@ -259,5 +290,9 @@ class RemoteDataProvider(private val service: WebSocketService) : IDataProvider
private fun toCount(message: SocketMessage): Observable<Int> {
return Observable.just(message.getIntOption(Messages.Key.COUNT, 0))
}
private fun isSuccessful(message: SocketMessage): Observable<Boolean> {
return Observable.just(message.getBooleanOption(Messages.Key.SUCCESS, false))
}
}
}

View File

@ -95,6 +95,11 @@ class Messages {
val FILTER = "filter"
val RELATIVE = "relative"
val PLAYING_CURRENT_TIME = "playing_current_time"
val PLAYLIST_ID = "playlist_id"
val SUBQUERY = "subquery"
val TYPE = "type"
val OPTIONS = "options"
val SUCCESS = "success";
}
}

View File

@ -86,9 +86,11 @@ class WebSocketService constructor(private val context: Context) {
else if (message.what == MESSAGE_SCHEDULE_PING) {
ping()
}
else if (message.what == MESSAGE_PING_EXPIRED) {
removeInternalCallbacks()
disconnect(state == State.Connected || autoReconnect)
else if (message.what == MESSAGE_PING_TIMEOUT) {
if (DISCONNECT_ON_PING_TIMEOUT) {
removeInternalCallbacks()
disconnect(state == State.Connected || autoReconnect)
}
}
result
@ -200,13 +202,13 @@ class WebSocketService constructor(private val context: Context) {
if (state == State.Connected) {
removeInternalCallbacks()
handler.removeMessages(MESSAGE_PING_EXPIRED)
handler.sendEmptyMessageDelayed(MESSAGE_PING_EXPIRED, PING_INTERVAL_MILLIS)
handler.removeMessages(MESSAGE_PING_TIMEOUT)
handler.sendEmptyMessageDelayed(MESSAGE_PING_TIMEOUT, PING_INTERVAL_MILLIS)
val ping = SocketMessage.Builder.request(Messages.Request.Ping).build()
send(ping, INTERNAL_CLIENT) { _: SocketMessage ->
handler.removeMessages(MESSAGE_PING_EXPIRED)
handler.removeMessages(MESSAGE_PING_TIMEOUT)
handler.sendEmptyMessageDelayed(MESSAGE_SCHEDULE_PING, PING_INTERVAL_MILLIS)
}
}
@ -579,7 +581,9 @@ class WebSocketService constructor(private val context: Context) {
private val MESSAGE_REMOVE_OLD_CALLBACKS = MESSAGE_BASE + 2
private val MESSAGE_AUTO_RECONNECT = MESSAGE_BASE + 3
private val MESSAGE_SCHEDULE_PING = MESSAGE_BASE + 4
private val MESSAGE_PING_EXPIRED = MESSAGE_BASE + 5
private val MESSAGE_PING_TIMEOUT = MESSAGE_BASE + 5
private val DISCONNECT_ON_PING_TIMEOUT = true
private val NEXT_ID = AtomicLong(0)