diff --git a/app/src/main/java/com/pranshulgg/weather_master_app/core/model/sources/WeatherSource.kt b/app/src/main/java/com/pranshulgg/weather_master_app/core/model/sources/WeatherSource.kt index d1f715089..96f3764cf 100644 --- a/app/src/main/java/com/pranshulgg/weather_master_app/core/model/sources/WeatherSource.kt +++ b/app/src/main/java/com/pranshulgg/weather_master_app/core/model/sources/WeatherSource.kt @@ -107,4 +107,10 @@ private val weatherSourcesGlobal = listOf( fun getWeatherSourcesGlobal(): List { return weatherSourcesGlobal +} + +fun WeatherSource.isSupportedFor(countryCode: String?): Boolean { + val source = weatherSourcesByCountry[countryCode] ?: emptyList() + + return source.isNotEmpty() } \ No newline at end of file diff --git a/app/src/main/java/com/pranshulgg/weather_master_app/feature/main/MainScreen.kt b/app/src/main/java/com/pranshulgg/weather_master_app/feature/main/MainScreen.kt index f01866dd4..fd216af97 100644 --- a/app/src/main/java/com/pranshulgg/weather_master_app/feature/main/MainScreen.kt +++ b/app/src/main/java/com/pranshulgg/weather_master_app/feature/main/MainScreen.kt @@ -1,6 +1,7 @@ package com.pranshulgg.weather_master_app.feature.main import android.util.Log +import android.widget.Toast import androidx.activity.compose.BackHandler import androidx.compose.material3.DrawerValue import androidx.compose.material3.ExperimentalMaterial3Api @@ -32,6 +33,7 @@ import com.pranshulgg.weather_master_app.core.ui.snackbar.SnackbarManager import com.pranshulgg.weather_master_app.feature.intro.IntroScreen import com.pranshulgg.weather_master_app.feature.locations.LocationsScreen import com.pranshulgg.weather_master_app.feature.main.ui.MainScreenBottomSheets +import com.pranshulgg.weather_master_app.feature.main.ui.MainScreenDialogs import com.pranshulgg.weather_master_app.feature.main.ui.NavigationDrawer import com.pranshulgg.weather_master_app.feature.shared.WeatherViewModel import com.pranshulgg.weather_master_app.feature.shared.ui.SharedBottomSheet @@ -47,14 +49,16 @@ data class MainScreenWeatherUiState( val blocks: List = WeatherBlock.getDefault(), val isInitialized: Boolean = false, val airQuality: AirQuality? = null, - val isAirQualityLoading: Boolean = false + val isAirQualityLoading: Boolean = false, + val isUnsupportedSource: Boolean = false ) data class MainScreenUiState( val isWeatherSourcesForLocationSheetOpen: Boolean = false, val isWeatherSourcesInfoForLocationSheetOpen: Boolean = false, val isNewVersionAvailable: Boolean = false, - val lastestVersionUrl: String = "https://github.com/PranshulGG/WeatherMaster/releases/latest" + val lastestVersionUrl: String = "https://github.com/PranshulGG/WeatherMaster/releases/latest", + val isUnsupportedSourceDialogOpen: Boolean = false ) @OptIn(ExperimentalMaterial3Api::class) @@ -92,6 +96,7 @@ fun MainScreen(navController: NavController) { val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed) val scope = rememberCoroutineScope() + val closeDrawer = { scope.launch { drawerState.close() } } @@ -103,6 +108,12 @@ fun MainScreen(navController: NavController) { closeDrawer() } + LaunchedEffect(uiState.isUnsupportedSource) { + if (uiState.isUnsupportedSource) { + viewModel.showUnsupportedSelectedSourceDialog() + } + } + LaunchedEffect(mainScreenUiState.isNewVersionAvailable) { if (mainScreenUiState.isNewVersionAvailable) { SnackbarManager.show( @@ -182,6 +193,13 @@ fun MainScreen(navController: NavController) { // WEATHER SOURCES INFO DIALOG MainScreenBottomSheets.WeatherSourcesInfoForLocationSheet(viewModel, activeLocation, sheetState) + + // SOURCE NOT AVAILABLE + MainScreenDialogs.UnsupportedSelectedSourceDialog( + mainScreenUiState.isUnsupportedSourceDialogOpen, + onDismiss = + viewModel::hideUnsupportedSelectedSourceDialog, + onConfirm = { viewModel.showWeatherSourcesForLocationSheet(uiState.isLoading) }) } diff --git a/app/src/main/java/com/pranshulgg/weather_master_app/feature/main/MainScreenViewModel.kt b/app/src/main/java/com/pranshulgg/weather_master_app/feature/main/MainScreenViewModel.kt index fca5a6b36..b8226dacf 100644 --- a/app/src/main/java/com/pranshulgg/weather_master_app/feature/main/MainScreenViewModel.kt +++ b/app/src/main/java/com/pranshulgg/weather_master_app/feature/main/MainScreenViewModel.kt @@ -49,6 +49,14 @@ class MainScreenViewModel @Inject constructor( _uiState.value = _uiState.value.copy(isWeatherSourcesInfoForLocationSheetOpen = false) } + fun hideUnsupportedSelectedSourceDialog() { + _uiState.value = _uiState.value.copy(isUnsupportedSourceDialogOpen = false) + } + + fun showUnsupportedSelectedSourceDialog() { + _uiState.value = _uiState.value.copy(isUnsupportedSourceDialogOpen = true) + } + private suspend fun checkForUpdates() { val isNewAvailable = try { githubRepository.isNewVersionAvailable( diff --git a/app/src/main/java/com/pranshulgg/weather_master_app/feature/main/ui/MainScreenDialogs.kt b/app/src/main/java/com/pranshulgg/weather_master_app/feature/main/ui/MainScreenDialogs.kt new file mode 100644 index 000000000..db1896a96 --- /dev/null +++ b/app/src/main/java/com/pranshulgg/weather_master_app/feature/main/ui/MainScreenDialogs.kt @@ -0,0 +1,24 @@ +package com.pranshulgg.weather_master_app.feature.main.ui + +import androidx.compose.runtime.Composable +import com.pranshulgg.weather_master_app.core.ui.components.TextAlertDialog + +object MainScreenDialogs { + + + @Composable + fun UnsupportedSelectedSourceDialog( + show: Boolean, + onDismiss: () -> Unit, + onConfirm: () -> Unit + ) { + TextAlertDialog( + show, + onDismiss = onDismiss, + onConfirm = onConfirm, + confirmText = "Change", + title = "Source not supported", + message = "The currently selected source is not supported in your region. Please choose a different source" + ) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/pranshulgg/weather_master_app/feature/shared/WeatherViewModel.kt b/app/src/main/java/com/pranshulgg/weather_master_app/feature/shared/WeatherViewModel.kt index 8af5dacdf..c95ce1b55 100644 --- a/app/src/main/java/com/pranshulgg/weather_master_app/feature/shared/WeatherViewModel.kt +++ b/app/src/main/java/com/pranshulgg/weather_master_app/feature/shared/WeatherViewModel.kt @@ -14,6 +14,7 @@ import com.pranshulgg.weather_master_app.core.model.domain.toMessageRes import com.pranshulgg.weather_master_app.core.model.domain.weather.Weather import com.pranshulgg.weather_master_app.core.model.domain.weather.WeatherBlock import com.pranshulgg.weather_master_app.core.model.sources.WeatherSource +import com.pranshulgg.weather_master_app.core.model.sources.isSupportedFor import com.pranshulgg.weather_master_app.core.model.weather.WeatherResult import com.pranshulgg.weather_master_app.core.model.weather.airquality.AirQualityResult import com.pranshulgg.weather_master_app.core.network.github.GithubRepository @@ -117,7 +118,7 @@ class WeatherViewModel @Inject constructor( setLoading(true) weatherJob?.cancel() val startTime = System.currentTimeMillis() - _uiState.value = _uiState.value.copy(isError = false) + _uiState.value = _uiState.value.copy(isError = false, isUnsupportedSource = false) weatherJob = viewModelScope.launch { @@ -253,7 +254,8 @@ class WeatherViewModel @Inject constructor( _uiState.value = _uiState.value.copy( isError = true, - weather = result.cacheWeather + weather = result.cacheWeather, + isUnsupportedSource = !location.source.isSupportedFor(location.countryCode?.uppercase()) ) }