From 9e7a09ee9acb900a249b1ab4f8826b5b9d889ca2 Mon Sep 17 00:00:00 2001 From: jozef Date: Mon, 25 Oct 2021 22:02:44 +0200 Subject: [PATCH] Small changes --- .../kotlin/online/hudacek/fxradio/Config.kt | 2 +- .../hudacek/fxradio/api/ApiServiceProvider.kt | 18 +++++++------- .../http/provider/DefaultClientProvider.kt | 8 ------- .../api/http/provider/HttpClientProvider.kt | 7 ++++++ .../fxradio/media/MediaPlayerFactory.kt | 24 ++++++++++++------- .../hudacek/fxradio/media/StreamMetaData.kt | 2 +- .../hudacek/fxradio/ui/menu/PlayerMenu.kt | 3 +-- .../fxradio/viewmodel/PlayerViewModel.kt | 2 +- 8 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/main/kotlin/online/hudacek/fxradio/Config.kt b/src/main/kotlin/online/hudacek/fxradio/Config.kt index 32933962..19869eeb 100644 --- a/src/main/kotlin/online/hudacek/fxradio/Config.kt +++ b/src/main/kotlin/online/hudacek/fxradio/Config.kt @@ -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" diff --git a/src/main/kotlin/online/hudacek/fxradio/api/ApiServiceProvider.kt b/src/main/kotlin/online/hudacek/fxradio/api/ApiServiceProvider.kt index 6499207d..dbc63d24 100644 --- a/src/main/kotlin/online/hudacek/fxradio/api/ApiServiceProvider.kt +++ b/src/main/kotlin/online/hudacek/fxradio/api/ApiServiceProvider.kt @@ -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] diff --git a/src/main/kotlin/online/hudacek/fxradio/api/http/provider/DefaultClientProvider.kt b/src/main/kotlin/online/hudacek/fxradio/api/http/provider/DefaultClientProvider.kt index 186fc06c..653d996e 100644 --- a/src/main/kotlin/online/hudacek/fxradio/api/http/provider/DefaultClientProvider.kt +++ b/src/main/kotlin/online/hudacek/fxradio/api/http/provider/DefaultClientProvider.kt @@ -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() - } } \ No newline at end of file diff --git a/src/main/kotlin/online/hudacek/fxradio/api/http/provider/HttpClientProvider.kt b/src/main/kotlin/online/hudacek/fxradio/api/http/provider/HttpClientProvider.kt index f2c4c080..88d9118c 100644 --- a/src/main/kotlin/online/hudacek/fxradio/api/http/provider/HttpClientProvider.kt +++ b/src/main/kotlin/online/hudacek/fxradio/api/http/provider/HttpClientProvider.kt @@ -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 @@ -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() } diff --git a/src/main/kotlin/online/hudacek/fxradio/media/MediaPlayerFactory.kt b/src/main/kotlin/online/hudacek/fxradio/media/MediaPlayerFactory.kt index a6cfcc7d..c329e310 100644 --- a/src/main/kotlin/online/hudacek/fxradio/media/MediaPlayerFactory.kt +++ b/src/main/kotlin/online/hudacek/fxradio/media/MediaPlayerFactory.kt @@ -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() } @@ -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) } \ No newline at end of file diff --git a/src/main/kotlin/online/hudacek/fxradio/media/StreamMetaData.kt b/src/main/kotlin/online/hudacek/fxradio/media/StreamMetaData.kt index cb3d6166..26ad3827 100644 --- a/src/main/kotlin/online/hudacek/fxradio/media/StreamMetaData.kt +++ b/src/main/kotlin/online/hudacek/fxradio/media/StreamMetaData.kt @@ -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) diff --git a/src/main/kotlin/online/hudacek/fxradio/ui/menu/PlayerMenu.kt b/src/main/kotlin/online/hudacek/fxradio/ui/menu/PlayerMenu.kt index 86a4e944..b5ded879 100644 --- a/src/main/kotlin/online/hudacek/fxradio/ui/menu/PlayerMenu.kt +++ b/src/main/kotlin/online/hudacek/fxradio/ui/menu/PlayerMenu.kt @@ -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() } } diff --git a/src/main/kotlin/online/hudacek/fxradio/viewmodel/PlayerViewModel.kt b/src/main/kotlin/online/hudacek/fxradio/viewmodel/PlayerViewModel.kt index 287faef3..906aed4e 100644 --- a/src/main/kotlin/online/hudacek/fxradio/viewmodel/PlayerViewModel.kt +++ b/src/main/kotlin/online/hudacek/fxradio/viewmodel/PlayerViewModel.kt @@ -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)