mirror of
https://github.com/owenlejeune/TVTime.git
synced 2025-11-18 01:30:54 -05:00
implement configuration endpoint
This commit is contained in:
@@ -8,12 +8,14 @@ import androidx.compose.runtime.DisposableEffect
|
|||||||
import androidx.compose.ui.platform.LocalContext
|
import androidx.compose.ui.platform.LocalContext
|
||||||
import androidx.compose.ui.platform.LocalFocusManager
|
import androidx.compose.ui.platform.LocalFocusManager
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
import androidx.navigation.compose.rememberNavController
|
import androidx.navigation.compose.rememberNavController
|
||||||
import com.kieronquinn.monetcompat.app.MonetCompatActivity
|
import com.kieronquinn.monetcompat.app.MonetCompatActivity
|
||||||
import com.owenlejeune.tvtime.extensions.rememberWindowSizeClass
|
import com.owenlejeune.tvtime.extensions.rememberWindowSizeClass
|
||||||
import com.owenlejeune.tvtime.ui.navigation.AppNavigationHost
|
import com.owenlejeune.tvtime.ui.navigation.AppNavigationHost
|
||||||
import com.owenlejeune.tvtime.ui.navigation.HomeScreenNavItem
|
import com.owenlejeune.tvtime.ui.navigation.HomeScreenNavItem
|
||||||
import com.owenlejeune.tvtime.ui.theme.TVTimeTheme
|
import com.owenlejeune.tvtime.ui.theme.TVTimeTheme
|
||||||
|
import com.owenlejeune.tvtime.ui.viewmodel.ConfigurationViewModel
|
||||||
import com.owenlejeune.tvtime.utils.KeyboardManager
|
import com.owenlejeune.tvtime.utils.KeyboardManager
|
||||||
import com.owenlejeune.tvtime.utils.SessionManager
|
import com.owenlejeune.tvtime.utils.SessionManager
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
@@ -34,6 +36,9 @@ class MainActivity : MonetCompatActivity() {
|
|||||||
lifecycleScope.launchWhenCreated {
|
lifecycleScope.launchWhenCreated {
|
||||||
monet.awaitMonetReady()
|
monet.awaitMonetReady()
|
||||||
setContent {
|
setContent {
|
||||||
|
val configurationViewModel = viewModel<ConfigurationViewModel>()
|
||||||
|
configurationViewModel.getConfigurations()
|
||||||
|
|
||||||
AppKeyboardFocusManager()
|
AppKeyboardFocusManager()
|
||||||
TVTimeTheme(monetCompat = monet) {
|
TVTimeTheme(monetCompat = monet) {
|
||||||
val windowSize = rememberWindowSizeClass()
|
val windowSize = rememberWindowSizeClass()
|
||||||
|
|||||||
@@ -61,6 +61,10 @@ class TmdbClient: KoinComponent {
|
|||||||
return client.create(AccountApi::class.java)
|
return client.create(AccountApi::class.java)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun createConfigurationService(): ConfigurationApi {
|
||||||
|
return client.create(ConfigurationApi::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
fun createV4AccountService(): AccountV4Api {
|
fun createV4AccountService(): AccountV4Api {
|
||||||
return clientV4.create(AccountV4Api::class.java)
|
return clientV4.create(AccountV4Api::class.java)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package com.owenlejeune.tvtime.api.tmdb.api.v3
|
||||||
|
|
||||||
|
import com.owenlejeune.tvtime.api.tmdb.api.v3.model.ConfigurationCountry
|
||||||
|
import com.owenlejeune.tvtime.api.tmdb.api.v3.model.ConfigurationDetails
|
||||||
|
import com.owenlejeune.tvtime.api.tmdb.api.v3.model.ConfigurationJob
|
||||||
|
import com.owenlejeune.tvtime.api.tmdb.api.v3.model.ConfigurationLanguage
|
||||||
|
import com.owenlejeune.tvtime.api.tmdb.api.v3.model.ConfigurationTimezone
|
||||||
|
import retrofit2.Response
|
||||||
|
import retrofit2.http.GET
|
||||||
|
|
||||||
|
interface ConfigurationApi {
|
||||||
|
|
||||||
|
@GET("configuration")
|
||||||
|
suspend fun getDetailsConfiguration(): Response<ConfigurationDetails>
|
||||||
|
|
||||||
|
@GET("configuration/countries")
|
||||||
|
suspend fun getCountriesConfiguration(): Response<List<ConfigurationCountry>>
|
||||||
|
|
||||||
|
@GET("configuration/jobs")
|
||||||
|
suspend fun getJobsConfiguration(): Response<List<ConfigurationJob>>
|
||||||
|
|
||||||
|
@GET("configuration/languages")
|
||||||
|
suspend fun getLanguagesConfiguration(): Response<List<ConfigurationLanguage>>
|
||||||
|
|
||||||
|
@GET("configuration/primary_translations")
|
||||||
|
suspend fun getPrimaryTranslationsConfiguration(): Response<List<String>>
|
||||||
|
|
||||||
|
@GET("configuration/timezones")
|
||||||
|
suspend fun getTimezonesConfiguration(): Response<List<ConfigurationTimezone>>
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.owenlejeune.tvtime.api.tmdb.api.v3.model
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
|
data class ConfigurationCountry(
|
||||||
|
@SerializedName("iso_3166_1")
|
||||||
|
val iso_3166_1: String,
|
||||||
|
@SerializedName("english_name")
|
||||||
|
val englishName: String,
|
||||||
|
@SerializedName("native_name")
|
||||||
|
val nativeName: String
|
||||||
|
)
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package com.owenlejeune.tvtime.api.tmdb.api.v3.model
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
|
data class ConfigurationDetails(
|
||||||
|
@SerializedName("images")
|
||||||
|
val images: ImagesConfiguration
|
||||||
|
) {
|
||||||
|
companion object {
|
||||||
|
val Empty = ConfigurationDetails(ImagesConfiguration.Empty)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
data class ImagesConfiguration(
|
||||||
|
@SerializedName("base_url")
|
||||||
|
val baseUrl: String,
|
||||||
|
@SerializedName("secure_base_url")
|
||||||
|
val secureBaseUrl: String,
|
||||||
|
@SerializedName("backdrop_sizes")
|
||||||
|
val backdropSizes: List<String>,
|
||||||
|
@SerializedName("logo_sizes")
|
||||||
|
val logoSizes: List<String>,
|
||||||
|
@SerializedName("poster_sizes")
|
||||||
|
val posterSizes: List<String>,
|
||||||
|
@SerializedName("profile_sizes")
|
||||||
|
val profileSizes: List<String>,
|
||||||
|
@SerializedName("still_sizes")
|
||||||
|
val stillSizes: List<String>
|
||||||
|
) {
|
||||||
|
companion object {
|
||||||
|
val Empty = ImagesConfiguration("", "", emptyList(), emptyList(), emptyList(), emptyList(), emptyList())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.owenlejeune.tvtime.api.tmdb.api.v3.model
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
|
data class ConfigurationJob(
|
||||||
|
@SerializedName("department")
|
||||||
|
val department: String,
|
||||||
|
@SerializedName("jobs")
|
||||||
|
val jobs: List<String>
|
||||||
|
)
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.owenlejeune.tvtime.api.tmdb.api.v3.model
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
|
data class ConfigurationLanguage(
|
||||||
|
@SerializedName("iso_639_1")
|
||||||
|
val iso_639_1: String,
|
||||||
|
@SerializedName("english_name")
|
||||||
|
val englishName: String,
|
||||||
|
@SerializedName("name")
|
||||||
|
val name: String
|
||||||
|
)
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
package com.owenlejeune.tvtime.api.tmdb.api.v3.model
|
||||||
|
|
||||||
|
import com.google.gson.annotations.SerializedName
|
||||||
|
|
||||||
|
data class ConfigurationTimezone(
|
||||||
|
@SerializedName("iso_3166_1")
|
||||||
|
val iso_3166_1: String,
|
||||||
|
@SerializedName("zones")
|
||||||
|
val zones: List<String>
|
||||||
|
)
|
||||||
@@ -33,6 +33,7 @@ val networkModule = module {
|
|||||||
single { get<TmdbClient>().createPeopleService() }
|
single { get<TmdbClient>().createPeopleService() }
|
||||||
single { get<TmdbClient>().createSearchService() }
|
single { get<TmdbClient>().createSearchService() }
|
||||||
single { get<TmdbClient>().createTvService() }
|
single { get<TmdbClient>().createTvService() }
|
||||||
|
single { get<TmdbClient>().createConfigurationService() }
|
||||||
|
|
||||||
single { AccountService() }
|
single { AccountService() }
|
||||||
single { AccountV4Service() }
|
single { AccountV4Service() }
|
||||||
|
|||||||
@@ -0,0 +1,118 @@
|
|||||||
|
package com.owenlejeune.tvtime.ui.viewmodel
|
||||||
|
|
||||||
|
import android.util.Log
|
||||||
|
import androidx.compose.runtime.mutableStateListOf
|
||||||
|
import androidx.compose.runtime.mutableStateOf
|
||||||
|
import androidx.lifecycle.ViewModel
|
||||||
|
import com.owenlejeune.tvtime.api.tmdb.api.v3.ConfigurationApi
|
||||||
|
import com.owenlejeune.tvtime.api.tmdb.api.v3.model.ConfigurationCountry
|
||||||
|
import com.owenlejeune.tvtime.api.tmdb.api.v3.model.ConfigurationDetails
|
||||||
|
import com.owenlejeune.tvtime.api.tmdb.api.v3.model.ConfigurationJob
|
||||||
|
import com.owenlejeune.tvtime.api.tmdb.api.v3.model.ConfigurationLanguage
|
||||||
|
import com.owenlejeune.tvtime.api.tmdb.api.v3.model.ConfigurationTimezone
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
import org.koin.core.component.KoinComponent
|
||||||
|
import org.koin.core.component.inject
|
||||||
|
import retrofit2.Response
|
||||||
|
|
||||||
|
class ConfigurationViewModel: ViewModel(), KoinComponent {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
private const val TAG = "ConfigurationViewModel"
|
||||||
|
}
|
||||||
|
|
||||||
|
private val service: ConfigurationApi by inject()
|
||||||
|
|
||||||
|
val detailsConfiguration = mutableStateOf(ConfigurationDetails.Empty)
|
||||||
|
val countriesConfiguration = mutableStateListOf<ConfigurationCountry>()
|
||||||
|
val jobsConfiguration = mutableStateListOf<ConfigurationJob>()
|
||||||
|
val languagesConfiguration = mutableStateListOf<ConfigurationLanguage>()
|
||||||
|
val primaryTranslationsConfiguration = mutableStateListOf<String>()
|
||||||
|
val timezonesConfiguration = mutableStateListOf<ConfigurationTimezone>()
|
||||||
|
|
||||||
|
fun getConfigurations() {
|
||||||
|
getDetailsConfiguration()
|
||||||
|
getCountriesConfiguration()
|
||||||
|
getJobsConfiguration()
|
||||||
|
getLanguagesConfiguration()
|
||||||
|
getPrimaryTranslationsConfiguration()
|
||||||
|
getTimezonesConfiguration()
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getDetailsConfiguration() {
|
||||||
|
getConfiguration(
|
||||||
|
{ service.getDetailsConfiguration() },
|
||||||
|
{ detailsConfiguration.value = it }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getCountriesConfiguration() {
|
||||||
|
getConfiguration(
|
||||||
|
{ service.getCountriesConfiguration() },
|
||||||
|
{
|
||||||
|
countriesConfiguration.clear()
|
||||||
|
countriesConfiguration.addAll(it)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getJobsConfiguration() {
|
||||||
|
getConfiguration(
|
||||||
|
{ service.getJobsConfiguration() },
|
||||||
|
{
|
||||||
|
jobsConfiguration.clear()
|
||||||
|
jobsConfiguration.addAll(it)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getLanguagesConfiguration() {
|
||||||
|
getConfiguration(
|
||||||
|
{ service.getLanguagesConfiguration() },
|
||||||
|
{
|
||||||
|
languagesConfiguration.clear()
|
||||||
|
languagesConfiguration.addAll(it)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getPrimaryTranslationsConfiguration() {
|
||||||
|
getConfiguration(
|
||||||
|
{ service.getPrimaryTranslationsConfiguration() },
|
||||||
|
{
|
||||||
|
primaryTranslationsConfiguration.clear()
|
||||||
|
primaryTranslationsConfiguration.addAll(it)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun getTimezonesConfiguration() {
|
||||||
|
getConfiguration(
|
||||||
|
{ service.getTimezonesConfiguration() },
|
||||||
|
{
|
||||||
|
timezonesConfiguration.clear()
|
||||||
|
timezonesConfiguration.addAll(it)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun <T> getConfiguration(
|
||||||
|
fetcher: suspend () -> Response<T>,
|
||||||
|
bodyHandler: (T) -> Unit
|
||||||
|
) {
|
||||||
|
CoroutineScope(Dispatchers.IO).launch {
|
||||||
|
val response = fetcher()
|
||||||
|
if (response.isSuccessful) {
|
||||||
|
response.body()?.let {
|
||||||
|
Log.d(TAG, "Successfully got configuration: $it")
|
||||||
|
bodyHandler(it)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.e(TAG, "Issue getting configuration")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user