From 0c59f4a451f3fc977888948627c17e444f6f82fc Mon Sep 17 00:00:00 2001 From: Victor Kabata Date: Thu, 28 Dec 2023 20:46:15 +0300 Subject: [PATCH 01/11] Update multiplatform Settings from v0.8.1 to v1.1.1 --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 374b5852..446b14dc 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -20,7 +20,7 @@ kotlinxSerializationJson = "1.6.0" kotlinxDateTime = "0.4.1" napier = "2.6.1" ktor = "2.3.6" -multiplatformSettings = "0.8.1" +multiplatformSettings = "1.1.1" kmpNativeCoroutines = "0.12.1-new-mm" buildKonfig = "0.13.3" kover = "0.6.1" From faade8ce595e478b3e7dcc5961a2aec1241f31a4 Mon Sep 17 00:00:00 2001 From: Victor Kabata Date: Thu, 28 Dec 2023 21:13:58 +0300 Subject: [PATCH 02/11] Update multiplatform settings wrappers utils --- .../com/vickbt/shared/utils/MultiplatformSettingsWrapper.kt | 6 +++--- .../com/vickbt/shared/utils/MultiplatformSettingsWrapper.kt | 4 ++-- .../com/vickbt/shared/utils/MultiplatformSettingsWrapper.kt | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/shared/src/androidMain/kotlin/com/vickbt/shared/utils/MultiplatformSettingsWrapper.kt b/shared/src/androidMain/kotlin/com/vickbt/shared/utils/MultiplatformSettingsWrapper.kt index a09b9860..27710d95 100644 --- a/shared/src/androidMain/kotlin/com/vickbt/shared/utils/MultiplatformSettingsWrapper.kt +++ b/shared/src/androidMain/kotlin/com/vickbt/shared/utils/MultiplatformSettingsWrapper.kt @@ -1,15 +1,15 @@ package com.vickbt.shared.utils import android.content.Context -import com.russhwolf.settings.AndroidSettings import com.russhwolf.settings.ObservableSettings +import com.russhwolf.settings.SharedPreferencesSettings actual class MultiplatformSettingsWrapper { - val context = ContextUtils.context + private val context = ContextUtils.context actual fun createSettings(): ObservableSettings { val sharedPreferences = context.getSharedPreferences("notflix_preferences", Context.MODE_PRIVATE) - return AndroidSettings(delegate = sharedPreferences) + return SharedPreferencesSettings(delegate = sharedPreferences) } } diff --git a/shared/src/iosMain/kotlin/com/vickbt/shared/utils/MultiplatformSettingsWrapper.kt b/shared/src/iosMain/kotlin/com/vickbt/shared/utils/MultiplatformSettingsWrapper.kt index 750db6e9..e1055451 100644 --- a/shared/src/iosMain/kotlin/com/vickbt/shared/utils/MultiplatformSettingsWrapper.kt +++ b/shared/src/iosMain/kotlin/com/vickbt/shared/utils/MultiplatformSettingsWrapper.kt @@ -1,12 +1,12 @@ package com.vickbt.shared.utils -import com.russhwolf.settings.AppleSettings +import com.russhwolf.settings.NSUserDefaultsSettings import com.russhwolf.settings.ObservableSettings import platform.Foundation.NSUserDefaults actual class MultiplatformSettingsWrapper { actual fun createSettings(): ObservableSettings { val nsUserDefault = NSUserDefaults.standardUserDefaults - return AppleSettings(delegate = nsUserDefault) + return NSUserDefaultsSettings(delegate = nsUserDefault) } } diff --git a/shared/src/jvmMain/kotlin/com/vickbt/shared/utils/MultiplatformSettingsWrapper.kt b/shared/src/jvmMain/kotlin/com/vickbt/shared/utils/MultiplatformSettingsWrapper.kt index f4c42585..3343f98d 100644 --- a/shared/src/jvmMain/kotlin/com/vickbt/shared/utils/MultiplatformSettingsWrapper.kt +++ b/shared/src/jvmMain/kotlin/com/vickbt/shared/utils/MultiplatformSettingsWrapper.kt @@ -1,12 +1,12 @@ package com.vickbt.shared.utils -import com.russhwolf.settings.JvmPreferencesSettings import com.russhwolf.settings.ObservableSettings +import com.russhwolf.settings.PreferencesSettings import java.util.prefs.Preferences actual class MultiplatformSettingsWrapper { actual fun createSettings(): ObservableSettings { val preferences: Preferences = Preferences.userRoot() - return JvmPreferencesSettings(delegate = preferences) + return PreferencesSettings(delegate = preferences) } } From 2cdbddbef9d5736404520c75fa5e49dccc4df8f8 Mon Sep 17 00:00:00 2001 From: Victor Kabata Date: Wed, 3 Jan 2024 14:47:04 +0300 Subject: [PATCH 03/11] Update multiplatform setting from v0.8.1 to v1.1.1 --- gradle/libs.versions.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 303aa651..44aa5990 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -21,7 +21,7 @@ kotlinxDateTime = "0.5.0" napier = "2.6.1" ktor = "2.3.7" sqlDelight = "2.0.1" -multiplatformSettings = "0.8.1" +multiplatformSettings = "1.1.1" kmpNativeCoroutines = "0.12.1-new-mm" buildKonfig = "0.13.3" kover = "0.6.1" From 32366ac83e8d80e6999659794947c1b977961485 Mon Sep 17 00:00:00 2001 From: Victor Kabata Date: Wed, 3 Jan 2024 14:50:16 +0300 Subject: [PATCH 04/11] Renamed NetworkResultState class to ResultState --- .../datasources/MovieDetailsRepositoryImpl.kt | 12 +++++----- .../data/datasources/MoviesRepositoryImpl.kt | 12 +++++----- .../shared/data/network/utils/SafeApiCall.kt | 18 +++++++-------- .../repositories/MovieDetailsRepository.kt | 8 +++---- .../domain/repositories/MoviesRepository.kt | 12 +++++----- .../vickbt/shared/utils/NetworkResultState.kt | 22 ------------------- .../com/vickbt/shared/utils/ResultState.kt | 22 +++++++++++++++++++ 7 files changed, 53 insertions(+), 53 deletions(-) delete mode 100644 shared/src/commonMain/kotlin/com/vickbt/shared/utils/NetworkResultState.kt create mode 100644 shared/src/commonMain/kotlin/com/vickbt/shared/utils/ResultState.kt diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/MovieDetailsRepositoryImpl.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/MovieDetailsRepositoryImpl.kt index d2b77b47..a356f257 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/MovieDetailsRepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/MovieDetailsRepositoryImpl.kt @@ -10,7 +10,7 @@ import com.vickbt.shared.domain.models.Cast import com.vickbt.shared.domain.models.Movie import com.vickbt.shared.domain.models.MovieDetails import com.vickbt.shared.domain.repositories.MovieDetailsRepository -import com.vickbt.shared.utils.NetworkResultState +import com.vickbt.shared.utils.ResultState import com.vickbt.shared.utils.toBoolean import io.ktor.client.HttpClient import io.ktor.client.call.body @@ -24,15 +24,15 @@ class MovieDetailsRepositoryImpl( private val favoriteMovieDao: FavoriteMovieDao ) : MovieDetailsRepository { - override suspend fun fetchMovieDetails(movieId: Int): Flow> { + override suspend fun fetchMovieDetails(movieId: Int): Flow> { val isMovieCached = isMovieFavorite(movieId = movieId) return if (isMovieCached == true) { try { val cachedFavoriteMovie = getFavoriteMovie(movieId = movieId) - flowOf(NetworkResultState.Success(data = cachedFavoriteMovie)) + flowOf(ResultState.Success(data = cachedFavoriteMovie)) } catch (e: Exception) { - flowOf(NetworkResultState.Failure(exception = e)) + flowOf(ResultState.Failure(exception = e)) } } else { flowOf( @@ -45,7 +45,7 @@ class MovieDetailsRepositoryImpl( } } - override suspend fun fetchMovieCast(movieId: Int): Flow> { + override suspend fun fetchMovieCast(movieId: Int): Flow> { return flowOf( safeApiCall { val response = httpClient.get(urlString = "movie/$movieId/credits").body() @@ -58,7 +58,7 @@ class MovieDetailsRepositoryImpl( override suspend fun fetchSimilarMovies( movieId: Int, page: Int - ): Flow?>> { + ): Flow?>> { return flowOf( safeApiCall { val response = httpClient.get(urlString = "movie/$movieId/similar") { diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/MoviesRepositoryImpl.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/MoviesRepositoryImpl.kt index 3f6c3c06..6f8d2f8e 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/MoviesRepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/MoviesRepositoryImpl.kt @@ -5,7 +5,7 @@ import com.vickbt.shared.data.network.models.MovieResultsDto import com.vickbt.shared.data.network.utils.safeApiCall import com.vickbt.shared.domain.models.Movie import com.vickbt.shared.domain.repositories.MoviesRepository -import com.vickbt.shared.utils.NetworkResultState +import com.vickbt.shared.utils.ResultState import io.ktor.client.HttpClient import io.ktor.client.call.body import io.ktor.client.request.get @@ -17,7 +17,7 @@ class MoviesRepositoryImpl constructor( private val httpClient: HttpClient ) : MoviesRepository { - override suspend fun fetchNowPlayingMovies(page: Int): Flow?>> { + override suspend fun fetchNowPlayingMovies(page: Int): Flow?>> { return flowOf( safeApiCall { val response = httpClient.get(urlString = "movie/now_playing") { @@ -33,7 +33,7 @@ class MoviesRepositoryImpl constructor( mediaType: String, timeWindow: String, page: Int - ): Flow?>> { + ): Flow?>> { return flowOf( safeApiCall { val response = httpClient.get(urlString = "trending/$mediaType/$timeWindow") { @@ -45,7 +45,7 @@ class MoviesRepositoryImpl constructor( ) } - override suspend fun fetchPopularMovies(page: Int): Flow?>> { + override suspend fun fetchPopularMovies(page: Int): Flow?>> { return flowOf( safeApiCall { val response = httpClient.get(urlString = "movie/popular") { @@ -57,7 +57,7 @@ class MoviesRepositoryImpl constructor( ) } - override suspend fun fetchUpcomingMovies(page: Int): Flow?>> { + override suspend fun fetchUpcomingMovies(page: Int): Flow?>> { return flowOf( safeApiCall { val response = httpClient.get(urlString = "movie/upcoming") { @@ -82,7 +82,7 @@ class MoviesRepositoryImpl constructor( override suspend fun searchMovie( movieName: String, page: Int - ): Flow?>> { + ): Flow?>> { return flowOf( safeApiCall { val response = httpClient.get(urlString = "search/movie") { diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/data/network/utils/SafeApiCall.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/data/network/utils/SafeApiCall.kt index 6620b245..819c727f 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/data/network/utils/SafeApiCall.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/data/network/utils/SafeApiCall.kt @@ -3,7 +3,7 @@ package com.vickbt.shared.data.network.utils import com.vickbt.shared.data.mappers.toDomain import com.vickbt.shared.data.network.models.ErrorResponseDto import com.vickbt.shared.domain.models.ErrorResponse -import com.vickbt.shared.utils.NetworkResultState +import com.vickbt.shared.utils.ResultState import io.ktor.client.call.body import io.ktor.client.plugins.ClientRequestException import io.ktor.client.plugins.RedirectResponseException @@ -11,26 +11,26 @@ import io.ktor.client.plugins.ServerResponseException import io.ktor.client.statement.HttpResponse import io.ktor.util.network.UnresolvedAddressException -suspend fun safeApiCall(apiCall: suspend () -> T): NetworkResultState { +suspend fun safeApiCall(apiCall: suspend () -> T): ResultState { return try { - NetworkResultState.Loading + ResultState.Loading - NetworkResultState.Success(apiCall.invoke()) + ResultState.Success(apiCall.invoke()) } catch (e: RedirectResponseException) { val error = parseNetworkError(e.response.body()) - NetworkResultState.Failure(exception = error) + ResultState.Failure(exception = error) } catch (e: ClientRequestException) { val error = parseNetworkError(e.response.body()) - NetworkResultState.Failure(exception = error) + ResultState.Failure(exception = error) } catch (e: ServerResponseException) { val error = parseNetworkError(e.response.body()) - NetworkResultState.Failure(exception = error) + ResultState.Failure(exception = error) } catch (e: UnresolvedAddressException) { val error = parseNetworkError(exception = e) - NetworkResultState.Failure(exception = error) + ResultState.Failure(exception = error) } catch (e: Exception) { val error = parseNetworkError(exception = e) - NetworkResultState.Failure(exception = error) + ResultState.Failure(exception = error) } } diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/domain/repositories/MovieDetailsRepository.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/domain/repositories/MovieDetailsRepository.kt index 5e06a395..00d9534b 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/domain/repositories/MovieDetailsRepository.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/domain/repositories/MovieDetailsRepository.kt @@ -4,22 +4,22 @@ import com.vickbt.shared.domain.models.Cast import com.vickbt.shared.domain.models.Movie import com.vickbt.shared.domain.models.MovieDetails import com.vickbt.shared.domain.utils.Constants.STARTING_PAGE_INDEX -import com.vickbt.shared.utils.NetworkResultState +import com.vickbt.shared.utils.ResultState import kotlinx.coroutines.flow.Flow interface MovieDetailsRepository { /**Fetch movie details from network source*/ - suspend fun fetchMovieDetails(movieId: Int): Flow> + suspend fun fetchMovieDetails(movieId: Int): Flow> /**Fetch movie cast from network source*/ - suspend fun fetchMovieCast(movieId: Int): Flow> + suspend fun fetchMovieCast(movieId: Int): Flow> /** Fetches similar movies from network source*/ suspend fun fetchSimilarMovies( movieId: Int, page: Int = STARTING_PAGE_INDEX - ): Flow?>> + ): Flow?>> /**Save movie details to local cache*/ suspend fun saveFavoriteMovie(movie: MovieDetails) diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/domain/repositories/MoviesRepository.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/domain/repositories/MoviesRepository.kt index 7422ebf9..22b0c740 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/domain/repositories/MoviesRepository.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/domain/repositories/MoviesRepository.kt @@ -2,26 +2,26 @@ package com.vickbt.shared.domain.repositories import com.vickbt.shared.domain.models.Movie import com.vickbt.shared.domain.utils.Constants.STARTING_PAGE_INDEX -import com.vickbt.shared.utils.NetworkResultState +import com.vickbt.shared.utils.ResultState import kotlinx.coroutines.flow.Flow interface MoviesRepository { /** Fetch Now Playing movies from data source*/ - suspend fun fetchNowPlayingMovies(page: Int = STARTING_PAGE_INDEX): Flow?>> + suspend fun fetchNowPlayingMovies(page: Int = STARTING_PAGE_INDEX): Flow?>> /** Fetch Trending movies from data source*/ suspend fun fetchTrendingMovies( mediaType: String = "movie", timeWindow: String = "week", page: Int = STARTING_PAGE_INDEX - ): Flow?>> + ): Flow?>> /** Fetch Popular movies from data source*/ - suspend fun fetchPopularMovies(page: Int = STARTING_PAGE_INDEX): Flow?>> + suspend fun fetchPopularMovies(page: Int = STARTING_PAGE_INDEX): Flow?>> /** Fetch Upcoming movies from data source*/ - suspend fun fetchUpcomingMovies(page: Int = STARTING_PAGE_INDEX): Flow?>> + suspend fun fetchUpcomingMovies(page: Int = STARTING_PAGE_INDEX): Flow?>> /** Get movies based on category from cache*/ /*@Deprecated("Pending caching implementation") @@ -31,5 +31,5 @@ interface MoviesRepository { suspend fun searchMovie( movieName: String, page: Int = STARTING_PAGE_INDEX - ): Flow?>> + ): Flow?>> } diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/utils/NetworkResultState.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/utils/NetworkResultState.kt deleted file mode 100644 index f6bbb1b6..00000000 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/utils/NetworkResultState.kt +++ /dev/null @@ -1,22 +0,0 @@ -package com.vickbt.shared.utils - -sealed class NetworkResultState { - data class Success(val data: T) : NetworkResultState() - data class Failure(val exception: Exception) : NetworkResultState() - object Loading : NetworkResultState() -} - -inline fun NetworkResultState.isLoading(crossinline action: (isLoading: Boolean) -> Unit): NetworkResultState { - if (this is NetworkResultState.Loading) action(true) else action(false) - return this -} - -inline fun NetworkResultState.onSuccess(crossinline action: (T) -> Unit): NetworkResultState { - if (this is NetworkResultState.Success) action(this.data) - return this -} - -inline fun NetworkResultState.onFailure(crossinline action: (exception: Exception) -> Unit): NetworkResultState { - if (this is NetworkResultState.Failure) action(this.exception) - return this -} diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/utils/ResultState.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/utils/ResultState.kt new file mode 100644 index 00000000..44ff5a0a --- /dev/null +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/utils/ResultState.kt @@ -0,0 +1,22 @@ +package com.vickbt.shared.utils + +sealed class ResultState { + data class Success(val data: T) : ResultState() + data class Failure(val exception: Exception) : ResultState() + object Loading : ResultState() +} + +inline fun ResultState.isLoading(crossinline action: (isLoading: Boolean) -> Unit): ResultState { + if (this is ResultState.Loading) action(true) else action(false) + return this +} + +inline fun ResultState.onSuccess(crossinline action: (T) -> Unit): ResultState { + if (this is ResultState.Success) action(this.data) + return this +} + +inline fun ResultState.onFailure(crossinline action: (exception: Exception) -> Unit): ResultState { + if (this is ResultState.Failure) action(this.exception) + return this +} From 94b66f41fe2908ccfb8c62f7cf9987c095c33595 Mon Sep 17 00:00:00 2001 From: Victor Kabata Date: Wed, 3 Jan 2024 15:00:35 +0300 Subject: [PATCH 05/11] Update settings repo with experimental annotation --- .../shared/data/datasources/SettingsRepositoryImpl.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/SettingsRepositoryImpl.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/SettingsRepositoryImpl.kt index ddd26eb5..ea6866d4 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/SettingsRepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/SettingsRepositoryImpl.kt @@ -1,23 +1,25 @@ package com.vickbt.shared.data.datasources +import com.russhwolf.settings.ExperimentalSettingsApi import com.russhwolf.settings.ObservableSettings -import com.russhwolf.settings.coroutines.getIntOrNullFlow +import com.russhwolf.settings.coroutines.getIntFlow import com.vickbt.shared.domain.repositories.SettingsRepository import com.vickbt.shared.domain.utils.Constants.KEY_IMAGE_QUALITY import com.vickbt.shared.domain.utils.Constants.KEY_THEME import kotlinx.coroutines.flow.Flow -class SettingsRepositoryImpl constructor(private val observableSettings: ObservableSettings) : +@ExperimentalSettingsApi +class SettingsRepositoryImpl(private val observableSettings: ObservableSettings) : SettingsRepository { override suspend fun savePreferenceSelection(key: String, selection: Int) = observableSettings.putInt(key = key, value = selection) override suspend fun getThemePreference(): Flow { - return observableSettings.getIntOrNullFlow(key = KEY_THEME) + return observableSettings.getIntFlow(key = KEY_THEME, defaultValue = 0) } override suspend fun getImageQualityPreference(): Flow { - return observableSettings.getIntOrNullFlow(key = KEY_IMAGE_QUALITY) + return observableSettings.getIntFlow(key = KEY_IMAGE_QUALITY, defaultValue = 0) } } From 01724bb6c20a37ba43ef3e4660badb37c2a28a9a Mon Sep 17 00:00:00 2001 From: Victor Kabata Date: Wed, 3 Jan 2024 15:14:32 +0300 Subject: [PATCH 06/11] Resolved multiplatform settings flakiness issue --- .../datasources/SettingsRepositoryImpl.kt | 4 +-- .../com/vickbt/shared/di/CommonModule.kt | 2 +- .../kotlin/com/vickbt/shared/di/Koin.kt | 2 +- .../domain/repositories/SettingsRepository.kt | 4 +-- .../ui/screens/settings/SettingsScreen.kt | 7 +---- .../ui/screens/settings/SettingsViewModel.kt | 28 ++++++++++--------- .../com/vickbt/shared/utils/Extensions.kt | 2 +- 7 files changed, 23 insertions(+), 26 deletions(-) diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/SettingsRepositoryImpl.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/SettingsRepositoryImpl.kt index ea6866d4..cc26f172 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/SettingsRepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/SettingsRepositoryImpl.kt @@ -15,11 +15,11 @@ class SettingsRepositoryImpl(private val observableSettings: ObservableSettings) override suspend fun savePreferenceSelection(key: String, selection: Int) = observableSettings.putInt(key = key, value = selection) - override suspend fun getThemePreference(): Flow { + override suspend fun getThemePreference(): Flow { return observableSettings.getIntFlow(key = KEY_THEME, defaultValue = 0) } - override suspend fun getImageQualityPreference(): Flow { + override suspend fun getImageQualityPreference(): Flow { return observableSettings.getIntFlow(key = KEY_IMAGE_QUALITY, defaultValue = 0) } } diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/di/CommonModule.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/di/CommonModule.kt index e7d4a21f..dfe1fcf3 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/di/CommonModule.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/di/CommonModule.kt @@ -14,7 +14,7 @@ import com.vickbt.shared.domain.utils.Constants.BASE_URL import com.vickbt.shared.domain.utils.Constants.URL_PATH import com.vickbt.shared.presentation.ui.screens.home.HomeViewModel import com.vickbt.shared.presentation.ui.screens.main.MainViewModel -import com.vickbt.shared.presentation.ui.screens.settings.SettingsViewModel +import com.vickbt.shared.ui.screens.settings.SettingsViewModel import com.vickbt.shared.ui.screens.details.DetailsViewModel import com.vickbt.shared.ui.screens.favorites.FavoritesViewModel import io.github.aakira.napier.DebugAntilog diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/di/Koin.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/di/Koin.kt index 695268cb..44899aa8 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/di/Koin.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/di/Koin.kt @@ -2,7 +2,7 @@ package com.vickbt.shared.di import com.vickbt.shared.presentation.ui.screens.home.HomeViewModel import com.vickbt.shared.presentation.ui.screens.main.MainViewModel -import com.vickbt.shared.presentation.ui.screens.settings.SettingsViewModel +import com.vickbt.shared.ui.screens.settings.SettingsViewModel import org.koin.core.Koin import org.koin.core.KoinApplication import org.koin.core.context.startKoin diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/domain/repositories/SettingsRepository.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/domain/repositories/SettingsRepository.kt index 63e93258..341e7da7 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/domain/repositories/SettingsRepository.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/domain/repositories/SettingsRepository.kt @@ -6,7 +6,7 @@ interface SettingsRepository { suspend fun savePreferenceSelection(key: String, selection: Int) - suspend fun getThemePreference(): Flow + suspend fun getThemePreference(): Flow - suspend fun getImageQualityPreference(): Flow + suspend fun getImageQualityPreference(): Flow } diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/settings/SettingsScreen.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/settings/SettingsScreen.kt index 2dc0a35a..a1f5c6b4 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/settings/SettingsScreen.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/settings/SettingsScreen.kt @@ -16,23 +16,18 @@ import androidx.compose.runtime.remember import androidx.compose.ui.Modifier import com.vickbt.shared.domain.utils.Constants.KEY_IMAGE_QUALITY import com.vickbt.shared.domain.utils.Constants.KEY_THEME -import com.vickbt.shared.presentation.ui.screens.settings.SettingsViewModel import com.vickbt.shared.ui.components.appbars.AppBar import com.vickbt.shared.ui.components.preferences.DialogPreferenceSelection import com.vickbt.shared.ui.components.preferences.PreferencesGroup import com.vickbt.shared.ui.components.preferences.TextPreference +import io.github.aakira.napier.Napier import org.koin.compose.koinInject private val themeLabels = listOf("Light", "Dark", "System Default") private val imageQualityLabels = listOf("High Quality", "Low Quality") -@OptIn(ExperimentalMaterial3Api::class) @Composable fun SettingsScreen(viewModel: SettingsViewModel = koinInject()) { - LaunchedEffect(key1 = viewModel) { - viewModel.getThemePreference() - viewModel.getImageQualityPreference() - } val settingsUiState = viewModel.settingsUiState.collectAsState().value diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/settings/SettingsViewModel.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/settings/SettingsViewModel.kt index 06950f40..765daa0e 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/settings/SettingsViewModel.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/settings/SettingsViewModel.kt @@ -1,28 +1,34 @@ -package com.vickbt.shared.presentation.ui.screens.settings +package com.vickbt.shared.ui.screens.settings import com.vickbt.shared.domain.repositories.SettingsRepository import com.vickbt.shared.utils.SettingsUiState import kotlinx.coroutines.CoroutineExceptionHandler import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.IO import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import org.koin.core.component.KoinComponent -class SettingsViewModel constructor(private val settingsRepository: SettingsRepository) : +class SettingsViewModel(private val settingsRepository: SettingsRepository) : KoinComponent { private val _settingsUiState = MutableStateFlow(SettingsUiState(isLoading = true)) val settingsUiState = _settingsUiState.asStateFlow() - private val viewModelScope = CoroutineScope(Dispatchers.Main) + private val viewModelScope = CoroutineScope(Dispatchers.IO) private val coroutineExceptionHandler = CoroutineExceptionHandler { _, exception -> _settingsUiState.update { it.copy(isLoading = false, error = exception.message) } } + init { + getThemePreference() + getImageQualityPreference() + } + fun savePreferenceSelection(key: String, selection: Int) = viewModelScope.launch(coroutineExceptionHandler) { settingsRepository.savePreferenceSelection(key = key, selection = selection) @@ -30,21 +36,17 @@ class SettingsViewModel constructor(private val settingsRepository: SettingsRepo fun getThemePreference() = viewModelScope.launch(coroutineExceptionHandler) { settingsRepository.getThemePreference().collect { theme -> - theme?.let { - _settingsUiState.update { it.copy(selectedTheme = theme, isLoading = false) } - } + _settingsUiState.update { it.copy(selectedTheme = theme, isLoading = false) } } } fun getImageQualityPreference() = viewModelScope.launch(coroutineExceptionHandler) { settingsRepository.getImageQualityPreference().collect { imageQuality -> - imageQuality?.let { - _settingsUiState.update { - it.copy( - selectedImageQuality = imageQuality, - isLoading = false - ) - } + _settingsUiState.update { + it.copy( + selectedImageQuality = imageQuality, + isLoading = false + ) } } } diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/utils/Extensions.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/utils/Extensions.kt index 3468e080..b94be38f 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/utils/Extensions.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/utils/Extensions.kt @@ -2,7 +2,7 @@ package com.vickbt.shared.utils import androidx.compose.runtime.Composable import androidx.compose.runtime.collectAsState -import com.vickbt.shared.presentation.ui.screens.settings.SettingsViewModel +import com.vickbt.shared.ui.screens.settings.SettingsViewModel import org.koin.compose.koinInject /** From e8a0542b14c94d72dfd7a7202003d704127fa6f7 Mon Sep 17 00:00:00 2001 From: Victor Kabata Date: Wed, 3 Jan 2024 15:19:47 +0300 Subject: [PATCH 07/11] Change multiplatform settings default values --- .../vickbt/shared/data/datasources/SettingsRepositoryImpl.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/SettingsRepositoryImpl.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/SettingsRepositoryImpl.kt index cc26f172..cda142bb 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/SettingsRepositoryImpl.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/data/datasources/SettingsRepositoryImpl.kt @@ -16,10 +16,10 @@ class SettingsRepositoryImpl(private val observableSettings: ObservableSettings) observableSettings.putInt(key = key, value = selection) override suspend fun getThemePreference(): Flow { - return observableSettings.getIntFlow(key = KEY_THEME, defaultValue = 0) + return observableSettings.getIntFlow(key = KEY_THEME, defaultValue = 2) } override suspend fun getImageQualityPreference(): Flow { - return observableSettings.getIntFlow(key = KEY_IMAGE_QUALITY, defaultValue = 0) + return observableSettings.getIntFlow(key = KEY_IMAGE_QUALITY, defaultValue = 1) } } From b0d1e5dd4fe0b921374aaeeb87e63f5d7098cd43 Mon Sep 17 00:00:00 2001 From: Victor Kabata Date: Wed, 3 Jan 2024 15:25:05 +0300 Subject: [PATCH 08/11] Update searchBar width based on searchBar active status --- .../kotlin/com/vickbt/shared/ui/screens/home/HomeScreen.kt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/home/HomeScreen.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/home/HomeScreen.kt index e2da8c10..187aca1d 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/home/HomeScreen.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/home/HomeScreen.kt @@ -90,7 +90,11 @@ fun HomeScreen( ) { //region Search SearchBar( - modifier = Modifier.fillMaxWidth().background(MaterialTheme.colorScheme.surface), + modifier = Modifier.background( + MaterialTheme.colorScheme.surface + ).also { + if (activeState) it.fillMaxWidth() else it.fillMaxWidth(.8f) + }, query = searchQuery, onQueryChange = { searchQuery = it }, onSearch = { viewModel.searchMovie(movieName = it) }, From 3d4650fb87b6875705826f9a1f84c2c943217196 Mon Sep 17 00:00:00 2001 From: Victor Kabata Date: Wed, 3 Jan 2024 15:26:47 +0300 Subject: [PATCH 09/11] Linting --- .../com/vickbt/shared/ui/screens/settings/SettingsScreen.kt | 4 ---- 1 file changed, 4 deletions(-) diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/settings/SettingsScreen.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/settings/SettingsScreen.kt index a1f5c6b4..c6378d38 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/settings/SettingsScreen.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/settings/SettingsScreen.kt @@ -5,10 +5,8 @@ import androidx.compose.foundation.layout.padding import androidx.compose.material.icons.Icons import androidx.compose.material.icons.rounded.Image import androidx.compose.material.icons.rounded.Lightbulb -import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Scaffold import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.MutableState import androidx.compose.runtime.collectAsState import androidx.compose.runtime.mutableStateOf @@ -20,7 +18,6 @@ import com.vickbt.shared.ui.components.appbars.AppBar import com.vickbt.shared.ui.components.preferences.DialogPreferenceSelection import com.vickbt.shared.ui.components.preferences.PreferencesGroup import com.vickbt.shared.ui.components.preferences.TextPreference -import io.github.aakira.napier.Napier import org.koin.compose.koinInject private val themeLabels = listOf("Light", "Dark", "System Default") @@ -28,7 +25,6 @@ private val imageQualityLabels = listOf("High Quality", "Low Quality") @Composable fun SettingsScreen(viewModel: SettingsViewModel = koinInject()) { - val settingsUiState = viewModel.settingsUiState.collectAsState().value val showThemeDialog = remember { mutableStateOf(false) } From d0e67f59fea6797ea9a74593e5d4b9ccfcc08aa1 Mon Sep 17 00:00:00 2001 From: Victor Kabata Date: Wed, 3 Jan 2024 15:32:22 +0300 Subject: [PATCH 10/11] Update appDesktop main class in build.gradle --- appDesktop/build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/appDesktop/build.gradle.kts b/appDesktop/build.gradle.kts index 482a8ae1..51708bfd 100644 --- a/appDesktop/build.gradle.kts +++ b/appDesktop/build.gradle.kts @@ -11,6 +11,6 @@ dependencies { compose.desktop { application { - mainClass = "com.vickbt.notflix.NotflixApplicationKt" + mainClass = "NotflixApplicationKt" } } From 70f37b1d51d729f9b3f45574e79519d4e147a322 Mon Sep 17 00:00:00 2001 From: Victor Kabata Date: Wed, 3 Jan 2024 15:36:25 +0300 Subject: [PATCH 11/11] Set search bar width to .85f --- .../kotlin/com/vickbt/shared/ui/screens/home/HomeScreen.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/home/HomeScreen.kt b/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/home/HomeScreen.kt index 187aca1d..a6cb8de3 100644 --- a/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/home/HomeScreen.kt +++ b/shared/src/commonMain/kotlin/com/vickbt/shared/ui/screens/home/HomeScreen.kt @@ -93,7 +93,7 @@ fun HomeScreen( modifier = Modifier.background( MaterialTheme.colorScheme.surface ).also { - if (activeState) it.fillMaxWidth() else it.fillMaxWidth(.8f) + if (activeState) it.fillMaxWidth() else it.fillMaxWidth(.85f) }, query = searchQuery, onQueryChange = { searchQuery = it },