Add .ifNotNull extension function as a more readable alternative to ?.let
This commit is contained in:
parent
8830d76497
commit
bb36116d70
|
@ -6,6 +6,7 @@ package org.moire.ultrasonic.domain
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import kotlin.LazyThreadSafetyMode.NONE
|
import kotlin.LazyThreadSafetyMode.NONE
|
||||||
import org.moire.ultrasonic.api.subsonic.models.Playlist as APIPlaylist
|
import org.moire.ultrasonic.api.subsonic.models.Playlist as APIPlaylist
|
||||||
|
import org.moire.ultrasonic.util.Util.ifNotNull
|
||||||
|
|
||||||
internal val playlistDateFormat by lazy(NONE) { SimpleDateFormat.getInstance() }
|
internal val playlistDateFormat by lazy(NONE) { SimpleDateFormat.getInstance() }
|
||||||
|
|
||||||
|
@ -17,7 +18,7 @@ fun APIPlaylist.toMusicDirectoryDomainEntity(): MusicDirectory = MusicDirectory(
|
||||||
fun APIPlaylist.toDomainEntity(): Playlist = Playlist(
|
fun APIPlaylist.toDomainEntity(): Playlist = Playlist(
|
||||||
this.id, this.name, this.owner,
|
this.id, this.name, this.owner,
|
||||||
this.comment, this.songCount.toString(),
|
this.comment, this.songCount.toString(),
|
||||||
this.created?.let { playlistDateFormat.format(it.time) } ?: "",
|
this.created.ifNotNull { playlistDateFormat.format(it.time) } ?: "",
|
||||||
public
|
public
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,7 @@ package org.moire.ultrasonic.domain
|
||||||
import java.text.SimpleDateFormat
|
import java.text.SimpleDateFormat
|
||||||
import kotlin.LazyThreadSafetyMode.NONE
|
import kotlin.LazyThreadSafetyMode.NONE
|
||||||
import org.moire.ultrasonic.api.subsonic.models.Share as APIShare
|
import org.moire.ultrasonic.api.subsonic.models.Share as APIShare
|
||||||
|
import org.moire.ultrasonic.util.Util.ifNotNull
|
||||||
|
|
||||||
internal val shareTimeFormat by lazy(NONE) { SimpleDateFormat.getInstance() }
|
internal val shareTimeFormat by lazy(NONE) { SimpleDateFormat.getInstance() }
|
||||||
|
|
||||||
|
@ -13,11 +14,11 @@ fun List<APIShare>.toDomainEntitiesList(): List<Share> = this.map {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun APIShare.toDomainEntity(): Share = Share(
|
fun APIShare.toDomainEntity(): Share = Share(
|
||||||
created = this@toDomainEntity.created?.let { shareTimeFormat.format(it.time) },
|
created = this@toDomainEntity.created.ifNotNull { shareTimeFormat.format(it.time) },
|
||||||
description = this@toDomainEntity.description,
|
description = this@toDomainEntity.description,
|
||||||
expires = this@toDomainEntity.expires?.let { shareTimeFormat.format(it.time) },
|
expires = this@toDomainEntity.expires.ifNotNull { shareTimeFormat.format(it.time) },
|
||||||
id = this@toDomainEntity.id,
|
id = this@toDomainEntity.id,
|
||||||
lastVisited = this@toDomainEntity.lastVisited?.let { shareTimeFormat.format(it.time) },
|
lastVisited = this@toDomainEntity.lastVisited.ifNotNull { shareTimeFormat.format(it.time) },
|
||||||
url = this@toDomainEntity.url,
|
url = this@toDomainEntity.url,
|
||||||
username = this@toDomainEntity.username,
|
username = this@toDomainEntity.username,
|
||||||
visitCount = this@toDomainEntity.visitCount.toLong(),
|
visitCount = this@toDomainEntity.visitCount.toLong(),
|
||||||
|
|
|
@ -211,7 +211,7 @@ class TrackCollectionFragment : Fragment() {
|
||||||
|
|
||||||
val handler = CoroutineExceptionHandler { _, exception ->
|
val handler = CoroutineExceptionHandler { _, exception ->
|
||||||
Handler(Looper.getMainLooper()).post {
|
Handler(Looper.getMainLooper()).post {
|
||||||
context?.let { CommunicationErrorHandler.handleError(exception, it) }
|
CommunicationErrorHandler.handleError(exception, context)
|
||||||
}
|
}
|
||||||
refreshAlbumListView!!.isRefreshing = false
|
refreshAlbumListView!!.isRefreshing = false
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,9 +39,11 @@ import timber.log.Timber
|
||||||
*/
|
*/
|
||||||
class CommunicationErrorHandler {
|
class CommunicationErrorHandler {
|
||||||
companion object {
|
companion object {
|
||||||
fun handleError(error: Throwable?, context: Context) {
|
fun handleError(error: Throwable?, context: Context?) {
|
||||||
Timber.w(error)
|
Timber.w(error)
|
||||||
|
|
||||||
|
if (context == null) return
|
||||||
|
|
||||||
AlertDialog.Builder(context)
|
AlertDialog.Builder(context)
|
||||||
.setIcon(android.R.drawable.ic_dialog_alert)
|
.setIcon(android.R.drawable.ic_dialog_alert)
|
||||||
.setTitle(R.string.error_label)
|
.setTitle(R.string.error_label)
|
||||||
|
|
|
@ -22,6 +22,7 @@ import org.moire.ultrasonic.domain.PlayerState
|
||||||
import org.moire.ultrasonic.util.CacheCleaner
|
import org.moire.ultrasonic.util.CacheCleaner
|
||||||
import org.moire.ultrasonic.util.Constants
|
import org.moire.ultrasonic.util.Constants
|
||||||
import org.moire.ultrasonic.util.Settings
|
import org.moire.ultrasonic.util.Settings
|
||||||
|
import org.moire.ultrasonic.util.Util.ifNotNull
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -113,7 +114,7 @@ class MediaPlayerLifecycleSupport : KoinComponent {
|
||||||
if (intentAction == Constants.CMD_PROCESS_KEYCODE) {
|
if (intentAction == Constants.CMD_PROCESS_KEYCODE) {
|
||||||
if (intent.extras != null) {
|
if (intent.extras != null) {
|
||||||
val event = intent.extras!![Intent.EXTRA_KEY_EVENT] as KeyEvent?
|
val event = intent.extras!![Intent.EXTRA_KEY_EVENT] as KeyEvent?
|
||||||
event?.let { handleKeyEvent(it) }
|
event.ifNotNull { handleKeyEvent(it) }
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
handleUltrasonicIntent(intentAction)
|
handleUltrasonicIntent(intentAction)
|
||||||
|
|
|
@ -26,6 +26,7 @@ import org.moire.ultrasonic.util.Settings
|
||||||
import org.moire.ultrasonic.util.ShareDetails
|
import org.moire.ultrasonic.util.ShareDetails
|
||||||
import org.moire.ultrasonic.util.TimeSpan
|
import org.moire.ultrasonic.util.TimeSpan
|
||||||
import org.moire.ultrasonic.util.TimeSpanPicker
|
import org.moire.ultrasonic.util.TimeSpanPicker
|
||||||
|
import org.moire.ultrasonic.util.Util.ifNotNull
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class handles sharing items in the media library
|
* This class handles sharing items in the media library
|
||||||
|
@ -79,7 +80,7 @@ class ShareHandler(val context: Context) {
|
||||||
|
|
||||||
if (!shareDetails.ShareOnServer && shareDetails.Entries.size == 1) return null
|
if (!shareDetails.ShareOnServer && shareDetails.Entries.size == 1) return null
|
||||||
if (shareDetails.Entries.isEmpty()) {
|
if (shareDetails.Entries.isEmpty()) {
|
||||||
fragment.arguments?.getString(Constants.INTENT_EXTRA_NAME_ID)?.let {
|
fragment.arguments?.getString(Constants.INTENT_EXTRA_NAME_ID).ifNotNull {
|
||||||
ids.add(it)
|
ids.add(it)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -28,6 +28,7 @@ import org.moire.ultrasonic.receiver.MediaButtonIntentReceiver
|
||||||
import org.moire.ultrasonic.service.DownloadFile
|
import org.moire.ultrasonic.service.DownloadFile
|
||||||
import org.moire.ultrasonic.service.RxBus
|
import org.moire.ultrasonic.service.RxBus
|
||||||
import org.moire.ultrasonic.service.plusAssign
|
import org.moire.ultrasonic.service.plusAssign
|
||||||
|
import org.moire.ultrasonic.util.Util.ifNotNull
|
||||||
import timber.log.Timber
|
import timber.log.Timber
|
||||||
|
|
||||||
private const val INTENT_CODE_MEDIA_BUTTON = 161
|
private const val INTENT_CODE_MEDIA_BUTTON = 161
|
||||||
|
@ -150,7 +151,7 @@ class MediaSessionHandler : KoinComponent {
|
||||||
// This probably won't be necessary once we implement more
|
// This probably won't be necessary once we implement more
|
||||||
// of the modern media APIs, like the MediaController etc.
|
// of the modern media APIs, like the MediaController etc.
|
||||||
val event = mediaButtonEvent.extras!!["android.intent.extra.KEY_EVENT"] as KeyEvent?
|
val event = mediaButtonEvent.extras!!["android.intent.extra.KEY_EVENT"] as KeyEvent?
|
||||||
event?.let { RxBus.mediaButtonEventPublisher.onNext(it) }
|
event.ifNotNull { RxBus.mediaButtonEventPublisher.onNext(it) }
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -257,13 +258,10 @@ class MediaSessionHandler : KoinComponent {
|
||||||
|
|
||||||
val index = cachedPlaylist?.indexOf(currentPlaying)
|
val index = cachedPlaylist?.indexOf(currentPlaying)
|
||||||
cachedPlayingIndex = if (index == null || index < 0) null else index.toLong()
|
cachedPlayingIndex = if (index == null || index < 0) null else index.toLong()
|
||||||
cachedPlaylist?.let { setMediaSessionQueue(it) }
|
cachedPlaylist.ifNotNull { setMediaSessionQueue(it) }
|
||||||
|
|
||||||
if (
|
if (cachedPlaylist != null && !Settings.shouldDisableNowPlayingListSending)
|
||||||
cachedPlayingIndex != null && cachedPlaylist != null &&
|
cachedPlayingIndex.ifNotNull { playbackStateBuilder.setActiveQueueItemId(it) }
|
||||||
!Settings.shouldDisableNowPlayingListSending
|
|
||||||
)
|
|
||||||
cachedPlayingIndex?.let { playbackStateBuilder.setActiveQueueItemId(it) }
|
|
||||||
|
|
||||||
// Save the playback state
|
// Save the playback state
|
||||||
mediaSession?.setPlaybackState(playbackStateBuilder.build())
|
mediaSession?.setPlaybackState(playbackStateBuilder.build())
|
||||||
|
@ -296,11 +294,8 @@ class MediaSessionHandler : KoinComponent {
|
||||||
playbackStateBuilder.setState(playbackState!!, cachedPosition, 1.0f)
|
playbackStateBuilder.setState(playbackState!!, cachedPosition, 1.0f)
|
||||||
playbackStateBuilder.setActions(playbackActions!!)
|
playbackStateBuilder.setActions(playbackActions!!)
|
||||||
|
|
||||||
if (
|
if (cachedPlaylist != null && !Settings.shouldDisableNowPlayingListSending)
|
||||||
cachedPlayingIndex != null && cachedPlaylist != null &&
|
cachedPlayingIndex.ifNotNull { playbackStateBuilder.setActiveQueueItemId(it) }
|
||||||
!Settings.shouldDisableNowPlayingListSending
|
|
||||||
)
|
|
||||||
playbackStateBuilder.setActiveQueueItemId(cachedPlayingIndex!!)
|
|
||||||
|
|
||||||
mediaSession?.setPlaybackState(playbackStateBuilder.build())
|
mediaSession?.setPlaybackState(playbackStateBuilder.build())
|
||||||
}
|
}
|
||||||
|
|
|
@ -538,7 +538,6 @@ object Util {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Broadcasts the given song info as the new song being played.
|
* Broadcasts the given song info as the new song being played.
|
||||||
*/
|
*/
|
||||||
fun broadcastNewTrackInfo(context: Context, song: MusicDirectory.Entry?) {
|
fun broadcastNewTrackInfo(context: Context, song: MusicDirectory.Entry?) {
|
||||||
|
@ -957,6 +956,14 @@ object Util {
|
||||||
return context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
return context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Executes the given block if this is not null.
|
||||||
|
* @return: the return of the block, or null if this is null
|
||||||
|
*/
|
||||||
|
fun <T : Any, R> T?.ifNotNull(block: (T) -> R): R? {
|
||||||
|
return this?.let(block)
|
||||||
|
}
|
||||||
|
|
||||||
data class NetworkInfo(
|
data class NetworkInfo(
|
||||||
var connected: Boolean = false,
|
var connected: Boolean = false,
|
||||||
var unmetered: Boolean = false
|
var unmetered: Boolean = false
|
||||||
|
|
Loading…
Reference in New Issue