Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Joseph5610 committed Oct 25, 2021
1 parent 637d55e commit 9e7a09e
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/main/kotlin/online/hudacek/fxradio/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ object Config {
object Paths {
private val appName = FxRadio.appName.toLowerCase()

val baseAppPath = System.getProperty("user.home") + "/.$appName/"
val baseAppPath = System.getProperty("user.home") + "/.$appName"
val confDirPath = "$baseAppPath/conf"
val cacheDirPath = "$baseAppPath/cache"
val dbPath = "$baseAppPath/$appName.db"
Expand Down
18 changes: 10 additions & 8 deletions src/main/kotlin/online/hudacek/fxradio/api/ApiServiceProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ import retrofit2.converter.gson.GsonConverterFactory
/**
* Provides Retrofit instance for [baseUrl]
*/
class ApiServiceProvider(private val baseUrl: String,
private val httpClientProvider: HttpClientProvider = DefaultClientProvider()) {
class ApiServiceProvider(
private val baseUrl: String,
private val httpClientProvider: HttpClientProvider = DefaultClientProvider()
) {

/**
* Retrofit client instance for [baseUrl] with custom [httpClient]
* Retrofit client instance for [baseUrl] with custom [httpClientProvider]
*/
val retrofit: Retrofit
get() = Retrofit.Builder()
.addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(baseUrl)
.client(httpClientProvider.client)
.build()
.addCallAdapterFactory(RxJava2CallAdapterFactory.createAsync())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(baseUrl)
.client(httpClientProvider.client)
.build()

/**
* Constructs Retrofit service class of type [T]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,4 @@ open class DefaultClientProvider : HttpClientProvider {
}
}.build()

/**
* Correctly kill all OkHttpClient threads
*/
override fun close() {
logger.info { "Closing OkHttpClient..." }
logger.debug { "Idle/All: ${connectionPool.idleConnectionCount()}/${connectionPool.connectionCount()}" }
super.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@

package online.hudacek.fxradio.api.http.provider

import mu.KotlinLogging
import okhttp3.Interceptor
import okhttp3.OkHttpClient

private val logger = KotlinLogging.logger {}

interface HttpClientProvider {

val client: OkHttpClient
Expand All @@ -29,6 +32,10 @@ interface HttpClientProvider {
* Default implementation of closing the OkHttpClient
*/
fun close() {
logger.info { "Closing OkHttpClient..." }
logger.debug {
"Idle/All: ${client.connectionPool().idleConnectionCount()}/${client.connectionPool().connectionCount()}"
}
client.dispatcher().executorService().shutdownNow()
client.connectionPool().evictAll()
}
Expand Down
24 changes: 15 additions & 9 deletions src/main/kotlin/online/hudacek/fxradio/media/MediaPlayerFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,34 @@ package online.hudacek.fxradio.media
import mu.KotlinLogging
import online.hudacek.fxradio.media.player.humble.HumblePlayerImpl
import online.hudacek.fxradio.media.player.vlc.VLCPlayerImpl
import online.hudacek.fxradio.util.Properties
import online.hudacek.fxradio.util.value

private val logger = KotlinLogging.logger {}

object MediaPlayerFactory {

private val defaultPlayerType = MediaPlayer.Type.VLC

/**
* Create MediaPlayer from String [playerType] identifier
* Create MediaPlayer
*/
fun create(playerType: String): MediaPlayer {
logger.debug { "MediaPlayer $playerType is initializing..." }
return when (playerType.asPlayerType()) {
fun create(): MediaPlayer {
val player = Properties.Player.value(defaultPlayerType.name)
logger.debug { "MediaPlayer $player is initializing..." }
return when (player.asPlayerType()) {
MediaPlayer.Type.VLC -> tryLoadVLCPlayer()
MediaPlayer.Type.Humble -> HumblePlayerImpl()
}
}

/**
* Create opposite playerType than provided in [playerType] var
* Toggle MediaPlayer
*/
fun toggle(playerType: MediaPlayer.Type): MediaPlayer {
logger.debug { "MediaPlayer $playerType is toggling..." }
return when (playerType) {
fun toggle(): MediaPlayer {
logger.debug { "MediaPlayer toggling..." }
val currentPlayer = Properties.Player.value(defaultPlayerType.name)
return when (currentPlayer.asPlayerType()) {
MediaPlayer.Type.Humble -> tryLoadVLCPlayer()
MediaPlayer.Type.VLC -> HumblePlayerImpl()
}
Expand All @@ -63,5 +69,5 @@ object MediaPlayerFactory {
MediaPlayer.Type.valueOf(this)
}.onFailure {
logger.error(it) { "This playerType is invalid. Using Humble MediaPlayer as fallback!" }
}.getOrDefault(MediaPlayer.Type.Humble)
}.getOrDefault(defaultPlayerType)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
package online.hudacek.fxradio.media

/**
* Data class for meta data change event holding current station name / stream title
* Data class for metadata change event holding current station name / stream title
*/
data class StreamMetaData(val stationName: String, val nowPlaying: String)
3 changes: 1 addition & 2 deletions src/main/kotlin/online/hudacek/fxradio/ui/menu/PlayerMenu.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ class PlayerMenu : BaseMenu("menu.player.controls") {
action {
playerViewModel.stateProperty.value = PlayerState.Stopped
playerViewModel.mediaPlayerProperty.value?.release()
playerViewModel.mediaPlayerProperty.value =
MediaPlayerFactory.toggle(playerViewModel.mediaPlayerProperty.value.playerType)
playerViewModel.mediaPlayerProperty.value = MediaPlayerFactory.toggle()
playerViewModel.commit()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class Player(station: Station = Station.dummy,
animate: Boolean = Properties.PlayerAnimated.value(true),
volume: Double = Properties.Volume.value(0.0),
trackName: String = "",
mediaPlayer: MediaPlayer = MediaPlayerFactory.create(Properties.Player.value("VLC"))) {
mediaPlayer: MediaPlayer = MediaPlayerFactory.create()) {
var animate: Boolean by property(animate)
var station: Station by property(station)
var volume: Double by property(volume)
Expand Down

0 comments on commit 9e7a09e

Please sign in to comment.